主要測量速度、里程、行駛時間及溫度。利用的是STC89C52、霍爾傳感器、矩陣鍵盤。
電路原理圖如下:
0.jpg (46.35 KB, 下載次數: 52)
下載附件
2018-11-23 02:53 上傳
單片機源程序:
- #include <REGX52.H>
- #include "12232.h"
- #include "stc_eep.h"
- sbit LCD_bk = P2^4;
- sbit KEY1 = P1^3;
- sbit KEY2 = P1^4;
- sbit KEY3 = P1^5;
- volatile unsigned int t_counter=0;
- volatile unsigned char int_flag=0,t0_flag=0;
- void t0_srv(void) interrupt 1 using 1
- {
- TR0 = 0;
- ET0 = 0;
- t_counter++;
- t0_flag = 1;
- if(t_counter<5)
- {
- IE0 = 0;
- }
- if(t_counter==5)
- {
- EX0 = 1; //50ms后開int0中斷, 防抖動.
- }
- TH0 = 216; //(65536-10000)/256
- //TL0 = 240; //(65536-10000)%256
- TL0 = 255;
- TR0 = 1;
- ET0 = 1;
- }
- void int0_srv(void) interrupt 0 using 0
- {
- EX0 = 0;
- int_flag = 1;
- }
- void delay_nms(unsigned int count)
- {
- unsigned int i,j;
- for(i=0;i<count;i++)
- for(j=0;j<45;j++);
- }
- void main(void)
- {
- unsigned char buff[]=(" 0.000km");
- unsigned char buff_2[]=(" 000.0km");
- unsigned char buff_1[]=("11");
- unsigned char sec=0,min=0,hour=0,counter=0;
- unsigned char time_buff[]=("00:00:00");
- unsigned int temp=0;
- unsigned int speed=0;
- unsigned int once_counter=0; //單次騎行計數
- unsigned char all_counter_1,all_counter_2,all_counter_3;
- unsigned long all_counter=0,temp1=0;
- EA = 0;
- IT0 = 1; //int0 下降沿中斷
- EX0 = 1; //允許外部中斷0
- TMOD = 0x09; //timer0 工作方式1 10ms溢出
- TH0 = 216; //(65536-10000)/256
- TL0 = 240; //(65536-10000)%256
- TR0 = 1;
- ET0 = 1;
- EA = 1;
-
- LCD_init();
- LCD_bk = 0;
- clrscr(0x00);
- //drawascii(10,0,"speed meater v0.1");
- //----------------------------------------------
- //--大字符顯示格式------------------------------
- //*
- draw_very_big_ascii(0,"12");
- draw_big_ascii(47,2,".3");
- drawascii(73,3," KM/H");
- drawascii(73,0,"00.000km");
- drawascii(73,1,"0000.0km");
- drawascii(73,2,"00:00:00");
- //*/
- //----------------------------------------------
- //--小字符顯示格式------------------------------
- /*
- drawascii(0,0,"SPD 12.3KM/H");
- drawascii(48,0," KM/H");
- drawascii(0,2,"max 29.8 | avg 13.2 ");
- drawascii(0,3,"12.345KM | 1234.56KM");
- */
- //STC_BYTE_program(0x20,0x20,0x1f);
- // if(STC_BYTE_read(0x20,0x20)==0x1f)
- // P1=0xf0;
- /*
- STC_SECTOR_erase(0x20,0x00);
- STC_BYTE_program(0x20,0x21,00);
- STC_BYTE_program(0x20,0x22,00);
- STC_BYTE_program(0x20,0x23,00);
- */
- if((KEY1 == 0)&&(KEY2 == 0))
- {
- delay_nms(20);
- if((KEY1 == 0)&&(KEY2 == 0))
- {
- drawascii(73,3,"CLEAR");
- while((KEY1 == 0)&&(KEY2 == 0));
- STC_SECTOR_erase(0x20,0x00);
- STC_BYTE_program(0x20,0x21,0);
- STC_BYTE_program(0x20,0x22,0);
- STC_BYTE_program(0x20,0x23,0);
- delay_nms(4000);
- drawascii(73,3," KM/H ");
- }
- }
- all_counter_1 = STC_BYTE_read(0x20,0x21);
- all_counter_2 = STC_BYTE_read(0x20,0x22);
- all_counter_3 = STC_BYTE_read(0x20,0x23);
- all_counter = (unsigned long)all_counter_1<<16 | (unsigned long)all_counter_2<<8 | (unsigned long)all_counter_3 ;
- temp1 = all_counter *2;
- buff_2[0] = temp1/1000000 + '0';
- buff_2[1] = temp1%1000000/100000 + '0';
- buff_2[2] = temp1%100000/10000 + '0';
- buff_2[3] = temp1%10000/1000 + '0';
- buff_2[5] = temp1%1000/100 + '0';
- drawascii(73,1,buff_2);
- while(1)
- {
- if(t0_flag == 1)
- {
- t0_flag = 0;
- counter++;
- if(counter == 100)
- {
- counter = 0;
- sec ++;
- if(sec == 60)
- {
- sec = 0;
- min ++;
- if(min == 60)
- {
- min = 0;
- hour ++;
- }
- }
- time_buff[0]=hour/10+'0';
- time_buff[1]=hour%10+'0';
- time_buff[3]=min/10+'0';
- time_buff[4]=min%10+'0';
- time_buff[6]=sec/10+'0';
- time_buff[7]=sec%10+'0';
- drawascii(73,2,time_buff);
- }
- }
- if(t_counter>1000)
- {
- t_counter = 0;
- //drawascii(0,1,"time out");
- }
- if(KEY3 == 0)
- {
- delay_nms(20);
- if(KEY3 == 0)
- {
- while(KEY3==0);
- LCD_bk = !LCD_bk;
- }
- }
- if(KEY1 == 0)
- {
- delay_nms(20);
- if(KEY1 == 0)
- {
- while(KEY1==0);
- drawascii(73,3,"WRITED");
- STC_SECTOR_erase(0x20,0x00);
- STC_BYTE_program(0x20,0x21,(unsigned char)all_counter>>16);
- STC_BYTE_program(0x20,0x22,(unsigned char)all_counter>>8);
- STC_BYTE_program(0x20,0x23,(unsigned char)all_counter);
- delay_nms(2000);
- drawascii(73,3," KM/H ");
- }
- }
-
- if(int_flag==1)
- {
- int_flag = 0;
- temp = t_counter;
- t_counter = 0;
- /*
- buff[0] = temp/1000 + '0';
- buff[1] = temp%1000/100 + '0';
- buff[2] = temp%100/10 + '0';
- buff[3] = temp%10 + '0';
- //drawascii(0,1,buff);
- */
- speed = 7351/temp; //顯示速度
- buff_1[0] = speed/100 + '0';
- buff_1[1] = speed%100/10 + '0';
- draw_very_big_ascii(0,buff_1);
- buff_1[0] = '.';
- buff_1[1] = speed%10 + '0';
- draw_big_ascii(47,2,buff_1);
- once_counter++;
- all_counter++; //顯示距離
- temp = (unsigned long)once_counter * 2040 /1000; //輪胎周長2040mm
- buff[0] = temp/10000 + '0';
- buff[1] = temp%10000/1000 + '0';
- buff[3] = temp%1000/100 + '0';
- buff[4] = temp%100/10 + '0';
- buff[5] = temp%10 + '0';
- drawascii(73,0,buff);
- temp1 = all_counter *2;
- buff_2[0] = temp1/1000000 + '0';
- buff_2[1] = temp1%1000000/100000 + '0';
- buff_2[2] = temp1%100000/10000 + '0';
- buff_2[3] = temp1%10000/1000 + '0';
- buff_2[5] = temp1%1000/100 + '0';
- drawascii(73,1,buff_2);
- }
- }
- }
復制代碼 全部資料51hei下載地址:
自行車測速(霍爾).rar
(90.44 KB, 下載次數: 84)
2018-11-22 18:48 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|