兼职项目 智能自行车
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

202 lines
3.5 KiB

#include "IRIN.h"
#include "i2c.h"
uint journey;
uint journey1;
extern uchar pwm_left;
unsigned int b;
unsigned int c;
extern uint time1;
extern uchar pwm_val_left;
unsigned int b2;
unsigned int c2;
uint money=7;
uint sudu=0;
uint count=0; //栅格是一圈20个
uchar ms=0;
uint speed=0;
uint r_s=0;
bit state=0;
bit s0=0;
bit flas=0;
uchar sec=0;
uchar sec1=0;
/*******************************************************************************
* 函 数 名 : delay
* 函数功能 : 延时函数,i=1时,大约延时10us
*******************************************************************************/
void delay11(int i)
{
while(i--);
}
void IRIN_Init(void)
{
TMOD=0x01 | 0x20 ;//定时器1工作方式1
TH0=0x3C;
TL0=0xb0;
ET0=1;
TR0=1;
EA=1;
EX1=1;
IT1=1;
}
void display()
{
if(state==0)
{
sudu=At24c02Read(10);
if(money>7)
{
money=At24c02Read(5);
}
LcdSetCursor(0,0);
LcdShowStr(0,0,"Speed:");
Lcd1602_Write_Data(0x30+sudu/100%10);
Lcd1602_Write_Data(0x30+sudu/10%10);
Lcd1602_Write_Data(0x30+sudu%10);
LcdShowStr(10,0,"KM/h");
LcdShowStr(0,1,"L:");
Lcd1602_Write_Data(journey/10000%10+0x30);
Lcd1602_Write_Data(journey/1000%10+0x30);
Lcd1602_Write_Data(journey/100%10+0x30);
Lcd1602_Write_Data(journey/10%10+0x30);
Lcd1602_Write_Data(journey%10+0x30);
LcdShowStr(7,1,"M");
if(journey>1000)
{
money=(journey-1000)*0.002+7;
At24c02Write(5,money);
}
money=money*10;
LcdShowStr(9,1,"Q:");
Lcd1602_Write_Data(money/1000%10+0x30);
Lcd1602_Write_Data(money/100%10+0x30);
Lcd1602_Write_Data(money/10%10+0x30);
LcdShowStr(14,1,".");
Lcd1602_Write_Data(money%10+0x30);
money=money/10;
}
}
void keypros()
{
if(k2==0)
{
delay11(1000); //消抖处理
if(k2==0)
{
sudu=At24c02Read(10); //读取EEPROM地址1内的数据
b2=At24c02Read(1);
c2=At24c02Read(8);
journey=(b2<<8) | c2;
money=At24c02Read(5);
}
while(!k2);
}
if(k1==0)
{
delay11(1000); //消抖处理
if(k1==0)
{
flas=1;
}
while(!k1);
}
if(k4==0)
{
delay11(1000); //消抖处理
if(k4==0)
{
if(pwm_left>=20)
{
pwm_left=1;
}
else
{
pwm_left++;
}
}
while(!k4);
}
}
void time0() interrupt 1 //定时器 0
{
TH0=0x3C;
TL0=0xb0; //50ms定时
ms++; //变量50ms加一
if(ms%5==0) s0=~s0;
if(ms%10==0) //500ms定时
{
if(sec!=0) sec--;
if(sec1!=0) sec1--;
ms=0; //清零变量
EX1=0; //关闭外部中断,保证变量在计算的时候不会增加
r_s=count; //因为是500ms检测一次转动数据,所以要想一秒转动的圈数是 speed=count*2 ,但是光栅是一圈20格,所以还要除以20;
//综合一下,一秒转动的圈数就是 speed=count*2/20 ==》 speed=counr/10;
count=0; //清零变量
EX1=1; //打开外部中断
speed=speed*0.5+(r_s*6)*0.5; // 这里r_s是一秒走的圈数,然后乘以60得到60秒走的圈数,单位变成了 r/min; 总方式 speed=count/10*60; ==>> speed=count*6;
if(flas==0)
{
sudu=(0.15*speed);
At24c02Write(10,sudu);
journey=journey+(r_s*2.5/20);
journey1=journey;
c = journey & 0x00FF;
At24c02Write(8,c);
delay11(1000);
b=journey1>>8;
At24c02Write(1,b);
}
else
{
journey=0;
money=7;
At24c02Write(1,journey);
At24c02Write(8,journey);
At24c02Write(5,money);
flas=0;
}
}
}
void int1() interrupt 2 //外部中断 1
{
count++; //中断每触发一次,变量加一
}