• 基于C51小车测速


    目录

    前期准备:

    测速模块

    测试原理和单位换算

    定时器和中断实现测速

    蓝牙和oled屏幕显示测速


    前期准备:

    (43条消息) 基于C51语音控制小车_菜鸟成长之路丶的博客-CSDN博客

    测速模块

    • 用途:广泛用于电机转速检测,脉冲计数,位置限位等 
    • 有遮挡,输出高电平;无遮挡,输出低电平
    • 接线
         VCC 接电源正极 3.3-5V  
         GND 接电源负极
         DO TTL 开关信号输出
         AO 此模块不起作用

    测试原理和单位换算

    轮子走一圈,经过一个周长, C = 2x3.14x 半径 = 3.14 x 直径( 6.5cm
    对应的码盘也转了一圈,码盘有 20 个格子,每经过一个格子,会遮挡(高电平)和不遮挡(低电平),
    那么一个脉冲就是走了 3.14 * 6.5 cm /20 = 1.0205CM
    定时器可以设计成一秒,统计脉冲数,一个脉冲就是 1cm
    假设一秒有 80 脉冲,那么就是 80cm/s

    定时器和中断实现测速

    uart.c文件

    1. #include "reg52.h"
    2. #include "motor.h"
    3. #include "string.h"
    4. sbit D5 = P3^7;
    5. #define SIZE 12
    6. sfr AUXR = 0x8E;
    7. char buffer[SIZE];
    8. void UartInit(void) //9600bps@11.0592MHz
    9. {
    10. AUXR = 0x01;
    11. SCON = 0x50; //配置串口工作方式1,REN使能接收
    12. TMOD &= 0x0F;
    13. TMOD |= 0x20;//定时器1工作方式位8位自动重装
    14. TH1 = 0xFD;
    15. TL1 = 0xFD;//9600波特率的初值
    16. TR1 = 1;//启动定时器
    17. EA = 1;//开启总中断
    18. ES = 1;//开启串口中断
    19. }
    20. void SendByte(char mydata)
    21. {
    22. SBUF = mydata;
    23. while(!TI);
    24. TI = 0;
    25. }
    26. void SendString(char *str)
    27. {
    28. while(*str != '\0'){
    29. SendByte(*str);
    30. str++;
    31. }
    32. }
    33. //M1qian M2 hou M3 zuo M4 you
    34. void Uart_Handler() interrupt 4
    35. {
    36. static int i = 0;//静态变量,被初始化一次
    37. char tmp;
    38. if(RI)//中断处理函数中,对于接收中断的响应
    39. {
    40. RI = 0;//清除接收中断标志位
    41. tmp = SBUF;
    42. if(tmp == 'M'){
    43. i = 0;
    44. }
    45. buffer[i++] = tmp;
    46. //灯控指令
    47. if(buffer[0] == 'M'){
    48. switch(buffer[1]){
    49. case '1':
    50. goForward();
    51. break;
    52. case '2':
    53. goBack();
    54. break;
    55. case '3':
    56. goLeft();
    57. break;
    58. case '4':
    59. goRight();
    60. break;
    61. default:
    62. stop();
    63. break;
    64. }
    65. }
    66. if(i == 12) {
    67. memset(buffer, '\0', SIZE);
    68. i = 0;
    69. }
    70. }
    71. }

    uart.h文件

    1. void UartInit(void);
    2. void SendString(char *str);
    3. void SendByte(char mydata);

    time.c文件

    1. #include "motor.h"
    2. #include "reg52.h"
    3. extern unsigned int speedCnt;
    4. unsigned int speed;
    5. char signal = 0;
    6. unsigned int cnt = 0;
    7. void Time0Init()
    8. {
    9. //1. 配置定时器0工作模式位16位计时
    10. TMOD = 0x01;
    11. //2. 给初值,定一个0.5出来
    12. TL0=0x33;
    13. TH0=0xFE;
    14. //3. 开始计时
    15. TR0 = 1;
    16. TF0 = 0;
    17. //4. 打开定时器0中断
    18. ET0 = 1;
    19. //5. 打开总中断EA
    20. EA = 1;
    21. }
    22. void Time0Handler() interrupt 1
    23. {
    24. cnt++; //统计爆表的次数. cnt=1的时候,报表了1
    25. //重新给初值
    26. TL0=0x33;
    27. TH0=0xFE;
    28. if(cnt == 2000){//爆表2000次,经过了1s
    29. signal = 1;
    30. cnt = 0; //当100次表示1s,重新让cnt从0开始,计算下一次的1s
    31. //计算小车的速度,也就是拿到speedCnt的值
    32. speed = speedCnt;
    33. speedCnt = 0;//1秒后拿到speedCnt个格子,就能算出这1s的速度,格子清零
    34. }
    35. }

    time.h文件

    void Time0Init();

    motor.c文件

    1. #include "reg52.h"
    2. sbit RightCon1A = P3^7;
    3. sbit RightCon1B = P3^3;
    4. sbit LeftCon1A = P3^4;
    5. sbit LeftCon1B = P3^5;
    6. void goForward()
    7. {
    8. LeftCon1A = 0;
    9. LeftCon1B = 1;
    10. RightCon1A = 0;
    11. RightCon1B = 1;
    12. }
    13. void goRight()
    14. {
    15. LeftCon1A = 0;
    16. LeftCon1B = 1;
    17. RightCon1A = 0;
    18. RightCon1B = 0;
    19. }
    20. void goLeft()
    21. {
    22. LeftCon1A = 0;
    23. LeftCon1B = 0;
    24. RightCon1A = 0;
    25. RightCon1B = 1;
    26. }
    27. void goBack()
    28. {
    29. LeftCon1A = 1;
    30. LeftCon1B = 0;
    31. RightCon1A = 1;
    32. RightCon1B = 0;
    33. }
    34. void stop()
    35. {
    36. LeftCon1A = 0;
    37. LeftCon1B = 0;
    38. RightCon1A = 0;
    39. RightCon1B = 0;
    40. }

    motor.h文件

    1. void goRight();
    2. void goLeft();
    3. void goForward();
    4. void goBack();
    5. void stop();

    delay.c文件

    1. #include "intrins.h"
    2. void Delay1000ms() //@11.0592MHz
    3. {
    4. unsigned char i, j, k;
    5. _nop_();
    6. i = 8;
    7. j = 1;
    8. k = 243;
    9. do
    10. {
    11. do
    12. {
    13. while (--k);
    14. } while (--j);
    15. } while (--i);
    16. }

    delay.h文件

    void Delay1000ms();

    main.c文件

    1. /**** Author: 上官可编程(抖音) ************
    2. **** 录播课堂代码,为小白学习功能实现 ****
    3. **** 代码合理性和效率不做评估 ************
    4. **** 2022/3/22 3:35 **********************
    5. ********************************************/
    6. #include "motor.h"
    7. #include "delay.h"
    8. #include "uart.h"
    9. #include "reg52.h"
    10. #include "time.h"
    11. #include "stdio.h"
    12. sbit speedIO = P3^2;//外部中断0
    13. unsigned int speedCnt = 0; //统计格子,脉冲次数
    14. extern unsigned int speed;//速度
    15. extern char signal; //主程序发速度数据的通知
    16. char speedMes[24]; //主程序发送速度数据的字符串缓冲区
    17. void Ex0Init()
    18. {
    19. EX0 = 1;//允许中断
    20. //EA = 1;在串口初始化函数中已经打开了总中断
    21. IT0 = 1;//外部中断的下降沿触发
    22. }
    23. void main()
    24. {
    25. Time0Init();//定时器0初始化
    26. UartInit();//串口相关初始化
    27. //外部中断初始化
    28. Ex0Init();
    29. while(1){
    30. if(signal){//定时器1s到点,把signal置一,主程序发送速度
    31. sprintf(speedMes,"speed:%d cm/s",speed);//串口数据的字符串拼装,speed是格子,每个格子1cm
    32. SendString(speedMes);//速度发出去
    33. signal = 0;//清0speed,下次由定时器1s后的中断处理中再置一
    34. }
    35. }
    36. }
    37. void speedHandler() interrupt 0 //外部中断处理函数
    38. {
    39. speedCnt++;//码盘转动了一个格子
    40. }

    支持蓝牙和oled屏幕显示测速

    除了以下几个文件要改,其他不需要修改,沿用上面的

    Oled.c文件

    1. #include "reg52.h"
    2. #include "intrins.h"
    3. #include "Oledfont.h"
    4. sbit scl = P1^2;
    5. sbit sda = P1^3;
    6. void IIC_Start()
    7. {
    8. scl = 0;
    9. sda = 1;
    10. scl = 1;
    11. _nop_();
    12. sda = 0;
    13. _nop_();
    14. }
    15. void IIC_Stop()
    16. {
    17. scl = 0;
    18. sda = 0;
    19. scl = 1;
    20. _nop_();
    21. sda = 1;
    22. _nop_();
    23. }
    24. char IIC_ACK()
    25. {
    26. char flag;
    27. sda = 1;//就在时钟脉冲9期间释放数据线
    28. _nop_();
    29. scl = 1;
    30. _nop_();
    31. flag = sda;
    32. _nop_();
    33. scl = 0;
    34. _nop_();
    35. return flag;
    36. }
    37. void IIC_Send_Byte(char dataSend)
    38. {
    39. int i;
    40. for(i = 0;i<8;i++){
    41. scl = 0;//scl拉低,让sda做好数据准备
    42. sda = dataSend & 0x80;//1000 0000获得dataSend的最高位,给sda
    43. _nop_();//发送数据建立时间
    44. scl = 1;//scl拉高开始发送
    45. _nop_();//数据发送时间
    46. scl = 0;//发送完毕拉低
    47. _nop_();//
    48. dataSend = dataSend << 1;
    49. }
    50. }
    51. void Oled_Write_Cmd(char dataCmd)
    52. {
    53. // 1. start()
    54. IIC_Start();
    55. //
    56. // 2. 写入从机地址 b0111 1000 0x78
    57. IIC_Send_Byte(0x78);
    58. // 3. ACK
    59. IIC_ACK();
    60. // 4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
    61. IIC_Send_Byte(0x00);
    62. // 5. ACK
    63. IIC_ACK();
    64. //6. 写入指令/数据
    65. IIC_Send_Byte(dataCmd);
    66. //7. ACK
    67. IIC_ACK();
    68. //8. STOP
    69. IIC_Stop();
    70. }
    71. void Oled_Write_Data(char dataData)
    72. {
    73. // 1. start()
    74. IIC_Start();
    75. //
    76. // 2. 写入从机地址 b0111 1000 0x78
    77. IIC_Send_Byte(0x78);
    78. // 3. ACK
    79. IIC_ACK();
    80. // 4. cotrol byte: (0)(0)000000 写入命令 (0)(1)000000写入数据
    81. IIC_Send_Byte(0x40);
    82. // 5. ACK
    83. IIC_ACK();
    84. ///6. 写入指令/数据
    85. IIC_Send_Byte(dataData);
    86. //7. ACK
    87. IIC_ACK();
    88. //8. STOP
    89. IIC_Stop();
    90. }
    91. void Oled_Init(void){
    92. Oled_Write_Cmd(0xAE);//--display off
    93. Oled_Write_Cmd(0x00);//---set low column address
    94. Oled_Write_Cmd(0x10);//---set high column address
    95. Oled_Write_Cmd(0x40);//--set start line address
    96. Oled_Write_Cmd(0xB0);//--set page address
    97. Oled_Write_Cmd(0x81); // contract control
    98. Oled_Write_Cmd(0xFF);//--128
    99. Oled_Write_Cmd(0xA1);//set segment remap
    100. Oled_Write_Cmd(0xA6);//--normal / reverse
    101. Oled_Write_Cmd(0xA8);//--set multiplex ratio(1 to 64)
    102. Oled_Write_Cmd(0x3F);//--1/32 duty
    103. Oled_Write_Cmd(0xC8);//Com scan direction
    104. Oled_Write_Cmd(0xD3);//-set display offset
    105. Oled_Write_Cmd(0x00);//
    106. Oled_Write_Cmd(0xD5);//set osc division
    107. Oled_Write_Cmd(0x80);//
    108. Oled_Write_Cmd(0xD8);//set area color mode off
    109. Oled_Write_Cmd(0x05);//
    110. Oled_Write_Cmd(0xD9);//Set Pre-Charge Period
    111. Oled_Write_Cmd(0xF1);//
    112. Oled_Write_Cmd(0xDA);//set com pin configuartion
    113. Oled_Write_Cmd(0x12);//
    114. Oled_Write_Cmd(0xDB);//set Vcomh
    115. Oled_Write_Cmd(0x30);//
    116. Oled_Write_Cmd(0x8D);//set charge pump enable
    117. Oled_Write_Cmd(0x14);//
    118. Oled_Write_Cmd(0xAF);//--turn on oled panel
    119. }
    120. void Oled_Clear()
    121. {
    122. unsigned char i,j; //-128 --- 127
    123. for(i=0;i<8;i++){
    124. Oled_Write_Cmd(0xB0 + i);//page0--page7
    125. //每个page从0列
    126. Oled_Write_Cmd(0x00);
    127. Oled_Write_Cmd(0x10);
    128. //0到127列,依次写入0,每写入数据,列地址自动偏移
    129. for(j = 0;j<128;j++){
    130. Oled_Write_Data(0);
    131. }
    132. }
    133. }
    134. void Oled_Show_Char(char row,char col,char oledChar){ //row*2-2
    135. unsigned int i;
    136. Oled_Write_Cmd(0xb0+(row*2-2)); //page 0
    137. Oled_Write_Cmd(0x00+(col&0x0f)); //low
    138. Oled_Write_Cmd(0x10+(col>>4)); //high
    139. for(i=((oledChar-32)*16);i<((oledChar-32)*16+8);i++){
    140. Oled_Write_Data(F8X16[i]); //写数据oledTable1
    141. }
    142. Oled_Write_Cmd(0xb0+(row*2-1)); //page 1
    143. Oled_Write_Cmd(0x00+(col&0x0f)); //low
    144. Oled_Write_Cmd(0x10+(col>>4)); //high
    145. for(i=((oledChar-32)*16+8);i<((oledChar-32)*16+8+8);i++){
    146. Oled_Write_Data(F8X16[i]); //写数据oledTable1
    147. }
    148. }
    149. /******************************************************************************/
    150. // 函数名称:Oled_Show_Char
    151. // 输入参数:oledChar
    152. // 输出参数:无
    153. // 函数功能:OLED显示单个字符
    154. /******************************************************************************/
    155. void Oled_Show_Str(char row,char col,char *str){
    156. while(*str!=0){
    157. Oled_Show_Char(row,col,*str);
    158. str++;
    159. col += 8;
    160. }
    161. }

    Oled.h文件

    1. void Oled_Write_Cmd(char dataCmd);
    2. void Oled_Write_Data(char dataData);
    3. void Oled_Init(void);
    4. void Oled_Clear();
    5. void Oled_Show_Str(char row,char col,char *str);

    Oledfont.h

    1. const unsigned char code F8X16[]=
    2. {
    3. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
    4. 0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
    5. 0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
    6. 0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
    7. 0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
    8. 0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
    9. 0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
    10. 0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
    11. 0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
    12. 0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
    13. 0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
    14. 0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
    15. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
    16. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
    17. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
    18. 0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
    19. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
    20. 0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
    21. 0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
    22. 0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
    23. 0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
    24. 0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
    25. 0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
    26. 0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
    27. 0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
    28. 0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
    29. 0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
    30. 0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
    31. 0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
    32. 0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
    33. 0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
    34. 0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
    35. 0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
    36. 0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
    37. 0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
    38. 0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
    39. 0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
    40. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
    41. 0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
    42. 0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
    43. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
    44. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
    45. 0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
    46. 0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
    47. 0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
    48. 0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
    49. 0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
    50. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
    51. 0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
    52. 0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
    53. 0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
    54. 0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
    55. 0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
    56. 0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
    57. 0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
    58. 0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
    59. 0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
    60. 0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
    61. 0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
    62. 0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
    63. 0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
    64. 0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
    65. 0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
    66. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
    67. 0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
    68. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
    69. 0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
    70. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
    71. 0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
    72. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
    73. 0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
    74. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
    75. 0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
    76. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
    77. 0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
    78. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
    79. 0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
    80. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
    81. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
    82. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
    83. 0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
    84. 0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
    85. 0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
    86. 0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
    87. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
    88. 0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
    89. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
    90. 0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
    91. 0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
    92. 0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
    93. 0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
    94. 0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
    95. 0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
    96. 0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
    97. 0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
    98. };

    main.c文件

    1. #include "motor.h"
    2. #include "delay.h"
    3. #include "uart.h"
    4. #include "reg52.h"
    5. #include "time.h"
    6. #include "stdio.h"
    7. #include "Oled.h"
    8. sbit speedIO = P3^2;//外部中断0
    9. unsigned int speedCnt = 0; //统计格子,脉冲次数
    10. extern unsigned int speed;//速度
    11. extern char signal; //主程序发速度数据的通知
    12. char speedMes[24]; //主程序发送速度数据的字符串缓冲区
    13. void Ex0Init()
    14. {
    15. EX0 = 1;//允许中断
    16. //EA = 1;在串口初始化函数中已经打开了总中断
    17. IT0 = 1;//外部中断的下降沿触发
    18. }
    19. void main()
    20. {
    21. Time0Init();//定时器0初始化
    22. UartInit();//串口相关初始化
    23. //外部中断初始化
    24. Ex0Init();
    25. Oled_Init();
    26. Oled_Clear();
    27. while(1){
    28. if(signal){//定时器1s到点,把signal置一,主程序发送速度
    29. sprintf(speedMes,"speed:%d cm/s",speed);//串口数据的字符串拼装,speed是格子,每个格子1cm
    30. SendString(speedMes);//速度发出去
    31. signal = 0;//清0speed,下次由定时器1s后的中断处理中再置一
    32. }
    33. Oled_Show_Str(2,2,speedMes);
    34. }
    35. }
    36. void speedHandler() interrupt 0 //外部中断处理函数
    37. {
    38. speedCnt++;//码盘转动了一个格子
    39. }

    wifi模块测速

    利用ESP8266模块来连接网络,模块可看(43条消息) C51之ESP-01s_菜鸟成长之路丶的博客-CSDN博客

    除了以下要修改的代码,其余文件需补充第一个案例的完整代码才能运行

    代码:

    esp8266.c文件

    1. #include "uart.h"
    2. //1 工作在路由模式
    3. code char LYMO[] = "AT+CWMODE=2\r\n";
    4. //2 使能多链接
    5. code char DLJ[] = "AT+CIPMUX=1\r\n";
    6. //3 建立TCPServer
    7. code char JLFW[] = "AT+CIPSERVER=1\r\n"; // default port = 333
    8. char AT_OK_Flag = 0; //OK返回值的标志位
    9. char Client_Connect_Flag = 0;
    10. void initWifi_AP()
    11. {
    12. SendString(LYMO);
    13. while(!AT_OK_Flag);
    14. AT_OK_Flag = 0;
    15. SendString(DLJ);
    16. while(!AT_OK_Flag);
    17. AT_OK_Flag = 0;
    18. }
    19. void waitConnect()
    20. {
    21. SendString(JLFW);
    22. while(!Client_Connect_Flag);
    23. AT_OK_Flag = 0;
    24. }

    esp8266.h文件

    1. void initWifi_AP();
    2. void waitConnect();

    main.c文件

    1. #include "motor.h"
    2. #include "delay.h"
    3. #include "uart.h"
    4. #include "reg52.h"
    5. #include "time.h"
    6. #include "stdio.h"
    7. #include "Oled.h"
    8. #include "esp8266.h"
    9. sbit speedIO = P3^2;//外部中断0
    10. unsigned int speedCnt = 0; //统计格子,脉冲次数
    11. extern unsigned int speed;//速度
    12. extern char signal; //主程序发速度数据的通知
    13. char speedMes[24]; //主程序发送速度数据的字符串缓冲区
    14. //发送数据
    15. char FSSJ[] = "AT+CIPSEND=0,5\r\n";
    16. void Ex0Init()
    17. {
    18. EX0 = 1;//允许中断
    19. //EA = 1;在串口初始化函数中已经打开了总中断
    20. IT0 = 1;//外部中断的下降沿触发
    21. }
    22. void main()
    23. {
    24. Time0Init();//定时器0初始化
    25. UartInit();//串口相关初始化
    26. Delay1000ms();//给espwifi模块上电时间
    27. initWifi_AP(); //初始化wifi工作在ap模式
    28. waitConnect(); //等待客户端的连接
    29. //外部中断初始化
    30. Ex0Init();
    31. Oled_Init();
    32. Oled_Clear();
    33. while(1){
    34. if(signal){//定时器1s到点,把signal置一,主程序发送速度
    35. SendString(FSSJ);
    36. Delay1000ms();
    37. sprintf(speedMes,"%dcms",speed);//串口数据的字符串拼装,speed是格子,每个格子1cm
    38. SendString(speedMes);//速度发出去
    39. signal = 0;//清0speed,下次由定时器1s后的中断处理中再置一
    40. }
    41. Oled_Show_Str(2,2,speedMes);
    42. }
    43. }
    44. void speedHandler() interrupt 0 //外部中断处理函数
    45. {
    46. speedCnt++;//码盘转动了一个格子
    47. }

  • 相关阅读:
    深度学习——卷积神经网络压缩方法总结(等待补充)
    Molecular Plant | ChIP-seq+RNA-seq解析E2F转录因子在植物复制胁迫响应中的独特和互补作用
    Linux端口及端口监听
    【SpringCloud】微服务技术栈入门6 - RestClient深入
    vue中el-table的行拖拽和el-dialog中表格的行拖拽
    如何查询淘宝天猫的宝贝类目
    Auto-WEKA(Waikato Environment for Knowledge Analysis)
    mysql中的各种日志文件redo log、undo log和binlog
    STM32 从0开始系统学习2
    个人博客系统(附源码)
  • 原文地址:https://blog.csdn.net/weixin_49001476/article/details/127354730