/****************************代码********************/
/***************/
时间:2014 年 5月19日
作者:fengjianxin
/***************/
#include<avr/io.h>//AVR通用头文件
#include<util/delay.h>//延时头文件
#define uint unsigned int//定义无符号整形
#define uchar unsigned char//定义8位无符号字符型如果要采用负号值这里改为 #define uchar char
#define PORT_B PORTB //拨号键端口
#define PORT_C PORTC //数码管段选端口
#define PORT_D PORTD //数码管位选端口
#define PI_NB PINB //拨号键 输入
const uchar Shuju[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6f};//数字数组
uchar HuanChong[3];//缓冲数组,一般对字符转十进制 很方便 不用太多公式转换
void Init()//初始化
{
DDRB = 0x00;
PORT_B = 0xff;
DDRC = 0xff;
PORT_C = 0x00;
DDRD = 0xff;
PORT_D = 0xff;
}
int main()
{
Init();
while(1)
{
uchar Num,i;
Num = PI_NB;
HuanChong[2] = (Num/100)?Num/100:0;//拆分字符型数据 百位
HuanChong[1] = Num%100/10;//十位
HuanChong[0] = Num%10;//个位
for(i = 0;i < 3;i ++)
{
PORT_D |= ~_BV(i+1) & 0xfe;//打开数码管位选
PORT_C = ~Shuju[HuanChong[2-i]];//向数码管段选送数据
_delay_ms(1);//延时一会 给硬件点时间
PORT_D &= ~_BV(i+1) & 0xfe;//关闭数码管位选
_delay_ms(1);//延时一会 for里面的 延时很重要 可以改的试试 在仿真上看看效果
}
}
}