📄 r_8.c
字号:
/***********************************************************************************
Project: SHT11 demo program (V2.0)
Filename: SHT11.c
Prozessor: 80C51 family
Compiler: Keil Version 6.14
Autor: MST
Copyrigth:
***********************************************************************************/
// 最后修改日期:2008-05-29 星期四
#include "iom8v.h" // 16K FLASH, 512 EEPROM, 1024 SRAM
#include "macros.h"
#include "eeprom.h"
#include <math.h> //Keil library
//--------------------- 串口 -------------------------------
#define fos 1000000
//#define bps 9600
//#define baud fos/bps/16-1
#define timex 100 // ms T1 定时
#define CLK 1024 // 1024 分频
#define TCONST 0xffff - ((fos/CLK)*timex/1000-1)
#define SET_HUMI_DZ 0x10
#define SET_CONST_DZ 0x12
#define CONST_SIGNE_DZ 0x14
#define K_ADD 0x04
#define K_SUB 0x10
#define K_SET 0x02
#define kadd_l PORTC &=~ BIT(PC2)
#define kadd_h PORTC |= BIT(PC2)
#define ksub_l PORTC &=~ BIT(PC4)
#define ksub_h PORTC |= BIT(PC4)
#define kset_l PORTD &=~ BIT(PD1)
#define kset_h PORTD |= BIT(PD1)
#define led_l PORTD &=~ BIT(PD0)
#define led_h PORTD |= BIT(PD0)
#define heat_l PORTC &=~ BIT(PC3)
#define heat_h PORTC |= BIT(PC3)
#define a1_l PORTB &=~ BIT(PB2)
#define a1_h PORTB |= BIT(PB2)
#define b1_l PORTB &=~ BIT(PB3)
#define b1_h PORTB |= BIT(PB3)
#define c1_l PORTD &=~ BIT(PD5)
#define c1_h PORTD |= BIT(PD5)
#define d1_l PORTD &=~ BIT(PD6)
#define d1_h PORTD |= BIT(PD6)
#define e1_l PORTD &=~ BIT(PD7)
#define e1_h PORTD |= BIT(PD7)
#define g1_l PORTB &=~ BIT(PB0)
#define g1_h PORTB |= BIT(PB0)
#define f1_l PORTB &=~ BIT(PB1)
#define f1_h PORTB |= BIT(PB1)
//#define f2_l PORTB &=~ BIT(PB6)
//#define f2_h PORTB |= BIT(PB6)
#define f2_l PORTC &=~ BIT(PC5)// 有孔板
#define f2_h PORTC |= BIT(PC5)
#define g2_l PORTB &=~ BIT(PB4)
#define g2_h PORTB |= BIT(PB4)
#define e2_l PORTD &=~ BIT(PD4)
#define e2_h PORTD |= BIT(PD4)
#define d2_l PORTD &=~ BIT(PD2)
#define d2_h PORTD |= BIT(PD2)
#define c2_l PORTD &=~ BIT(PD3)
#define c2_h PORTD |= BIT(PD3)
#define b2_l PORTC &=~ BIT(PC0)
#define b2_h PORTC |= BIT(PC0)
#define a2_l PORTC &=~ BIT(PC1)
#define a2_h PORTC |= BIT(PC1)
#define SCK_L PORTB &=~ BIT(PB7)
#define SCK_H PORTB |= BIT(PB7)
#define DATA_L PORTB &=~ BIT(PB5)
#define DATA_H PORTB |= BIT(PB5)
#define noACK 0
#define ACK 1
//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
float C1 = -4.0; // for 12 Bit
float C2 = 0.0405; // for 12 Bit
float C3 = -0.0000028; // for 12 Bit
float T1 = 0.01; // for 14 Bit @ 5V
float T2 = 0.00008; // for 14 Bit @ 5V
typedef union
{ unsigned int i;
unsigned int f;
} value;
char set_humi;
char my_humi, my_temp;
// in timer1
char times, time_up, counter, ss_counter;
char set_start;
char rh_ss_flag, const_ss_flag;
char rh_seting, const_seting;
char const_signe;
signed set_const;
//-----------------------------------------------------------------------------
// modul-var
//-----------------------------------------------------------------------------
enum {TEMP,HUMI}; // 0,1 温湿度
void port_init(void)
{
//PORTA = 0xff;
//DDRA = 0xff;
PORTB = 0xff;
DDRB = 0xff;
PORTC = 0xff;
DDRC = 0xff;
PORTD = 0xff;
DDRD = 0xff;
}
//===========================================================
// 硬件资源初始化集
//===========================================================
void init_devices(void)
{
port_init(); // 端口初始化
timer1_init();
GICR = 0x00; // 通用中断控制寄存器
TIMSK = 0x04; // T1 溢出中断使能
}
void WDT_init(void)
{
WDR(); // 初始化前先喂狗
WDTCR = 0x0F; // WDT使能,使用2048K分频
}
void WDT_off(void) // 关闭看门狗
{
WDTCR |= (1<<WDTOE) | (1<<WDE); // 置 WDTOE、WDE 为 1
WDTCR = 0x00; // 关闭 WDT
}
//===========================================================
// timer1_init
//===========================================================
void timer1_init(void) // 1 S 定时
{
TCCR1B = 0; // 停止定时器1
TCNT1H = TCONST >> 8;
TCNT1L = TCONST;
OCR1AH = 0; // 不使用输出比较匹配A
OCR1AL = 0;
OCR1BH = 0; // 不使用输出比较匹配B
OCR1BL = 0;
TCCR1A = 0; // COMP and PWM = 0
TCCR1B = 5; // 启动定时器1, 用1024分频
}
//-----------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
//-----------------------------------------------------------------------------
void main()
{
char wait=0;
value humi_val, temp_val; // =i, =f
//unsigned long dew_point;
char error,checksum;
delay_ms(100);
CLI();
init_devices(); // port and uart init
delay_ms(100);
//================ data init ======================
set_humi = EEPROMread(SET_HUMI_DZ);
const_signe = EEPROMread(CONST_SIGNE_DZ);
wait = EEPROMread(SET_CONST_DZ);
set_const = wait;
if(set_const > 9) set_const = 9;
if(const_signe == 1) set_const = -set_const;
//set_const = 1;
//const_signe = -9;
const_seting = 0;
rh_ss_flag = 0; const_ss_flag = 0;
if((read_key()&K_SUB)==0)
{
disp_const(set_const);
while(1)
{
delay_ms(50); // 等待放开 SET 键
if((read_key()&K_SUB) != 0) break;
WDR();
}
const_seting = 1;
const_ss_flag = 1;
}
WDT_init();
SEI();
s_connectionreset(); //reset communication
while(1)
{ // loop
key_fx();
if(time_up)
{//up
time_up = 0;
error=0;
error += s_measure((char *) &humi_val.i, &checksum,HUMI); //measure humidity
error += s_measure((char *) &temp_val.i, &checksum,TEMP); //measure temperature
if(error!=0)
s_connectionreset(); // 发生错误时,传感器复位
else
{
humi_val.f = humi_val.i; // 湿度 整-浮数 转换
temp_val.f = temp_val.i; // 温度 整-浮数 转换
// 湿度修正
calc_sth11(&humi_val.f, &temp_val.f); //calculate humidity, temperature
// 显示的温湿度值
my_temp = temp_val.f;
my_humi = humi_val.f;
if((rh_seting == 0)&&(const_seting == 0))disp_num(my_humi);
}
WDR();
} // up
if(my_humi > set_humi)
{
heat_l;
led_l;
}
else if(my_humi < set_humi)
{
heat_h;
led_h;
}
WDR();
} // loop
}
//===========================================================
// T1 中断处理
//===========================================================
#pragma interrupt_handler timer1_ovf_isr: 9 // for M16 or M8
//#pragma interrupt_handler timer1_ovf_isr: 10 // for M32
//#pragma interrupt_handler timer1_ovf_isr: 15 // for M64
void timer1_ovf_isr(void)
{ // 100 ms
times++; counter++; ss_counter++;
if(times > 9)
{
times = 0;
time_up = 1;
}
if(counter > 200) counter = 200;
if(rh_ss_flag == 1)
{
if(ss_counter < 5)
disp_num(set_humi);
else if(ss_counter < 10)
disp_num(200);
else if(ss_counter >= 10)
ss_counter = 0;
}
if(const_ss_flag == 1)
{
if(ss_counter < 5)
disp_const(set_const);
else if(ss_counter < 10)
disp_const(200);
else if(ss_counter >= 10)
ss_counter = 0;
}
TCNT1H = TCONST >> 8;
TCNT1L = TCONST;
TCCR1B = 0x05; // 启动定时器1, 用1024分频
TIMSK |= BIT(TOIE1); // 开 T1 中断
}
char read_key(void)
{
char k_add, k_sub, k_set, kkk0;
DDRC &=~ BIT(PC2); //kadd
DDRC &=~ BIT(PC4); //ksub
DDRD &=~ BIT(PD1); //kadd
DDRC |= BIT(PC2); //kadd
DDRC |= BIT(PC4); //ksub
DDRD |= BIT(PD1); //kadd
kkk0 = PINC;
k_add = kkk0 & 0x04;
k_sub = kkk0 & 0x10;
k_set = PIND & 0x02;
kkk0 = k_add | k_sub | k_set; // = 0x16
return (kkk0);
}
void key_fx(void)
{
char kkk, aaa;
kkk = read_key();
if(((kkk&0x16)==0x16)&&(counter > 50))
{
rh_seting = 0; const_seting = 0;
rh_ss_flag = 0; // 停止闪烁
const_ss_flag = 0; // 停止闪烁
set_start = 0;
}
else if((kkk&0x16)==0x16)
return;
//---------------------------------------------------------------------
if(((kkk&K_ADD)==0)&&(rh_seting == 0)&&(const_seting == 0))
{ // 当前温度 查询
counter = 0;
disp_num(my_temp);
delay_ms(3000);
}
else if(((kkk&K_SUB)==0)&&(rh_seting == 0)&&(const_seting == 0))
{ // 湿度设置值 查询
counter = 0;
disp_num(set_humi);
delay_ms(3000);
}
else if(((kkk&K_ADD)==0)&&(rh_seting == 1))
{ // 增加 湿度控制设置值
rh_ss_flag = 0;
counter = 0;
WDR();
set_humi++;
if(set_humi > 99) set_humi = 99;
EEPROMwrite(SET_HUMI_DZ, set_humi);
disp_num(set_humi);
delay_ms(200);
rh_ss_flag = 1;
counter = 0;
}
else if(((kkk&K_SUB)==0)&&(rh_seting == 1))
{ // 减少 湿度控制设置值
rh_ss_flag = 0;
counter = 0;
WDR();
set_humi--;
if(set_humi <= 1) set_humi = 1;
EEPROMwrite(SET_HUMI_DZ, set_humi);
disp_num(set_humi);
delay_ms(200);
rh_ss_flag = 1;
counter = 0;
}
else if(((kkk&K_ADD)==0)&&(const_seting == 1))
{ // 归零输入 +
WDR();
const_ss_flag = 0;
counter = 0;
if(set_const < 9)
set_const++;
else
set_const = 9;
disp_const(set_const);
aaa = set_const;
EEPROMwrite(SET_CONST_DZ, aaa);
WDR();
if(set_const < 0)
EEPROMwrite(CONST_SIGNE_DZ, 1);
else
EEPROMwrite(CONST_SIGNE_DZ, 0);
delay_ms(200);
const_ss_flag = 1;
counter = 0;
}
else if(((kkk&K_SUB)==0)&&(const_seting == 1))
{ // 归零输入 -
WDR();
const_ss_flag = 0;
counter = 0;
if(set_const > -9)
set_const--;
else
set_const = -9;
disp_const(set_const);
aaa = set_const;
EEPROMwrite(SET_CONST_DZ, aaa);
WDR();
if(set_const < 0)
EEPROMwrite(CONST_SIGNE_DZ, 1);
else
EEPROMwrite(CONST_SIGNE_DZ, 0);
delay_ms(200);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -