基于ATMEGA16單片機的頻率計設(shè)計
硬件初始化函數(shù)HD_init.c:
#includeiom16v.h>
#includemacros.h>
#define uchar unsigned char
#define uint unsigned int
#include"HD_init.h"
extern unsigned int FREQ; //定義全局變量,用于計算
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; //計數(shù)值歸零
TCNT1L=0;
time_num=250;//定時器0定時4ms,乘以250為1s
}
}
//定時T1初始化進入計數(shù)模式
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x06; //start Timer
}
//定時器T0初始化定時4ms
void timer0_init(void)
{
TCCR0 = 0x00;//停止定時器
TCNT0 = 0x83;//初始值
OCR0 = 0x82;//匹配值
TIMSK |= 0x01;//中斷允許
TCCR0 = 0x04;//啟動定時器
}
延時函數(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; //定義全局變量,用于計算
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);//此延時在仿真時非常重要
PORTD=0XFF;
PORTD=~BIT(k);
PORTA|=BIT(4);
PORTA=~BIT(4);
}
void display()//實際上只用最后一個就行,保留這么多
//就是為了留一種算法
{
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ù):
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
評論