谁能帮忙调试一下这个shtxx在msp430上的程序,从官方51程序改成MSP430f149的
我从官方的51程序改成MSP430f149的程序,我已经调试了好几天了,但是总是有问题,检测到的数据没有规律,也找不出是哪里有问题,只好到这里求助了。我在网上也找了一些说是430上的程序,用了也不行。其中还有一些我调试的痕迹。
/*************************************/
/*公 司:大连北方测控工程有限公司 */
/*工 程:DHTxx例程 (V1.0) */
/*文 件:DHTxx_Sample_Code.c */
/*C P U:80C51 */
/*编译环境:Keil Version 6.14 */
/*硬件环境:电源5V、时钟11.05926MHz */
//1.3版
/*************************************/
//#include <AT89s53.h> //Microcontroller specific library, e.g. port definitions
#include<msp430x14x.h>
#include "bianliang.h"
//#include <intrins.h> //Keil library (is used for _nop()_ operation)
#include <math.h> //Keil library
#include <stdio.h> //Keil library
#include"intrinsics.h"
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
typedef union
{ unsigned int i;
float f;
} value;
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};
unsigned char DATA;
//#define DATA P1_1
//#define SCK P1_0
#define noACK 0
#define ACK 1
//adrcommandr/w
#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
//#define SHT_SCK BIT0
//#define SHT_DAT BIT0
//#define SHT_SCK_OUT P4DIR|=SHT_SCK //时钟线为输出
//#define SHT_SCK_1 P4OUT|=SHT_SCK //SHT_SCK_1
//#define SHT_SCK_0 P4OUT&=~SHT_SCK//SHT_SCK_0
//#define SHT_DAT_OUT P5DIR|=SHT_DAT //数据线为输出
//#define SHT_DAT_IN P5DIR&=~SHT_DAT //数据线为输入
//#define SHT_DAT_1 P5OUT|=SHT_DAT //SHT_DAT=1
//#define SHT_DAT_0 P5OUT&=~SHT_DAT//SHT_DAT=0
//#define Read_SHT_Dat P5IN&0x01
#define nop delay_us(8)
#define SCK_1 P5OUT|=BIT1;P5DIR|=BIT1;//时钟线输出1
#define SCK_0 P5OUT&=~BIT1;P5DIR|=BIT1;//时钟线输出0
#define DATA_0 P5OUT|=BIT0;P5DIR|=BIT0;//数据线输出1
#define DATA_1 P5OUT&=~BIT0;P5DIR|=BIT0;//数据线输出0
#define DATAIN P5DIR&=~BIT0;DATA=P5IN&0x01//数据线输入
//#define DATA P5IN&0x01//数据线输入
//sbit SCK = P3^1;
//sbit DATA = P3^0;
/*****************************函数声明*****************************************/
extern void lcd12864_en_char1(UINT8 page,UINT8 seg,UINT8 d1,UINT8 fan);
extern void lcd12864_setxy_page(unsigned char x,unsigned char page);
extern void Lcd_init(void);
extern void LCD_fir(void);
//----------------------------------------------------------------------------------
lcd12864_char(UINT8 page,UINT8 seg,UINT32 d1,UINT8 fan)
{
UINT32 a=0;
a=d1;
UINT8 w1,w2,w3,w4,w5,w6;
w1=a/100000;
w2=a%100000/10000;
w3=a%10000/1000;
w4=a%1000/100;
w5=a%100/10;
w6=a%10/1;
lcd12864_en_char1(page,seg,w1,fan);//0正常显示,1反白显示
lcd12864_en_char1(page,seg+8,w2,fan);//0正常显示,1反白显示
lcd12864_en_char1(page,seg+16,w3,fan);//0正常显示,1反白显示
lcd12864_en_char1(page,seg+24,w4,fan);//0正常显示,1反白显示
lcd12864_en_char1(page,seg+32,w5,fan);//0正常显示,1反白显示
lcd12864_en_char1(page,seg+40,w6,fan);//0正常显示,1反白显示
}
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
{
unsigned char i,error=0;
for (i=0x80;i>0;i/=2) //shift bit for masking
{ //高位开始
SCK_0;nop;
if (i & value) {DATA_1;} //masking value with i , write to SENSI-BUS
else
DATA_0;
nop;
SCK_1; //clk for SENSI-BUS
nop;nop;nop; //pulswith approx. 5 us
//SCK_0;nop;
}
SCK_0;nop;
DATA_1; //release DATA-line
SCK_1; //clk #9 for ack
nop;
DATAIN;
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK_0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
{
unsigned char i,val=0;
DATA_1; //release DATA-line
for (i=0x80;i>0;i/=2) //shift bit for masking
{ SCK_1; //clk for SENSI-BUS
nop;
DATAIN;
nop;
if (DATA) val=(val | i); //read bit
SCK_0;
nop;
}
DATAIN;
DATA=!ack; //in case of "ack==1" pull down DATA-Line
SCK_1; //clk #9 for ack
nop;nop;nop; //pulswith approx. 5 us
SCK_0;
DATA_1; //release DATA-line
lcd12864_char(0xb0,64,val,0);
return val;
}
//----------------------------------------------------------------------------------
void s_transstart(void)
//----------------------------------------------------------------------------------
// generates a transmission start
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA_1; SCK_0; //Initial state
nop;
SCK_1;DATA_0;
nop;
SCK_0;
nop;nop;nop;
SCK_1;
nop;
DATA_1;
nop;
SCK_0;
}
//----------------------------------------------------------------------------------
void s_connectionreset(void)
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA_1; SCK_0; //Initial state
for(i=0;i<10;i++) //9 SCK cycles
{ SCK_1;
SCK_0;
}
s_transstart(); //transmission start
}
//----------------------------------------------------------------------------------
char s_softreset(void)
//----------------------------------------------------------------------------------
// resets the sensor by a softreset
{
unsigned char error=0;
s_connectionreset(); //reset communication
error+=s_write_byte(RESET); //send RESET-command to sensor
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_read_statusreg(unsigned char *p_value, unsigned char *p_checksum)
//----------------------------------------------------------------------------------
// reads the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error=s_write_byte(STATUS_REG_R); //send command to sensor
*p_value=s_read_byte(ACK); //read status register (8-bit)
*p_checksum=s_read_byte(noACK); //read checksum (8-bit)
return error; //error=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_write_statusreg(unsigned char *p_value)
//----------------------------------------------------------------------------------
// writes the status register with checksum (8-bit)
{
unsigned char error=0;
s_transstart(); //transmission start
error+=s_write_byte(STATUS_REG_W);//send command to sensor
error+=s_write_byte(*p_value); //send value of status register
return error; //error>=1 in case of no response form the sensor
}
//----------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
//----------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
{
unsigned error=0;
unsigned long i;
s_transstart(); //transmission start
switch(mode){ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for (i=0;i<525000;i++)
{
DATAIN;
if(DATA==0) break; //wait until sensor has finished the measurement
}
DATAIN;
if(DATA) error+=1; // or timeout (~2 sec.) is reached
*(p_value)=s_read_byte(ACK); //read the first byte (MSB)
lcd12864_char(0xb0,64,*(p_value),0);
*(p_value+1)=s_read_byte(ACK); //read the second byte (LSB)
lcd12864_char(0xb2,64,*(p_value+1),0);
*p_checksum =s_read_byte(noACK);//read checksum
return error;
}
//----------------------------------------------------------------------------------
//void init_uart()
//----------------------------------------------------------------------------------
//9600 bps @ 11.059 MHz
//{SCON= 0x52;
//TMOD= 0x20;
//TCON= 0x69;
//TH1 = 0xfd;
//}
//----------------------------------------------------------------------------------------
void calc_sth11(float *p_humidity ,float *p_temperature)
//----------------------------------------------------------------------------------------
// calculates temperature [癈] and humidity [%RH]
// input :humi (12 bit)
// temp (14 bit)
// output:humi [%RH]
// temp [癈]
{ const float C1=-4.0; // for 12 Bit
const float C2=+0.0405; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
float rh=*p_humidity; // rh: Humidity 12 Bit
float t=*p_temperature; // t: Temperature 14 Bit
float rh_lin; // rh_lin:Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C :Temperature [癈]
t_C=t*0.01 - 40; //calc. temperature from ticks to [癈]
rh_lin=C3*rh*rh + C2*rh + C1; //calc. humidity from ticks to [%RH]
rh_true=(t_C-25)*(T1+T2*rh)+rh_lin; //calc. temperature compensated humidity [%RH]
if(rh_true>100)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature [癈]
*p_humidity=rh_true; //return humidity[%RH]
}
//--------------------------------------------------------------------
float calc_dewpoint(float h,float t)
//--------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [癈]
// output:dew point [癈]
{ float logEx,dew_point;
logEx=0.66077+7.5*t/(237.3+t)+(log10(h)-2);
dew_point = (logEx - 0.66077)*237.3/(0.66077+7.5-logEx);
return dew_point;
}
//----------------------------------------------------------------------------------
void main()
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity (12 bit) and temperature (14 bit)
// 3. calculate humidity [%RH] and temperature [癈]
// 4. calculate dew point [癈]
// 5. print temperature, humidity, dew point
{ value humi_val,temp_val;
//float dew_point;
unsigned char error,checksum;
unsigned long i;
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
Lcd_init();//初始化LCD,必须的
LCD_fir();//开机显示欢迎界面
//P2OUT &= (~BIT5);
P2OUT |= BIT5;
P2DIR = 0xff;
P2OUT &= (~BIT5);
//init_uart();//串口初始化,设置波特率
s_connectionreset();//SHT传输重启
while(1)
{ error=0;
//采用温度14位(0~32767)量程(-40~123.8℃),湿度12位(0~8191)量程(0~100%RH)
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI);//measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP);//measure temperature
lcd12864_char(0xb0,0,humi_val.i,0);
lcd12864_char(0xb2,0,temp_val.i,0);
if(error!=0) s_connectionreset(); //in case of an error: connection reset
else
{ humi_val.f=(float)humi_val.i; //converts integer to float
temp_val.f=(float)temp_val.i; //converts integer to float
//humi_val.f=1191;
//temp_val.f=2767;
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
//dew_point=calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
humi_val.i=(int)humi_val.f;
if(temp_val.f<0)
{
temp_val.i=(int)temp_val.f;
temp_val.i=(~temp_val.i)+1;
}
elsetemp_val.i=(int)temp_val.f;
lcd12864_char(0xb4,32,humi_val.i,0);
lcd12864_char(0xb6,32,temp_val.i,0);
//printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for (i=0;i<320000;i++); //(be sure that the compiler doesn't eliminate this line!)
//-----------------------------------------------------------------------------------
}
} 51软件通过的话,软件问题应该不大,硬件上找找。
是液晶不显示吗
看夜景的电压是多少伏的
430只能驱动3.6V的液晶 回复【1楼】liuzq1981
-----------------------------------------------------------------------
液晶没有问题。问题就在sht的通讯上。我用硬件调试,湿度的高8为是55,低8位是91,放在一起是14171,已经超过了湿度测量值14位的范围了。 本帖最后由 猪星星 于 2012-5-2 10:30 编辑
楼主你现在问题解决了吗?我的也是用的这个程序,也显示有问题啊!{:cry:}
还有就是你的#include "bianliang.h"在哪呢?
页:
[1]