#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#define uint unsigned int
#define uchar unsigned char
//定义宏========================================
#define BUZ PTA_PTA4
#define LED_BUF PTD
#define WP PTA_PTA1
#define SCL PTA_PTA2
#define SDA PTA_PTA3
#define SCL_DR DDRA_DDRA2
#define SDA_DR DDRA_DDRA3
#define Read_CLK PTA_PTA0
//==============================
#define DDRA_int 0xfe
#define DDRB_int 0x03
#define DDRD_int 0x7f
#define PTA_int 0x10
#define PTB_int 0x03
#define PTD_int 0xff
#define LEDBUF_OFF 0x03
//==============================
#define FIGE_F2MS 0x01
#define FIGE_F6MS 0x02
#define FIGE_F1S 0x04
//===============================
#define TestAck_FIGE 0x01
#define C_TestAck_FIGE 0xfe
#define OK 0x01
#define fail 0x00;
#define CLR_F2MS 0xfe
#define CLR_F6MS 0xfd
#define CLR_F1S 0xfb
#define E1 0x01;
#define E2 0x03;
#define E3 0x03;
//定义变量=======================================
uchar cont_2ms;
uchar cont_6ms;
uint cont_1s;
uchar sec;
uchar time_fige;
uchar LEDBUF_STEP;
//-----------------------
uchar keydown_fige;
uchar key_cont;
uchar key_buf;
uchar key;
//------------------------
uchar beef_fige;
uchar beef_daley;
uchar beef_time;
uchar INT0_cont;
uchar eep_changfige;
uchar dsp_figebuf;//
//------------------------
uchar *dsp_buff;
uchar dsp_fige;
uchar sen_step;
uchar HIVOR_cont;
uchar err_dsp;
//-------------------------
uchar HIVOR_50mscont;
uchar HIVOR_TIME;
uchar HIVOR_timebuf;
uchar HVOR_FIGE;
uchar INT0_cont_buf;
uchar HIVOR_Sec;
// time_fige每一位代表的含义
//--------------------------------------------
//0x01 2ms标记
//0x02 10ms标记
//0x04 1s标记
//---------------------------------------------
//表格==========================================
//RAM =============================
uchar LED_databuf[2]={0}; //数码管数据缓冲
uchar Read_Da_buff[2]={0}; //eeprom接收数据缓冲
uchar sendata_addr[2]={0}; //eeprom发送数据缓冲
uchar sec_dsp_da[2]={0};//秒针数据缓冲
uchar eep_da_set[2]={9,25};
uchar dsp_L_H[2]={10,11};
//ROM==============================
const uchar beef_long_tap[5]={20,100,150,200,250};//音长
const uchar beef_tap[2]={0x41,0x01};
const uchar LED_BUFBIT[2]={0xfe,0xfd};//数码管片选
const uchar LED_TAP[14]={0x08,0x2f,0x44,0x05,0x23,0x11,0x10,0x0f,0x00,0x01,0xf8,0x22,0xff,0x50};
void delay(void)
{ uchar a=10;
while(a)
{a--;
}
;
}
//定时器初始化==================================
void InitTimer(void)
{
TMODH = 0; /* Modulo value set to create 1s overflow - interrupt */
TMODL = 250;
TSC=0x40;
}
//===初始化变量==================================
void MCU_int(void)
{
//端口初始化===================
PTD=PTD_int;
PTB=PTB_int;
PTA=PTA_int;
DDRA=DDRA_int;
DDRB=DDRB_int;
DDRD=DDRD_int;
//变量初始化==================
InitTimer();//定时器初始化
INTSCR=0x00;//外中断初始化
EnableInterrupts; //开总断
CONFIG1=0; //启动看门狗
//beef_time= beef_tap[1];//响一声蜂鸣
eep_changfige=1;
}
//外中断=========================================
interrupt 2 void INT0_Interrupt(void)
{ INTSCR=0x04;
if((Read_CLK==0)&&(HIVOR_TIME>0))
{
HIVOR_timebuf=HIVOR_TIME;
HIVOR_TIME=0;
HVOR_FIGE=1;
if(HIVOR_timebuf<90)
{ INT0_cont++;
}
else
{ INT0_cont=0;
HIVOR_TIME=0;
}
}
else
{ INT0_cont=0;
HIVOR_TIME=0;
}
}
//溢出中断=======================================
interrupt 6 void Timer_Interrupt(void)
{
TSC=TSC&0x7f;
TMODH = 0;
TMODL = 250;
cont_2ms++;
cont_6ms++;
cont_1s++;
HIVOR_50mscont++;
if(beef_fige==0x01)
PTA=PTA^0x10;
else
BUZ=1;
if(cont_2ms>32)
{ cont_2ms=0;
time_fige=time_fige|FIGE_F2MS;
}
if(cont_6ms>48)
{ cont_6ms=0;
time_fige=time_fige|FIGE_F6MS;
if(beef_daley>0)
beef_daley--;
}
if(HIVOR_50mscont>80)
{ HIVOR_50mscont=0;
if(HIVOR_TIME<200)
HIVOR_TIME++;
}
if(cont_1s>8000)
{ if(HIVOR_TIME<90)
{ HIVOR_Sec++;
if(HIVOR_Sec==3)
INT0_cont_buf=INT0_cont;
else
INT0_cont_buf=HIVOR_Sec;
INT0_cont=0;
}
cont_1s=0;
sec++;
if(sec>99)
sec=0;
}
}
//显示驱动===========================================
void dsp_dir(void)
{ if((time_fige&FIGE_F2MS)==FIGE_F2MS)
{
time_fige=time_fige&CLR_F2MS;//清标记
PTB=PTB|LEDBUF_OFF;//关显示
PTD=LED_databuf[LEDBUF_STEP]; //更新段
PTB=PTB&LED_BUFBIT[LEDBUF_STEP];//启动显示
LEDBUF_STEP++;//更新片选,
if(LEDBUF_STEP>1)
LEDBUF_STEP=0;
}
}
//二进制数据转换成十进制显示数据=============================
void chang_2to10(uchar *dsp_da,uchar *dsp_buf)
{
*dsp_buf=LED_TAP[*dsp_da/10];
dsp_buf=dsp_buf+1;
*dsp_buf=LED_TAP[*dsp_da%10];
}
//=显示管理==========================================
void dsp_cr(uchar *dsp_fige2)
{ LED_databuf[1]=LED_TAP[12];
LED_databuf[0]=LED_TAP[12];
switch(*dsp_fige2)
{ case 0: chang_2to10( &HIVOR_cont, LED_databuf);break;
case 1: if(sec&1)
{
LED_databuf[0]=LED_TAP[13];
LED_databuf[1]=LED_TAP[err_dsp];
}
break;
case 2: ;chang_2to10( (eep_da_set+sen_step), LED_databuf);
break;
case 3: ;LED_databuf[0]=LED_TAP[dsp_L_H[sen_step]];
LED_databuf[1]=LED_TAP[12];
break;
}
}
//按键扫描=========================================
void key_dir(void)
{ if((time_fige&FIGE_F6MS)==FIGE_F6MS)
{
time_fige=time_fige&CLR_F6MS;
if((PTB&0xe0)!=0xe0)
{ key_cont++;
if(key_cont==10)
{ keydown_fige=keydown_fige|0x01;
key_buf=PTB&0xe0;
}
if((key_cont>250)&&((keydown_fige&0x02)!=0x02))
{
key_cont=251;
keydown_fige=keydown_fige|0x02;
key=key_buf|0x01;
//LED_databuf[0]=LED_TAP[7];
}
}
else
{
if(keydown_fige==0x01)
{ key=key_buf;
//LED_databuf[0]=LED_TAP[0];
}
key_cont=0;
keydown_fige=keydown_fige&0xfc;
key_buf=0;
}
}
}
//蜂鸣器管理================================================
void Bell_dir(void)
{ if(beef_daley==0)
{ if(beef_fige==1)
{ beef_daley=50;
beef_fige=0;
}
else
if((beef