运行本例时,按下K1可使直流电机正转,按下K2可使直流电机反转,按下K3时停止。
Proteus运行时截图:
Atmel Studio6.2 编译通过截图:
程序清单:
- /*
- * GccApplication35.c
- *
- * Created: 2014-12-16 20:52:59
- * Author: Administrator
- */
- #define F_CPU 4000000UL
- #include <util/delay.h>
- #include <avr/io.h>
- #include <stdint.h>
- #define K1_DOWN()((PIND & _BV(PD1))==0x00)
- #define K2_DOWN()((PIND& _BV(PD4))==0x00)
- #define K3_DOWN() ((PIND & _BV(PD7))==0x00)
- #define LED1_ON() (PORTC = 0B11111110)
- #define LED2_ON() (PORTC = 0B11111101)
- #define LED3_ON() (PORTC = 0B11111011)
- #define MA_0() (PORTB &= ~_BV(PB2))
- #define MA_1() (PORTB |= _BV(PB2))
- #define MB_0() (PORTB &= ~_BV(PB1))
- #define MB_1() (PORTB |= _BV(PB1))
- int main(void)
- {
- DDRB = 0xFF;PORTB = 0xFF;
- DDRC = 0xFF;PORTC = 0xFF;
- DDRD = 0x00;PORTD = 0xFF;
- LED3_ON();
- while(1)
- {
- if(K1_DOWN())
- {
- while(K1_DOWN());
- LED1_ON();MA_0();MB_1();
- }
- if(K2_DOWN())
- {
- while(K2_DOWN());
- LED2_ON(); MA_1();MB_0();
- }
- if(K3_DOWN())
- {
- while(K3_DOWN())
- LED3_ON();MA_0();MB_0();
- }
- }
- }
|