在MSP430 Launchpad MSP430g2452平台上的SHT10 温湿度传感器
//********************************File Information*******************************
//** File Name: Sht10.c
//** Platform: MSP430 LaunchPad MSP430G2452
//** System Function: Sht10 Sensirion Inc driver code
//** Created by: ding
//** Created date: 2014-09-15
//** Version: V1.0
//******************************************************************************
//---------------------------------------
//硬件连线-接口定义:
//SCK: -->P1.5 //定义通讯时钟端口
//SDA: -->P1.6 //定义通讯数据端口
//---------------------------------------
#include #include //#include //#include //宏定义,延时函数,参数为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 //没有返回ACK #define ACK 1 //返回ACK #define HUMIDITY 2 #define TEMPERATURE 1 #define SCK BIT5 //P1.5 #define SDA BIT6 //P1.6 #define SCK_H P1OUT|=SCK //高 #define SCK_L P1OUT&=~SCK //低 #define SDA_H P1OUT|=SDA //高 #define SDA_L P1OUT&=~SDA //低 typedef union //定义了两个共用体 { unsigned int i; float f; }value; /********************************************************************************************************** **Function Name: S_Init **Description: 初始化 **Input Parameters: no **Output Parameters: no **********************************************************************************************************/ void S_Init() { P1SEL&=~(SCK+SDA); P1DIR|=SCK; P1DIR&=~SDA; //BCSCTL1=(XT2OFF+RSEL2); //关闭XT2,1MHz DOC //DCOCTL=DCO2; //设定DCO频率为1MHz } /********************************************************************************************************** **Function Name: S_Transstart **Description: start时序 ** generates a transmission start ** _____ ________ ** DATA: |_______| ** ___ ___ ** SCK : ___| |___| |______ **********************************************************************************************************/ void S_Transstart() { P1DIR|=SDA; SDA_H;SCK_L; __no_operation(); SCK_H; __no_operation(); SDA_L; __no_operation(); SCK_L; __no_operation(); __no_operation(); __no_operation(); SCK_H; __no_operation(); SDA_H; __no_operation(); SCK_L; P1DIR&=~SDA; } /********************************************************************************************************** **Function Name: S_WriteByte **Description: 写函数 **********************************************************************************************************/ char S_WriteByte(unsigned char value) { // writes a byte on the Sensibus and checks the acknowledge unsigned char i,error=0; P1DIR|=SDA; for(i=0x80;i>0;i/=2) //shift bit for masking { if(i&value) SDA_H; //masking value with i , write to SENSI-BUS else SDA_L; SCK_H; //clk for SENSI-BUS __no_operation();__no_operation();__no_operation();//pulswith approx. 5 us SCK_L; } SDA_H; //release DATA-line P1DIR&=~SDA; //Change SDA to be input 0:input 1:ouput SCK_H; //clk #9 for ack error=P1IN; //check ack (DATA will be pulled down by SHT11) error&=SDA; P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput SCK_L; if(error) return 1; //error=1 in case of no acknowledge return 0; } /********************************************************************************************************** **Function Name: S_ReadByte **Description: 读函数 **Input Parameters: ack--->reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" **********************************************************************************************************/ char S_ReadByte(unsigned char ack) { // reads a byte form the Sensibus and gives an acknowledge in case of "ack=1" unsigned char i,val=0; P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput SDA_H; //release DATA-line P1DIR&=~SDA; //Change SDA to be input 0:input 1:ouput for(i=0x80;i>0;i/=2) //shift bit for masking { SCK_H; //clk for SENSI-BUS if(P1IN&SDA) val=(val|i); //read bit SCK_L; } P1DIR|=SDA; //Change SDA to be output 0:input 1:ouput if(ack) //in case of "ack==1" pull down DATA-Line SDA_L; else SDA_H; SCK_H; //clk #9 for ack __no_operation();__no_operation();__no_operation();//pulswith approx. 5 us SCK_L; SDA_H; //release DATA-line P1DIR&=~SDA; //Change SDA to be output 0:input 1:ouput return val; } /********************************************************************************************************** **Function Name: S_Connectionreset **Description: 连接复位函数 ** communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart ** _____________________________________________________ ________ ** DATA: |_______| ** _ _ _ _ _ _ _ _ _ ___ ___
上一篇:msp430f149通过蓝牙HC-05与手机通讯
下一篇:Proteus中MSP430与SHT11的IIC通信问题
推荐阅读最新更新时间:2024-11-21 11:09