u012243626
V R X
采纳率0%
2015-12-23 14:52 阅读 1.7k
已结题

基于lpc2114做sht11在lcd1602显示代码调试驱执行不了,求大神指教

/****************************************************************************

  • 基于LPC2114做的一个SHT11温湿度传感器在LCD1602显示 ****************************************************************************/ #include "config.h" #include

#define RS (1<<8)

#define RW (1<<9)

#define E (1<<10)

#define BUSY (1<<7)
#define SCK (1<<16)
#define DATA (1<<17)
//#define DBPort 0x80
#define FIRSTLINE 0x80

#define SECONDLINE 0xc0

#define uchar unsigned char
enum {TEMP,HUMI};

#define noACK 0 //继续传输数据,用于判断是否结束通讯
#define ACK 1 //结束数据传输;
//地址 命令 读/写
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0

unsigned char str1[]="TEMP: %C";

unsigned char str2[]="HUMI: %RH";
unsigned char CRC_Table[];

//长软件延时,时间为ms
void DelayMs(unsigned char ms)

{

unsigned int i;

for(;ms>0;ms--)

for(i=0;i<50000;i++);

}

void delay_us(unsigned int nus)

{

unsigned int i;
unsigned char j;
for(i = 0; i < nus; i++)
{
for(j = 0; j < 6; j++){}
}

}
void delay_ms(unsigned int nms)

{

unsigned int i;
for(i = 0; i < nms; i++)
{
delay_us(2);
}

}

/*******************************************************

  • LCD16021函数

********************************************************/

// 检查LCD是否忙碌,忙碌等待,不忙了返回

void CheckBusy()

{

IO0DIR = 0x700;//0-7输入,8-10输出 ,读状态

while(1)  
{  
    //读状态,RS=L,RW=H,E=H  
    IO0CLR = RS;  
    IO0SET = RW;  
    IO0SET = E;  

    if(!(IO0PIN & BUSY))      //如果不忙了,返回  
    {  
        break;  
    }  
    IO0CLR = E;//如果忙了,清空E标志,继续等待      
}  
IO0DIR = 0x7ff;//IO口全部恢复为输出   

}

//写命令(指令)函数
void WriteCommand(unsigned char command)

{

CheckBusy(); //检查是否BUSY ,忙碌一直在此等待

//写指令,RS=L,RW=L,然后D0~D7=指令码,E=高脉冲发送命令  
IO0CLR = RS;  
IO0CLR = RW;  
IO0CLR = 0xff;//清空端口  
IO0SET = command;  
IO0SET = E;  
IO0CLR = E;

}

//写数据函数
//显示数据之前要先设定显示位置

void WriteData(unsigned char data)

{

CheckBusy(); //检查是否BUSY ,忙碌一直在此等待

//写指令,RS=H,RW=L,然后D0~D7=指令码,E=高脉冲发送命令  
IO0SET = RS;  
IO0CLR = RW;   
IO0CLR = 0xff;//清空端口  
IO0SET = data;  
IO0SET = E;//发送数据  
IO0CLR = E;//恢复,以便继续使用  

}

//功能:设置显示位置
//坐标自动加1
void Set_XY(unsigned char x,unsigned char y)

{

switch(x)

{

case 0:

y+=FIRSTLINE;

break;

case 1:

y+=SECONDLINE;

break;

}

WriteCommand(y); //显示坐标更新 设置数据指针

}

//功能:在指定位置显示单个字符

void DisplayChar(unsigned char x,unsigned char y,unsigned char data)

{

Set_XY(x,y);

WriteData(data);

}

// 功能:在指定位置显示字符串

void DisplayString(unsigned char x,unsigned char y,unsigned char *str)

{

Set_XY(x,y);

while(*str)

{

WriteData(*str);

str++;

}

}

// 功能:初始化LCD1602函数
void InitLcd()

{

//写命令

WriteCommand(0x01);//显示清屏,数据指针、所有显示清零

WriteCommand(0x38);//设置16*2显示,5*7点阵,8位数据接口

WriteCommand(0x0f);//显示开及光标设置显示器开、光标开、闪烁开

}

/*******************************************************

  • SHT11函数

********************************************************/

//启动传输
void START_SHT11()

{

IO1SET = SCK;
IO1SET = DATA;
delay_us(5);
IO1CLR = DATA;
delay_us(5);
IO1CLR = SCK;
delay_us(5);
IO1SET = SCK;
delay_us(5);
IO1SET = DATA;

}

//应答信号
void SHT11_answer()
{
IO1CLR = SCK;
delay_us(5);
IO1SET = SCK;
delay_us(5);
//while( IO1SET = DATA);
IO1DIR = (~0x3ffff); //为输入功能
//IO1PIN = IO1PIN | 0x00020000
while(IO1PIN & 0x00020000);
IO1CLR = SCK;
delay_us(5);
IO1SET = DATA;
}

//单片机向SHT11发送应答信号
void MCU_answer()
{
IO1CLR = SCK;
delay_us(5);
IO1CLR = DATA;
delay_us(5);
IO1SET = SCK;
delay_us(5);
IO1CLR = SCK;
delay_us(5);
IO1SET = DATA;
}

void SHT11_DATA_READY()
{
IO1DIR = (~0x3ffff); //为输入功能
//IO1PIN = IO1PIN | 0x00020000
while(IO1PIN & 0x00020000);
//while( IO1SET = DATA);
}

//连接复位
void reconnect()
{
unsigned char i;
IO1SET = DATA;
IO1SET = SCK; //准备
for(i = 0; i < 9; i++) //DATA保持高,SCK时钟触发9次,发送启动传输,通迅即复位
{
IO1SET = SCK;

IO1CLR = SCK;
}
START_SHT11(); //启动传输
}

