单片机设计流水灯,该怎么解决
单片机设计流水灯
刚刚学习单片机,老师留得作业使用ATmega16设计流水灯,A B C D 四口从低位向高位依次点亮,然后从高位向低位依次熄灭。如果只用一个口 PORTA 我会,但是不知道怎样写四个的
------解决方案--------------------
给你下代码吧,
刚刚学习单片机,老师留得作业使用ATmega16设计流水灯,A B C D 四口从低位向高位依次点亮,然后从高位向低位依次熄灭。如果只用一个口 PORTA 我会,但是不知道怎样写四个的
------解决方案--------------------
给你下代码吧,
- C/C++ code
#include <iom16v.h> #include <macros.h> void port_init(void) { PORTA = 0x00; DDRA = 0x00; PORTB = 0x00; DDRB = 0x02; PORTC = 0x00; //m103 output only DDRC = 0x00; PORTD = 0x00; DDRD = 0x00; } //call this routine to initialize all peripherals void init_devices(void) { //stop errant interrupts until set up CLI(); //disable all interrupts port_init(); MCUCR = 0x00; GICR = 0x00; TIMSK = 0x00; //timer interrupt sources SEI(); //re-enable interrupts //all peripherals are now initialized }
------解决方案--------------------
具体主函数楼主参考我的代码自己修改下吧:
- C/C++ code
#include <iom16v.h> #define uchar unsigned char #define uint unsigned int void delay(uint ticks); void main() { init_devices(); while(1) { PORTB^=0x02;//把IO口取反 delay(5000); } } void delay(uint ticks) { uchar i; while(ticks--)for(i=100;i!=0;i--);//约0.1mS }