以下是动态数码管的电路图。
代码:动态数码管。
/* *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] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07}; //the main function void main() { //init your system initSystem(); while(1) { //start your function,and this is core solution for(flag=0;flag<8;flag++) { PORTC = ~(PORTC | BIT(0)); PORTA = table[flag]; PORTB = ~BIT(flag); PORTC = PORTC | BIT(0); delay(); } } } //the method of init system void initSystem() { //control PA DDRA = 0xFF; PORTA = table[0]; //to make PB port output DDRB = 0xFF; //to make PB port output high level PORTB = 0xFE; //control PC0 DDRC = DDRC | BIT(0); PORTC = PORTC | BIT(0); } //the sub method of delay void delay() { uint i,j; for(i=0;i<10;i++) for(j=0;j<5;j++); }
不断改变delay()函数中变量的值,会出现不一样的效果。主要是熟悉AVR单片机IO口的使用。
时间: 2024-11-04 09:18:33