- 在STC89C52RC 上,XYZ都没有数据,我用的LCD1602屏是 IIC的,会不会起冲突啊。。。
复制代码- #include<reg52.h>
- #include<PCF8574.h>
- #include<stdio.h>
- #include "math.h"
- #include "adxl345.h"
- #define uchar unsigned char
- #define uint unsigned int
- unsigned char jl[]="jl00M";
- unsigned char bs[]= "bs00";
- unsigned char ReadAdxl345;
- char dis0[16];
- xdata char dis1[16];
- xdata char dis2[16];
- unsigned long time_20ms=0;
- unsigned char ReportFlag=0;
- unsigned int BuShu=0;
- float JuLi=0;
- float KaLuLi=0;
- unsigned char statFlag = 0;
- void Init_Timer0(void);
- void SendStr(unsigned char *s,unsigned char length);
- void UART_Init(void);
- void SendByte(unsigned char dat);
- void BuShuCheck(void);
- void delay1(uchar x)
- {
- uchar a,b;
- for(a=x;a>0;a--)
- for(b=200;b>0;b--);
- }
- void delayms(unsigned int xms)
- {
- unsigned int x,y;
- for(x=xms;x>0;x--)
- for(y=110;y>0;y--);
- }
- void write_com(uchar com)
- { uchar com1,com2;
- com1=com|0x0f;
- write_add(com1&0xfc);
- delay1(2);
- write_add(com1&0xf8);
- com2=com<<4;
- com2=com2|0x0f;
- write_add(com2&0xfc);
- delay1(2);
- write_add(com2&0xf8);
-
- }
- void write_date(uchar date)
- {
- uchar date1,date2;
- date1=date|0x0f;
- write_add(date1&0xfd);
- delay1(2);
- write_add(date1&0xf9);
- date2=date<<4;
- date2=date2|0x0f;
- write_add(date2&0xfd);
- delay1(2);
- write_add(date2&0xf9);
-
- }
- void init_lcd()
- {
- write_com(0x33);
- delayms(6);
- write_com(0x32);
- delayms(6);
- write_com(0x28);
- delayms(6);
- write_com(0x01);
- delayms(6);
- write_com(0x06);
- delayms(6);
- write_com(0x0c);
- //write_LCD_Command(0x0f);
- delayms(6);
- }
- void ShowString(unsigned char x,unsigned char y,unsigned char *str)
- {
-
- if(x == 1)
- {
- write_com(0x80 | y-1);
- }
- if(x == 2)
- {
- write_com(0xc0 | y-1);
- }
- //?????
-
- while(*str!='\0')
- {
- write_date(*str);
- str++;
- }
- }
- void main(void)
- { Init_Timer0();
- UART_Init();
- if(Single_Read_ADXL345(0X00)==0xe5)
- {
- delayms(5);
- }
- else
- {
- delayms(3);
- }
- while(1)
- {
- BuShuCheck();
- bs[2]='0'+BuShu/10;
- bs[3]='0'+BuShu%10;
- jl[2]='0'+(BuShu/2)/10;
- jl[3]='0'+(BuShu/2)%10;
- init_lcd();
- ShowString(2,12,jl);
- ShowString(1,13,bs);
- delayms(10000);
- }
- }
- void BuShuCheck(void)
- {
- static unsigned char ErrorNum=0;
- static unsigned char NormalNum=0;
- static unsigned char CheckNum=0;
- static unsigned char shuNum=0;
- static unsigned char ceNum=0;
- static unsigned int remBuShu=0;
- ReadAdxl345=0;
- ReadData_x();
- CheckNum++;
- if((temp_X<650)||(abs(temp_Y)>400))
- {
-
- ErrorNum++;
- shuNum++;
- }
- else
- {NormalNum++; ceNum++;}
- if((NormalNum!=0)&&(ErrorNum!=0))
- {
- ErrorNum=0;
- NormalNum=0;
- BuShu++;
- JuLi=(float)(BuShu/2)*0.45;
- KaLuLi=JuLi*70*0.832;
- }
- if(CheckNum>=20)
- {
- if((ceNum!=0)&&(shuNum==0))
- {
- statFlag = 0;
- }
- else if((ceNum==0)&&(shuNum!=0))
- {
- statFlag = 1;
- }
- else if((ceNum!=0)&&(shuNum!=0))
- {
- if((BuShu - remBuShu)>=6)
- {statFlag = 2;}
- else
- {statFlag = 3;}
- remBuShu = BuShu;
- }
- CheckNum=0;
- ceNum=0;
- shuNum=0;
- }
- }
- void Init_Timer0(void)
- {
- TMOD |= 0x01;
- TH0=(65536-20000)/256;
- TL0=(65536-20000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void Timer0_isr(void) interrupt 1
- {
- TH0=(65536-20000)/256;
- TL0=(65536-20000)%256;
-
- time_20ms++;
- if(time_20ms%5==0)
- {
- ReadAdxl345=1;
- }
- if(time_20ms%50==0)
- {
- ReportFlag=1;
- }
- }
- void UART_Init(void)
- {
- SCON = 0x50;
- TMOD |= 0x20;
- TH1 = 0xFD;
- TL1 = TH1;
- TR1 = 1;
- EA = 1;
- ES = 1;
- }
- void DelayUs2x(unsigned char t)
- {
- while(--t);
- }
- void SendByte(unsigned char dat)
- {
- unsigned char time_out;
- time_out=0x00;
- SBUF = dat;
- while((!TI)&&(time_out<100))
- {time_out++;DelayUs2x(10);}
- TI = 0;
- void SendStr(unsigned char *s,unsigned char length)
- {
- unsigned char NUM;
- NUM=0x00;
- while(NUM<length)
- {
- SendByte(*s);
- s++;
- NUM++;
- }
- }
- void UART_SER (void) interrupt 4
- {
- if(RI)
- {
- RI=0;
- }
- if(TI)
- TI=0;
- }
复制代码
|