当前位置:   article > 正文

第十四届蓝桥杯单片机省赛真题_蓝桥杯14届单片机省赛真题

蓝桥杯14届单片机省赛真题

逻辑部分纯手写简单 零基础模板套用即可

main.c

  1. #include "smg.h"
  2. #include "key.h"
  3. #include "led.h"
  4. #include "iic.h"
  5. #include "onewire.h"
  6. #include "ds1302.h"
  7. #include "timer.h"
  8. #include "uart.h"
  9. #include "ult.h"
  10. uchar display_state;
  11. uchar LED[] = {0,0,0,0,0,0,0,0};
  12. uchar SMG[] = {10,10,10,10,10,10,10,10};
  13. uchar dot[] = {0,0,0,0,0,0,0,0};
  14. uchar distance_shezhi = 30;
  15. uchar light_value;
  16. uint temp;
  17. uchar state;
  18. uchar distance_now;
  19. void led_pro()
  20. {
  21. LED[0] = (display_state == 0)?1:0;
  22. LED[1] = (display_state == 1)?1:0;
  23. LED[2] = (distance_now > distance_shezhi)?state:0;
  24. }
  25. void smg_pro()
  26. {
  27. if(display_dly<200)return;
  28. display_dly = 0;
  29. distance_now = read_distance();
  30. switch(display_state)
  31. {
  32. case 0:
  33. SMG[0] = 11;
  34. SMG[1] = 1;
  35. SMG[2] = 10;
  36. SMG[3] = 10;
  37. SMG[4] = 10;
  38. SMG[5]=(distance_now<100)?10:distance_now/100;
  39. SMG[6]=(distance_now<10)?10:distance_now/10%10;
  40. SMG[7]= distance_now %10;
  41. break;
  42. case 1:
  43. SMG[0] = 11;
  44. SMG[1] = 2;
  45. SMG[2] = 10;
  46. SMG[3] = 10;
  47. SMG[4] = 10;
  48. SMG[5] = distance_shezhi / 100;
  49. SMG[6] = distance_shezhi / 10 % 10;
  50. SMG[7] = distance_shezhi % 10;
  51. }
  52. }
  53. void key_pro()
  54. {
  55. uchar key;
  56. if(key_dly < 10) return;
  57. else key_dly = 0;
  58. key = key_scan2();
  59. switch(key)
  60. {
  61. case 13:
  62. display_state ++ ;
  63. if(display_state == 2)
  64. display_state = 0;
  65. break;
  66. case 14:
  67. if(display_state == 0)
  68. distance_shezhi = distance_now;
  69. break;
  70. case 15:
  71. if(display_state == 1)
  72. distance_shezhi = distance_shezhi + 10;
  73. break;
  74. case 16:
  75. if(display_state == 1)
  76. {
  77. if(distance_shezhi >= 10)
  78. distance_shezhi = distance_shezhi - 10;
  79. else if(distance_shezhi < 10)
  80. distance_shezhi = 0;
  81. }
  82. break;
  83. case 10:
  84. printf("Distance:%dcm\r\n",(uint)distance_now);
  85. break;
  86. }
  87. }
  88. void main()
  89. {
  90. system_init();
  91. Timer1Init();
  92. EA = 1;
  93. UartInit();
  94. while(1)
  95. {
  96. led_pro();
  97. key_pro();
  98. smg_pro();
  99. }
  100. }

smg.c

  1. #include "smg.h"
  2. code segment[] = {
  3. // 0 1 2 3 4 5 6 7 8 9 熄灭 U
  4. 0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F, 0x00,0x3E
  5. };
  6. void smg(uchar * temp,uchar *dot,uchar pos)
  7. {
  8. P0=0xff;
  9. hc573(7);
  10. P0=0x01<<pos;
  11. hc573(6);
  12. if(dot[pos]==0)
  13. P0=~segment[temp[pos]];
  14. else
  15. P0=(~segment[temp[pos]]) & 0x7f;
  16. hc573(7);
  17. }

led.c

  1. #include "led.h"
  2. void led(uchar * LED,uchar pos)
  3. {
  4. static uchar temp=0xff;
  5. if(LED[pos])
  6. temp &= ~(0x01<<pos);
  7. else
  8. temp |= 0x01<<pos;
  9. P0=temp;
  10. hc573(4);
  11. }

