这个自行车利用霍尔元件测速测距并显示的程序在速度显示上总有问题,另外还有个键入身高体重显示建议行驶路程的功能也有问题。
速度显示用数码管就没用问题,数值大概个位数到十几之间。suggestion是建议路程函数,就是简单的height-weight,不知道为什么显示出来就是几百米。
#define MAIN_Fosc 12000000L //¶¨òåÖ÷ê±Öó
#include "15W4KxxS4.H"
#include<GPIO.h>
#include<lcd1602.h>
int cesu(int k);
/**********************
òy½Å±eÃû¶¨òå
***********************/
int t=0;
uint32 n=0,m; //è|êyòÔ¼°×aò»è|μÄê±¼ä
uint16 k=0; //¼ÆêyÆ÷¼Æê±
uint8 a[4];
uint8 p;
uint8 code table1[]="L:"; //òaÏÔê¾μÄÄúèY1·Åèëêy×étable1
uint8 code table2[]="V:"; //òaÏÔê¾μÄÄúèY2·Åèëêy×étable2
uint8 code table5[]="Height:";
uint8 code table6[]="Weight:";
uint8 code table7[]="suggestion:";
uint16 table3[],table4[];
uint8 code ASCII[18] = {'0','1','2','3','4','5','6','7','8','9','.','-','M','/','S','K','G','C'};
/**********************
òy½Å±eÃû¶¨òå
***********************/
sbit COL4=P5^2; //4*4¾ØÕó¼ì2aáD¼ì2a¶Ë¿ú
sbit COL3=P5^3; //4*4¾ØÕó¼ì2aáD¼ì2a¶Ë¿ú
sbit COL2=P2^6; //4*4¾ØÕó¼ì2aáD¼ì2a¶Ë¿ú
sbit COL1=P2^7; //4*4¾ØÕó¼ì2aáD¼ì2a¶Ë¿ú
sbit ROW4=P4^4; //4*4¾ØÕó¼ì2aDD¼ì2a¶Ë¿ú
sbit ROW3=P4^5; //4*4¾ØÕó¼ì2aDD¼ì2a¶Ë¿ú
sbit ROW2=P4^6; //4*4¾ØÕó¼ì2aDD¼ì2a¶Ë¿ú
sbit ROW1=P4^7; //4*4¾ØÕó¼ì2aDD¼ì2a¶Ë¿ú
sbit LED_B=P1^4; //à¶é«LEDóÃIO¿úP06
sbit LED_R=P3^4; //oìé«LEDóÃIO¿úP07
sbit LED_G=P1^5; //Âìé«LEDóÃIO¿úP07
sbit beep=P3^7;
/**************************************************************************************
* Ãè êö : °′¼üé¨Ãèoˉêy
* èë 2Î : ÎT
* ·μ»ØÖμ : Äĸö°′¼ü°′ÏÂμĶÔó|Öμ
**************************************************************************************/
uint8 KeyScan(void)
{
uint8 X_temp,Y_temp,temp;
X_temp=0xF0; //áDÖμ¸33õÖμ
Y_temp=0x0F; //DDÖμ¸33õÖμ
P2M1 &= 0x3F; P2M0 |= 0xC0; //éèÖÃP2.6~P2.7ÎaÇ¿íÆíìêä3ö
P4M1 &= 0x0F; P4M0 |= 0xF0; //éèÖÃP4.4~P4.7ÎaÇ¿íÆíìêä3ö
P5M1 &= 0xF3; P5M0 |= 0x0C; //éèÖÃP5.2~P5.3ÎaÇ¿íÆíìêä3ö
ROW1=1;ROW2=1;ROW3=1;ROW4=1; //DDÖøß
COL1=0;COL2=0;COL3=0;COL4=0; //áDÖÃμí
//ËùóÃμ½DDIO¿úÅäÖÃÎaêäè룬½øDD¼ì2a
delay_ms(10);
P4M1 &= 0x0F; P4M0 &= 0x0F; //éèÖÃP4.4~P4.7Îa׼˫Ïò¿ú
delay_ms(10);
if(ROW1 == 0) //¼ì2aDD1μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(ROW1 == 0)
Y_temp &= 0x0E;
}
if(ROW2 == 0) //¼ì2aDD2μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(ROW2 == 0)
Y_temp &= 0x0D;
}
if(ROW3 == 0) //¼ì2aDD3μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(ROW3 == 0)
Y_temp &= 0x0B;
}
if(ROW4 == 0) //¼ì2aDD4μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(ROW4 == 0)
Y_temp &= 0x07;
}
P2M1 &= 0x3F; P2M0 |= 0xC0; //éèÖÃP2.6~P2.7ÎaÇ¿íÆíìêä3ö
P4M1 &= 0x0F; P4M0 |= 0xF0; //éèÖÃP4.4~P4.7ÎaÇ¿íÆíìêä3ö
P5M1 &= 0xF3; P5M0 |= 0x0C; //éèÖÃP5.2~P5.3ÎaÇ¿íÆíìêä3ö
ROW1=0;ROW2=0;ROW3=0;ROW4=0; //DDÖÃμí
COL1=1;COL2=1;COL3=1;COL4=1; //áDÖøß
//ËùóÃμ½áDIO¿úÅäÖÃÎaêäè룬½øDD¼ì2a
delay_ms(10);
P2M1 &= 0x3F; P2M0 &= 0x3F; //éèÖÃP2.6~P2.7Îa׼˫Ïò¿ú
P5M1 &= 0xF3; P5M0 &= 0xF3; //éèÖÃP5.2~P5.3Îa׼˫Ïò¿ú
delay_ms(10);
if(COL1 == 0) //¼ì2aáD1μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(COL1 == 0)
X_temp &= 0xE0;
}
if(COL2 == 0) //¼ì2aáD2μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(COL2 == 0)
X_temp &= 0xD0;
}
if(COL3 == 0) //¼ì2aáD3μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(COL3 == 0)
X_temp &= 0xB0;
}
if(COL4 == 0) //¼ì2aáD4μçƽêÇ·ñÎaμíμçƽ
{
delay_ms(10);
if(COL4 == 0)
X_temp &= 0x70;
}
//½«DDÖμoíáDÖμoÏ2¢£¬μÃμ½°′¼ü¶Ôó|μıàÂëÖ죬¸ÃÖμóë16¸ö°′¼üò»ò»¶Ôó|
temp = X_temp|Y_temp;
temp = ~temp;
//½«°′¼ü¼ì2aμÄÔ-ê¼±àÂëÖμ½aÎö¶Ôó|°′¼üÖμDÅÏ¢
switch (temp)
{
case 0x11:return 1; //1
case 0x21:return 2; //2
case 0x41:return 3; //3
case 0x81:return 4; //4
case 0x12:return 5; //5
case 0x22:return 6; //6
case 0x42:return 7; //7
case 0x82:return 8; //8
case 0x14:return 9; //9
case 0x24:return 10; //0
case 0x44:return 11; //a
case 0x84:return 12; //b
case 0x18:return 13; //c
case 0x28:return 14; //d
case 0x48:return 15; //e
case 0x88:return 16; //f
default: return 0;
}
}
//=====================================================
//¼Æê±Æ÷3õê¼»ˉ
void init(void)
{
TMOD|=0X15;
TH0=65535/256;
TL0=65535%256;
TH1=0xFC;
TL1=0x18;
EA=1;
ET0=1;
ET1=1;
TR0=1;
PT0=1;
PT1=0;
}
/**************************************
1|ÄüÃèêö£oÑóê±oˉêy
èë¿ú2Îêy£ouint16 x £¬¸ÃÖμÎa1ê±£¬Ñóê±1ms
·μ»ØÖμ£oÎT
***************************************/
void delay_ms(uint16 x)
{
uint16 j,i;
for(j=0;j<x;j++)
{
for(i=0;i<1100;i++);
}
}
/**************************************
1|ÄüÃèêö£oLCD1602D′Ãüáîoˉêy
èë¿ú2Îêy£uint8 com
·μ»ØÖμ£oÎT
***************************************/
void lcd_wcom(uint8 com)
{
rs=0; //Ñ¡ÔñÖ¸áî¼Ä′æÆ÷
rw=0; //Ñ¡ÔñD′
P0=com; //°ÑÃüáî×ÖËíèëP0
delay_ms(1); //Ñóê±ò»D¡»á¶ù£¬èÃ1602×¼±¸½óêÕêy¾Y
en=1; //ê1ÄüÏßμçƽ±ä»ˉ£¬ÃüáîËíèë1602μÄ8λêy¾Y¿ú
en=0;
}
/**************************************
1|ÄüÃèêö£oLCD1602D′êy¾Yoˉêy
èë¿ú2Îêy£ouint8 dat
·μ»ØÖμ£oÎT
***************************************/
void lcd_wdat(uint8 dat)
{
rs=1; //Ñ¡Ôñêy¾Y¼Ä′æÆ÷
rw=0; //Ñ¡ÔñD′
P0=dat; //°ÑòaÏÔê¾μÄêy¾YËíèëP0
delay_ms(1); //Ñóê±ò»D¡»á¶ù£¬èÃ1602×¼±¸½óêÕêy¾Y
en=1; //ê1ÄüÏßμçƽ±ä»ˉ£¬êy¾YËíèë1602μÄ8λêy¾Y¿ú
en=0;
}
/**************************************
1|ÄüÃèêö£oLCD16023õê¼»ˉoˉêy
èë¿ú2Îêy£oÎT
·μ»ØÖμ£oÎT
***************************************/
void lcd_init(void)
{
lcd_wcom(0x38); //8λêy¾Y£¬Ë«áD£¬5*7×ÖDÎ
lcd_wcom(0x0c); //¿aÆôÏÔê¾Æᣬ1Ø1a±ê£¬1a±ê2»éá˸
lcd_wcom(0x06); //ÏÔê¾μØÖ·μYÔö£¬¼′D′ò»¸öêy¾Yoó£¬ÏÔê¾Î»ÖÃóòòÆò»Î»
lcd_wcom(0x01); //ÇåÆá
}
/**************************************
1|ÄüÃèêö£oLCD1602°′Ö¸¶¨Î»ÖÃÏÔê¾ò»¸ö×Ö·û
èë¿ú2Îêy£ouint8 X, uint8 Y, uint8 DData
·μ»ØÖμ£oÎT
***************************************/
void DisplayOneChar(uint8 X, uint8 Y, uint8 DData)
{
Y &= 0x1;
X &= 0xF; //ÏTÖÆX2»Äü′óóú15£¬Y2»Äü′óóú1
if (Y) X |= 0x40; //μ±òaÏÔê¾μú¶tDDê±μØÖ·Âë+0x40;
X |= 0x80; //Ëã3öÖ¸áîÂë
lcd_wcom(X); //·¢Ãüáî×Ö
lcd_wdat(DData); //·¢êy¾Y
}
/**************************************
1|ÄüÃèêö£oLCD1602°′Ö¸¶¨Î»ÖÃÏÔê¾ò»′®×Ö·û
èë¿ú2Îêy£ouint8 X, uint8 Y, uint8 DData X′ú±íμúX+1áD£¬Y′ú±íμúY+1DD
·μ»ØÖμ£oÎT
***************************************/
void DisplayListChar(uint8 X, uint8 Y, uint8 *DData)
{
uint8 ListLength;
ListLength = 0;
Y &= 0x1;
X &= 0xF; //ÏTÖÆX2»Äü′óóú15£¬Y2»Äü′óóú1
while (DData[ListLength]>0x19) //èôμ½′ï×Ö′®Î2ÔòíË3ö
{
if (X <= 0xF) //X×ø±êó|D¡óú0xF
{
DisplayOneChar(X, Y, DData[ListLength]); //ÏÔê¾μ¥¸ö×Ö·û
ListLength++;
X++;
}
}
}
//============================================
//¼ÆêyÆ÷¼Æêy
void time0(void) interrupt 1
{
m=k;
n++;
k=0;
TH0=65535/256;
TL0=65535%256;
}
//=======================================
//¼Æê±Æ÷¼Æê±
void time1(void) interrupt 3
{
k++;
TH1=0xFC;
TL1=0x18;
}
//=========================================
/速度和路程计算并显示
void calculate(float l,float v)
测速测距
{
uint8 length[7], speed[4];
//·3ì¸÷λ¼ÆËã
length[0]=l/10000;
length[1]=l/1000-length[0]*10;
length[2]=l/100-length[0]*100-length[1]*10;
length[3]=l/10-length[0]*1000-length[1]*100-length[2]*10;
length[4]=l-length[0]*10000-length[1]*1000-length[2]*100-length[3]*10;
length[5]=ASCII[10];
length[6]=(l-length[0]*10000-length[1]*1000-length[2]*100-length[3]*10-length[4])*10;
//Ëù¶è¸÷λ¼ÆËã
speed[0]=(int)(v*10)/100;
speed[1]=(int)(v*10)/10%10;
speed[2]=ASCII[10];
speed[3]=(int)(v*10)%10;
//ÏÔê¾Â·3ì
DisplayOneChar( 2,0 , ASCII[length[0]] );
DisplayOneChar( 3,0 , ASCII[length[1]] );
DisplayOneChar( 4,0 , ASCII[length[2]] );
DisplayOneChar( 5,0 , ASCII[length[3]] );
DisplayOneChar( 6,0 , ASCII[length[4]] );
DisplayOneChar( 7,0 , ASCII[10] );
DisplayOneChar( 8,0 , ASCII[length[6]] );
DisplayOneChar( 9,0 , ASCII[12] );
//ÏÔê¾Ëù¶è
DisplayOneChar( 4, 1, ASCII[10] );
DisplayOneChar( 6, 1, ASCII[12] );
DisplayOneChar( 7, 1, ASCII[13] );
DisplayOneChar( 8, 1, ASCII[14] );
DisplayOneChar( 2, 1, ASCII[ speed[0] ] );
DisplayOneChar( 3, 1, ASCII[ speed[1] ] );
DisplayOneChar( 5, 1, ASCII[ speed[3] ] );
}
//=========================================================================
//===¼üÖμoˉêy
int Keyboard()
{
uint8 k,z;
int count=0;
while(1)
{
k=KeyScan();
if(k)
{
delay_ms(100);
switch(k)
{
case 1: //°′Ï°′¼üD
{
z=1;
break;
}
case 2: //°′Ï°′¼üC
{
z=2;
break;
}
case 3: //°′Ï°′¼üB
{
z=3;
break;
}
case 4: //°′Ï°′¼üA
{
z=4;
break;
}
case 5: //°′Ï°′¼ü#
{
z=5;
break;
}
case 6: //°′Ï°′¼ü9
{
z=6;
break;
}
case 7: //°′Ï°′¼ü6
{
z=7;
break;
}
case 8: //°′Ï°′¼ü3
{
z=8;
break;
}
case 9: //°′Ï°′¼ü0
{
z=9;
break;
}
case 10: //°′Ï°′¼ü8
{
z=0;
break;
}
}
break;
}
}
return z;
}
//===========================================================================
//===α¼üÖμoˉêy
int keyboard1()
{
int z=0;
uint8 k;
while(1)
{
k=KeyScan();
if(k)
break;
if(P25==1)
{
z=1;
break;
}
}
return z;
}
//===========================================================================
//测速
int cesu(int t)
{
int count;
float l,v;
count=0;
DisplayListChar(0,0,table1);
DisplayListChar(0,1,table2); //′óμú¶tDDμú9áD¿aê¼ÏÔê¾ÄúèY2
k=1;
while(!n)
{
v=0;
l=2.2*n;
calculate(l,v);
if(P25==0)
count=1;
if((P25==1)&&(count==1))
{
delay_ms(150);
break;
}
}
while(n)
{
l=2.2*n;
v=2200/m;
calculate(l,v);
if(P25==0)
count=1;
if((P25==1)&&(count==1))
{
delay_ms(150);
break;
}
}
return t;
}
//==========================================================================
//推荐
uint32 suggest(uint8 Weight,uint8 Height)
{
uint8 suggestion;
suggestion=(Height-Weight)*1000/8;
return suggestion;
}
/***************************************************************************
* Ãè êö : Ö÷oˉêy
* èë 2Î : ÎT
* ·μ»ØÖμ : ÎT
**************************************************************************/
void main()
{
uint8 height[3],Height,weight[2],Weight;
GPIO();
init();
lcd_init(); //òo¾§3õê¼»ˉ
while(1)
{
while(P25==0)
{
t=0;
if(P25==0)
delay_ms(150);
DisplayListChar(0,0,table5);
DisplayListChar(0,1,table6);
DisplayOneChar( 10, 0, ASCII[17]);
DisplayOneChar( 11, 0, ASCII[12]);
DisplayOneChar( 9, 1, ASCII[15]);
DisplayOneChar( 10, 1, ASCII[16]);
if(keyboard1())
break;
height[0]=Keyboard();
DisplayOneChar( 7, 0, ASCII[height[0]]);
if(keyboard1())
break;
height[1]=Keyboard();
DisplayOneChar( 8, 0, ASCII[height[1]]);
if(keyboard1())
break;
height[2]=Keyboard();
DisplayOneChar( 9, 0, ASCII[height[2]]);
if(keyboard1())
break;
weight[0]=Keyboard();
DisplayOneChar( 7, 1, ASCII[weight[0]]);
if(keyboard1())
break;
weight[1]=Keyboard();
DisplayOneChar( 8, 1, ASCII[weight[1]]);
Weight=weight[0]*10+weight[1];
Height=height[0]*100+height[1]*10+height[2];
delay_ms(2500);
lcd_wcom(0x01);
while(1)
{
DisplayListChar(0,0,table7);
DisplayOneChar(5 , 1, ASCII[12]);
DisplayOneChar(0 , 1, ASCII[suggest(Weight,Height)/10000]);
DisplayOneChar(1 , 1, ASCII[suggest(Weight,Height)/1000%10]);
DisplayOneChar(2 , 1, ASCII[suggest(Weight,Height)/100%10]);
DisplayOneChar(3 , 1, ASCII[suggest(Weight,Height)%100/10]);
DisplayOneChar(4 , 1, ASCII[suggest(Weight,Height)%10]);
if(P25==1)
break;
}
}
lcd_wcom(0x01);
while(P25==1)
{
if(P25==1)
delay_ms(100);
if(!t)
cesu(t);
}
lcd_wcom(0x01);
}
}
|