哪个大神带我看一下
#include <msp430x14x.h>
#define uchar unsigned char
#define uint unsigned int
#define LCD_RS_EN P3OUT |= BIT0 //寄存器选择输入
#define LCD_RS_CLR P3OUT &= ~BIT0
#define LCD_RW_EN P3OUT |= BIT1 //液晶读/写控制
#define LCD_RW_CLR P3OUT &= ~BIT1
#define LCD_EN_EN P3OUT |= BIT2 //液晶使能控制
#define LCD_EN_CLR P3OUT &= ~BIT2
#define LCD_PSB_EN P6OUT |= BIT2 //串/并方式控制
#define LCD_PSB_CLR P6OUT &= ~BIT2
//#define LCD
#define HIGH P2OUT|=BIT1;
#define LOW P2OUT&=~BIT1;
char temph,templ,humdh,humdl,check,cal;
uchar address=0;
uchar string[]={"温湿测试系统"};
uchar str_t[]={"温 度:"};
uchar str_r[]={"湿 度:"};
uchar str_suit[]={"适宜度:"};
uchar str1[]={"适 宜"};
uchar str2[]={"不适宜"};
void delay_1ms(uint z)
{
uint i,j;
for(i=z;i>0;i--)
for(j=80;j>0;j--);
}
void DelayNus(uint n)//延时1US函数
{
TACTL|= TASSEL_2+TACLR+ID_3;//定时器A,时钟为SCLK,8分频;
TACTL |= MC_1; //增计数到CCR0
TACCR0 = n;
while((TACTL & BIT0)!=0x01); //等待
TACTL &= ~MC_1; //停止计数
TACTL &= ~BIT0; //清除中断标志
}
void write_date(uchar date)//12864 写数据
{
LCD_RS_EN;
LCD_RW_CLR;
P4OUT = date;
delay_1ms(5);
LCD_EN_EN;
delay_1ms(5);
LCD_EN_CLR;
}
void write_cmd(uchar cmd)//12864 写指令
{
LCD_RS_CLR;
LCD_RW_CLR;
P4OUT = cmd;
delay_1ms(5);
LCD_EN_EN;
delay_1ms(5);
LCD_EN_CLR;
}
void init12864()//初始化 12864
{
LCD_PSB_EN; //bingxing
write_cmd(0x30); //jiben zhi lin ji
delay_1ms(5);
write_cmd(0x0c);
delay_1ms(5);
write_cmd(0x01);
delay_1ms(10);
}
void port_int()
{
P4DIR |= 0XFF;
P3DIR |= 0XFF;
}
void init_clk()
{
int i;
WDTCTL=WDTPW+WDTHOLD;
BCSCTL1&=~XT2OFF;
do
{
IFG1 &=~OFIFG;
for(i=0xff;i>0;i--);
}
while(IFG1&OFIFG);
BCSCTL2|= SELM_2+SELS;//SCLK,MCLK都选择为外部时钟
}
void display(uchar pos_x,uchar pos_y,uchar string1[],int str_length)
{
uchar i;
if(pos_x==1)
address=0x80+pos_y;
else if(pos_x==2)
address=0x90+pos_y;
else if(pos_x==3)
address=0x88+pos_y;
else
address=0x98+pos_y;
write_cmd(0x80);
write_cmd(address);
delay_1ms(10);
for(i=0;i<str_length;i++)
write_date(string1[i]);
}
char receive(void) //接受函数
{
char temp,cnt=0; //临时变量用于存储接受数据
while(cnt<8)
{
while((P2IN&BIT1)==0); //等待50us的低电平结束
DelayNus(35);
if(P2IN&BIT1) //长于30us定义为1
{
temp++;
temp<<=1;
while(P2IN&BIT1); //结束高电平,即下一位数据的到来
}
else
{
temp<<=1;
}
if(cnt!=7)
while(!(P2IN&BIT1)); //最后一次给函数返回留下时间
cnt++;
}
return temp;
}
void work_data(void)//DHT11 的初始化函数
{
P2DIR |= BIT1; //设置P2.1为输出状态
HIGH;
_NOP();_NOP();_NOP();
LOW;
DelayNus(18000); //开始信号
HIGH;
DelayNus(30);
P2DIR&=~BIT1; //设置P2.1为输入状态,检测传感器响应
DelayNus(20); //20US后 P2IN 是否为低电平,不为低电平 说明复位失败,重新开始
while((P2IN&BIT1)!=0)//如果没有检测到响应信号 继续发送开始信号,如果没有检测到以上的复位,则重复执行
{
P2DIR |= BIT1; //设置P2.1为输出状态
HIGH;
_NOP();_NOP();_NOP();
LOW;
DelayNus(18000); //开始信号
HIGH;
DelayNus(30);
P2DIR&=~BIT1; //设置P2.1为输入状态,检测传感器响应
DelayNus(20);
}
while((P2IN&BIT1)==0);//等待拉高,准备输出数据
while(P2IN&BIT1); //等待低电平,输出数据
//下面开始接受数据
humdh=receive();
humdl=receive();
temph=receive();
templ=receive();
check=receive();
cal=humdh+humdl+temph+templ;
}
void main()
{
char R_shi,R_ge,T_shi,T_ge,T_xs1,T_xs2,R_xs;
init_clk();
port_int();
init12864();
display(1,1,string,12);
display(2,0,str_t,8);
display(3,0,str_r,8);
display(4,0,str_suit,8);
delay_1ms(2000); //越过传感器不稳定状态
while(1)
{
work_data();
if(check==cal)
{
R_shi = 0x30+humdh / 10;
R_ge = 0x30+humdh % 10;
R_xs = 0x30+humdl /10;
T_shi = 0x30+temph / 10;
T_ge = 0x30+temph % 10;
T_xs1 = 0x30+templ/10;
T_xs2 = 0x30+templ%10;
write_cmd(0x94);
write_date(T_shi);
write_date(T_ge);
write_date('.');
write_date(T_xs1);
write_date(T_xs2);
write_date(' ');
write_date(str_t[4]);
write_date(str_t[5]);
write_cmd(0x8C);
write_date(R_shi);
write_date(R_ge);
write_date('.');
write_date(R_xs);
write_date(' ');
write_date('%');
write_date('R');
write_date('H');
}
if(temph>10 && temph<30 && humdh>30 && humdh<60)
display(4,4,str1,6);
else
display(4,4,str2,6);
delay_1ms(3000);//等待下次采样
}
}
|