完成全部要求,需要的可以下载- #include "stm32f10x.h"
- #include "lcd.h"
- #include "stdio.h"
- #include "tim.h"
- #include "i2c.h"
- #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
- #define LED0 GPIO_Pin_8
- #define LED1 GPIO_Pin_9
- #define LED2 GPIO_Pin_10
- #define LED3 GPIO_Pin_11
- #define LED4 GPIO_Pin_12
- #define LED5 GPIO_Pin_13
- #define LED6 GPIO_Pin_14
- #define LED7 GPIO_Pin_15
- #define key0 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)
- #define key1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8)
- #define key2 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1)
- #define key3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2)
- u32 TimingDelay = 0;
- void led_init(void);
- void led_control(u16 led,u8 state);
- void key_init(void);
- u8 key_scan(void);
- void delay_ms(u32 nTime);
- void at24c02_write(u8 add,u8 data);
- u8 at24c02_read(u8 add);
- u8 str[20];
- u8 key_temp=0;
- u16 ji_speed=0; //即时速度
- u16 jun_speed=0; //平均速度
- u16 speed_num=0; //当时行驶里程
- u8 shi=0,fen=0,miao=0;
- u16 licheng=0;
- s16 pwm_out_num=0; //初始5HZ
- u8 pwm_change=0;
- extern u16 cap_start;
- extern u16 cap_count;
- extern u16 cap_value;
- u32 CapPwm_pinlv=0;
- extern u16 flag_miao;
- u8 led_flag=0;
- int main(void)
- {
- SysTick_Config(SystemCoreClock/1000);
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- led_init();
- key_init();
- i2c_init();
- led_control(LED_ALL,1);
- delay_ms(200);
-
- STM3210B_LCD_Init();
- LCD_Clear(Blue);
- LCD_SetBackColor(Blue);
- LCD_SetTextColor(White);
-
- pwm_out_config((1000/pwm_out_num)*10-1,7200-1);
- TIM2_Cap_Init(65536-1,7200-1);
-
- if(at24c02_read(0xff)!=0xaa) //首次写入清0
- {
- at24c02_write(0xff,0xaa);
- at24c02_write(0x00,0);
- at24c02_write(0x01,0);
- }
-
- licheng=at24c02_read(0x00);
- licheng=licheng+(at24c02_read(0x01)<<8);
-
- while(1)
- {
- sprintf(str,"%s %d ","Vim(Km/h):",ji_speed);
- LCD_DisplayStringLine(Line1,str);
- sprintf(str,"%s %d ","Vavg(Km/h):",jun_speed);
- LCD_DisplayStringLine(Line3,str);
- sprintf(str,"%s%2d:%2d:%2d","T(h:m:s):",shi,fen,miao);
- LCD_DisplayStringLine(Line5,str);
- sprintf(str,"%s %d","S(Km):",licheng);
- LCD_DisplayStringLine(Line7,str);
- LCD_DisplayStringLine(Line9," Total(Km):200");
-
-
- if(flag_miao==1)
- {
- flag_miao=0;
- if(cap_start==2) //计算频率
- {
- cap_start=0;
- CapPwm_pinlv=1000/(((65535*cap_count)+cap_value)/10);
-
- }
-
-
- ji_speed=CapPwm_pinlv*2*3.6;
- speed_num+=ji_speed;
- jun_speed=(speed_num/3.2)/(miao+fen*60+shi*60*60)*3.2;
- licheng+=ji_speed;
-
- at24c02_write(0x00,licheng&0xff);
- at24c02_write(0x01,(licheng>>8)&0xff);
-
-
- if(ji_speed>=90)
- {
- led_flag^=1;
- led_control(LED0,led_flag);
- }
- else
- led_control(LED0,1);
-
- if(CapPwm_pinlv>0)
- {
- if(++miao==60){miao=0;fen++;}
- if(fen==59){fen=0;shi++;}
- if(shi==23){shi=0;}
- }
- }
- key_temp=key_scan();
- switch(key_temp)
- {
- case 1:{pwm_change=1;if(++pwm_out_num>=20)pwm_out_num=20; }break;
- case 2:{pwm_change=1;if(--pwm_out_num<=0)pwm_out_num=0;}break;
- }
-
- if(pwm_change==1) //改变频率
- {
- pwm_change=0;
- pwm_out_config((1000/pwm_out_num)*10-1,7200-1);
- }
-
- }
- }
- void at24c02_write(u8 add,u8 data)
- {
- I2CStart();
- I2CSendByte(0xa0);
- I2CWaitAck();
- I2CSendByte(add);
- I2CWaitAck();
- I2CSendByte(data);
- I2CWaitAck();
- I2CStop();
- delay_ms(5);
-
- }
- u8 at24c02_read(u8 add)
- {
- u8 dat=0;
-
- I2CStart();
- I2CSendByte(0xa0);
- I2CWaitAck();
- I2CSendByte(add);
- I2CWaitAck();
- I2CStop();
-
- I2CStart();
- I2CSendByte(0xa1);
- I2CWaitAck();
- dat=I2CReceiveByte();
- I2CWaitAck();
- I2CStop();
- return dat;
-
- }
- void led_init(void)
- {
-
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE);
- /* Configure PD0 and PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = LED_ALL;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- }
- void led_control(u16 led,u8 state)
- {
- if(state==1)
- {
- GPIO_SetBits(GPIOD,GPIO_Pin_2);
- GPIO_SetBits(GPIOC,led);
- GPIO_ResetBits(GPIOD,GPIO_Pin_2);
-
- }else if(state==0)
- {
- GPIO_SetBits(GPIOD,GPIO_Pin_2);
- GPIO_ResetBits(GPIOC,led);
- GPIO_ResetBits(GPIOD,GPIO_Pin_2);
- }
-
- }
- void key_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE);
- /* Configure PD0 and PD2 in output pushpull mode */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
全部程序51hei下载地址:
第四届-里程仪.7z
(592.19 KB, 下载次数: 20)
|