新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > AVR單片機(jī)(學(xué)習(xí)ing)-ATMEGA16的定時(shí)/計(jì)數(shù)器

AVR單片機(jī)(學(xué)習(xí)ing)-ATMEGA16的定時(shí)/計(jì)數(shù)器

作者: 時(shí)間:2016-11-27 來(lái)源:網(wǎng)絡(luò) 收藏
初始化:


然后是按下INT0:


最后是按下S1:


基本上就這點(diǎn)吧~~這里邊比較難理解的就是那個(gè)去取反的重定義!!??!再I(mǎi)AR工具的應(yīng)用里會(huì)有介紹(就是持續(xù)更新的那個(gè)~~畢竟有很多要更新的~~)
然后就是程序了~~
//------------------------------------------------------------------------------
//4位顯示秒表試驗(yàn)
//使用INT0鍵進(jìn)行計(jì)時(shí)的開(kāi)始和停止,使用S1鍵作為計(jì)時(shí)值得清除。定時(shí)器T0被用作掃描4
//位數(shù)碼管(1ms),定時(shí)器T1則用來(lái)計(jì)時(shí)(10ms)
#include"ioavr.h"
#include"intrinsics.h"
typedef unsigned char uchar;
typedef unsigned intuint;
__flash uchar seg[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,
0x7c,0x39,0x5e,0x79,0x71};//共陰極數(shù)碼管
__flash uchar act[4]={0xfe,0xfd,0xfb,0xf7};
uintcnt;// 全局變量,計(jì)時(shí)值
uchar flag_start;//全局變量,秒表啟動(dòng)標(biāo)志
uchar i;//每次進(jìn)入T0,對(duì)第i個(gè)數(shù)碼管刷新
#define CPL_BIT(x,y)(x^=(1<//就是將x所對(duì)應(yīng)的第y位取反
#define s1(PIND&0X10)//對(duì)S1進(jìn)行按鍵檢測(cè)的時(shí)候用到,如果檢測(cè)到,對(duì)cnt清0,你懂得why~~
#define xtal8//由于這里用到的是8MHz的,所以定義為8,寫(xiě)延時(shí)函數(shù)的時(shí)候會(huì)用到,回頭我會(huì)把IAR for AVR的精確延時(shí)寫(xiě)到博客上~~
//------------------------------------------------------------------------------
//延時(shí)1ms的函數(shù),沒(méi)有參數(shù)傳遞
voiddelay_1ms()
{
uint i;
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
//------------------------------------------------------------------------------
//延時(shí)nms的函數(shù),有參數(shù)傳遞
voiddelay_nms(uint n)
{
uint i=0;
while(i {
delay_1ms();
i++;
}
}
//------------------------------------------------------------------------------
//端口定義函數(shù),用來(lái)定義ABCD四個(gè)端口的輸入輸出
voidport_init()
{
DDRA=0XFF;
PORTA=0X00;
DDRC=0XFF;
PORTC=0XFF;
DDRD=0X00;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//T0的初始化定義
voidtimer0_init()
{
TCNT0=0X83;//1ms的定時(shí)初值
TCCR0=0X03;//T0的計(jì)數(shù)預(yù)分頻取64,這就打開(kāi)了T0的計(jì)數(shù)功能
}
//------------------------------------------------------------------------------
//T1的初始化函數(shù)
voidtimer1_init()
{
TCNT1H=0XD8;//10ms的定時(shí)初值
TCNT1L=0XF0;
}
//------------------------------------------------------------------------------
//按鍵檢測(cè)函數(shù)
voidscan_s1()
{
if(s1==0)
{
delay_nms(10);
if(s1==0)
cnt=0;
}
}
//------------------------------------------------------------------------------
//芯片初始化函數(shù),包含上面的所有初始化,以及沒(méi)有涉及的
void device_init()
{
__disable_interrupt();//disable all interrupts,呵呵,不解釋都知道,SREG?這個(gè)我不知道在不在里面,從下面的來(lái)看應(yīng)該在,回頭會(huì)具體分析的~~哈
port_init();
timer0_init();
timer1_init();
MCUCR=0X00;//INT0為低電平時(shí)產(chǎn)生中斷請(qǐng)求
GICR=0X40;//使能INT0
TIMSK=0X05;//使能T0和T1溢出中斷
__enable_interrupt();//re-enable interrupts
}
//------------------------------------------------------------------------------
//main
voidmain()
{
device_init();
while(1)
{
if(flag_start==0x01)
TCCR1B=0X02;//如果啟動(dòng)標(biāo)志位為1,則啟動(dòng)T1,計(jì)數(shù)預(yù)分頻取8
if(flag_start==0x00)
{
TCCR1B=0X00;//相反,定時(shí)器標(biāo)志位為0,則關(guān)閉T1,停止計(jì)數(shù),檢測(cè)S1,如果按下,對(duì)cnt清0
scan_s1();
}
}
}
//------------------------------------------------------------------------------
//INT0
#pragma vector=INT0_vect
__interrupt voidint0()
{
CPL_BIT(flag_start,0);//取反啟動(dòng)標(biāo)志,這里對(duì)第0位取反
delay_nms(10);
}
//------------------------------------------------------------------------------
//TIMER0_OVF
#pragma vector=TIMER0_OVF_vect
__interrupt voidtimer0_ovf()
{
SREG=0X80;
TCNT0=0X83;
if(++i>3)
i=0;
switch(i)
{
case 0: PORTA=seg[cnt];PORTC=act[i];break;
case 1: PORTA=seg[cnt0/10];PORTC=act[i];break;
case 2: PORTA=seg[cnt00/100];PORTC=act[i];break;
case 3: PORTA=seg[cnt/1000];PORTC=act[i];break;
default: break;
}
}
//------------------------------------------------------------------------------
//TIMER1_OVF
#pragma vector=TIMER1_OVF_vect
__interrupt voidtimer1_ovf()
{
TCNT1H=0XD8;
TCNT1L=0XF0;
if(++cnt>999)
cnt=0;
}


上一頁(yè) 1 2 3 4 下一頁(yè)

評(píng)論


技術(shù)專(zhuān)區(qū)

關(guān)閉