timer.c

  1. #include "timer.h"
  2. unsigned long systick_ms;
  3. uchar state_relay;//继电器控制
  4. uchar pos; //数码管led扫描
  5. uchar key_dly; //按键调度
  6. uint display_dly,collect_dly; //显示调度,采集调度
  7. uint fre; //频率变量
  8. uint count; //1s计时
  9. void Timer0Init(void) //100微秒@12.000MHz
  10. {
  11. AUXR &= 0x7F; //定时器时钟12T模式
  12. TMOD &= 0xF0; //设置定时器模式
  13. TL0 = 0x9C; //设置定时初值
  14. TH0 = 0xFF; //设置定时初值
  15. TF0 = 0; //清除TF0标志
  16. TR0 = 1; //定时器0开始计时
  17. ET0 = 1;
  18. }
  19. void Timer0Init_Count(void) //计数模式
  20. {
  21. TMOD &= 0xF0; //设置定时器模式
  22. TMOD |= 0x05; //16位不自动重装
  23. TL0 = 0; //设置定时初值
  24. TH0 = 0; //设置定时初值
  25. TF0 = 0; //清除TF0标志
  26. TR0 = 1; //定时器0开始计数
  27. ET0 = 1;
  28. }
  29. void Timer1Init(void) //1毫秒@12.000MHz
  30. {
  31. AUXR &= 0xBF; //定时器时钟12T模式
  32. TMOD &= 0x0F; //设置定时器模式
  33. TL1 = 0x18; //设置定时初值
  34. TH1 = 0xFC; //设置定时初值
  35. TF1 = 0; //清除TF1标志
  36. TR1 = 1; //定时器1开始计时
  37. ET1 = 1;
  38. }
  39. void timer0() interrupt 1
  40. {
  41. }
  42. void timer1() interrupt 3
  43. {
  44. if(++count == 200)
  45. {
  46. state = ~state;
  47. count=0;
  48. }
  49. systick_ms++;
  50. key_dly++;
  51. display_dly++;
  52. collect_dly++;
  53. smg(SMG,dot,pos);
  54. led(LED,pos);
  55. if(++pos == 8) pos = 0;
  56. }

ult.c

  1. #include "ult.h"
  2. sbit TX = P1^0; // 发射引脚
  3. sbit RX = P1^1; // 接收引脚
  4. uchar read_distance(void)
  5. {
  6. uchar distance,num = 10;
  7. TX = 0;
  8. CL = 0xF3; // 设置定时初值
  9. CH = 0xFF; // 设置定时初值
  10. CR = 1; // 定时器0计时
  11. // TX引脚发送40KHz方波信号驱动超声波发送探头
  12. while(num--)
  13. {
  14. while(!CF);
  15. TX ^= 1;
  16. CL = 0xF3; // 设置定时初值
  17. CH = 0xFF; // 设置定时初值
  18. CF = 0;
  19. }
  20. CR = 0;
  21. CL = 0; // 设置定时初值
  22. CH = 0; // 设置定时初值
  23. CR = 1;
  24. while(RX && !CF); // 等待收到脉冲
  25. CR = 0;
  26. if(CF) // 发生溢出
  27. {
  28. CF = 0;
  29. distance = 255;
  30. }
  31. else // 计算距离
  32. distance = ((CH<<8)+CL)*0.017;
  33. return distance;
  34. }

sys.c  && delay.c

  1. #include "sys.h"
  2. void hc573(uchar channel)
  3. {
  4. switch(channel)
  5. {
  6. case 4: P2 = P2 & 0x1f | 0x80; break;
  7. case 5: P2 = P2 & 0x1f | 0xa0; break;
  8. case 6: P2 = P2 & 0x1f | 0xc0; break;
  9. case 7: P2 = P2 & 0x1f | 0xe0; break;
  10. }
  11. P2 = P2 & 0x1f;
  12. }
  13. void system_init()
  14. {
  15. P0 = 0x00;
  16. hc573(5);
  17. P0 = 0xff;
  18. hc573(4);
  19. }
  20. #include "delay.h"
  21. void Delayms(uint xms) //@12.000MHz
  22. {
  23. unsigned char i, j;
  24. while(xms--)
  25. {
  26. i = 12;
  27. j = 169;
  28. do
  29. {
  30. while (--j);
  31. } while (--i);
  32. }
  33. }
  34. void Delayus(uint xus) //@12.000MHz
  35. {
  36. while(xus--)
  37. {
  38. _nop_();
  39. _nop_();
  40. _nop_();
  41. _nop_();
  42. }
  43. }

uart.c

  1. #include "uart.h"
  2. uchar Uart_Rxindex;
  3. uchar Uart_Rxbuf[10];
  4. void UartInit(void) //4800bps@12.000MHz
  5. {
  6. SCON = 0x50; // 8位数据,可变波特率
  7. AUXR |= 0x01; // 串口1选择定时器2为波特率发生器
  8. AUXR |= 0x04; // 定时器2时钟为Fosc, 即1T
  9. T2L = 0x8F; // 设定定时初值
  10. T2H = 0xFD; // 设定定时初值
  11. AUXR |= 0x10; // 启动定时器2
  12. ES = 1; // 允许串口中断
  13. }
  14. void Uart0(void) interrupt 4
  15. {
  16. if(RI)
  17. {
  18. if(Uart_Rxindex == 10)
  19. Uart_Rxindex = 0;
  20. Uart_Rxbuf[Uart_Rxindex++] = SBUF;
  21. RI = 0;
  22. }
  23. }
  24. char putchar(char ch)
  25. {
  26. SBUF = ch;
  27. while (TI == 0); // 等待发送完成
  28. TI = 0; // 发送完成标志位置0
  29. return ch;
  30. }

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

闽ICP备14008679号