这是一篇 2017–2018 年的嵌入式学习笔记,保留当时的工具、代码和实验过程。 查看系列目录。
原图(暂不可用)
串口发送
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <reg51.h>
#define uint unsigned int #define uchar unsigned char
void delay(uint timer) { uchar j = 124; while(timer--) { while(j--); } }
void timer1_init() { TMOD = 0x20; TL1 = 0xFD; TH1 = 0xfd; TR1 = 1; }
void serial_init() { PCON = 0; SCON = 0X40; }
void main() { uchar send_data = 0; uchar i = 0; timer1_init(); serial_init(); IE = 0x00; while(1) { send_data = 'A'; for(i=0;i<26;i++) { SBUF = send_data; while(TI==0); TI=0; send_data ++; delay(100); } delay(3000); } }
|
串口收发
原图(暂不可用)
串口收发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| #include <reg51.h>
#define uint unsigned int #define uchar unsigned char
void timer1_init() { TMOD = 0x20; TL1 = 0xFD; TH1 = 0xfd; TR1 = 1; }
void serial_init() { PCON = 0X80; SCON = 0X50; }
void main() { uchar tmp_data = 0; timer1_init(); serial_init(); IE = 0x00; while(1) { while(RI==0); RI=0; tmp_data = SBUF; tmp_data+=32; SBUF = tmp_data; while(TI==0); TI=0; } }
|
串口中断实现收发
原图(暂不可用)
串口中断实现收发
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| #include <reg51.h>
#define uint unsigned int #define uchar unsigned char
void timer1_init() { TMOD = 0x20; TL1 = 0xFD; TH1 = 0xfd; TR1 = 1; }
void serial_init() { PCON = 0X80; SCON = 0X50; ES = 1; }
void main() { uchar tmp_data = 0; timer1_init(); serial_init(); EA=1; while(1) { ; } }
void serial_inter() interrupt 4 { uchar tmp_data = 0; if(RI==1) { RI = 0; tmp_data = SBUF; tmp_data += 32; SBUF = tmp_data; } if(TI==1) { TI=0; } }
|
深入串口通信
原图(暂不可用)
深入串口通信
delay.h
1 2 3 4 5 6 7 8
| #include <main.h>
#ifndef _DELAY_H #define _DELAY_H
void delay(uint timer);
#endif
|
delay.c
1 2 3 4 5 6 7 8 9 10
| #include "main.h"
void delay(uint timer) { uchar j = 124; while(timer--) { while(j--); } }
|
display.h
1 2 3 4 5 6
| #ifndef _DISPLAY_H #define _DISPLAY_H value
void disp_num(void); void horse_led(void); #endif
|
display.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| #include "reg51.h" #include "main.h" #include "delay.h"
uchar code seg[] = {0xC0,0xF9,0xA4,0xB0,0x99, 0x92,0x82,0xF8,0x80,0x90, 0xff,0x0c};
void disp_num() { uchar i = 0;
for (i = 0; i < 10; i++) { P0 = seg[i]; delay(500); } }
void horse_led(void) { uchar i = 0; for(i = 0; i < 8; i++) { P1 = ~(1<<i); delay(300); } }
|
serial.h
1 2 3 4 5 6
| #ifndef _SERIAL_H #define _SERIAL_H value
void deal_protocol(void);
#endif
|
serial.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <reg51.h> #include "main.h"
uchar g_pSendBUFF[6]; uchar g_pRecvBUFF[6]; extern uchar g_ucRecvNum; extern uchar g_ucStat;
void serial_inter() interrupt 4 { static uchar sSendNum = 0; uchar tmp = 0;
if(RI == 1) { RI = 0; tmp = SBUF; if(g_ucRecvNum==0&&tmp==0xeb) { g_pRecvBUFF[0] = tmp; g_ucRecvNum++; }else { if (g_ucRecvNum > 0) { g_pRecvBUFF[g_ucRecvNum] = tmp; g_ucRecvNum++; } }
} if (TI == 1) { TI = 0; sSendNum++; if (sSendNum < 6) { SBUF = g_pSendBUFF[sSendNum]; } else { sSendNum = 0; } } }
void deal_protocol(void) { uchar chk_sum = 0; if (g_ucRecvNum >= 6) { g_ucRecvNum = 0; if(g_pRecvBUFF[1] != 0x06) { return; } if(g_pRecvBUFF[5] != 0x90) { return; }
chk_sum = g_pRecvBUFF[1] + g_pRecvBUFF[2] + g_pRecvBUFF[3]; if (chk_sum != g_pRecvBUFF[4]) { return; }
if(g_pRecvBUFF[2] == 0x01) { switch(g_pRecvBUFF[3]) { case 1: g_ucStat = 1; g_pSendBUFF[2] = 0x01; g_pSendBUFF[3] = 0; break;
case 2: g_ucStat = 2; g_pSendBUFF[2] = 0x01; g_pSendBUFF[3] = 0;
default: break; } } else { g_pSendBUFF[2] = 0x02; g_pSendBUFF[3] = g_ucStat; }
g_pSendBUFF[0] = 0xeb; g_pSendBUFF[1] = 0x06; g_pSendBUFF[4] = g_pSendBUFF[1] + g_pSendBUFF[2] + g_pSendBUFF[3]; g_pSendBUFF[5] = 0x90; SBUF = g_pSendBUFF[0]; } }
|