#include "ioCC2430.h" //包含头文件,相应的板子以及传感器一些信息 #include "hal.h" #include
//#include #include
typedef union //定义联合体, { unsigned int i; float f; } value; //定义联合体类型名称为value
#define noACK 0 #define ACK 1 #define STATUS_REG_W 0x06 //0x06 = 0000 0110 #define STATUS_REG_R 0x07 //0x07 = 0000 0111 #define MEASURE_TEMP 0x03 //0x03 = 0000 0011 #define MEASURE_HUMI 0x05 //0x05 = 0000 0101 #define RESET 0x1e //0x1e = 0001 1110 #define SDA P1_6 //定义SDA代表的是P1_6脚 #define SCL P1_7 #define begin P2_0 unsigned char d1,d2,d3,d4,d5,d6,d7; //定义无符号字符型变量
void Wait(unsigned int ms) //定义wait函数,主要用于软件循环,延时作用 { unsigned char g,k; while(ms) { for(g = 0;g <= 167; g++) { for(k = 0;k <= 48; k++); } ms--; } }
void QWait() //1us的延时 { asm("NOP"); //加入汇编操作语句,空操作,主要用于机器周期执行 asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); }
void initUART(void) //初始化单片机的串口 { IO_PER_LOC_USART0_AT_PORT0_PIN2345(); //具体函数的定义与用法,你得参考头文件中的程序代码了 IO_DIR_PORT_PIN(1, 6, IO_OUT); IO_DIR_PORT_PIN(1, 7, IO_OUT); //IO_IMODE_PORT_PIN(1, 6, IO_IMODE_TRI); //IO_IMODE_PORT_PIN(1, 7, IO_IMODE_TRI); IO_DIR_PORT_PIN(2, 0, IO_OUT); IO_FUNC_PORT_PIN(2, 0, IO_FUNC_GIO); //SET_MAIN_CLOCK_SOURCE(RC); SET_MAIN_CLOCK_SOURCE(CRYSTAL); UART_SETUP(0, 115200, HIGH_STOP); //设置传输数据的波特率115200 UTX0IF = 1; U0CSR |= 0XC7; //U0CSR = U0CSR | 0x1010 0111 (进行位或操作) IEN0 |= 0x84; SDA = 1; SCL = 0; }
int putchar (int c) //定义输入字符函数,给的参数是一个整型的数 { if (c == '\n') //判断参数c的值是否和'\n'的值相等 { while (!UTX0IF); //执行的时候UTX0IF的值是0,此处不是很理解? UTX0IF = 0; //给UTX0IF赋0 U0DBUF = 0x0d; //U0DBUF赋值0x0d = 0000 1011 } while (!UTX0IF); UTX0IF = 0; return (U0DBUF = c); //如果c的值不是'\n'也就是换行符的时候,将c的值传递到U0DBUF寄存器中 }
char s_write_byte(unsigned char value) //定义写字节函数(8位) { unsigned char i,error = 0; for (i = 0x80;i > 0;i /= 2) //i 赋初始值0x80 = 128, 执行判断是i > 0,执行语句是i = i / 2; 即i = 128,64,32,16,8,4,2,1,0.5(0),8位 { if (i & value) SDA = 1; else SDA = 0; SCL = 1; //此时SCL端口处,也就是p1_7引脚处是高电平 QWait(); //因为写入需要时间,所以程序之中加入下面几条语句 QWait(); QWait(); QWait(); QWait(); SCL = 0; //使能p1_7眼角处低电平,使的数据写入(具体需要看单片机控制芯片的手册 asm("NOP"); asm("NOP"); } SDA = 1; SCL = 1; asm("NOP"); error = SDA; QWait(); QWait(); QWait(); SDA = 1; SCL = 0; return error; }
char s_read_byte(unsigned char ack) //读取数据,按照字节位的顺序读取(8位)128 = 1000 0000 ,64 = 0100 0000, 32 = 0010 0000 ,16 = 0001 0000, 8 = 0000 1000, 4 = 0000 0100 , 2 = 0000 0010, 1 = 0000 0001 { unsigned char i,val = 0; SDA= 1; for(i = 0x80;i > 0;i /= 2) //同上 { SCL = 1; if (SDA) //判断SDA处是否有高电平 val = (val | i); //进行或操作 else val = (val | 0x00); SCL = 0; QWait(); QWait(); QWait(); QWait(); QWait(); } SDA = !ack; SCL = 1; QWait(); QWait(); QWait(); QWait(); QWait(); SCL = 0; SDA = 1;
return val; //返回读取到的数据,一个字节,八位 }
void s_transstart(void) //传输使能函数,就是给控制器引脚处相应电平,使对应模块工作 { SDA = 1; SCL = 0; QWait(); QWait(); SCL = 1; QWait(); QWait(); SDA = 0; QWait(); QWait(); SCL = 0; QWait(); QWait(); QWait(); QWait(); QWait(); SCL = 1; QWait(); QWait(); SDA = 1; QWait(); QWait(); SCL = 0; QWait(); QWait(); }
void s_connectionreset(void) //复位操作函数 { unsigned char i; SDA = 1; SCL = 0; for(i = 0;i < 9; i++) { SCL = 1; QWait(); QWait(); SCL = 0; QWait(); QWait(); } s_transstart(); //调用开始函数 }
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode) //函数,主要统计传输的数据个数 { unsigned er = 0; unsigned int i,j; s_transstart(); switch(mode) { case 3 :er += s_write_byte(3); break; case 5 :er += s_write_byte(5); break; default :break; } for(i = 0;i < 65535;i++) { for(j = 0;j < 65535;j++) {if(SDA == 0) {
break;
} } if(SDA == 0) { break; }
} if(SDA) { er += 1; } *(p_value) = s_read_byte(ACK); *(p_value + 1) = s_read_byte(ACK); *p_checksum = s_read_byte(noACK); d6 = *(p_value); d7=*(p_value + 1); return er; }
void calc_sth11(float *p_humidity ,float *p_temperature)//计算温度值 { const float C1 =- 4.0; const float C2 =+ 0.0405; const float C3 =- 0.0000028; const float T1 =+ 0.01; const float T2 =+ 0.00008; float rh =* p_humidity; float t =* p_temperature; float rh_lin; float rh_true; float t_C; t_C = t * 0.01 - 44.0 ; rh_lin = C3 * rh * rh + C2 * rh + C1; rh_true = (t * 0.01 - 40.0 - 25) * (T1 + T2 * rh) + rh_lin; if(rh_true > 100) { rh_true = 100; } if(rh_true < 0.1) { rh_true = 0.1; } *p_temperature = t_C; *p_humidity = rh_true; }
void main() //主函数 { value humi_val,temp_val; //声明两个联合体变量 unsigned char error,checksum; //声明两个无符号的字符型变量 initUART(); //初始化串口 P1INP |= 0xC0; //初始化P1引脚 , 0xC0 = 1010 0000 ,使P1_7和P1_5引脚为1 begin = 0; s_connectionreset(); while(1) //无限循环操作 { error = 0; error += s_measure((unsigned char*) &humi_val.i,&checksum,5); //读入串口的数据进行温度的计算 d1 = d6; d2 = d7; error += s_measure((unsigned char*) &temp_val.i,&checksum,3); d3 = d6; d4 = d7; if(error != 0) s_connectionreset(); else { humi_val.f = (float)humi_val.i; temp_val.f = (float)temp_val.i; humi_val.f = d1 * 256 + d2; temp_val.f = d3 * 256 + d4; calc_sth11(&humi_val.f,&temp_val.f); printf("temp:%5.1fC humi:%5.1f%%\n",temp_val.f,humi_val.f); // printf("t1:%x h1:%x\n",d1,d2); //printf("t2:%x h2:%x\n",d3,d4); } Wait(150); } }