/***********************************************************
Project : AVR数码管计数显示
Chip type : ATmega16
Frequency : 8M
Software : Codevision 1.24.6
Author : shishuwu
Date : 09.02.22
Comments : 按键分别接PB.0,PB.1口,位选端接PC口,段选端接PA口
*************************************************************/
#include<MEGA16.h>
#include<delay.h>
const unsigned char table[10]=
{0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char Data[4]; //显示初始值:0 0 0 0
unsigned int CNT="0"; //初始计数值:0
unsigned char Key_Up; //加计数按键
unsigned char Key_Down; //减计数按键
void Display(unsigned char *p) //动态显示函数,参数p为待显示的数组名
{
PORTC="0xfe";
PORTA="table"[p[3]];
delay_ms(5);
PORTC="0xfd";
PORTA="table"[p[2]];
delay_ms(5);
PORTC="0xfb";
PORTA="table"[p[1]];
delay_ms(5);
PORTC="0xf7";
PORTA="table"[p[0]];
delay_ms(5);
}
//计数值处理函数。参数i:计数值;Data[]:处理数据存放的数组名//
//功能:此函数用于将计数值拆分为BCD码的千,百,十,一数据,用于查表显示//
void Process(unsigned char i)
{
Data[0]=i/1000;
Data[1]=i%1000/100;
Data[2]=i%100/10;
Data[3]=i%10;
}
void Init_IO(void) //初始化I/O口
{
DDRA="0xff"; //设置A口为推挽1输出
PORTA="0xff";
DDRB="0x00"; //设置B口为带上拉电阻输入
PORTB="0x03";
DDRC="0x0f"; //设置C口为推挽1输出
PORTC="0x0f";
}
void Get_Key(void)
{
while((PINB&0x01)==0) //加键接PB.0口
{
Key_Up=1;
}
while((PINB&0x02)==0) //减键接PB.1口
{
Key_Down=1;
}
}
void main(void)
{
Init_IO(); //初始化I/O口
PORTA="0xff"; //点亮以测试所有的数码管
PORTC="0x00";
delay_ms(1000); //延时
PORTC="0xff"; //熄灭所有的数码管
while(1)
{
Get_Key();
if(Key_Up==1) //加最大到9999
{
if(CNT!=9999)
{
CNT+=1;
Key_Up=0;
}
}
if(Key_Down==1) //减最小到0
{
if(CNT!=0)
{
CNT-=1;
Key_Down=0;
}
}
Process(CNT);
Display(Data);
}
}
文章评论(0条评论)
登录后参与讨论