本帖最后由 dami 于 2020-7-18 22:42 编辑
赛元易码魔盒体验——2,串口命令去点灯
1,使用串口0 UART0 和P00
2,使用赛元易码魔盒生成代码。
3,使用KEIL 编译。
4,main.c主函数前添加代码
void rx_to_buf(unsigned char x);
#define RXMAX 50
char rx0[RXMAX];
char rx_point=0;
void delayms(int x)
{
int i,j;
for(i=0;i<5000;i++)
for(j=0;j<x;j++){};
}
void LED1_ON(void){
GPIO_WriteLow(GPIO0, GPIO_PIN_0);
}
void led1_off(void)
{
GPIO_WriteHigh(GPIO0, GPIO_PIN_0);
}
void SC_GPIO_Init(void)
{
GPIO_Init(GPIO0, GPIO_PIN_0,GPIO_MODE_OUT_PP);
/*GPIO_Init write here*/
}
void rx_to_buf(unsigned char x)
{
if(rx_point>=RXMAX)
return;
rx0[rx_point]=x;
rx_point++;
if(rx_point>=RXMAX)
rx_point =0;
}
void rx_command(void)
{
if((rx0[0] =='l')&&(rx0[1] =='e')&&(rx0[2] =='d')&&(rx0[3] =='o')&&(rx0[4] =='n'))
{
LED1_ON();
printf("LED1 ON! \n");
rx_point =0;
rx0[0] =rx0[1] =rx0[2] =rx0[3] =rx0[4] =rx0[5];
}
if((rx0[0] =='l')&&(rx0[1] =='e')&&(rx0[2] =='d')&&(rx0[3] =='o')&&(rx0[4] =='f')&&(rx0[5] =='f'))
{
led1_off();
printf("LED1 OFF! \n");
rx_point =0;
rx_point =0;
rx0[0] =rx0[1] =rx0[2] =rx0[3] =rx0[4] =rx0[5];
}
if(rx_point >=6)
rx_point =0;
}
4,中断 函数中加入代码:extern void rx_to_buf(unsigned char x);
void UART0Interrupt() interrupt 4
{
if(UART0_GetFlagStatus(UART0_FLAG_TI))
{
UART0_ClearFlag(UART0_FLAG_TI); //Çå³ýTI
Uart0SendFlag = 1;
}
else if(UART0_GetFlagStatus(UART0_FLAG_RI))
{
unsigned char x;
UART0_ClearFlag(UART0_FLAG_RI); //Çå³ýRI
Uart0ReceiveFlag=1;
x=UART0_ReceiveData8();
rx_to_buf(x);
}
}
5,主程序main中调用
void main(void)
{
Uart0Init();
SC_GPIO_Init();
enableInterrupts();
printf("Uart0 contrl led\n please send ledon ledoff command to control led flash");
while(1)
{
rx_command();
delayms(100);
}
}
6,编译运行
7,运行结果:
|