集美大学 单片机考试复习资料 (基于飞思卡尔AW60)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

集美大学机电专业单片机考试复习资料

飞思卡尔A W60单片机复习

1. 串口程序

#include /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

void MCUInit(void)

{

SOPT = 0b01100000; //$70 System Options Register(write once)

ICGC2 = 0b00110000; //$30 internal clock generation 2

ICGC1 = 0b01111000; //$78 internal clock generation

while(!ICGS1_LOCK); //等待FLL稳定

PTBDD=0xff ;

PTBD=0xff ;

}

void SCIInit()

{

unsigned int ubgs,baud=9600;

unsigned char sysclk=20;

//1.计算波特率并设置:ubgs = fsys/(波特率*16)(其中fsys=sysclk*1000000)

ubgs = sysclk*(10000/(baud/100))/16; //理解参考上一行,此处便于CPU运算

SCI1BDH= (unsigned char)((ubgs & 0xFF00) >> 8);

SCI1BDL= (unsigned char)(ubgs & 0x00FF);

SCI1C1= 0; //无校验,正常模式(开始信号+ 8位数据(先发最低位) + 停止信号)

SCI1C2= (0| SCI1C2_TE_MASK | SCI1C2_RE_MASK );//允许发送,允许接收,查询方式收发}

void SCISend1(unsigned char ch)

{

while(!(SCI1S1 & SCI1S1_TDRE_MASK));//判断发送缓冲区是否为空

SCI1D = ch;

}

void SCISendN(unsigned char n, unsigned char ch[])

{

unsigned i;

for (i = 0; i < n; i++)

SCISend1(ch[i]);

}

unsigned char SCIRe1(unsigned char *p)

{

unsigned int k;

unsigned char i;

for (k = 0; k < 0x0b; k++)//有时间限制

if((SCI1S1 & SCI1S1_RDRF_MASK) != 0)//判断接收缓冲区是否满

{

i = SCI1D;

*p = 0x00;

break;

}

if (k >= 0x0b) //接收失败

{

i = 0xff;

*p = 0x01;

}

return i;

}

unsigned char SCIReN(unsigned n,unsigned char ch[])

{

unsigned char m;

unsigned char fp; //接收标志

m = 0;

while (m < n)

{

ch[m] = SCIRe1(&fp);

if (fp == 1)

{

return 1; //接收失败

}

m++;

}

return 0; //接收成功

}

void main(void)

{

unsigned char SerialBuff[]="Hello! World!"; //初始化存放接收数据的数组DisableInterrupts; //禁止总中断

MCUInit();

SCIInit();

EnableInterrupts; //开放总中断

SCISendN(13,SerialBuff); //串口发送"Hello World!"

for(;;)

{

if((SCI1S1&SCI1S1_RDRF_MASK)!=0)

{

PTBD=SCI1D ;

if((SCI1S1&SCI1S1_TDRE_MASK)!=0)

SCI1D=PTBD;

}

else

PTBD=0x00;

}

}

2.按键程序

#include /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */ #define uchar unsigned char

#define uint unsigned int

unsigned char table[]="I love mcu!";

unsigned char table1[]=" very much!";

#define rsout PTCDD |= (1<<4)

#define rsset PTCD |= (1<<4)

#define rsclr PTCD &=~(1<<4)

#define rwout PTCDD |= (1<<6)

#define rwset PTCD |= (1<<6)

#define rwclr PTCD &=~(1<<6)

#define enout PTFDD |= (1<<6)

#define enset PTFD |= (1<<6)

#define enclr PTFD &=~(1<<6)

void mcu_init(void)

{

PTADD = 0XFF;

rsout;

rwout;

enout;

}

void MCUInit(void)

{

SOPT = 0b01110000;

ICGC2 = 0b00110000;

ICGC1 = 0b01111000;

while(!ICGS1_LOCK);

}

void Delayms(uint MS)

{

uint i,j;

for( i=0;i

for(j=0;j<1141;j++);

}

void Delay(uint count)

{

相关文档
最新文档