AVR單片機(jī)做的電子時(shí)鐘
#include
#include
#include
#include
#include
#include
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#definextal8
#defineCSPA5
#defineSIDPA6
#defineSCLKPA7
#defineSet_CS()DDRA|=(1<
#defineClr_CS()DDRA|=(1<
#defineHH2//定義時(shí)為2
#defineMM1//定義分為1
#defineSS0//定義秒為0
volatileunsignedcharTime_h_m_s[3]={0,0,0};//時(shí)間的時(shí)分秒存貯單元
volatileunsignedcharflash_sign=0x00;
//====================================================================
//函數(shù)聲明
voidDelay(uintms);//延時(shí)子程序
voidSerial_W_1byte_to_LCD(ucharRS,ucharW_data);
voidSerial_send_cmd(ucharsend_cmd);
voidSerial_send_data(ucharsend_data);
voidWrite_8bits(ucharW_bits);
voidLCD_Init(void);
voidSerial_send_string_to_xy(ucharrow,ucharcol,uchar*p);
voidDisplay_cLOCk1(ucharrow,ucharcol,uchar*time);
/*=============================================================
功能:TIMER2溢出中斷函數(shù)
=================================================================*/
SIGNAL(SIG_OUTPUT_COMPARE2)//_VECTOR(3)
{
statICunsignedcharhalf_second;
if((++half_second)>=2)
{
if((++Time_h_m_s[SS])>=60)
{
Time_h_m_s[SS]=0;
if((++Time_h_m_s[MM])>=60)
{
Time_h_m_s[MM]=0;
if((++Time_h_m_s[HH])>=24)
Time_h_m_s[HH]=0;
}
}
half_second=0x00;
}
flash_sign&=0x01;
flash_sign^=0x01;//置位閃動(dòng)標(biāo)志位(該位為0時(shí)閃動(dòng))
}
/*******************************************************************
函數(shù)名:Port_init
入口參數(shù):無(wú)
出口參數(shù):無(wú)
建立日期:2008年05月09日
修改日期:
函數(shù)作用:MCU端口初始化函數(shù)
說(shuō)明:
********************************************************************/
voidPort_init(void)
{
DDRC=~(1<
ASSR|=(1
}
/*******************************************************************
函數(shù)名:Serial_send_string
入口參數(shù):*p_send_data
出口參數(shù):無(wú)
建立日期:2008年05月09日
修改日期:
函數(shù)作用:發(fā)送字符串子函數(shù)
說(shuō)明:
********************************************************************/
voidSerial_send_string(uchar*p_send_data)
{
uchar*p_temp;
p_temp=p_send_data;
while(*p_temp!=0)
{
Serial_send_data(*p_temp++);
}
}
//========================
/********************************************************************
函數(shù)名:Serial_send_string_to_xy
入口參數(shù):row,col,*p
出口參數(shù):無(wú)
建立日期:2008年04月13日
修改日期:
函數(shù)作用:
說(shuō)明:
********************************************************************/
voidSerial_send_string_to_xy(ucharrow,ucharcol,uchar*p)
{
switch(row)
{
case0:Serial_send_cmd(0x80+col);
break;
case1:Serial_send_cmd(0x90+col);
break;
case2:Serial_send_cmd(0x88+col);
break;
case3:Serial_send_cmd(0x98+col);
break;
default:break;
}
while(*p!=0)
{
Serial_send_data(*p++);//寫(xiě)數(shù)據(jù)到RAM
}
}
/********************************************************************/
intmain(void)
{
PORTA=0XFF;//
DDRA=0XFF;//PA口全部設(shè)為輸出模式
Port_init();
Clr_CS();
Clr_SID();
Clr_SCLK();
LCD_Init();
Delay(10);
LCD_Init();//初始化兩邊是關(guān)鍵,否則液晶上電重起將不能顯示
sei();//開(kāi)總中斷
while(1)
{
asm("nop");
asm("nop");
Serial_send_cmd(0x30);//基本指令集,,繪圖顯示OFF
//Serial_send_cmd(0x01);//清除顯示
Serial_send_string_to_xy(0,0,"現(xiàn)在時(shí)間:");
Display_clock1(1,3,Time_h_m_s);
}
}
/*******************************************************************
函數(shù)名:LCD_Init
入口參數(shù):無(wú)
出口參數(shù):無(wú)
建立日期:2008年05月09日
修改日期:
函數(shù)作用:12864液晶初始化函數(shù)
說(shuō)明:
********************************************************************/
voidLCD_Init(void)
{
ucharcmd;
Delay(50);
cmd=0x30;//功能設(shè)置8位數(shù)據(jù),基本指令
Serial_send_cmd(cmd);
Delay(20);
cmd=0x0C;//顯示狀態(tài)ON,游標(biāo)OFF,反白OFF
Serial_send_cmd(cmd);//寫(xiě)指令
Delay(20);
cmd=0x01;//清除顯示
Serial_send_cmd(cmd);//寫(xiě)指令
Delay(20);
cmd=0x02;//地址歸位
Serial_send_cmd(cmd);//寫(xiě)指令
Delay(20);
cmd=0x80;//設(shè)置DDRAM地址
Serial_send_cmd(cmd);//寫(xiě)指令
Delay(20);//延時(shí)
}
/*******************************************************************
函數(shù)名:Serial_send_cmd
入口參數(shù):cmd
出口參數(shù):無(wú)
建立日期:2008年4月13日
修改日期:
函數(shù)作用:寫(xiě)一個(gè)字節(jié)指令的到12864液晶,
說(shuō)明:
********************************************************************/
voidSerial_send_cmd(ucharsend_cmd)
{
Serial_W_1byte_to_LCD(0,send_cmd);
}
/*******************************************************************
函數(shù)名:Serial_send_data
入口參數(shù):cmd
出口參數(shù):無(wú)
建立日期:2008年4月13日
修改日期:
函數(shù)作用:寫(xiě)一個(gè)字節(jié)數(shù)據(jù)到12864液晶,
說(shuō)明:
********************************************************************/
voidSerial_send_data(ucharsend_data)
{
Serial_W_1byte_to_LCD(1,send_data);
}
名:Serial_Serial_W_1byte_to_LCD_to_LCD
入口參數(shù):RW、RS、W_data
出口參數(shù):無(wú)
建立日期:2007年3月3日
修改日期:
函數(shù)作用:寫(xiě)一個(gè)字節(jié)的數(shù)據(jù)到12864液晶,包括指令和數(shù)據(jù)
說(shuō) 明:RW=1,從液晶讀數(shù)據(jù)到MCU;RW=0,寫(xiě)一個(gè)數(shù)據(jù)到液晶;
(一般RW都設(shè)為0,即只向液晶寫(xiě)數(shù)據(jù),不讀數(shù)據(jù))
RS=1,寫(xiě)入的是數(shù)據(jù);RS=0,寫(xiě)入的是指令;
一般模式:RW=0,RS=1;寫(xiě)數(shù)據(jù)
RW=0,RS=0;寫(xiě)指令
********************************************************************/
void Serial_W_1byte_to_LCD(uchar RS, uchar W_data)
{
uchar H_data,L_data,S_ID = 0xf8; //11111 RWRS 0
if(RS == 0)
{
S_ID &= ~0x02;
}
else //if(RS==1)
{
S_ID "= 0X02;
}
H_data = W_data;
H_data &= 0xf0; //屏蔽低4位的數(shù)據(jù)
L_data = W_data; //xxxx0000格式
L_data <<= 4; //xxxx0000格式
L_data &= 0xf0; //屏蔽高4位的數(shù)據(jù)
Set_CS();
asm("nop");
asm("nop");
Write_8bits(S_ID); //發(fā)送S_ID
asm("nop");
Write_8bits(H_data); //發(fā)送H_data
asm("nop");
Write_8bits(L_data); //發(fā)送L_data
asm("nop");
Clr_CS();
}
/********************************************************************
函 數(shù) 名:Write_8bits
入口參數(shù):W_bits
出口參數(shù):無(wú)
建立日期:2007年3月3日
修改日期:
函數(shù)作用:負(fù)責(zé)串行輸出8個(gè)bit位
說(shuō) 明:
********************************************************************/
void Write_8bits(uchar W_bits)
{
uchar i,Temp_data;
Temp_data = W_bits;
for(i=0; i<8; i++)
{
if((Temp_data&0x80)==0)
{
Clr_SID();
}
else
{
Set_SID();
}
asm("nop");
asm("nop");
Set_SCLK();
asm("nop");
asm("nop");
Clr_SCLK();
asm("nop");
asm("nop");
Temp_data <<= 1;
}
Clr_SID();
}
/********************************************************************
函 數(shù) 名:Delay
入口參數(shù):ms
出口參數(shù):無(wú)
建立日期:2007年3月3日
修改日期:
函數(shù)作用:毫秒級(jí)的延時(shí)程序,當(dāng)晶振為12MHz時(shí),xtal=12;
說(shuō) 明:
********************************************************************/
void Delay(uint ms)
{
uint i;
while(ms--)
{
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
}