本帖最后由 ddllxxrr 于 2014-10-9 20:53 编辑
本程序通过四个按键来控制两排LED的显示,每次只有一个灯亮。
分别控制着第一排的上下和第二排水的上下。
程序如下:
- /*
- * GccApplication14.c
- *
- * Created: 2014-10-9 20:27:09
- * Author: Administrator
- */
- #define F_CPU 8000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdint.h>
- int8_t i=0,j=0;
- void Move_LED()
- {
- if((PINB &0x01)==0x00) i= (i-1) & 0x07;
- else if ((PINB & 0x02)==0x00) i=(i+1) &0x07;
- else if ((PINB & 0x04)==0x00) j = (j-1)&0x07;
- else if ((PINB & 0x08)==0x00) j = (j+1)&0x07;
-
- PORTC = ~(1<<i);
- PORTD = ~(1<<j);
- }
- int main(void)
- {
- int8_t Recent_Key = 0x00;
- DDRB = 0x00;PORTB=0xFF;
- DDRC = 0xFF;PORTC=0xFE;
- DDRD = 0xFF;PORTD=0xFE;
- while(1)
- {
- if(PINB!= Recent_Key)
- {
- Recent_Key = PINB;
- Move_LED();
- _delay_ms(10);
- }
- }
- }
仿真截图:
编译通过截图:
|