ch579温湿度监测代码
时间: 2023-07-09 22:48:18 浏览: 53
以下是一个基于CH579芯片和DHT11传感器的温湿度监测代码示例,供参考:
```c
#include <ch579.h>
#include <stdio.h>
#include <stdlib.h>
#define DHT_PORT P1
#define DHT_PIN 3
volatile uint16_t T_RH[40];
void delay_us(uint32_t us)
{
uint32_t i;
for(i=0; i<us*16; i++) {
__nop();
}
}
void DHT11_start(void)
{
DHT_PORT->OUT &= ~(1<<DHT_PIN);
DHT_PORT->DIR |= (1<<DHT_PIN);
delay_us(20000);
DHT_PORT->DIR &= ~(1<<DHT_PIN);
delay_us(40);
}
uint8_t DHT11_read(void)
{
uint8_t i, j;
uint8_t byte = 0;
for(i=0; i<8; i++) {
while(!(DHT_PORT->IN & (1<<DHT_PIN)));
delay_us(30);
if(DHT_PORT->IN & (1<<DHT_PIN)) {
byte |= (1<<(7-i));
}
while((DHT_PORT->IN & (1<<DHT_PIN)));
}
return byte;
}
void DHT11_handler(void)
{
static uint8_t cnt = 0;
static uint8_t byte_cnt = 0;
static uint8_t byte_data = 0;
if(cnt == 0) {
DHT11_start();
} else if(cnt <= 40) {
T_RH[cnt-1] = TIM0->CNT;
} else {
byte_data <<= 1;
if(T_RH[byte_cnt] > 40) {
byte_data |= 0x01;
}
byte_cnt++;
if(byte_cnt == 8) {
byte_cnt = 0;
if((T_RH[0]+T_RH[1]+T_RH[2]+T_RH[3]+T_RH[4]) == byte_data) {
printf("Temperature: %d.%d C, Humidity: %d%%\n", T_RH[2]/10, T_RH[2]%10, T_RH[0]);
}
}
}
cnt++;
if(cnt == 85) {
cnt = 0;
}
}
int main(void)
{
CLK->CKDIVR = 0x01; // 12MHz
TIM0->ARR = 0xFFFF;
TIM0->CR1 = 0x01;
TIM0->CR2 = 0x00;
TIM0->IER = 0x01;
NVIC_EnableIRQ(TIM0_IRQn);
while(1) {
DHT11_handler();
delay_us(1000);
}
return 0;
}
```
上面的代码实现了一个简单的DHT11温湿度监测程序,具体流程如下:
1. 初始化时钟和定时器。
2. 在定时器中断服务程序中,记录每个时刻的计数值(用于计算时间)。
3. 在主循环中,定时调用DHT11_handler()函数。
4. 在DHT11_handler()函数中,实现DHT11协议,读取温湿度数据。
5. 如果数据校验正确,将温湿度值输出到串口。
请注意,以上代码仅供参考,具体的实现需要根据自己的硬件和软件环境进行修改。另外,由于CH579并不是一款常见的芯片,可能会比较难以找到相关的资料和支持。建议您在开发前,先了解清楚相关的技术和资源情况。