#include<iom128v.h>
unsigned char const Dsy_index[]=
{
0x01,0x02,0x40,0x80 //位选信号
};
unsigned char Dsy_buf[]=
{
16,16,16 //数据缓区,初始化时显示“- ”
};
unsigned char const Dsy_code_cn[]=
{
0x3f,0x06,0x5b,0x4f,0X66,0x6d,0x7d,0x07,0x7f,
0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0X40,0X00//共阴极0.1.2.7.4.5.6.7.8.9.A.b.C.d.E.F.-
};
unsigned char Key_code; //存放拨码开关的值
void delay(void) //延时函数
{
unsigned int i;
for(i=1;i<100;i++);
}
void delay_1ms(void) //延时1ms
{
unsigned int i;
for(i=1;i<(unsigned int)(8*143-2);i++);
}
void delay_ms(unsigned int time)//毫秒延时函数
{
unsigned int i=0;
while(i<time)
{
delay_1ms();
i++;
}
}
void Port_init(void)
{
PORTA = 0x00;
DDRA = 0xFF; //数码管的数据端口
PORTB = 0xFF;
DDRB = 0x00; //拨码开关输入端口
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xFF; //数据管的位选择端口
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
PORTG = 0x00;
DDRG = 0x00;
}
unsigned char Key_scan() //读取拨码开关状态
{
PORTB = 0xff; //上拉电阻
if(PINB!=Key_code)
{
delay_ms(10); //延时去抖动
if(PINB!=Key_code)
{
Key_code=PINB;
}
}
return Key_code;
}
void Num_dsy(unsigned char num)
{
unsigned char i,j;
Dsy_buf[0]=num/100; //求出百位
Dsy_buf[1]=num/10%10; //求出十位
Dsy_buf[2]=num%10; //求出个位
for(i=0;i<3;i++)
{
PORTA=Dsy_code_cn[Dsy_buf[i]]; //输出数据
PORTD=~Dsy_index[3-i]; //选出对应的行
delay_ms(5);
}
}
void main(void)
{
Port_init();
// PORTC=0xef;
while(1)
{
Num_dsy(Key_scan()); //显示拨码开关状态
}
}