|
|
基于51单片机的串口通信
- #include <REGX52.H>
- #include "Delay.h"
- #include "UART.h"
- #include "math.h"
- #include "string.h"
- #include "stdio.h"
- #include "LCD1602.h"
- sbit BUZZER=P1^7;
- sbit LED=P1^0;
- sbit IR=P2^1;
- unsigned char rx_buffer[10];
- unsigned char rx_index = 0;
- unsigned int ad_value = 0;
- bit new_data_flag = 0;
- char tx_buf[20];
- void main()
- {
- UART_Init();
- BUZZER=0;
- LED=1;
- LCD_Init();
- while(1)
- {
-
- if(new_data_flag)
- {
- new_data_flag = 0;
-
- if(ad_value > 30)
- {
- LED = 0;
- BUZZER=1;
- UartSendStr(tx_buf);
- UartSendStr("Waring!\r\n");
- }
- else
- {
- LED = 1;
- BUZZER=0; }
- sprintf(tx_buf, "SC_value=%u%%\r\n", ad_value);
-
-
- }
- LCD_ShowString(1,1,tx_buf);
- }
- }
- void UART_Routine() interrupt 4
- {
- if(RI == 1)
- {
- unsigned char ch = SBUF;
- RI = 0;
- if(ch == '\n') // ????
- {
- rx_buffer[rx_index] = '\0'; // ????????
- // ???? "ad:XXX"
- if(sscanf(rx_buffer, "SC:%u", &ad_value) == 1)
- {
- new_data_flag = 1; // ???????
- }
- rx_index = 0; // ?????
- }
- else if(ch == '\r')
- {
- // ?????
- }
- else
- {
- if(rx_index < sizeof(rx_buffer) - 1)
- rx_buffer[rx_index++] = ch; // ?????
- else
- rx_index = 0; // ?????
- }
- }
- }
复制代码
|
-
-
双机烟雾.zip
296.29 KB, 下载次数: 0, 下载积分: 黑币 -5
|