|
本帖最后由 明日香 于 2020-12-9 13:30 编辑
风向传感器与单片机连接,用LCD1602显示风向数据,传感器返回的的应答帧数据是9个,其中LCD显示第7个数据
求大家帮忙看看哪里有问题:
这是单片机原理图:
这是传感器说明书:
以下为源程序:
- #include <stc12c5a.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- sbit E=P2^2; //1602使能引脚
- sbit RW=P2^1; //1602读写引脚
- sbit RS=P2^0; //1602数据/命令选择引脚
- uchar table[24]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
- uchar wind_direction[8]={0x01,0x03,0x00,0x00,0x00,0x02,0xC4,0x0B};//风向传感器问询帧
- uchar wind_direction1;//单片机接收风向数据
- uchar strwind[3];
- uchar num=0;
- void delay(uint z)//延时函数
- {
- uint x,y;
- for(x=z;x>0;x--);
- for(y=110;y>0;y--);
- }
- void wcmd(uchar del)//写命令函数
- {
- RS=0;//命令选择端
- delay(10);
- RW=0;//写命令
- delay(10);
- P0=del; //把命令字写入
- delay(10);
- E=1;//使能
- delay(10);
- E=0;
- }
- void lcd1602_writedata(uchar dat)//写数据函数
- {
- delay(10);
- RS=1;//数据选择端
- delay(10);
- RW=0;//写命令
- delay(10);
- P0=dat; //把要显示的数据写入
- delay(10);
- E=1;
- delay(10);
- E=0;
- }
- void lcd1602_init()//LCD初始化
- {
- wcmd(0x38);
- wcmd(0x0C);
- wcmd(0x06);
- wcmd(0x01);
- }
- void init() //系统初始化
- {
- TMOD |=0X20;//定时器T1,方式2,波特率由PCON寄存器的SMOD决定
- SCON=0x50; //REN RI TI,RI为0,TI为0
- //串行口1方式1 SCON是串行口1的串行控制寄存器,REN为1,允许接收
- PCON=0x00;//各工作方式波特率加倍
- TH1=0xfD;//9600bps@11.0592
- TL1=0xfD;
- TR1=1; //定时器1中断打开
- EA=1;//cpu总中断允许位,1为开放中断
- ES=1;// 1允许串行口中断
- }
- void main()
- {
- uchar m=0;
- init();
- while(1)
- {
- int i;
- for(i=0;i<8;i++)
- {
- SBUF=wind_direction[i];//风向数据
- while(TI==0);
- {
- } //数据发送结束时TI自动置1
- TI=0;
-
- }
- delay(1000);
- strwind[0]=(wind_direction1/100%10)+'0';
- strwind[1]=(wind_direction1/10%10)+'0';
- strwind[2]=(wind_direction1%10)+'0';
- lcd1602_init();
- delay(1000);
- wcmd(0x80);
- delay(1000);
- for(m=0;m<2;m++)
- {
- lcd1602_writedata(strwind[m]);
- }
- delay(5000);
- }
- }
- void UART_1() interrupt 4
- {
- //接收完缓冲数据后RI置1
- if(RI)//如果RI为1,即如果收到
- {
- RI=0; //RI置0
- if(num==0)
- {
- if(SBUF==0x01)
- {
- table[num++] = SBUF;
- }
- }
- else
- {
- table[num++]=SBUF;//风向数据
- if(num>8)
- {
- num=0;
- wind_direction1=table[6];//风向数据给wind_direction1[0]
- }
- }
- }
- }
复制代码
最终运行后LCD只显示00,请问程序哪里错了?是不是初始化或者中断部分有问题?求大家帮忙解答,谢谢。
|
|