|
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- int main(void)
- {
- u8 t;
- u8 len;
- u16 times=0;
- u8 com;
- u8 count=5;
-
- Stm32_Clock_Init(9); //系統(tǒng)時(shí)鐘設(shè)置
- delay_init(72); //延時(shí)初始化
- uart_init(72,115200);//串口初始化為115200
- LED_Init(); //初始化與LED連接的硬件接口
- printf("\r\nhello world!\r\n");
- while(1)
- {
- if(USART_RX_STA&0X8000)
- {
- //數(shù)據(jù)接收完整后進(jìn)行發(fā)送的過程
- len=USART_RX_STA&0x3fFF;//得到此次接收到的數(shù)據(jù)長(zhǎng)度
- if(len==1){
- printf("\r\n數(shù)據(jù)的長(zhǎng)度=%d\n",len);
- printf("\r\n您發(fā)送的消息為:\r\n\r\n");
- while((USART1->SR & 0x40) != 0x40); //置位TE將使得USART在第一個(gè)數(shù)據(jù)幀前發(fā)送一空閑幀
- for(t=0;t<len;t++)
- {
- USART1->DR=USART_RX_BUF[t];
- com=USART_RX_BUF[t];
- while((USART1->SR&0X40)==0);//等待發(fā)送結(jié)束
- }
- printf("\r\n\r\n");//插入換行
- USART_RX_STA=0;
-
- switch(com){
- case 0x30:
- //LED1變換,發(fā)送命令‘0’
- LED1=!LED1;
- delay_ms(20);
- break;
- case 0x31:
- //LED1和LED2閃爍,發(fā)送命令‘1’
- do{
- LED1=0;
- LED2=1;
- delay_ms(20);
- LED1=1;
- LED2=0;
- delay_ms(20);
- count--;
- }while(count);
- if(count==0) count=5;
- break;
-
- }
- }else{
- printf("\r\n發(fā)送的指令錯(cuò)誤,請(qǐng)重新發(fā)送\r\n");
- USART_RX_STA=0;
- }
- }else
- {
- times++;
- if(times%5000==0)
- {
- printf("\r\nALIENTEK NANO STM32開發(fā)板 串口實(shí)驗(yàn)\r\n");
- printf("正點(diǎn)原子@ALIENTEK\r\n\r\n\r\n");
- }
- if(times%200==0)printf("請(qǐng)輸入數(shù)據(jù),以回車鍵結(jié)束\r\n");
- if(times%30==0)LED0=!LED0;//閃爍LED,提示系統(tǒng)正在運(yùn)行.
- delay_ms(10);
- }
- }
- }
復(fù)制代碼
|
-
-
KEIL.7z
2020-10-31 03:45 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
79.65 KB, 下載次數(shù): 25, 下載積分: 黑幣 -5
評(píng)分
-
查看全部評(píng)分
|