主程序
#include <avr/io.h>
#include <avr/common.h>
#include <avr/interrupt.h>
#include "mytype.h"
#include <util/delay.h>
#include "COMM.h"
#include "1820.h"
#include "DS1307.H"
#include "24CXX.H"
#include "IIC.h"
extern uchar templ,temph; //外部变量,温度的高低八位
extern char dot_tab[16]; //小数表(外部,来自于18B20文件)
extern unsigned char time_buf[7]; //1307时间缓冲 时分秒,日月年
uint number_count=0; //24C128数据写入计数,写入一条数据+1,最后发送依靠这个值
char temp_buf[7]={' ',0,0,0,'.',0};
char sendbuf[13]={"00:00 00.0C"};
char number_buf[7]={0,0,0,0,0,' ','\0'};
uchar time_count=0; //计数,用于按键调用
uchar RW_buf[4]={0,1,2,3}; //24Cxx读写缓冲
uchar flag;
uchar a;
uchar key_state=0; //按键状态机状态值
uchar key_keep=0; //按键保持时间计数,N*10ms
uchar key_release=0; //释放时间计时,N*10ms
uchar key_double=0; //双击标志
void Mcu_Init(void); //MCU初始化
void Timer_Init(void); //定时器初始化
void Control_SM(void);//控制状态机,主程序驱动依靠本状态机
uchar Key_Read(void);//按键 长按 短按 双击
void Serial_Time_Set(void);//串口时间设定,用于DS1307时间设定
void Send_Data();//数据发送
void Write_Data();//数据写入IIC
void Data_2_Temp(void); //数据转化为温度
void Time_2_Disp(void);
int main(void)
{
Mcu_Init();
if(!Rst_1820())Send_String("OK");
Set_1820();
while(1)
{
PORTB=0xff;
if(time_count >= 200)
{
time_count=0;
a=Key_Read();
}
Control_SM();
//TODO:: Please write your application code
}
return 0;
}
void Mcu_Init(void)
{
Set_Bit(IO_DIR,LED);
Set_Bit(IO_PORT,LED);
Clr_Bit(IO_DIR,KEY);
Set_Bit(IO_PORT,KEY);
Usart_Init();
Timer_Init();
IIC_Init();
sei();
Send_String("MCU init ");
}
void Timer_Init(void)
{
TCCR0A = 0x00;
TCCR0B = 0x01;//No Prescaling
TCNT0 = 206;//50us
TIMSK0 = 1<<TOIE0;//Overflow Interrupt Enabled
sei();//Global Interrupts Enabled
}
ISR(TIMER0_OVF_vect)
{
TCNT0 = 206;//set time 50us real time 68us
time_count++;
}
void Control_SM(void)
{
switch(control_state)
{
case wait://等待按键命令
if(a==2)
{
number_count=0;
control_state++;
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
_delay_ms(500);
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
}
if(a==3)
{
control_state=send;
Clr_Bit(IO_PORT,LED);
_delay_ms(1000);
Set_Bit(IO_PORT,LED);
}
break;
case work://
if(a==3)
{
control_state=send;
Clr_Bit(IO_PORT,LED);
_delay_ms(1000);
Set_Bit(IO_PORT,LED);
}
Read_Time_Burst(time_buf);
if (time_buf[0]==0)
{
RW_buf[0]=time_buf[2];
RW_buf[1]=time_buf[1];
Get_Temp_1820();
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
Get_Temp_1820();
RW_buf[2]=temph;
RW_buf[3]=templ;
Write_Block_24CXX(number_count*4,RW_buf,4);
// Write_Data();
number_count++;
Time_2_Disp(); //数据转ASCII时间
Data_2_Temp(); //数据转ASCII温度
Send_String(sendbuf); //发送数据
Send_Char('\n');
}
break;
case send://
Send_String("total number:");
number_buf[0]=number_count/10000+0x30;
number_buf[1]=(number_count/1000)%10+0x30;
number_buf[2]=(number_count/100)%10+0x30;
number_buf[3]=(number_count/10)%10+0x30;
number_buf[4]=number_count%10+0x30;
Send_String(number_buf);
Send_Char('\n');
Send_Data();
control_state=wait;
break;
default:
control_state=wait;
break;
}
}
uchar Key_Read(void)//按键判断
{
uchar key_num=0;
switch(key_state)
{
case 0://按键按下判断
if(!Get_Bit(IO_IN,KEY))
{
key_state++;
}
if(key_double) key_release++;
if(key_release>=key_second)//大于等待时间,执行单击
{
key_num = key_s;
key_double=0;
key_release=0;
key_keep=0;
}
break;
case 1://按键确认判断
if(!Get_Bit(IO_IN,KEY))
{
key_state++;
key_keep++;
}
else key_state=0;
break;
case 2://按键释放判断
if(!Get_Bit(IO_IN,KEY))//未释放,长按判断
{
if(key_keep<long_seconds)key_keep++;
else
{
if(!key_double)key_num=key_l;//长按
key_state++;
key_double=0;//先短后长认为长按
key_release=0;
key_keep=0;
}
}
else//已释放,单双击判断,单双击按住案件的时间不计算在间隔内。
{
if(key_double)//之前已按下过一次
{
key_double=0;//单击标记清除
key_release=0;//等待时间清零
key_keep=0;
key_num = key_d;//输出双击
}
else key_double=1;//之前未按下
key_state++;
}
break;
case 3://长按等待释放或连发
if(Get_Bit(IO_IN,KEY)) key_state=0;
break;
default:
key_state=0;
break;
}
return key_num;
}
void Write_Data(void)//数据写入IIC
{
uchar i;
for(i=0;i<4;i++)
{
Write_Byte_24CXX(number_count*4+i,RW_buf[i]);
_delay_us(500);
}
}
void Send_Data(void)//数据发送
{
uint i=0;
for (i=0;i<number_count;i++)
{
Read_Block_24CXX(i*4,RW_buf,4);
Time_2_Disp(); //数据转ASCII时间
Data_2_Temp(); //数据转ASCII温度
number_buf[0]=(i+1)/10000+0x30;
number_buf[1]=((i+1)/1000)%10+0x30;
number_buf[2]=((i+1)/100)%10+0x30;
number_buf[3]=((i+1)/10)%10+0x30;
number_buf[4]=(i+1)%10+0x30;
Send_String(number_buf);
Send_String(sendbuf); //发送数据
Send_Char('\n'); //回车
}
Send_String("Finish"); //发送完成后显示结束
Send_Char('\n');
}
void Time_2_Disp(void)
{
sendbuf[0]=(RW_buf[0]>>4)+0x30;
sendbuf[1]=(RW_buf[0]&0x0f)+0x30;
sendbuf[3]=(RW_buf[1]>>4)+0x30;
sendbuf[4]=(RW_buf[1]&0x0f)+0x30;
}
void Data_2_Temp(void)
{
uchar temp1,temp2;
uint temp;
if(temph&0xf0)
{
temp=65536-(RW_buf[2]<<8)+RW_buf[3];
temp1=temp>>8;
temp2=(char)temp;
sendbuf[6]='-';
sendbuf[7]=(temp1/10)%10+0x30;
sendbuf[8]=temp1%10+0x30;
sendbuf[10]=dot_tab[temp2];
}
else
{
temp1=(RW_buf[2]<<4)+(RW_buf[3]>>4);
temp2=RW_buf[3]&0x0f;
sendbuf[6]=temp1/100+0x30;
sendbuf[7]=(temp1/10)%10+0x30;
sendbuf[8]=temp1%10+0x30;
sendbuf[10]=dot_tab[temp2];
}
}
void Serial_Time_Set(void)
{
Write_Time_Burst(time_buf);//时钟突发写
}
#include <avr/io.h>
#include <avr/common.h>
#include <avr/interrupt.h>
#include "mytype.h"
#include <util/delay.h>
#include "COMM.h"
#include "1820.h"
#include "DS1307.H"
#include "24CXX.H"
#include "IIC.h"
extern uchar templ,temph; //外部变量,温度的高低八位
extern char dot_tab[16]; //小数表(外部,来自于18B20文件)
extern unsigned char time_buf[7]; //1307时间缓冲 时分秒,日月年
uint number_count=0; //24C128数据写入计数,写入一条数据+1,最后发送依靠这个值
char temp_buf[7]={' ',0,0,0,'.',0};
char sendbuf[13]={"00:00 00.0C"};
char number_buf[7]={0,0,0,0,0,' ','\0'};
uchar time_count=0; //计数,用于按键调用
uchar RW_buf[4]={0,1,2,3}; //24Cxx读写缓冲
uchar flag;
uchar a;
uchar key_state=0; //按键状态机状态值
uchar key_keep=0; //按键保持时间计数,N*10ms
uchar key_release=0; //释放时间计时,N*10ms
uchar key_double=0; //双击标志
void Mcu_Init(void); //MCU初始化
void Timer_Init(void); //定时器初始化
void Control_SM(void);//控制状态机,主程序驱动依靠本状态机
uchar Key_Read(void);//按键 长按 短按 双击
void Serial_Time_Set(void);//串口时间设定,用于DS1307时间设定
void Send_Data();//数据发送
void Write_Data();//数据写入IIC
void Data_2_Temp(void); //数据转化为温度
void Time_2_Disp(void);
int main(void)
{
Mcu_Init();
if(!Rst_1820())Send_String("OK");
Set_1820();
while(1)
{
PORTB=0xff;
if(time_count >= 200)
{
time_count=0;
a=Key_Read();
}
Control_SM();
//TODO:: Please write your application code
}
return 0;
}
void Mcu_Init(void)
{
Set_Bit(IO_DIR,LED);
Set_Bit(IO_PORT,LED);
Clr_Bit(IO_DIR,KEY);
Set_Bit(IO_PORT,KEY);
Usart_Init();
Timer_Init();
IIC_Init();
sei();
Send_String("MCU init ");
}
void Timer_Init(void)
{
TCCR0A = 0x00;
TCCR0B = 0x01;//No Prescaling
TCNT0 = 206;//50us
TIMSK0 = 1<<TOIE0;//Overflow Interrupt Enabled
sei();//Global Interrupts Enabled
}
ISR(TIMER0_OVF_vect)
{
TCNT0 = 206;//set time 50us real time 68us
time_count++;
}
void Control_SM(void)
{
switch(control_state)
{
case wait://等待按键命令
if(a==2)
{
number_count=0;
control_state++;
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
_delay_ms(500);
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
}
if(a==3)
{
control_state=send;
Clr_Bit(IO_PORT,LED);
_delay_ms(1000);
Set_Bit(IO_PORT,LED);
}
break;
case work://
if(a==3)
{
control_state=send;
Clr_Bit(IO_PORT,LED);
_delay_ms(1000);
Set_Bit(IO_PORT,LED);
}
Read_Time_Burst(time_buf);
if (time_buf[0]==0)
{
RW_buf[0]=time_buf[2];
RW_buf[1]=time_buf[1];
Get_Temp_1820();
Clr_Bit(IO_PORT,LED);
_delay_ms(500);
Set_Bit(IO_PORT,LED);
Get_Temp_1820();
RW_buf[2]=temph;
RW_buf[3]=templ;
Write_Block_24CXX(number_count*4,RW_buf,4);
// Write_Data();
number_count++;
Time_2_Disp(); //数据转ASCII时间
Data_2_Temp(); //数据转ASCII温度
Send_String(sendbuf); //发送数据
Send_Char('\n');
}
break;
case send://
Send_String("total number:");
number_buf[0]=number_count/10000+0x30;
number_buf[1]=(number_count/1000)%10+0x30;
number_buf[2]=(number_count/100)%10+0x30;
number_buf[3]=(number_count/10)%10+0x30;
number_buf[4]=number_count%10+0x30;
Send_String(number_buf);
Send_Char('\n');
Send_Data();
control_state=wait;
break;
default:
control_state=wait;
break;
}
}
uchar Key_Read(void)//按键判断
{
uchar key_num=0;
switch(key_state)
{
case 0://按键按下判断
if(!Get_Bit(IO_IN,KEY))
{
key_state++;
}
if(key_double) key_release++;
if(key_release>=key_second)//大于等待时间,执行单击
{
key_num = key_s;
key_double=0;
key_release=0;
key_keep=0;
}
break;
case 1://按键确认判断
if(!Get_Bit(IO_IN,KEY))
{
key_state++;
key_keep++;
}
else key_state=0;
break;
case 2://按键释放判断
if(!Get_Bit(IO_IN,KEY))//未释放,长按判断
{
if(key_keep<long_seconds)key_keep++;
else
{
if(!key_double)key_num=key_l;//长按
key_state++;
key_double=0;//先短后长认为长按
key_release=0;
key_keep=0;
}
}
else//已释放,单双击判断,单双击按住案件的时间不计算在间隔内。
{
if(key_double)//之前已按下过一次
{
key_double=0;//单击标记清除
key_release=0;//等待时间清零
key_keep=0;
key_num = key_d;//输出双击
}
else key_double=1;//之前未按下
key_state++;
}
break;
case 3://长按等待释放或连发
if(Get_Bit(IO_IN,KEY)) key_state=0;
break;
default:
key_state=0;
break;
}
return key_num;
}
void Write_Data(void)//数据写入IIC
{
uchar i;
for(i=0;i<4;i++)
{
Write_Byte_24CXX(number_count*4+i,RW_buf[i]);
_delay_us(500);
}
}
void Send_Data(void)//数据发送
{
uint i=0;
for (i=0;i<number_count;i++)
{
Read_Block_24CXX(i*4,RW_buf,4);
Time_2_Disp(); //数据转ASCII时间
Data_2_Temp(); //数据转ASCII温度
number_buf[0]=(i+1)/10000+0x30;
number_buf[1]=((i+1)/1000)%10+0x30;
number_buf[2]=((i+1)/100)%10+0x30;
number_buf[3]=((i+1)/10)%10+0x30;
number_buf[4]=(i+1)%10+0x30;
Send_String(number_buf);
Send_String(sendbuf); //发送数据
Send_Char('\n'); //回车
}
Send_String("Finish"); //发送完成后显示结束
Send_Char('\n');
}
void Time_2_Disp(void)
{
sendbuf[0]=(RW_buf[0]>>4)+0x30;
sendbuf[1]=(RW_buf[0]&0x0f)+0x30;
sendbuf[3]=(RW_buf[1]>>4)+0x30;
sendbuf[4]=(RW_buf[1]&0x0f)+0x30;
}
void Data_2_Temp(void)
{
uchar temp1,temp2;
uint temp;
if(temph&0xf0)
{
temp=65536-(RW_buf[2]<<8)+RW_buf[3];
temp1=temp>>8;
temp2=(char)temp;
sendbuf[6]='-';
sendbuf[7]=(temp1/10)%10+0x30;
sendbuf[8]=temp1%10+0x30;
sendbuf[10]=dot_tab[temp2];
}
else
{
temp1=(RW_buf[2]<<4)+(RW_buf[3]>>4);
temp2=RW_buf[3]&0x0f;
sendbuf[6]=temp1/100+0x30;
sendbuf[7]=(temp1/10)%10+0x30;
sendbuf[8]=temp1%10+0x30;
sendbuf[10]=dot_tab[temp2];
}
}
void Serial_Time_Set(void)
{
Write_Time_Burst(time_buf);//时钟突发写
}