ATmega128(time 0)
//定時(shí)器用普通模式,溢出中斷,加1計(jì)數(shù)
//定時(shí)器初值計(jì)算,定時(shí)10ms,1024分頻為例,(FF-X)*F_CPU/1024=0.01s
//秒表在數(shù)碼管上顯示
//編譯環(huán)境 AVR Studio 4.17/AVR GCC,優(yōu)化度-0s
//系統(tǒng)時(shí)鐘7.3728MHZ,設(shè)置熔絲位為外部高頻石英晶體振蕩,啟動時(shí)間4.1ms
//***********************************************************************
//包含文件
//***********************************************************************
#include
#include
#define F_CPU7372800// 單片機(jī)主頻為7.3728MHz,用于延時(shí)子程序和計(jì)算定時(shí)器初始值
#include
#include
#include
#include //中斷信號頭文件
//***********************************************************************
//定義變量區(qū)
//***********************************************************************
#define f_count 183 //定時(shí)器初值,定時(shí)器加一計(jì)數(shù)
#define timer_clk 0x07//時(shí)鐘1024分頻
#define delay_us(x) _delay_us(x) //AVR GCC延時(shí)函數(shù) x(us)
#define delay_ms(x) _delay_ms(x) //AVR GCC延時(shí)函數(shù) x(ms)
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define Data_IO PORTA //數(shù)碼管數(shù)據(jù)口
#define Data_DDR DDRA //數(shù)碼管數(shù)據(jù)口方向寄存器
#define D_LE0 PORTD &= ~(1 << PD4) //數(shù)碼管段控制位為0,鎖存端口數(shù)據(jù)
#define D_LE1 PORTD |= (1 << PD4) //數(shù)碼管段控制位為1,鎖存器輸出與端口一致
#define W_LE0 PORTD &= ~(1 << PD5) //數(shù)碼管位控制位為0
#define W_LE1 PORTD |= (1 << PD5) //數(shù)碼管位控制位為1
//***********************************************************************
//共陰數(shù)碼管顯示的斷碼表0~F
//***********************************************************************
uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar time_1s_count=0;
//volatile uchar time_1s_flag;
uint second;
uint A1,A2,A3;
//***********************************************************************
//IO端口初始化
//***********************************************************************
void system_init()
{
Data_IO=0xFF; //數(shù)據(jù)口為輸出
Data_DDR=0xFF;
PORTD=0xFF; //74HC573的控制口,設(shè)置為輸出
DDRD=0xFF;
PORTB=0XFF;
DDRB=0XFF;
}
//*************************************************************************
//定時(shí)器0初始化子程序
//*************************************************************************
void timer0_init() //定時(shí)器初始化
{
TCCR0=0x07; //普通模式,OC0不輸出,1024分頻
TCNT0=f_count; //初值,定時(shí)為10ms
TIFR=0x01; //清中斷標(biāo)志位
TIMSK=0x01; //使能定時(shí)器0溢出中斷
}
//*************************************************************************
//數(shù)據(jù)處理函數(shù)
//*************************************************************************
void data_do(uint temp_d)
{
uchar A2t;
A1=temp_d/100; //分出百,十,和個位
A2t=temp_d%100;
A2=A2t/10;
A3=A2t%10;
}
//*************************************************************************
//74HC573控制數(shù)碼管動態(tài)掃描顯示函數(shù),顯示采集到的溫度
//*************************************************************************
void Display_Timer0(void)
{
uchar i,j;
system_init();
j=0x01; //此數(shù)據(jù)用來控制位選
for(i=0;i<5;i++) //用后3位數(shù)碼管來顯示
{
D_LE1;
W_LE1;
Data_IO=~j;
W_LE0;
j=(j<<1);
Data_IO=0x00;
D_LE0;
delay_ms(1);
}
D_LE1;
W_LE1;
Data_IO=~j;
W_LE0;
j=(j<<1);
Data_IO=table[A1];
D_LE0;
delay_ms(1);
D_LE1;
W_LE1;
Data_IO=~j;
W_LE0;
j=(j<<1);
Data_IO=table[A2];
D_LE0;
delay_ms(1);
D_LE1;
W_LE1;
Data_IO=~j;
W_LE0;
j=(j<<1);
Data_IO=table[A3];
D_LE0;
delay_ms(1);
D_LE1;
W_LE1;
Data_IO=0xff;
W_LE0;
}
//*************************************************************************
//定時(shí)器0中斷服務(wù)子程序
//*************************************************************************
ISR(SIG_OVERFLOW0) //中斷服務(wù)程序
{
TCNT0=f_count; //定時(shí)器賦初值,非自動重裝模式
if(time_1s_count!=100)
{
time_1s_count++; //定時(shí)器定時(shí)10ms,計(jì)數(shù)100次為1s
// time_1s_flag=0; //未到1s,標(biāo)志位為0
}
else
{
if(second!=999) //最大計(jì)時(shí)999s
{
second++;
}
else
second=0; //到999s則清0
time_1s_count=0;
// time_1s_flag=1;
data_do(second); //將秒轉(zhuǎn)化為BCD碼,供顯示
}
}
//*************************************************************************
//主程序
//*************************************************************************
int main(void)
{
system_init(); //系統(tǒng)初始化
timer0_init(); //定時(shí)器0初始化,完成定時(shí)器相關(guān)配置
time_1s_count=0; //計(jì)數(shù)值清0
SREG|=0x80; //開啟全局中斷
while(1)
{
Display_Timer0(); //顯示計(jì)數(shù)值
}
}
評論