AVR單片機(jī)(學(xué)習(xí)ing)-ATMEGA16的定時(shí)/計(jì)數(shù)器
初始化:
然后是按下INT0:
最后是按下S1:
基本上就這點(diǎn)吧~~這里邊比較難理解的就是那個(gè)去取反的重定義!!??!再I(mǎi)AR工具的應(yīng)用里會(huì)有介紹(就是持續(xù)更新的那個(gè)~~畢竟有很多要更新的~~)
然后是按下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 int uint;
__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};
uint cnt; // 全局變量,計(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 xtal 8 //由于這里用到的是8MHz的,所以定義為8,寫(xiě)延時(shí)函數(shù)的時(shí)候會(huì)用到,回頭我會(huì)把IAR for AVR的精確延時(shí)寫(xiě)到博客上~~
//------------------------------------------------------------------------------
//延時(shí)1ms的函數(shù),沒(méi)有參數(shù)傳遞
void delay_1ms()
{
uint i;
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
//------------------------------------------------------------------------------
//延時(shí)nms的函數(shù),有參數(shù)傳遞
void delay_nms(uint n)
{
uint i=0;
while(i {
delay_1ms();
i++;
}
}
//------------------------------------------------------------------------------
//端口定義函數(shù),用來(lái)定義ABCD四個(gè)端口的輸入輸出
void port_init()
{
DDRA=0XFF;
PORTA=0X00;
DDRC=0XFF;
PORTC=0XFF;
DDRD=0X00;
PORTD=0XFF;
}
//------------------------------------------------------------------------------
//T0的初始化定義
void timer0_init()
{
TCNT0=0X83; //1ms的定時(shí)初值
TCCR0=0X03; //T0的計(jì)數(shù)預(yù)分頻取64,這就打開(kāi)了T0的計(jì)數(shù)功能
}
//------------------------------------------------------------------------------
//T1的初始化函數(shù)
void timer1_init()
{
TCNT1H=0XD8; //10ms的定時(shí)初值
TCNT1L=0XF0;
}
//------------------------------------------------------------------------------
//按鍵檢測(cè)函數(shù)
void scan_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
void main()
{
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 void int0()
{
CPL_BIT(flag_start,0); //取反啟動(dòng)標(biāo)志,這里對(duì)第0位取反
delay_nms(10);
}
//------------------------------------------------------------------------------
//TIMER0_OVF
#pragma vector=TIMER0_OVF_vect
__interrupt void timer0_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 void timer1_ovf()
{
TCNT1H=0XD8;
TCNT1L=0XF0;
if(++cnt>999)
cnt=0;
}
//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 int
__flash uchar seg[16]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,
__flash uchar act[4]={0xfe,0xfd,0xfb,0xf7};
uint
uchar flag_start;
uchar i;
#define CPL_BIT(x,y)
#define s1
#define xtal
//------------------------------------------------------------------------------
//延時(shí)1ms的函數(shù),沒(méi)有參數(shù)傳遞
void
{
}
//------------------------------------------------------------------------------
//延時(shí)nms的函數(shù),有參數(shù)傳遞
void
{
}
//------------------------------------------------------------------------------
//端口定義函數(shù),用來(lái)定義ABCD四個(gè)端口的輸入輸出
void
{
}
//------------------------------------------------------------------------------
//T0的初始化定義
void
{
}
//------------------------------------------------------------------------------
//T1的初始化函數(shù)
void
{
}
//------------------------------------------------------------------------------
//按鍵檢測(cè)函數(shù)
void
{
}
//------------------------------------------------------------------------------
//芯片初始化函數(shù),包含上面的所有初始化,以及沒(méi)有涉及的
void device_init()
{
}
//------------------------------------------------------------------------------
//main
void
{
}
//------------------------------------------------------------------------------
//INT0
#pragma vector=INT0_vect
__interrupt void
{
}
//------------------------------------------------------------------------------
//TIMER0_OVF
#pragma vector=TIMER0_OVF_vect
__interrupt void
{
}
//------------------------------------------------------------------------------
//TIMER1_OVF
#pragma vector=TIMER1_OVF_vect
__interrupt void
{
}
關(guān)鍵詞:
AVR單片機(jī)ATMEGA16定時(shí)計(jì)數(shù)
相關(guān)推薦
技術(shù)專(zhuān)區(qū)
- FPGA
- DSP
- MCU
- 示波器
- 步進(jìn)電機(jī)
- Zigbee
- LabVIEW
- Arduino
- RFID
- NFC
- STM32
- Protel
- GPS
- MSP430
- Multisim
- 濾波器
- CAN總線(xiàn)
- 開(kāi)關(guān)電源
- 單片機(jī)
- PCB
- USB
- ARM
- CPLD
- 連接器
- MEMS
- CMOS
- MIPS
- EMC
- EDA
- ROM
- 陀螺儀
- VHDL
- 比較器
- Verilog
- 穩(wěn)壓電源
- RAM
- AVR
- 傳感器
- 可控硅
- IGBT
- 嵌入式開(kāi)發(fā)
- 逆變器
- Quartus
- RS-232
- Cyclone
- 電位器
- 電機(jī)控制
- 藍(lán)牙
- PLC
- PWM
- 汽車(chē)電子
- 轉(zhuǎn)換器
- 電源管理
- 信號(hào)放大器
評(píng)論