//写字节程序
void write_sht11(unsigned char cmd)

{
unsigned char i;
//IO1DIR = DATA;
for (i = 0; i < 8; i++)
{
IO1CLR = SCK;
if(cmd & 0x80)
IO1SET = DATA;
else
IO1CLR = DATA;
delay_us(5);
IO1SET = SCK;
cmd = cmd << 1;
delay_us(5);
IO1CLR = SCK;
}
IO1SET = DATA; //释放总线
}

//读字节程序
unsigned char read_sht11(void)
{
unsigned char j, temp = 0, data = 0;
//IO1DIR = (~DATA);
IO1SET = DATA; //释放数据线
for(j = 0; j < 8; j++)
{
IO1SET = SCK;
data = data << 1;
delay_us(5);
IO1DIR = (~0x3ffff); //为输入功能
IO1PIN = IO1PIN | 0x00020000;
temp = ((IO1PIN & 0x00020000) >> 17);

if(temp)
data = (data | 0x01); //读一位数据线的值
else
data = (data & 0xfe);
IO1CLR = SCK;
delay_us(5);

}
IO1SET = DATA;
return(data);
}

//通信结束
void SHT11_end()
{
IO1SET = DATA;
delay_us(5);
IO1SET = SCK;
delay_us(5);
IO1CLR = SCK;
delay_us(5);
}

//寄存器设置函数
void SHT11_Write_Register()
{
START_SHT11();
write_sht11(0x06);
SHT11_answer();
write_sht11(0x00); //12 and 14 bit temperrature
SHT11_answer();
}

//温湿度测量
unsigned short SHT11_Measure(unsigned char command)
{
unsigned short dat_1=0;
unsigned char data_high,data_low,CRC;
unsigned char val,tt,i;
START_SHT11();
write_sht11(command);
SHT11_answer();
SHT11_DATA_READY();
data_high = read_sht11();
MCU_answer();
data_low = read_sht11();
MCU_answer();
CRC = read_sht11();
SHT11_end();
dat_1 = ( dat_1 | data_high);
dat_1 = ((dat_1< val = CRC_Table[(command^0x00)];
val = CRC_Table[(val^data_high)];
val = CRC_Table[(val^data_low)];
for( i = 0; i {
tt = tt tt |= val & 0x01;
val=val >> 1;
}
val = tt;
if(val == CRC)
return(dat_1);
else
return(2);
}

//温度计算校正
float SHT11_Convert_Tempeture14bit(unsigned int dat_1)
{
float tempeture1,flag_tempeture;
tempeture1=-40+0.01*dat_1;
if(tempeture1>100.0)
{
flag_tempeture = 1;
}
else if(tempeture1 < 0.0)
{
flag_tempeture = 1;
}
else
{
flag_tempeture = 0;
}
return(tempeture1);
}

//湿度计算校正
float SHT11_Convert_Humidity12bit(unsigned int dat,float temp)
{
float RHline,RHtrue,flag_humidity;
RHline=-4+0.0405*dat-0.0000028*dat*dat;
RHtrue=(temp-25)*(0.01+0.00008*dat)+RHline;
if(RHtrue<10.0)
{
flag_humidity=1;
}
else
{
flag_humidity=0;
}
return(RHtrue);
}

void main()
{
unsigned int temp,humi;

unsigned char wendu[6]; //用于记录温度
unsigned char shidu[6]; //用于记录湿度
float tempeture1,RHtrue;
unsigned short dat_1=0;

PINSEL0 &= 0xff00000;           //低11位为IO功能  
IO0DIR = 0x7ff;                 //为输出功能  
IO0CLR = 0x7ff;                 //清零  
PINSEL1 &= 0xfffffff0;          //低11位为IO功能  
IO1DIR = 0x3ffff;               //P1.16为输出功能  

InitLcd();                      //初始化LCD1602 
//显示数据  
DisplayString(0,0,str1);  
DisplayString(1,0,str2);

reconnect();                    //复位启动传输

while(1)
{
    dat_1 = SHT11_Measure(MEASURE_TEMP);    //温度测量
    if( dat_1 = 2)
    {
        reconnect();                        //如果发生错误,系统复位
        tempeture1 = SHT11_Convert_Tempeture14bit(dat_1);
    }
    else
    {
        temp = tempeture1 * 10;                  
        wendu[0]=temp%10+'0';               //温度百位 
        wendu[1]=temp/10+'0';               //温度十位  
        wendu[2]=temp%100/10+'0';           //温度个位
        wendu[3]=0x2E;                      //小数点
        wendu[4]=temp%10+'0';               //温度小数点后第一位       
        DisplayString(0,5,wendu);           //设置温度显示位置,输出温度       
    }

    dat_1 = SHT11_Measure(MEASURE_HUMI);    //湿度测量
    if( dat_1 = 2)
    {
        reconnect();                        //如果发生错误,系统复位
        RHtrue=SHT11_Convert_Humidity12bit(dat_1,tempeture1); 
    }
    else
    { 
        humi = RHtrue * 10;                  
        shidu[0]=humi%10+'0';               //湿度百位
        shidu[1]=humi/10+'0';               //湿度十位  
        shidu[2]=humi%100/10+'0';           //湿度个位
        shidu[3]=0x2E;                      //小数点
        shidu[4]=humi%10+'0';               //湿度小数点后第一位
        DisplayString(1,5,shidu);           //设置湿度显示位置,输出湿度
    }
    DelayMs(80);                             //等待足够长的时间,以现行下一次转换                    
}
return 0;

}

  • 点赞
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