当前位置:   article > 正文

蓝桥杯单片机设计与开发必背笔记模板_auxr &= 0xef;

auxr &= 0xef;

前言

本人为2023年蓝桥杯单片机设计与开发省赛一等奖国赛三等奖获奖选手。

能力并不是很强,比我强的同学也大有所在,不过也是希望能给即将参加蓝桥杯比赛的同学提供一些力所能及的帮助或者启示。

学习之路老师推荐:bilibili:小蜜蜂老师的干货铺,bilibili:Alice_西风

其实无论学习什么都要学会总结笔记,别人做的再好,你也很难记得住。

看完课程之后可以试着总结一些通用的底层代码并加以记忆。这样考试的时候就可以提前写出来个模板出来,再往里面填逻辑代码不就行了~

基础代码

初始化

  1. #include "Init.h"
  2. void Init_System()
  3. {
  4. P0=0xff;
  5. P2=P2&0x1f|0x80;
  6. P2&=0x1f;
  7. P0=0x00;
  8. P2=P2&0x1f|0xa0;
  9. P2&=0x1f;
  10. }

矩阵按键

  1. #include "Key.h"
  2. unsigned char Key_Read()
  3. {
  4. unsigned char temp;
  5. //AUXR&=0xef; //关闭T2R,即定时器2 避免串口和矩阵按键互相干扰
  6. P44=0;P42=1;P35=1;P34=1;
  7. if(P33==0)temp=4;
  8. if(P32==0)temp=5;
  9. if(P31==0)temp=6;
  10. if(P30==0)temp=7;
  11. P44=1;P42=0;P35=1;P34=1;
  12. if(P33==0)temp=8;
  13. if(P32==0)temp=9;
  14. if(P31==0)temp=10;
  15. if(P30==0)temp=11;
  16. P44=1;P42=1;P35=0;P34=1;
  17. if(P33==0)temp=12;
  18. if(P32==0)temp=13;
  19. if(P31==0)temp=14;
  20. if(P30==0)temp=15;
  21. P44=1;P42=1;P35=1;P34=0;
  22. if(P33==0)temp=16;
  23. if(P32==0)temp=17;
  24. if(P31==0)temp=18;
  25. if(P30==0)temp=19;
  26. //P3=0xff; //清除
  27. //AUXR|=0x10; //打开T2R,即定时器2(串口波特率发生所用的定时器)
  28. return temp;
  29. }

