开发工具:
文件大小: 5kb
下载次数: 0
上传时间: 2018-05-02
详细说明:
#include #include //----------------------------------------------------- //*定义25045端口--------- sbit CS = P1^1; //*片选 sbit SO = P1^0; //*串行输出 sbit SI = P1^3; //*串行输入 sbit SCK = P1^2; //*串行时钟输入 char command; //*指令字 //------------------------------------- sbit K1 = P3^0; //计程开关 int pulsdate; //*计数脉冲变量 int lengthsdate1; //*保存本次里程数的整数 int lengthsdate2; //*保存本次里程数的小数 int totallength; //*总里程数 //------------------------------------------- int timer0date1; //*定时器0记数变量变1 int timer0d ate2; //*定时器0记数变量变2 //*初始化2051工作寄存器--------- void init(void) { TMOD = 0x11; //0001 0001B T0:16位定时中断;T1:6位定时中断,方式1 TL0 = 0xfe; //时钟 0.00108506944444444444ms TH0 = 0xdb; //10ms=0x2400,0xffff-0x2400=0xdbff PT0 = 0; //定时器0中断优先级 PT1 = 0; PX0 = 0; PX1 = 1; //外部中断1中断优先级 ET0 = 1; //扫描开始时开T0中断 IE1 = 1; //开外部中断1 IT1 = 1; //边沿触发 TR0 = 1; //开T0 中断 EA = 1; //开总中断 command= 0x00; pulsdate=0x00; lengthsdate1=0x00; lengthsdate2=0x00; timer0date1=0x00; timer0date2=0x00; } //*把状态值State写入25045状态寄存器函数 void Write25045State(char State) { int i; SCK=0; CS=0; for(i=0;i<8;i++) { State<<1; SCK=1; SCK=0; SI=CY; } CS=1; } //*读25045状态寄存器函数,返回值为状态寄存器的值 char Read25045State(void) { int i; char State0; CS = 0; Write25045State(0x06); Write25045State(0x05); SCK=0; for(i=0;i<8;i++) { SCK=1; SCK=0; CY =SO; State0>>1; } CS=1; return State0; } //*从25045的data1存储器中读取数据 char Read25045MEM(char data1) { int i; char out1; CS = 1; CS = 0; command = 0x03; for(i=0;i<8;i++) { command<<1; SCK=0; SI=CY; SCK=1; } command= data1; for(i=0;i<8;i++) { command<<1; SCK=0; SI=CY; SCK=1; } SCK=0; for(i=0;i<8;i++) { SCK=1; SCK=0; CY =SO; out1>>1; } CS=1; return out1; } //*把date2写出25045MEM void Write25045MEM(char data2,char Memory1) { int i; CS = 1; CS = 0; command = 0x02; for(i=0;i<8;i++) { command<<1; SCK=0; SI=CY; SCK=1; } command= Memory1; for(i=0;i<8;i++) { command<<1; SCK=0; SI=CY; SCK=1; } for(i=0;i<8;i++) { data2<<1; SCK=0; SI=CY; SCK=1; } CS=1; } //*X25045芯片复位函数 void reset25045(void) { CS=0; CS=1; } // void init25045(void) { int i; char data5; CS=0; Write25045State(0x06); Write25045State(0x01); data5=0x04; for(i=0;i<8;i++) { data5<<1; SCK=0; SI=CY; SCK=1; } CS=1; } //*写LCM1010函数 void Writelcm1010(char data3 , char data6) { ; } //*定义外部中断1函数 void Int1(void) interrupt 2 { char totall; char totalh; pulsdate++; lengthsdate2+=16; if(lengthsdate2>10000) lengthsdate2-=10000; if(pulsdate>=624) { lengthsdate2=0; lengthsdate1++; pulsdate-=624; } if(pulsdate==0) { totallength++; totall=totallength; Write25045MEM(totall,0x00); totalh=(totallength>>8); Write25045MEM(totalh,0x01); } } //*定义定时器0中断函数 void Timer0(void) interrupt 1 { char i; bit j; bit k; char a; timer0date1++; timer0date2++; if(timer0date1>=4) { timer0date1-=4; } if (k==K1) { a++; } else { k=K1; a=0; } if(i>=20) { j=k; a=20; } if(!j) { switch(timer0date1) { case 0: i=lengthsdate2/100; Writelcm1010(i,timer0date1); break; case 1: i=lengthsdate1; Writelcm1010(i,timer0date1); break; case 2: i=(lengthsdate1/10); Writelcm1010(i,timer0date1); break; case 3: i=(lengthsdate1/100); Writelcm1010(i,timer0date1); break; default: break; } } else { switch(timer0date1) { case 0: i=totallength; Writelcm1010(i,timer0date1); break; case 1: i=(totallength/10); Writelcm1010(i,timer0date1); break; case 2: i=(totallength/100); Writelcm1010(i,timer0date1); break; case 3: i=(totallength/1000); Writelcm1010(i,timer0date1); break; default: break; } } } //*主函数 void main(void) { char totall; char totalh; init(); init25045(); totall=Read25045MEM(0x00); totalh=Read25045MEM(0x01); totallength=totalh; totallength<<8; totallength=totall; while(1) { if(timer0date2>=30) { timer0date2=0; init25045(); } } ...展开收缩
(系统自动生成,下载前可以参看下载内容)
下载文件列表
相关说明
- 本站资源为会员上传分享交流与学习,如有侵犯您的权益,请联系我们删除.
- 本站是交换下载平台,提供交流渠道,下载内容来自于网络,除下载问题外,其它问题请自行百度。
- 本站已设置防盗链,请勿用迅雷、QQ旋风等多线程下载软件下载资源,下载后用WinRAR最新版进行解压.
- 如果您发现内容无法下载,请稍后再次尝试;或者到消费记录里找到下载记录反馈给我们.
- 下载后发现下载的内容跟说明不相乎,请到消费记录里找到下载记录反馈给我们,经确认后退回积分.
- 如下载前有疑问,可以通过点击"提供者"的名字,查看对方的联系方式,联系对方咨询.