[cpp] view plaincopy
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define LED_D1 PTDD_PTDD0
#define LED_D1_DIR PTDDD_PTDDD0
#define LED_ON 0
#define LED_OFF 1
unsigned char SCANF_SEND_STRINGS[] = "HELLO MCU WORLD";
unsigned char *pSendString = SCANF_SEND_STRINGS;
unsigned char ReceiveString;
/*************************************************************/
/* 初始化总线时钟PEE模式 */
/* 外部晶振频率为4MHz,总线频率16MHz */
/*************************************************************/
void INIT_MCG(void)
{
MCGC2=0x36;
while(!MCGSC_OSCINIT);
MCGC1=0xb8;
while(MCGSC_IREFST);
while(MCGSC_CLKST!=0b10);
MCGC2_LP = 1;
MCGC1 = 0x90;
MCGC3 = 0x48;
MCGC2_LP = 0;
while(!MCGSC_PLLST);
while(!MCGSC_LOCK);
MCGC1 = 0x10;
while(MCGSC_CLKST!=0b11);
}
//延时函数
void delay(void)
{
unsigned int i;
unsigned int j;
for(i = 0; i < 100; i++)
for(j = 0; j < 4000; j++);
}
void main(void) {
DisableInterrupts;
SOPT1 = 0;
INIT_MCG();
EnableInterrupts; /* enable interrupts */
/* include your code here */
LED_D1_DIR = 1;
LED_D1 = LED_OFF;
//以下为设置波特率,以及初始化SCI1控制寄存器
SCI1BDH = 0X00;
SCI1BDL = 0X68;//波特率设置为9600
SCI1C1 = 0x00; //设置SCI1为正常模式,八位数据位,无奇偶校验
SCI1C2 = 0x2C; //RE = 1(bit2)接收器打开
//TE = 1(bit3)发送器打开
while(1)
{
}
for(;;) {
__RESET_WATCHDOG(); /* feeds the dog */
} /* loop forever */
/* please make sure that you never leave main */
}
/*************************************************************/
/* SCI1中断向量接收函数 */
/* 向量编号为17,中断来源RDRF等 */
/*************************************************************/
interrupt 17 void SCI1_RECEIVE(void)
{
while(!SCI1S1_RDRF);// SCI1S1_RDRF = 1时,表示接收数据寄存器满,可以取出数据
ReceiveString = SCI1D; //将接收寄存器中的数据保存至ReceiveString
LED_D1 = ~LED_D1;
while(!SCI1S1_TDRE);//SCI1S1_TDRE = 1时,发送数据寄存器(缓存区)空,可以发送数据
SCI1D = ReceiveString;//将接收到的数据,放至发送数据寄存器(缓存区),发送
}
|