程序的目的是把采集的温度通过串口显示,现在做的程度是我把硬件连接好后,用MSP430仿真器查看温湿度的数值不正确,比如humi_val.i用quick watch查看value为62850这样的数据,单片机的txd用示波器观察没有波形,可能程序存在很大问题,现在比较着急,希望各位指点一二啊,调好后,会把正确的程序贴出,共同进步,也供其他人参考,谢谢。由于程序比较长,以附件上传了,问题可能出现在一下部分,贴出来:#include <msp430x16x.h>/*宏定义,延时函数,参数为1时相应延时分别为1us和1ms*/#define CPU_F (double)1000000#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))/*常量定义*/#define uint unsigned int#define uchar unsigned char#define ulong unsigned long //adr command r/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 bitselect 0x01 //选择温度与湿度的低位读#define noACK 0#define ACK 1#define TEMPERATURE 1#define HUMIDITY 2#define SCK BIT4#define SDA BIT5
#define SCK_H P6OUT|=SCK //输出1#define SCK_L P6OUT&=~SCK //输出0#define SDA_H P6OUT|=SDA#define SDA_L P6OUT&=~SDA
typedef union { unsigned int i;float f;}value;/************************************************************************************************************Function Name: S_Init**Description: 初始化**Input Parameters: 无**Output Parameters: 无**********************************************************************************************************/void S_Init(){P6SEL&=~(SCK+SDA); //选择P6.4 为IO端口输出, P6.5输入P6DIR|=SCK;P6DIR&=~SDA;//BCSCTL1=(XT2OFF+RSEL2); //关闭XT2,1MHz DOC //DCOCTL=DCO2; //设定DCO频率为1MHz }/************************************************************************************************************Function Name: CLK_Init**Description: 时钟初始化**Input Parameters: 无**Output Parameters: 无**********************************************************************************************************/void CLK_Init(){unsigned char i;BCSCTL1 &= ~XT2OFF;//打开XT振荡器BCSCTL2 |= SELM1 + SELS;//MCLK为8MHZ,SMCLK为1MHZdo{IFG1 &= ~OFIFG;//清除震荡错误标志for(i = 0;i < 100;i++);//延时等待_NOP();}while((IFG1 & OFIFG) != 0);//如果标志为1,则循环等待IFG1 &= ~OFIFG;//清除震荡错误标志}/************************************************************************************************************Function Name: init_usart**Description: 初始化**Input Parameters: 无**Output Parameters: 无**********************************************************************************************************/void Init_USART(){UCTL0 |= SWRST; //串口复位UCTL0 |= CHAR; // 选择8位字符UTCTL0 |= SSEL0; // UCLK = ACLKP3SEL |= 0x30; // 选择P3.4和P3.5做UART通信端口P3DIR |=BIT4; //P3.4为输出方向//P3DIR |= 0x10; // 串口发送数据端口为输出,接收数据端口为输入UBR00 = 0x41; // 波特率9600UBR10 = 0x03; //UMCTL0 = 0x00; // ModulationME1 |= UTXE0 + URXE0; // 使能USART0的发送和接受IE1 |= URXIE0 + UTXIE0; // 使能USART0的收发中断//IFG1 &= ~UTXIFG0;UCTL0 &= ~SWRST; // 初始化UART状态机_EINT();}/************************************************************************************************************Function Name: print output **Description: **Input Parameters: 无**Output Parameters: 无**********************************************************************************************************/void Send_Data(uchar *x){ while(*x) { while ((IFG1&UTXIFG0)==0); x++; }}
void main(){ value humi_val,temp_val;//float dew_point;uchar TEMP[4];uchar HUMI[4];unsigned char error,checksum;unsigned int i;S_Init();CLK_Init();S_Connectionreset();WDTCTL=WDTPW+WDTHOLD; //Stop watchdog timer to prevent time out resetwhile(1){ error=0; error+=S_Measure((unsigned char*) &humi_val.i,&checksum,HUMIDITY); //measure humidity error+=S_Measure((unsigned char*) &temp_val.i,&checksum,TEMPERATURE); //measure temperature 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 S_Calculate (&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*=10; HUMI[0]= humi_val.i /100+0x30; HUMI[1]= humi_val.i /10%10+0x30; HUMI[2]='.'; HUMI[3]= humi_val.i %10+0x30; Send_Data(HUMI); temp_val.i*=10; TEMP[0]= temp_val.i/100+0x30; TEMP[1]= temp_val.i/10%10+0x30; TEMP[2]='.'; TEMP[3]= temp_val.i%10+0x30; Send_Data(TEMP); } //----------wait approx. 0.8s to avoid heating up SHTxx------------------------------ for (i=0;i<40000;i++); //(be sure that the compiler doesn't eliminate this line!) //----------------------------------------------------------------------------------- _NOP();}} 其他部分都跟官网提供的程序基本相同,估计问题不大。
此帖出自
小平头技术问答
一周热门 更多>