新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > 基于ATMEGA16單片機(jī)的頻率計(jì)設(shè)計(jì)

基于ATMEGA16單片機(jī)的頻率計(jì)設(shè)計(jì)

作者: 時(shí)間:2012-08-06 來源:網(wǎng)絡(luò) 收藏
硬件初始化函數(shù)HD_init.c:
#includeiom16v.h>
#includemacros.h>
#define uchar unsigned char
#define uint unsigned int
#include"HD_init.h"
extern unsigned int FREQ; //定義全局變量,用于計(jì)算
uint time_num=250;//250*4ms=1s

//端口初始化函數(shù)
void PORT_Init()
{
DDRB = 0xFD;//PB1作為輸入口
}
//T0溢出中斷服務(wù)程序
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; //重賦值
if(!--time_num)
{
FREQ=TCNT1L;
FREQ|=TCNT1H8; //這就是讀取低八位和高八位的值的方法
TCNT1H=0; //計(jì)數(shù)值歸零
TCNT1L=0;
time_num=250;//0定時(shí)4ms,乘以250為1s
}
}
//定時(shí)T1初始化進(jìn)入計(jì)數(shù)模式
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x06; //start Timer
}
//T0初始化定時(shí)4ms
void timer0_init(void)
{
TCCR0 = 0x00;//停止
TCNT0 = 0x83;//初始值
OCR0 = 0x82;//匹配值
TIMSK |= 0x01;//中斷允許
TCCR0 = 0x04;//啟動(dòng)定時(shí)器
}
延時(shí)函數(shù)(delay.c):
#define uchar unsigned char
#define uint unsigned int
#include"delay.h"
void delay(uint ms)
{
uint i,j;
for(i=0;ims;i++)
{
for(j=0;j1141;j++);
}
}
顯示函數(shù)(show.c)
#includeiom16v.h>
#includemacros.h>
#define uchar unsigned char
#define uint unsigned int
#include"show.h"
extern unsigned int FREQ; //定義全局變量,用于計(jì)算
const table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d, 0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void show(uchar j,uchar k)
{
DDRA|=BIT(3);
DDRA|=BIT(4);
DDRD=0XFF;
PORTA|=BIT(3);
PORTD=table[j];
PORTA=~BIT(3);
delay(3);//此延時(shí)在仿真時(shí)非常重要
PORTD=0XFF;
PORTD=~BIT(k);
PORTA|=BIT(4);
PORTA=~BIT(4);
}
void display()//實(shí)際上只用最后一個(gè)就行,保留這么多
//就是為了留一種算法
{
if(FREQ10)
{
show(FREQ%10,1);
show(0,2);
show(0,3);
show(0,0);
}
else if(FREQ100)
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(0,3);
show(0,0);
}
else if(FREQ1000)
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(FREQ%1000/100,3);
show(0,0);
}
else
{
show(FREQ%10,1);
show(FREQ%100/10,2);
show(FREQ%1000/100,3);
show(FREQ/1000,0);
}
}
以下是接口函數(shù):

點(diǎn)擊瀏覽下一頁

show.h:
#define uchar unsigned char
#define uint unsigned int
#ifndef delay_h
#define delay_h
extern void show(uchar j,uchar k);
extern void display();
#endif

HD_init.h:
#ifndef HD_init_H
#define HD_init_H
extern void PORT_Init();
extern void timer0_init(void);
extern void timer1_init(void);
#endif

delay.h:
#define uchar unsigned char
#define uint unsigned int
#ifndef delay_h
#define delay_h
extern void delay(uint ms);
#endif

上一頁 1 2 下一頁

關(guān)鍵詞: ATMEGA16 頻率計(jì) 定時(shí)器

評(píng)論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