求关于msp430 对温湿度sht10采样,1602液晶显示的程序!!!!重金!!要正确的哦
#define _sht10_c_#include "main.h"/****************************************************************function : shtinit**description : 温湿度传感器i/o 初始化**input : none**return : none**remark : none***************************************************************/void shtinit(void){ p8sel &= ~(bit4 + bit5); // set p8.4,5 general i/o p8dir |= (bit4 + bit5); // sda scl output sht_sck_high(); sht_data_high(); shtconnectionrst(); // sht10 communication reset shtwritereg(sht_8bit_cmd);}/****************************************************************function : shtstart**description : sht 启动传输**input : none**return : none**remark : none***************************************************************/void shtstart(void){ sht_data_high(); sht_sck_low(); _nop(); _nop(); sht_sck_high(); _nop(); _nop(); sht_data_low(); _nop(); _nop(); sht_sck_low(); _nop(); _nop(); _nop(); _nop(); sht_sck_high(); _nop(); _nop(); sht_data_high(); _nop(); _nop(); sht_sck_low();}/****************************************************************function : shtconnectionrst**description : sht 通讯复位时序**input : none**return : none**remark : none***************************************************************/void shtconnectionrst(void){ u8 i; sht_data_output(); sht_data_high(); sht_sck_low(); for(i = 0; i < 9; i++) // 9 sck cycles { sht_sck_high(); _nop(); _nop(); sht_sck_low(); _nop(); _nop(); } shtstart();}/****************************************************************function : shtsend8bit**description : sht 发送一个字节**input : byte→发送的数据**return : err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtsend8bit(u8 byte){ u8 i; u8 err_code = 0; for (i = 0; i < 8; i++) { if ((byte & 0x80) == 0) { sht_data_low(); } else { sht_data_high(); } sht_sck_high(); _nop(); _nop(); byte = byte << 1; sht_sck_low(); } sht_data_high( ); // release data-line _nop(); _nop(); sht_data_input(); _nop(); _nop(); sht_sck_high(); _nop(); _nop(); err_code = sht_read_data(); sht_sck_low(); sht_data_output(); return err_code;}/****************************************************************function : shtreceive8bit**description : sht 接收一个字节**input : ack →ack**return : byte →接收的数据** err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtreceive8bit(u8 ack){ u8 i; u8 byte = 0; sht_data_output(); sht_data_high(); // release data-line _nop(); _nop(); sht_data_input(); _nop(); _nop(); for (i = 0; i < 8; i++) { byte = byte << 1; sht_sck_high(); _nop(); _nop(); if (sht_read_data() == 1) { byte = byte | 0x01; } else { byte = byte & 0xfe; } sht_sck_low(); _nop(); _nop(); } sht_data_output(); if (ack == 0) { sht_data_high(); } else { sht_data_low(); } sht_sck_high(); _nop(); _nop(); _nop(); _nop(); sht_sck_low(); sht_data_high(); return byte;}/****************************************************************function : shtsoftwarerst**description : sht 软件复位**input : none**return : err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtsoftwarerst(void){ u8 err_code = 0; shtconnectionrst(); err_code = shtsend8bit(reset_sht); return err_code;}/****************************************************************function : shtwritereg**description : sht 写状态寄存器**input : value →写入的寄存器内容**return : err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtwritereg(u8 value){ u8 err_code = 0; shtstart(); err_code += shtsend8bit(w_state_reg); err_code += shtsend8bit(value); return err_code;}/****************************************************************function : shtreadreg**description : sht 读状态寄存器**input : *dst →数据源指针** *crc →crc校验值**return : err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtreadreg(u8 *dst, u8 *crc){ u8 err_code = 0; shtstart(); err_code += shtsend8bit(r_state_reg); *dst = shtreceive8bit(ack); *crc = shtreceive8bit(noack); return err_code;}/****************************************************************function : shtcalcvalue**description : sht 温湿度的计算与修正**input : *humidity →湿度数据源指针** *temperature →温度数据源指针**return : none**remark : a3 = 温度;a4 = 湿度***************************************************************/void s_calculate(u16 *humidity, u16 *temperature){ const float c1 = -4.0; // for 8 bit const float c2 = +0.648; // for 8 bit const float c3 = -0.0000072; // for 8 bit const float d1 = -39.63; // for 12 bit @ 3v const float d2 = +0.04; // for 12 bit @ 3v const float t1 = 0.01; // for 8 bit const float t2 = 0.00128; // for 8 bit float rh = *humidity; // rh: humidity [ticks] 12 bit float t = *temperature; // t: temperature [ticks] 14 bit float rh_lin = 0; // rh_lin: humidity linear float rh_true = 0; // rh_true: temperature compensated humidity float t_c = 0; // t_c : temperature [癈] t_c = t*d2 + d1; //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) //cut if the value ** outside of { rh_true = 100; } if (rh_true < 0.1) //the physical possible range { rh_true = 0.1; } adcvalue[2] = t_c + 40; // a3 温度提高40℃ adcvalue[3] = rh_true; // a4 湿度}/****************************************************************function : shtsample**description : sht 采样**input : *dst →数据源指针** *crc →crc校验值** mode →采集模式**return : err_code →0:正常,!0:出错**remark : none***************************************************************/u8 shtsample(u8 *dst, u8 *crc, u8 mode){ u8 err_code = 0; u16 timecnt = 2000; shtstart(); switch (mode) { case measure_temp: err_code += shtsend8bit(measure_temp); break; case measure_humi: err_code += shtsend8bit(measure_humi); break; default: break; } sht_data_input(); do { delayms(1); }while ((sht_read_data()) && (--timecnt)); if (!timecnt) { err_code++; } else { *dst = shtreceive8bit(ack); *(dst + 1) = shtreceive8bit(ack); *crc = shtreceive8bit(noack); } return err_code;}/****************************************************************function : shtconversion**description : sht 温湿度转换**input : none**return : none**remark : a3 = 温度;a4 = 湿度***************************************************************/void shtconversion(void){ u8 checksum = 0, err_code = 0; u8 temp_array[2], humi_array[2]; u16 temp = 0, humi = 0; err_code += shtsample(temp_array, &checksum, measure_temp); err_code += shtsample(humi_array, &checksum, measure_humi); if (err_code) // in case of an error: connection reset { shtconnectionrst(); adcvalue[2] = 0; // a3 温度提高40℃ adcvalue[3] = 0; // a4 湿度 } else { temp = (temp_array[0] << 8) + temp_array[1]; humi = (u16)humi_array[1]; s_calculate(&humi, &temp); }}#ifndef _sht10_h_#define _sht10_h_#ifndef _sht10_c_#define sht10_ext extern#else#define sht10_ext#endif#define ack 1#define noack 0#define sht_sck_high() p8out |= bit4 // sck 高电平#define sht_sck_low() p8out &= ~bit4 // sck 低电平#define sht_data_high() p8out |= bit5 // data 高电平#define sht_data_low() p8out &= ~bit5 // data 低电平#define sht_read_data() (p8in & 0x20) >> 5 // 读取data 状态#define sht_data_input() p8dir &= ~bit5 // 设置data 为输入#define sht_data_output() p8dir |= bit5 // 设置data 为输出#define measure_temp 0x03 // 测量温度#define measure_humi 0x05 // 测量湿度#define w_state_reg 0x06 // 写状态寄存器#define r_state_reg 0x07 // 读状态寄存器#define sht_8bit_cmd 0x01 // 设置湿度值为8bit 命令#define reset_sht 0x1e // 复位, 清空状态寄存器sht10_ext void shtinit(void);sht10_ext void shtconnectionrst(void);sht10_ext u8 shtwritereg(u8 value);sht10_ext u8 shtreadreg(u8 *dst, u8 *crc);sht10_ext u8 shtsample(u8 *dst, u8 *crc, u8 mode);sht10_ext void shtconversion(void);#endif每月1k的出货量,未出过问题的驱动程序 20210311