/*
STC15单片机
实验名称:读取STH3x温湿度传感器,并在UART串口打印数值
实验内容:
UART串口9600波特率
printf函数打印信息(putchar)
#define FOSC 22118400L //晶振频率 22.1184MHz
实验器材:
STC15W408AS_DIP16 x1
SHT3x温湿度传感器模块
SHT3x传感器:
- void SHT3X_Init()
- {
- IIC_Start(); //发送起始信号
- SHT3X_WriteByte(0x44<<1|0); //写设备地址
- SHT3X_WriteCmd(0x2130); // meas. periodic 1 mps, high rep.
- IIC_Stop(); //停止信号
- }
复制代码
打印信息:
[15:35:16.358]接收←>New Messages...
>SHT3x_ReadData...
>R_Buf 75 be 6d 81 9e 70
# SHT3x_Data_Read #
>T_R 30142 H_R 33182
# SHT3x_Data_Calc #
>T_C 0354 H_C 0506
# SHT3x_Crc_Chk #
>T CRC_R 6d H CRC_R 70
>T CRC_C 6d H CRC_C 70
[15:35:21.812]接收←>New Messages...
>SHT3x_ReadData...
>R_Buf 75 be 6d 81 df 7c
# SHT3x_Data_Read #
>T_R 30142 H_R 33247
# SHT3x_Data_Calc #
>T_C 0354 H_C 0507
# SHT3x_Crc_Chk #
>T CRC_R 6d H CRC_R 7c
>T CRC_C 6d H CRC_C 7c
*/
main.c :
- #include "config.h"
- #include "delay.h"
- #include "uart.h"
- #include "sht3x_iic.h"
- #define LED_ON P12 = 0
- #define LED_OFF P12 = 1
- bit timer_flag_5s = 0; //5s定时触发标志位
- uint8 delay_count = 0; //delay延迟计数器
- //主函数
- void main(void)
- {
- //设置STC15W408AS单片机端口为标准模式
- Config_Port();
- //串口初始化,波特率9600
- UART_Config(9600);
- //初始化SHT30传感器
- SHT3X_Init();
- LED_OFF; //LED指示灯熄灭
-
- // 事件处理循环
- while(1)
- {
- if (timer_flag_5s) //每隔5s执行以下分支
- {
- LED_ON; //LED指示灯点亮
- if(DEBUG_MODE) printf(">New Messages...\r\n");
- SHT3x_ReadData();
-
- if(DEBUG_MODE) printf(">SHT3x_ReadData...\r\n");
- if(DEBUG_MODE) printf(">R_Buf %02bx %02bx %02bx %02bx %02bx %02bx\r\n",
- SHT3X_Read_Buf[0], SHT3X_Read_Buf[1], SHT3X_Read_Buf[2],
- SHT3X_Read_Buf[3], SHT3X_Read_Buf[4], SHT3X_Read_Buf[5]);
- if(DEBUG_MODE) printf("\r\n# SHT3x_Data_Read #\r\n");
- if(DEBUG_MODE) printf(">T_R %u H_R %u\r\n", p_SHT3X_Data->Read_T_Val, p_SHT3X_Data->Read_H_Val);
- if(DEBUG_MODE) printf("\r\n# SHT3x_Data_Calc #\r\n");
- if(DEBUG_MODE) printf(">T_C %04d H_C %04d\r\n", SHT3X_T_Val, SHT3X_H_Val);
- if(DEBUG_MODE) printf("\r\n# SHT3x_Crc_Chk #\r\n");
- if(DEBUG_MODE) printf(">T CRC_R %02bx H CRC_R %02bx\r\n", p_SHT3X_Data->Read_T_Crc, p_SHT3X_Data->Read_H_Crc);
- if(DEBUG_MODE) printf(">T CRC_C %02bx H CRC_C %02bx\r\n", SHT3X_CalcCrc((uint8 *)&(p_SHT3X_Data->Read_T_Val), 2), SHT3X_CalcCrc((uint8 *)&(p_SHT3X_Data->Read_H_Val), 2));
- LED_OFF; //LED指示灯熄灭
- timer_flag_5s = 0;
- }
- //延时周期200ms
- delay_x_ms(200);
- //5s定时触发标志位
- if(25 == ++delay_count)
- {
- delay_count = 0;
- timer_flag_5s = 1;
- }
- }
- }
复制代码
原理图: 无
仿真: 无
代码:
UART_PRINTF_DELAY_SHT3x_STC15W408AS.zip
(9.2 KB, 下载次数: 22)
|