本例中T/C1与 OC1A引角连接,所生成的率频由该引角输出。 
 
本例通过4个按键分别调节频率值的千位、百位、十位、个位,通过示波可观察不同的频率。 
 
Proteus截图: 
 
Studio6.2编译通过截图: 
 
程序清单: 
- /*
 
 -  * GccApplication9.c
 
 -  *
 
 -  * Created: 2014-11-4 20:37:39
 
 -  *  Author: Administrator
 
 -  */ 
 
  
- #define F_CPU 1000000UL
 
 - #include <avr/io.h>
 
 - #include <util/delay.h>
 
 - #include <stdint.h>
 
  
- #define  K1 (uint8_t)(~_BV(PC0))
 
 - #define  K2 (uint8_t)(~_BV(PC2))
 
 - #define  K3 (uint8_t)(~_BV(PC4))
 
 - #define  K4 (uint8_t)(~_BV(PC6))
 
  
- const uint8_t SEG_CODE[] = {0x3f,0x06,0x58,0x4f,0x66,0x6d,0x7d,0x7d,0x07,0x7f,0x6f};
 
 - uint8_t FRQ_DATA[] = {0,1,0,0};
 
 - uint8_t Key_State;
 
  
- void Show_FRQ_ON_DSY()
 
 - {
 
 -         uint8_t i = 0;
 
 -         for(i=0;i<4;i++)
 
 -         {
 
 -                 PORTB = ~_BV(i);
 
 -                 PORTA = SEG_CODE[FRQ_DATA[i]];
 
 -                 if(i==0) PORTA |= 0x80;
 
 -                 _delay_ms(2);
 
 -         }
 
 - }
 
  
- void Set_Frequency()
 
 - {
 
 -         uint16_t f;
 
 -         f=FRQ_DATA[0]*1000+FRQ_DATA[1]*100+FRQ_DATA[2]*10+FRQ_DATA[3];
 
 -         OCR1A = F_CPU/2.0/f;
 
 - }
 
 - int main(void)
 
 - {
 
 -     uint8_t i=0;Key_State = 0xFF;
 
 -         DDRA = 0xFF; PORTA = 0xFF;
 
 -         DDRB = 0xFF; PORTB = 0xFF;
 
 -         DDRD = 0xFF; PORTD = 0xFF;
 
 -         DDRC = 0x00; PORTC = 0xFF;
 
 -         TCCR1A = 0x40;
 
 -         TCCR1B = 0x09;
 
 -         TCNT1 = 0;
 
 -         Set_Frequency();
 
 -         
 
 -         while(1)
 
 -     {
 
 -         if(PINC^Key_State)
 
 -                 {
 
 -                     Key_State = PINC;
 
 -                         if(Key_State != 0xFF)
 
 -                         {
 
 -                                 switch(Key_State)
 
 -                                 {
 
 -                                         case K1:i=0;break;
 
 -                                         case K2:i=1;break;
 
 -                                         case K3:i=2;break;
 
 -                                         case K4:i=3;break;
 
 -                                 }
 
 -                                 FRQ_DATA[i] = (FRQ_DATA[i]+1)%10;
 
 -                                 Set_Frequency();
 
 -                         }
 
 -                 }
 
 -                 Show_FRQ_ON_DSY();
 
 -     }
 
 - }
 
  
 
 
 
  |