数码管

  1. #include "Seg.h"
  2. code unsigned char seg_dula[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff,0x8e,0x89,0x92,0xc6};
  3. code unsigned char seg_wela[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
  4. void Seg_Disp(unsigned char wela,dula,point)
  5. {
  6. P0=0xff;
  7. P2=P2&0x1f|0xe0;
  8. P2&=0x1f;
  9. P0=seg_wela[wela];
  10. P2=P2&0x1f|0xc0;
  11. P2&=0x1f;
  12. P0=seg_dula[dula];
  13. if(point)
  14. P0&=0x7f;
  15. P2=P2&0x1f|0xe0;
  16. P2&=0x1f;
  17. }

LED、BEEP、Relay

  1. #include "Led.h"
  2. void Led_Disp(unsigned char addr,flag)
  3. {
  4. static unsigned char temp_l,temp_old_l;
  5. if(flag)
  6. temp_l|=0x01<<addr;
  7. else
  8. temp_l&=~0x01<<addr;
  9. if(temp_l!=temp_old_l)
  10. {
  11. P0=~temp_l;
  12. P2=P2&0x1f|0x80;
  13. P2&=0x1f;
  14. temp_old_l=temp_l;
  15. }
  16. }
  17. void BEEP(unsigned char flag)
  18. {
  19. if(flag)
  20. temp|=0x40;
  21. else
  22. temp&=~0x40;
  23. if(temp!=temp_old)
  24. {
  25. P0=temp;
  26. P2=P2&0x1f|0xa0;
  27. P2&=0x1f;
  28. temp_old=temp;
  29. }
  30. }
  31. void Realy(unsigned char flag)
  32. {
  33. if(flag)
  34. temp|=0x10;
  35. else
  36. temp&=~0x10;
  37. if(temp!=temp_old)
  38. {
  39. P0=temp;
  40. P2=P2&0x1f|0xa0;
  41. P2&=0x1f;
  42. temp_old=temp;
  43. }
  44. }

串口

  1. #include "Uart.h"
  2. void UartInit(void) //9600bps@12.000MHz
  3. {
  4. SCON = 0x50; //8???,?????
  5. AUXR |= 0x01; //??1?????2???????
  6. AUXR &= 0xFB; //?????12T??
  7. T2L = 0xE6; //???????
  8. T2H = 0xFF; //???????
  9. AUXR |= 0x10; //???2????
  10. ES=1;
  11. EA=1;
  12. }
  13. extern putchar(char c)
  14. {
  15. SBUF=c;
  16. while(TI==0);
  17. TI=0;
  18. return c;
  19. }

超声波

  1. #include "utlrasound.h"
  2. #include "intrins.h"
  3. sbit Tx=P1^0;
  4. sbit Rx=P1^1;
  5. void Delay12us() //@12.000MHz
  6. {
  7. unsigned char i;
  8. _nop_();
  9. _nop_();
  10. i = 38;
  11. while (--i);
  12. }
  13. void Ut_Ware_Init()
  14. {
  15. unsigned char i;
  16. for(i=0;i<8;i++)
  17. {
  18. Tx=1;
  19. Delay12us();
  20. Tx=0;
  21. Delay12us();
  22. }
  23. }
  24. unsigned char Ut_Ware_Data()
  25. {
  26. unsigned int time;
  27. CMOD=0x00;
  28. CH=CL=0;
  29. Ut_Ware_Init();
  30. CR=1;
  31. while((Rx==1)&&(CF==0));
  32. CR=0;
  33. if(CF==0)
  34. {
  35. time=CH<<8|CL;
  36. return time*0.017;
  37. }
  38. else
  39. {
  40. CF=0;
  41. return 0;
  42. }
  43. }

时钟DS1302

  1. #include "ds1302.h"
  2. #include "reg52.h"
  3. #include "intrins.h"
  4. sbit SCK=P1^7;
  5. sbit SDA=P2^3;
  6. sbit RST=P1^3;
  7. ..........
  8. void Set_Rtc(unsigned char* ucRtc)
  9. {
  10. unsigned char i;
  11. Write_Ds1302_Byte(0x8e,0x00);
  12. for(i=0;i<3;i++)
  13. Write_Ds1302_Byte(0x84-2*i,ucRtc[i]);
  14. Write_Ds1302_Byte(0x8e,0x80);
  15. }
  16. void Get_Rtc(unsigned char* ucRtc)
  17. {
  18. unsigned char i;
  19. for(i=0;i<3;i++)
  20. ucRtc[i]=Read_Ds1302_Byte(0x85-2*i);
  21. }

光敏电阻和EEPROM

  1. #include "iic.h"
  2. #include "reg52.h"
  3. #include "intrins.h"
  4. sbit sda=P2^1;
  5. sbit scl=P2^0;
  6. ........
  7. void Da_Wirte(unsigned char dat)
  8. {
  9. I2CStart();
  10. I2CSendByte(0x90);
  11. I2CWaitAck();
  12. I2CSendByte(0x41);
  13. I2CWaitAck();
  14. I2CSendByte(dat);
  15. I2CWaitAck();
  16. I2CStop();
  17. }
  18. unsigned char Ad_Read(unsigned char addr)
  19. {
  20. unsigned char temp;
  21. I2CStart();
  22. I2CSendByte(0x90);
  23. I2CWaitAck();
  24. I2CSendByte(addr);
  25. I2CWaitAck();
  26. I2CStart();
  27. I2CSendByte(0x91);
  28. I2CWaitAck();
  29. temp=I2CReceiveByte();
  30. I2CSendAck(1);
  31. I2CStop();
  32. return temp;
  33. }
  34. void EEPROM_Write(unsigned char* EEPROM_String,unsigned char addr,num)
  35. {
  36. I2CStart();
  37. I2CSendByte(0xa0);
  38. I2CWaitAck();
  39. I2CSendByte(addr);
  40. I2CWaitAck();
  41. while(num--)
  42. {
  43. I2CSendByte(*EEPROM_String++);
  44. I2CWaitAck();
  45. I2C_Delay(200);
  46. }
  47. I2CStop();
  48. }
  49. void EEPROM_Read(unsigned char* EEPROM_String,unsigned char addr,num)
  50. {
  51. I2CStart();
  52. I2CSendByte(0xa0);
  53. I2CWaitAck();
  54. I2CSendByte(addr);
  55. I2CWaitAck();
  56. I2CStart();
  57. I2CSendByte(0xa1);
  58. I2CWaitAck();
  59. while(num--)
  60. {
  61. *EEPROM_String++=I2CReceiveByte();
  62. if(num) I2CSendAck(0);
  63. I2CSendAck(1);
  64. }
  65. I2CStop();
  66. }

温度

  1. #include "onewire.h"
  2. #include "reg52.h"
  3. sbit DQ=P1^4;
  4. ........
  5. float Temp_Read()
  6. {
  7. unsigned char low,high;
  8. init_ds18b20();
  9. Write_DS18B20(0xcc);
  10. Write_DS18B20(0x44);
  11. init_ds18b20();
  12. Write_DS18B20(0xcc);
  13. Write_DS18B20(0xbe);
  14. low=Read_DS18B20();
  15. high=Read_DS18B20();
  16. return (high<<8|low)/16.0;
  17. }

C51类型.h文件模板

  1. #ifndef __DS1302_H
  2. #define __DS1302_H
  3. #endif

功能划分

LED频率亮灭

例:当按下按键4时,LED0以0.2秒为闪烁,亮灭10次

  1. xdata unsigned long int Ms_Tick;//系统计时器
  2. case 4:
  3. ........
  4. Measurement_Complete_Flag = 1;//拉高测量完成标志位
  5. ........
  6. break;
  7. ucLed[0] = Measurement_Complete_Flag?Led_Star_Flag?1:0:0;
  8. //按键按下时Measurement_Complete_Flag为真,当定时器为0.2秒把Led_Star_Flag置为真时,
  9. //定时器中写
  10. /* 计时相关 */
  11. Ms_Tick++;
  12. if(Measurement_Complete_Flag)
  13. {
  14. if(!(Ms_Tick % 1000))
  15. {
  16. Led_Star_Flag ^= 1;
  17. if(++Count == 6) //亮5次灭5次
  18. {
  19. Count = 0;
  20. Measurement_Complete_Flag = 0;
  21. }
  22. }
  23. }

频率输出

  1. void Timer0_Init(void) //1??@12.000MHz
  2. {
  3. AUXR &= 0x7F; //?????12T??
  4. TMOD &= 0xF0; //???????
  5. TMOD |= 0x05;
  6. TL0 = 0; //???????
  7. TH0 = 0; //???????
  8. TF0 = 0; //??TF0??
  9. TR0 = 1; //???0????
  10. }
  11. void Timer1Server() interrupt 3
  12. {
  13. if(++Key_slow_down==20)Key_slow_down=0;
  14. if(++Seg_slow_down==500)Seg_slow_down=0;
  15. if(++Uart_slow_down==200)Uart_slow_down=0;
  16. if(++Seg_Pos==8)Seg_Pos=0;
  17. Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
  18. Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
  19. if(++Timer_1000ms==1000)
  20. {
  21. TR0=0;
  22. Timer_1000ms=0;
  23. Freq=TH0<<8|TL0;
  24. TH0=TL0=0;
  25. TR0=1;
  26. }
  27. }
  28. Seg_Buf[3]=Freq/10000%10;
  29. Seg_Buf[4]=Freq/1000%10;
  30. Seg_Buf[5]=Freq/100%10;
  31. Seg_Buf[6]=Freq/10%10;
  32. Seg_Buf[7]=Freq%10;
  33. pos=3; //定义起始扫描变量
  34. while(Seg_Buf[pos]==0) //高位为零时熄灭
  35. {
  36. Seg_Buf[pos]=10;
  37. if(++pos==(7-(int)Hz_flag))break;
  38. }

读取温度

  1. //temp为float类型
  2. temp=Temp_Read();
  3. Seg_Buf[4]=(unsigned char)temp/10%10;
  4. Seg_Buf[5]=(unsigned char)temp%10;
  5. Seg_Buf[6]=(unsigned int)(temp*100)/10%10;
  6. Seg_Buf[7]=(unsigned int)(temp*100)%10;
  7. Seg_Point[5]=1;

读取电压

  1. //d_val为float类型
  2. d_val=Ad_Read(0x43)/51.0; //0x43为光敏,0x41为电阻,/51.0为数据转换
  3. Seg_Point[5]=1;
  4. Seg_Buf[5]=(unsigned char)d_val%10;
  5. Seg_Buf[6]=(unsigned int)(d_val*100)/10%10;
  6. Seg_Buf[7]=(unsigned int)(d_val*100)%10;

写入电压

  1. //写入0.4V,需要*51进行数据转换
  2. Da_Write(51*0.4);

写入EEPROM

  1. //当写入数据为数组时,中间变量务必是8的倍数,最后变量为写入数据长度
  2. EEPROM_Write(dat,0,2);
  3. //当写入数据为变量时
  4. EEPROM_Write(&a,8,1);

读取EEPROM

  1. //读取的数据写入数组时,中间变量务必是8的倍数,最后变量为写入数据长度
  2. EEPROM_Read(dat,0,2);
  3. //当读取的数据为变量时
  4. EEPROM_Write(&a,8,1);

时钟初始化

  1. unsigned char ucRtc_disp[3]={0x16,0x59,0x50};
  2. Set_Rtc(ucRtc_disp);

时钟写入

Get_Rtc(ucRtc_disp);

串口

  1. void Uart_Proc()
  2. {
  3. if(Uart_Slow_Down) return;
  4. Uart_Slow_Down = 1;
  5. if(Uart_Recv_Index>0) //接收到数据
  6. {
  7. if(Uart_Recv_Index==4||Uart_Recv_Index==6) //数据长度
  8. {
  9. if(Uart_Recv[0] == 'S' && Uart_Recv[1] == 'T' && Uart_Recv[2] == '\r' && Uart_Recv[3] == '\n')
  10. printf("$%d,%.2f\r\n",(unsigned int)distance,temperature);
  11. else if(Uart_Recv[0] == 'P' && Uart_Recv[1] == 'A' && Uart_Recv[2] == 'R' && Uart_Recv[3] == 'A' && Uart_Recv[4] == '\r' && Uart_Recv[5] == '\n')
  12. printf("#%d,%d\r\n",(unsigned int)Parm_Ctrol[0],(unsigned int)Parm_Ctrol[1]);
  13. else printf("ERROR\r\n");
  14. }
  15. else
  16. {
  17. printf("ERROR\r\n");
  18. }
  19. if(Uart_Flag == 1)
  20. {
  21. Uart_Flag = 0;
  22. printf("ERROR\r\n");
  23. }
  24. Uart_Flag = Uart_Recv_Index = 0;
  25. }
  26. }

长按短按

  1. if(Key_Down == 12 || Key_Down == 13) //假设按键12、13
  2. Key_Flag = 1;
  3. if(Timer_1000Ms < 1000 && Key_Flag == 1) //短按
  4. {
  5. switch(Key_Up)
  6. {
  7. case 12: //短按12
  8. break;
  9. case 13: //短按13
  10. break;
  11. }
  12. }
  13. else //长按
  14. {
  15. switch(Key_Old)
  16. {
  17. case 12:
  18. break;
  19. case 13:
  20. break;
  21. }
  22. }

数组储存历史数据

  1. unsigned char Ultra_Data[2];//超声波测距数据储存数组 0-本次测量 1-上次测量
  2. unsigned char Historical_Data[10];//历史储存数据
  3. unsigned char Historical_Data_Index;//历史储存数据索引
  4. switch(Key_Down)
  5. {
  6. case 4:
  7. Ultra_Data[1] = Ultra_Data[0];//储存上一次测量值
  8. Ultra_Data[0] = Rd_Ulra();//读取这次测量值
  9. Measurement_Complete_Flag = 1;//拉高测量完成标志位
  10. Historical_Data[Historical_Data_Index] = Ultra_Data[1];//保存历史值
  11. if(++Historical_Data_Index == 9)
  12. Historical_Data_Index = 0;
  13. break;
  14. }

PWM

例:若当前测量到的频率数据大于频率参数,通过竞赛板上的电机驱动引脚N MOTOR(J3-6)输出 1KHz 80%占空比的脉冲信号:否则该引脚输出 1KHz 20%占空比的脉冲信号。

  1. unsigned char PWM_Count;//脉宽频率计数值 1KHZ = 1MS
  2. unsigned char PWM_Level;//占空比数据储存变量
  3. PWM_Level = ucLed[3]?8:2;
  4. /* 定时器2中断初始化函数 */
  5. void Timer2Init(void) //100微秒@12.000MHz
  6. {
  7. AUXR &= 0xFB; //定时器时钟12T模式
  8. T2L = 0x9C; //设置定时初始值
  9. T2H = 0xFF; //设置定时初始值
  10. AUXR |= 0x10; //定时器2开始计时
  11. IE2 |= (1<<2); //定时器中断2打开
  12. }
  13. void Timer2Server() interrupt 12
  14. {
  15. if(++PWM_Count == 10) PWM_Count = 0;//脉冲周期为1MS
  16. Pulse(PWM_Count < PWM_Level);//PWM输出
  17. }

定时器2

  1. void Timer2_Init(void) //100微秒@12.000MHz
  2. {
  3. AUXR &= 0xFB; //定时器时钟12T模式
  4. T2L = 0x9C; //设置定时初始值
  5. T2H = 0xFF; //设置定时初始值
  6. AUXR |= 0x10; //定时器2开始计时
  7. IE2 |= (1<<2); //******加上这句*******//
  8. }
  9. void Timer2Server() interrupt 12
  10. {
  11. }

主函数main模板

  1. #include "Init.h"
  2. #include "Key.h"
  3. #include "Seg.h"
  4. #include "Led.h"
  5. #include "Uart.h"
  6. #include "stdio.h"
  7. #include "onewire.h"
  8. #include "iic.h"
  9. unsigned char Key_val,Key_down,Key_up,Key_old;
  10. unsigned char Key_slow_down;
  11. unsigned char Seg_Buf[8]={10,10,10,10,10,10,10,10};
  12. unsigned char Seg_Point[8]={0,0,0,0,0,0,0,0};
  13. unsigned char Seg_Pos;
  14. unsigned char Seg_slow_down;
  15. unsigned char ucLed[8]={0,0,0,0,0,0,0,0};
  16. unsigned char Uart_Recv[10];
  17. unsigned char Uart_Read[10];
  18. unsigned char Uart_recv_index;
  19. unsigned char Uart_slow_down;
  20. void Key_Porc()
  21. {
  22. if(Key_slow_down)return;
  23. Key_slow_down=1;
  24. Key_val=Key_Read();
  25. Key_down=Key_val&(Key_old^Key_val);
  26. Key_up=~Key_val&(Key_old^Key_val);
  27. Key_old=Key_val;
  28. }
  29. void Seg_Porc()
  30. {
  31. if(Seg_slow_down)return;
  32. Seg_slow_down=1;
  33. }
  34. void Led_Porc()
  35. {
  36. }
  37. void Uart_Porc()
  38. {
  39. if(Uart_slow_down)return;
  40. Uart_slow_down=1;
  41. }
  42. void Timer1_Init(void) //1??@12.000MHz
  43. {
  44. AUXR &= 0xBF; //?????12T??
  45. TMOD &= 0x0F; //???????
  46. TL1 = 0x18; //???????
  47. TH1 = 0xFC; //???????
  48. TF1 = 0; //??TF1??
  49. TR1 = 1; //???1????
  50. ET1=1;
  51. EA=1;
  52. }
  53. void Timer1Server() interrupt 3
  54. {
  55. if(++Key_slow_down==20)Key_slow_down=0;
  56. if(++Seg_slow_down==500)Seg_slow_down=0;
  57. if(++Uart_slow_down==200)Uart_slow_down=0;
  58. if(++Seg_Pos==8)Seg_Pos=0;
  59. Seg_Disp(Seg_Pos,Seg_Buf[Seg_Pos],Seg_Point[Seg_Pos]);
  60. Led_Disp(Seg_Pos,ucLed[Seg_Pos]);
  61. }
  62. void UartServer() interrupt 4
  63. {
  64. if(RI==1)
  65. {
  66. RI=0;
  67. Uart_Recv[Uart_recv_index]=SBUF;
  68. Uart_recv_index++;
  69. }
  70. if(Uart_recv_index > 6) //存储6个数据到数组
  71. {
  72. Uart_recv_index = 0;
  73. //功能
  74. }
  75. }
  76. void main()
  77. {
  78. Init_System();
  79. Timer1_Init();
  80. Timer0_Init();
  81. UartInit();
  82. while(1)
  83. {
  84. Key_Porc();
  85. Seg_Porc();
  86. Led_Porc();
  87. Uart_Porc();
  88. }
  89. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/560784
推荐阅读
相关标签
  

闽ICP备14008679号