找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1593|回复: 1
打印 上一主题 下一主题
收起左侧

关于ADXL345传感器程序的问题

[复制链接]
跳转到指定楼层
楼主
ID:487028 发表于 2020-4-4 16:35 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. 在STC89C52RC 上,XYZ都没有数据,我用的LCD1602屏是 IIC的,会不会起冲突啊。。。
复制代码
  1. #include<reg52.h>
  2. #include<PCF8574.h>
  3. #include<stdio.h>
  4. #include "math.h"
  5. #include "adxl345.h"

  6. #define uchar unsigned char
  7. #define uint  unsigned int

  8. unsigned char jl[]="jl00M";
  9. unsigned char bs[]= "bs00";
  10. unsigned char ReadAdxl345;     
  11. char dis0[16];     
  12. xdata char dis1[16];   
  13. xdata char dis2[16];   
  14. unsigned long time_20ms=0;
  15. unsigned char ReportFlag=0;  

  16. unsigned int BuShu=0;  
  17. float JuLi=0;
  18. float KaLuLi=0;  
  19. unsigned char statFlag = 0;
  20. void Init_Timer0(void);   
  21. void SendStr(unsigned char *s,unsigned char length);
  22. void UART_Init(void);
  23. void SendByte(unsigned char dat);
  24. void BuShuCheck(void);
  25. void delay1(uchar x)
  26. {
  27.         uchar a,b;
  28.         for(a=x;a>0;a--)
  29.          for(b=200;b>0;b--);
  30. }

  31. void delayms(unsigned int xms)  
  32. {
  33.     unsigned int x,y;
  34.     for(x=xms;x>0;x--)
  35.         for(y=110;y>0;y--);
  36. }

  37. void write_com(uchar com)   
  38. {        uchar com1,com2;
  39.         com1=com|0x0f;
  40.         write_add(com1&0xfc);
  41.         delay1(2);
  42.     write_add(com1&0xf8);
  43.         com2=com<<4;
  44.         com2=com2|0x0f;
  45.         write_add(com2&0xfc);
  46.         delay1(2);
  47.     write_add(com2&0xf8);
  48.         
  49. }

  50. void write_date(uchar date)  
  51. {
  52.         uchar date1,date2;
  53.         date1=date|0x0f;
  54.         write_add(date1&0xfd);
  55.         delay1(2);
  56.         write_add(date1&0xf9);
  57.         date2=date<<4;
  58.         date2=date2|0x0f;
  59.         write_add(date2&0xfd);
  60.         delay1(2);
  61.         write_add(date2&0xf9);
  62.                
  63. }

  64. void init_lcd()   
  65. {                                                                                                  
  66.         write_com(0x33);
  67.         delayms(6);
  68.         write_com(0x32);
  69.         delayms(6);
  70.         write_com(0x28);
  71.         delayms(6);
  72.         write_com(0x01);
  73.         delayms(6);
  74.         write_com(0x06);
  75.         delayms(6);
  76.         write_com(0x0c);
  77.         //write_LCD_Command(0x0f);
  78.         delayms(6);
  79. }


  80. void ShowString(unsigned char x,unsigned char y,unsigned char *str)
  81. {
  82.         
  83.         if(x == 1)
  84.         {
  85.                 write_com(0x80 | y-1);
  86.         }
  87.         if(x == 2)
  88.         {
  89.                 write_com(0xc0 | y-1);
  90.         }
  91.         //?????
  92.         
  93.         while(*str!='\0')
  94.         {
  95.                 write_date(*str);
  96.                 str++;
  97.         }

  98. }


  99. void main(void)
  100. {        Init_Timer0();     
  101.         UART_Init();
  102.                 if(Single_Read_ADXL345(0X00)==0xe5)   
  103.         {
  104.                 delayms(5);
  105.         }
  106.         else
  107.         {
  108.                 delayms(3);
  109.         }
  110.     while(1)
  111.                 {
  112.                                 BuShuCheck();        
  113.                         bs[2]='0'+BuShu/10;
  114.                 bs[3]='0'+BuShu%10;
  115.                 jl[2]='0'+(BuShu/2)/10;
  116.                 jl[3]='0'+(BuShu/2)%10;
  117.                             init_lcd();
  118.                                         ShowString(2,12,jl);
  119.                                         ShowString(1,13,bs);
  120.                         delayms(10000);
  121. }
  122. }

  123. void BuShuCheck(void)
  124. {
  125.         static unsigned char ErrorNum=0;         
  126.         static unsigned char NormalNum=0;

  127.         static unsigned char CheckNum=0;   
  128.         static unsigned char shuNum=0;
  129.         static unsigned char ceNum=0;
  130.         static unsigned int remBuShu=0;
  131.         ReadAdxl345=0;
  132.         ReadData_x();
  133.         CheckNum++;
  134.         if((temp_X<650)||(abs(temp_Y)>400))   
  135.         {
  136.                        
  137.                 ErrorNum++;  
  138.                 shuNum++;
  139.         }
  140.         else
  141.         {NormalNum++; ceNum++;}        

  142.         if((NormalNum!=0)&&(ErrorNum!=0))   
  143.         {
  144.                 ErrorNum=0;
  145.                 NormalNum=0;
  146.                 BuShu++;
  147.                 JuLi=(float)(BuShu/2)*0.45;
  148.                 KaLuLi=JuLi*70*0.832;   
  149.         }

  150.         if(CheckNum>=20)
  151.         {

  152.                 if((ceNum!=0)&&(shuNum==0))
  153.                 {
  154.                         statFlag = 0;
  155.                 }
  156.                 else if((ceNum==0)&&(shuNum!=0))
  157.                 {
  158.                         statFlag = 1;
  159.                 }
  160.                 else if((ceNum!=0)&&(shuNum!=0))
  161.                 {
  162.                         if((BuShu - remBuShu)>=6)
  163.                         {statFlag = 2;}
  164.                         else
  165.                         {statFlag = 3;}
  166.                         remBuShu = BuShu;                     
  167.                 }
  168.                 CheckNum=0;
  169.                 ceNum=0;  
  170.                 shuNum=0;                        
  171.         }
  172. }
  173. void Init_Timer0(void)
  174. {
  175.         TMOD |= 0x01;                 
  176.         TH0=(65536-20000)/256;     
  177.         TL0=(65536-20000)%256;
  178.         EA=1;     
  179.         ET0=1;  
  180.         TR0=1;   
  181. }

  182. void Timer0_isr(void) interrupt 1
  183. {
  184.         TH0=(65536-20000)/256;   
  185.         TL0=(65536-20000)%256;
  186.         
  187.         time_20ms++;
  188.         if(time_20ms%5==0)
  189.         {
  190.                 ReadAdxl345=1;
  191.         }
  192.         if(time_20ms%50==0)
  193.         {
  194.                 ReportFlag=1;
  195.         }
  196. }


  197. void UART_Init(void)
  198. {
  199.     SCON  = 0x50;      
  200.     TMOD |= 0x20;      
  201.     TH1   = 0xFD;     
  202.         TL1 = TH1;  
  203.     TR1   = 1;                           
  204.     EA    = 1;   
  205.     ES    = 1;        
  206. }
  207. void DelayUs2x(unsigned char t)
  208. {   
  209. while(--t);
  210. }
  211. void SendByte(unsigned char dat)
  212. {
  213.         unsigned char time_out;
  214.         time_out=0x00;
  215.         SBUF = dat;   
  216.         while((!TI)&&(time_out<100))
  217.         {time_out++;DelayUs2x(10);}  
  218.         TI = 0;     

  219. void SendStr(unsigned char *s,unsigned char length)
  220. {
  221.         unsigned char NUM;
  222.         NUM=0x00;
  223.         while(NUM<length)   
  224.         {
  225.                 SendByte(*s);
  226.                 s++;   
  227.                 NUM++;  
  228.            }
  229. }

  230. void UART_SER (void) interrupt 4
  231. {
  232.         if(RI)  
  233.         {
  234.                 RI=0;   
  235.         }
  236.         if(TI)
  237.         TI=0;
  238. }
复制代码


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:487028 发表于 2020-4-4 16:35 | 只看该作者
temp_X、temp_Y,temp_Z  都没有显示
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表