main.c
上传用户:xs588588
上传日期:2021-03-30
资源大小:242k
文件大小:4k
- #include "I2C.h"
- #include "Led.h"
- #define N 624
- int nInputCount;
- int nIn_Flag;
- int nTimeCount;
- int nWrite_Flag;
- unsigned char seg[]={0x3f,0x06,0x5b,0x4f, /* 0 1 2 3*/
- 0x66,0x6d,0x7d,0x07, /* 4 5 6 7 */
- 0x7f,0x6f,0x77,0x7c, /* 8 9 A B */
- 0x39,0x5e,0x79,0x71 /* C D E F */
- };
- void Init_TimerB(void);
- void main(void)
- {
- unsigned char nValue;
- char nTemp0;
- char nTemp1;
- char nTemp2;
- char nTemp3;
- char nTemp4;
- char nTemp5;
- int nSpeed;
- int nSpeed1;
- int nSpeed2;
- int nSpeed3;
- // 关闭看门狗
- WDTCTL = WDTPW + WDTHOLD;
- //关闭中断
- _DINT();
- //初始化时钟
- Init_CLK();
- //端口初始化
- Init_Port();
- I2C_Initial();
- nInputCount = 0;
- nIn_Flag = 0;
- nTimeCount = 0;
- nWrite_Flag = 0;
- nTemp0 = 0;
- nTemp1 = 0;
- nTemp2 = 0;
- nTemp3 = 0;
- nTemp4 = 0;
- nTemp5 = 0;
- //打开中断
- _EINT();
-
- //读出初始的里程数
- ReadRandom(0,&nTemp0);
- if(nTemp0 == 0xff) nTemp0 = 0;
- ReadRandom(1,&nTemp1);
- if(nTemp1 == 0xff) nTemp1 = 0;
- ReadRandom(2,&nTemp2);
- if(nTemp2 == 0xff) nTemp2 = 0;
- ReadRandom(3,&nTemp3);
- if(nTemp3 == 0xff) nTemp3 = 0;
- ReadRandom(4,&nTemp4);
- if(nTemp4 == 0xff) nTemp4 = 0;
- ReadRandom(5,&nTemp5);
- if(nTemp5 == 0xff) nTemp5 = 0;
- //显示初始里程数据
- //清除锁存信号
- STCLK_Lo();
- DataOut(seg[nTemp5]);
- DataOut(seg[nTemp4]);
- DataOut(seg[nTemp3]);
- DataOut(seg[nTemp2]);
- DataOut(seg[nTemp1]);
- DataOut(seg[nTemp0]);
- //给锁存信号,显示上面的6位数据
- STCLK_Hi();
- for(;;)
- {
- if(nIn_Flag == 1)
- {
- nIn_Flag = 0;
- //等待时间计数器设置完
- while(1)
- {
- if(nWrite_Flag == 0)
- {
- break;
- }
- }
- //计算速度
- nSpeed = (int)(1 * 3600 / nTimeCount);
- nTimeCount = 0;
- nSpeed3 = (int)(nSpeed / 100);
- nSpeed2 = (int)((nSpeed - 100 * nSpeed3) / 10);
- nSpeed1 = nSpeed - 100 * nSpeed3 - 10 * nSpeed2;
- //显示速度
- //清除锁存信号
- STCLK_Lo1();
- DataOut1(seg[nSpeed3]);
- DataOut1(seg[nSpeed2]);
- DataOut1(seg[nSpeed1]);
- //给锁存信号,显示上面的3位数据
- STCLK_Hi1();
- //处理里程参数
- nTemp0 += 1;
- if(nTemp0 >= 10)
- {
- nTemp0 = 0;
- nTemp1 += 1;
- if(nTemp1 >= 10)
- {
- nTemp1 = 0;
- nTemp2 += 1;
- if(nTemp2 >= 10)
- {
- nTemp2 = 0;
- nTemp3 += 1;
- if(nTemp3 >= 10)
- {
- nTemp3 = 0;
- nTemp4 += 1;
- if(nTemp4 >= 10)
- {
- nTemp4 = 0;
- nTemp5 += 1;
- if(nTemp5 >= 10)
- {
- nTemp0 = 0;
- nTemp1 = 0;
- nTemp2 = 0;
- nTemp3 = 0;
- nTemp4 = 0;
- nTemp5 = 0;
- }//if(nTemp5 >= 10)
- }//if(nTemp4 >= 10)
- }//if(nTemp3 >= 10)
- }//if(nTemp2 >= 10)
- }//if(nTemp1 >= 10)
- }//if(nTemp0 >= 10)
- //显示里程数据
- //清除锁存信号
- STCLK_Lo();
- DataOut(seg[nTemp5]);
- DataOut(seg[nTemp4]);
- DataOut(seg[nTemp3]);
- DataOut(seg[nTemp2]);
- DataOut(seg[nTemp1]);
- DataOut(seg[nTemp0]);
- //给锁存信号,显示上面的6位数据
- STCLK_Hi();
- //将里程数据写入EEPROM
- WriteSingleByte(0,nTemp0);
- WriteSingleByte(1,nTemp1);
- WriteSingleByte(2,nTemp2);
- WriteSingleByte(3,nTemp3);
- WriteSingleByte(4,nTemp4);
- WriteSingleByte(5,nTemp5);
- }
- }
- return;
- }
- void Init_TimerB(void)
- {
- // 选择ACLK,清除TAR
- TBCTL = TBSSEL0 + TBCLR;
- // TBCCR0 中断允许
- TBCCTL0 = CCIE;
- // 时间间隔为 1 s
- TBCCR0 = 32768;
- // 增记数模式
- TBCTL |= MC0;
- return;
- }
- ///////////////////////////////////////
- // 处理来自端口 P2.0 的中断
- interrupt [PORT1_VECTOR] void INPUT_ISR(void)
- {
- if(P2IFG & BIT0)
- {
- //增加脉冲计数器
- nInputCount += 1;
- if(nInputCount >= 8 * N)
- {
- //行使到一公里
- nInputCount = 0;
- //设置标志
- nIn_Flag = 1;
- }
- // 清除中断标志位
- P2IFG &= ~(BIT0);
- }
- }
- //定时器B的中断处理
- interrupt [TIMERB0_VECTOR] void TimerB_ISR(void)
- {
- //设置写标志
- nWrite_Flag = 1;
- //秒记数器加1
- nTimeCount += 1;
- //解除写标志
- nWrite_Flag = 0;
- }