找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2014|回复: 0
打印 上一主题 下一主题
收起左侧

蓝桥杯 第四届 里程仪STM32源程序

[复制链接]
跳转到指定楼层
楼主
ID:371067 发表于 2020-9-28 16:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
完成全部要求,需要的可以下载
  1. #include "stm32f10x.h"
  2. #include "lcd.h"
  3. #include "stdio.h"
  4. #include "tim.h"
  5. #include "i2c.h"

  6. #define LED_ALL GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15
  7. #define LED0 GPIO_Pin_8
  8. #define LED1 GPIO_Pin_9
  9. #define LED2 GPIO_Pin_10
  10. #define LED3 GPIO_Pin_11
  11. #define LED4 GPIO_Pin_12
  12. #define LED5 GPIO_Pin_13
  13. #define LED6 GPIO_Pin_14
  14. #define LED7 GPIO_Pin_15

  15. #define key0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
  16. #define key1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
  17. #define key2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
  18. #define key3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)

  19. u32 TimingDelay = 0;

  20. void led_init(void);
  21. void led_control(u16 led,u8 state);
  22. void key_init(void);
  23. u8 key_scan(void);
  24. void delay_ms(u32 nTime);
  25. void at24c02_write(u8 add,u8 data);
  26. u8 at24c02_read(u8 add);


  27. u8 str[20];
  28. u8 key_temp=0;
  29. u16 ji_speed=0;                //即时速度
  30. u16 jun_speed=0;        //平均速度
  31. u16 speed_num=0;        //当时行驶里程
  32. u8 shi=0,fen=0,miao=0;
  33. u16 licheng=0;
  34. s16 pwm_out_num=0;                //初始5HZ
  35. u8 pwm_change=0;
  36. extern u16 cap_start;
  37. extern u16 cap_count;
  38. extern u16 cap_value;
  39. u32 CapPwm_pinlv=0;
  40. extern u16 flag_miao;
  41. u8 led_flag=0;

  42. int main(void)
  43. {

  44.         SysTick_Config(SystemCoreClock/1000);
  45.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
  46.        
  47.         led_init();
  48.         key_init();
  49.         i2c_init();
  50.         led_control(LED_ALL,1);
  51.         delay_ms(200);
  52.        
  53.         STM3210B_LCD_Init();
  54.         LCD_Clear(Blue);
  55.         LCD_SetBackColor(Blue);
  56.         LCD_SetTextColor(White);
  57.        
  58.         pwm_out_config((1000/pwm_out_num)*10-1,7200-1);
  59.         TIM2_Cap_Init(65536-1,7200-1);
  60.        
  61.         if(at24c02_read(0xff)!=0xaa)                //首次写入清0
  62.         {
  63.                 at24c02_write(0xff,0xaa);
  64.                 at24c02_write(0x00,0);
  65.                 at24c02_write(0x01,0);
  66.         }
  67.        
  68.         licheng=at24c02_read(0x00);
  69.         licheng=licheng+(at24c02_read(0x01)<<8);
  70.        
  71.         while(1)
  72.         {
  73.                 sprintf(str,"%s %d  ","Vim(Km/h):",ji_speed);
  74.                 LCD_DisplayStringLine(Line1,str);
  75.                 sprintf(str,"%s %d    ","Vavg(Km/h):",jun_speed);
  76.                 LCD_DisplayStringLine(Line3,str);
  77.                 sprintf(str,"%s%2d:%2d:%2d","T(h:m:s):",shi,fen,miao);
  78.                 LCD_DisplayStringLine(Line5,str);
  79.                 sprintf(str,"%s %d","S(Km):",licheng);
  80.                 LCD_DisplayStringLine(Line7,str);
  81.                 LCD_DisplayStringLine(Line9,"       Total(Km):200");
  82.                
  83.                
  84.                 if(flag_miao==1)
  85.                 {
  86.                         flag_miao=0;
  87.                         if(cap_start==2)                //计算频率
  88.                         {
  89.                                 cap_start=0;
  90.                                 CapPwm_pinlv=1000/(((65535*cap_count)+cap_value)/10);
  91.                                
  92.                         }
  93.                        
  94.                        
  95.                         ji_speed=CapPwm_pinlv*2*3.6;
  96.                         speed_num+=ji_speed;
  97.                         jun_speed=(speed_num/3.2)/(miao+fen*60+shi*60*60)*3.2;
  98.                         licheng+=ji_speed;
  99.        
  100.                         at24c02_write(0x00,licheng&0xff);
  101.                         at24c02_write(0x01,(licheng>>8)&0xff);
  102.                        
  103.                        
  104.                         if(ji_speed>=90)
  105.                         {
  106.                                 led_flag^=1;
  107.                                 led_control(LED0,led_flag);
  108.                         }
  109.                         else
  110.                                 led_control(LED0,1);
  111.                        
  112.                         if(CapPwm_pinlv>0)       
  113.                         {
  114.                                 if(++miao==60){miao=0;fen++;}
  115.                                 if(fen==59){fen=0;shi++;}
  116.                                 if(shi==23){shi=0;}
  117.                         }


  118.                 }
  119.                 key_temp=key_scan();         
  120.                 switch(key_temp)
  121.                 {
  122.                         case 1:{pwm_change=1;if(++pwm_out_num>=20)pwm_out_num=20; }break;
  123.                         case 2:{pwm_change=1;if(--pwm_out_num<=0)pwm_out_num=0;}break;
  124.                 }
  125.                
  126.                 if(pwm_change==1)                //改变频率
  127.                 {
  128.                         pwm_change=0;
  129.                         pwm_out_config((1000/pwm_out_num)*10-1,7200-1);
  130.                 }
  131.                
  132.         }
  133. }
  134. void at24c02_write(u8 add,u8 data)
  135. {
  136.         I2CStart();
  137.         I2CSendByte(0xa0);
  138.         I2CWaitAck();
  139.         I2CSendByte(add);
  140.         I2CWaitAck();
  141.         I2CSendByte(data);
  142.         I2CWaitAck();
  143.         I2CStop();
  144.         delay_ms(5);
  145.        
  146. }
  147. u8 at24c02_read(u8 add)
  148. {
  149.         u8 dat=0;
  150.        
  151.         I2CStart();
  152.         I2CSendByte(0xa0);
  153.         I2CWaitAck();
  154.         I2CSendByte(add);
  155.         I2CWaitAck();
  156.         I2CStop();
  157.        
  158.         I2CStart();
  159.         I2CSendByte(0xa1);
  160.         I2CWaitAck();
  161.         dat=I2CReceiveByte();
  162.         I2CWaitAck();
  163.         I2CStop();
  164.         return dat;
  165.        
  166. }

  167. void led_init(void)
  168. {
  169.        
  170.         GPIO_InitTypeDef GPIO_InitStructure;
  171.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);

  172.         /* Configure PD0 and PD2 in output pushpull mode */
  173.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
  174.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  175.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  176.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  177.        
  178.         GPIO_InitStructure.GPIO_Pin = LED_ALL;
  179.         GPIO_Init(GPIOC, &GPIO_InitStructure);

  180. }
  181. void led_control(u16 led,u8 state)
  182. {
  183.         if(state==1)
  184.         {
  185.                 GPIO_SetBits(GPIOD,GPIO_Pin_2);
  186.                 GPIO_SetBits(GPIOC,led);
  187.                 GPIO_ResetBits(GPIOD,GPIO_Pin_2);
  188.                
  189.         }else if(state==0)
  190.         {
  191.                 GPIO_SetBits(GPIOD,GPIO_Pin_2);
  192.                 GPIO_ResetBits(GPIOC,led);
  193.                 GPIO_ResetBits(GPIOD,GPIO_Pin_2);
  194.         }
  195.        
  196. }

  197. void key_init(void)
  198. {
  199.         GPIO_InitTypeDef GPIO_InitStructure;
  200.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);

  201.         /* Configure PD0 and PD2 in output pushpull mode */
  202.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
  203.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  204. ……………………

  205. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

全部程序51hei下载地址:
第四届-里程仪.7z (592.19 KB, 下载次数: 20)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表