这个程序就是按下一个键就发一种声音, 以下是仿真:
以下是Studio6.2编译通过截图:
以下是程序:
- /*
- * GccApplication18.c
- *
- * Created: 2014-10-14 20:30:19
- * Author: Administrator
- */
- #define __DELAY_BACKWARD_COMPATIBLE__
- #define F_CPU 0x8000000UL
- #include <avr/io.h>
- #include <util/delay.h>
- #include <stdint.h>
- #define BEEP() (PORTD ^= 0x80)
- #define K1_DOWN() ((PINB & 0x10)== 0x00)
- #define K2_DOWN() ((PINB & 0x20)== 0x00)
- #define K3_DOWN() ((PINB & 0x40)== 0x00)
- #define K4_DOWN() ((PINB & 0x80)== 0x00)
- void Play(uint8_t ddd)
- {
- uint8_t i;
- for(i=0;i<100;i++)
- {
- BEEP();_delay_ms(ddd);
- }
- }
- int main(void)
- {
- uint8_t Pre_key = 0xFF;
- DDRB = 0x00;PORTB = 0xFF;
- DDRD = 0xFF;
- while(1)
- {
- while(Pre_key == PINB);
- Pre_key = PINB;
- if(K1_DOWN()) Play(1);
- if(K2_DOWN()) Play(2);
- if(K3_DOWN()) Play(3);
- if(K4_DOWN()) Play(4);
-
- }
-
- }
|