这一节我们将讲解数码管的使用,由于原理在学51单片机的时候已经学习过,在这里只需要利用前面学习的对AVR单片机的IO口操作方式就可以自己写练习题了。
下面的数码管是共阳极数码管。
代码:显示从0到9。
/*
*author:ChenLu
*date:2014.11.20
*/
//input the head file so that the program can work normally
//iom16v---know the register
//macros---know the BIT(x)
#include<iom16v.h>
#include<macros.h>
//use those can make your study very conveninet
#define uint unsigned int
#define uchar unsigned char
//display methods
void initSystem();
void delay();
//display the variable data
uchar flag;
uchar table[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
//the main function
void main()
{
//init your system
initSystem();
while(1)
{
//start your function,and this is core solution
for(flag=0;flag<10;flag++)
{
<span style="white-space:pre"> </span>PORTB = table[flag];
delay();
delay();
delay();
delay();
delay();
delay();
delay();
}
}
}
//the method of init system
void initSystem()
{
//to make PB port output
DDRB = 0xFF;
//to make PB port output high level
PORTB = 0xFF;
}
//the sub method of delay
void delay()
{
uchar i,j;
for(i=0;i<200;i++)
for(j=0;j<200;j++);
}初学者可以做做其他的例子来练习。
原文地址:http://blog.csdn.net/kotei_88_luluc_66/article/details/41311167