#include<iom128v.h>
#include<macros.h>
#pragma interrupt_handler uart_rx:19
#pragma interrupt_handler miao:15
#define data_output DDRA|=0X01;
#define data_input DDRA&=0XFE;
#define sck_output DDRA|=0X02;
#define sck_input DDRA&=0XFD;
#define data_1 PORTA|=0X01;
#define data_0 PORTA&=0XFE;
#define sck_1 PORTA|=0X02;
#define sck_0 PORTA&=0XFD;
#define data_in (PINA&0X01)
#define ack_0 0
#define ack_1 1
#define cm_temp 0x03 // 温度测量
#define cm_humi 0x05 //湿度测量
#define write_REG 0X06 //写状态寄存器
#define read_REG 0X07 //读状态寄存器
#define reset 0x1e //软件复位
#define TEMP 0
#define HUMI 1
const float C1=-4.0; // for 12 Bit
const float C2=+0.045; // for 12 Bit
const float C3=-0.0000028; // for 12 Bit
const float T1=+0.01; // for 14 Bit @ 5V
const float T2=+0.00008; // for 14 Bit @ 5V
unsigned char rdata,flag=0,flag1=0,flag2=0,flag3=0; //rdata串口中断接收数据,flag接受中断标志,flag1是readkey标志 ,flag2闹钟标志,flag3温湿度标志
unsigned char nhourtemp=0x00; //闹钟时计数标志
unsigned char nminutetemp=0x00;//闹钟分计数标志
static unsigned int szset=0; //szset时钟设置标志
static unsigned int nzset=0; //nzset闹钟设置标志
unsigned char nhour=0;//闹钟
unsigned char num=0;//闹钟
unsigned char nminute=0;
unsigned char nmode=0x31;
//unsigned char npluse=0x30;
unsigned char arry[20];
static int count=0;
#define mclk 8000000
void delay(unsigned int N); //ms延时?
void com_init(unsigned int baud); //串口初始化
void uart_rx(); //串口接收
void uart_sendB(unsigned char data); //发送一个字符
void uart_sentstr(unsigned char *p,unsigned char s); //向串口发送一个数据串
unsigned char read_key(); //读取键值
unsigned char key_function(); //键功能
void sys_init(); //系统初始化
void qk_sz(); //清空数组
void beep(); //蜂鸣器
void nz_alarm();
unsigned char hand[2]={0XAA,0X00}; //握手
unsigned char qdtp1[3]={0XAA,0X70,0X01}; //切换到图片1
unsigned char handfh[20]={0XAA,0X00,0X4F,0X4B,0X5F,0X56,0X35,0X2E,0X36,0X03,0X04,0X6B,0X00,0X00,0XCC,0X33,0XC3,0X3C };// 握手返回指令
unsigned char end[4]={0XCC,0X33,0XC3,0X3C}; //结束指令
unsigned char szxs[11]={0XAA,0X9B,0XFF,0X01,0X04,0XF8,0X1F,0X00,0X23,0X00,0XBC}; //时钟显示
unsigned char nzxs1[6]={0XAA,0X6F,0X00,0X55,0X01,0XB3};
unsigned char nzxs2[6]={0X20,0XC4,0XA3,0XCA,0xBD,0X20}; //打开闹钟
unsigned char dkszyear[9]={0xAA,0x44,0x01,0x00,0x45,0x00,0xdc,0x19,0x03}; //时钟打开年
unsigned char dkszmonth[9]={0xAA,0x44,0x01,0x00,0x75,0x00,0xdc,0x19,0x03}; //时钟打开月
unsigned char dkszday[9]={0xAA,0x44,0x01,0x00,0xA7,0x00,0xdc,0x19,0x03}; //时钟打开天
unsigned char dkszhour[9]={0xAA,0x44,0x01,0x00,0xD7,0x00,0xdc,0x19,0x03}; //时钟打开小时
unsigned char dkszminute[9]={0xAA,0x44,0x01,0x01,0x07,0x00,0xdc,0x19,0x03}; //时钟打开分钟
unsigned char gbgb[9]={0xAA,0x44,0x00,0x01,0x07,0x00,0xdc,0x19,0x03}; //关闭光标
unsigned char readsz[3]={0xAA,0x9B,0x5A}; //读取时钟公历
unsigned char writesz[6]={0xAA,0XE7,0X55,0XAA,0X5A,0XA5}; //写时钟
unsigned char dknhour[9]={0xAA,0x44,0x01,0x00,0x56,0x01,0xD1,0x11,0x03 }; //打开闹钟时
unsigned char dknminute[9]={0xAA,0x44,0x01,0x00,0x79,0x01,0xD1,0x11,0x03}; //打开闹钟分
unsigned char dknmode[9]={0xAA,0x44,0x01,0x00,0xD5,0x01,0xD1,0x11,0x03}; //打开闹钟模式
unsigned char nhtemp[2]={0x30,0x30};
unsigned char nmtemp[2]={0x30,0x30};
unsigned char wendu[6]={0xAA,0x6F,0x00,0xB6,0x00,0x47}; //温度显示位置
unsigned char shidu[6]={0xAA,0x6F,0x01,0x3E,0x00,0x47};
void delay_nms(unsigned int n);
void s_transstart(void);
char write_byte(unsigned char value);//写一个字节
char read_byte(unsigned char ack);//读一个字节
void calc_sth11(float *p_humidity ,float *p_temperature);
int measure(unsigned char mode);
void wenshidu();
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
{asm("nop");}
}
void s_transstart(void) //启动时序
{
data_output;
sck_output;
data_1;
sck_0;
delay_nms(20);
sck_1;
delay_nms(20);
data_0;
delay_nms(20);
sck_0;
delay_nms(20);
sck_1;
delay_nms(20);
data_1;
delay_nms(20);
sck_0;
delay_nms(20);
}
char write_byte(unsigned char value) //写入一个字节的命令,并且检查传感器是否正确接收了这个数据,返回值为0表示正确接收
{
unsigned char i;
char error;
sck_output;
data_output;
sck_0;
data_0;
for(i=0x80;i>0;i/=2)
{
sck_0;
if(i & value )
{data_1;}
else
{data_0;}
delay_nms(20);
sck_1;
delay_nms(20);
/* sck_0;
delay_nms(20);*/
}
sck_0;
data_input;
delay_nms(20);
//data_1;
//delay_nms(20);
sck_1;
delay_nms(20);
//data_0;
//delay_nms(20);
if(data_in)
error=1;
else
error=0;
delay_nms(20);
sck_0;
data_output;
return error;
}
char read_byte(unsigned char ack) //读一个字节的数据,并且向传感器发出一个字节的“已接收”信号
{
unsigned char i,val=0;
data_input;
//sck_output;
// data_1;
// data_input;
// data_0;
//delay_little();
sck_0;
for(i=0x80;i>0;i/=2)
{
sck_1;
delay_nms(20);
if(data_in)
val=(val|i);
sck_0;
delay_nms(20);
}
// data_output;
if(ack==1)
{data_1;}
else
{data_0;}
data_output;
delay_nms(20);
sck_1;
delay_nms(20);
/*sck_0;
delay_nms(20);
data_1;
delay_nms(20);*/
return val;
}
/*void connectionreset(void) //连接
{
unsigned char i;
data_output;
sck_output;
data_1;
sck_0;
for(i=0;i<9;i++)
{ sck_1;
delay_little();
sck_0;
delay_little();
}
s_transstart();
}*/
void calc_sth11(float *p_humidity ,float *p_temperature)
{
float rh=*p_humidity; // rh: Humidity [Ticks] 12 Bit
float t=*p_temperature; // t: Temperature [Ticks] 14 Bit
float rh_lin; // rh_lin: Humidity linear
float rh_true; // rh_true: Temperature compensated humidity
float t_C; // t_C : Temperature
t_C=t*0.01 - 40; //calc. temperature from ticks
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)rh_true=100; //cut if the value is outside of
if(rh_true<0.1)rh_true=0.1; //the physical possible range
*p_temperature=t_C; //return temperature
*p_humidity=rh_true; //return humidity[%RH]
}
int measure(unsigned char mode)
{
//unsigned char error=0;
//unsigned int i;
unsigned char a,b;
s_transstart();
switch(mode)
{
case 0 : write_byte(cm_temp); break;
case 1 : write_byte(cm_humi); break;
default: break;
}
data_input;
while(data_in);
//读取两个字节的数据
a=read_byte(0);
b=read_byte(1);
return (((int)a)<<8)+b;
}
void time1_init()
{
TCCR1B=0X04;
TCNT1H=0X85;
TCNT1L=0XED;
TIMSK|=BIT(2);
SREG|=BIT(7);
}
void wenshidu()
{
unsigned char i,j,k,m,n;
float temp,humi;
temp=measure(TEMP);
humi=measure(HUMI);
calc_sth11(&humi,&temp);
i=(int)temp/10+0X30;
j=(int)temp%10+0X30;
if(humi>100)
{
n=(int)humi/100+0x30;
}
k=(int)humi/10+0X30;
m=(int)humi%10+0X30;
uart_sentstr(wendu,6);
uart_sendB(i);
uart_sendB(j);
uart_sendB(0XA1);
uart_sendB(0XE6);
uart_sentstr(end,4);
delay(100);
uart_sentstr(shidu,6);
if(humi>100)
{uart_sendB(n);}
uart_sendB(k);
uart_sendB(m);
uart_sendB(0X25);
uart_sendB(0X52);
uart_sendB(0X48);
uart_sents
评论1