C Program Dimmer Example

How to write a C Program Dimmer Example in C Programming Language ?


Solution:

  1. #define F_CPU 8000000UL
  2. #include <avr/io.h>
  3. #include <util/delay.h>
  4. #include <avr/interrupt.h>
  5. volatile unsigned char h=20;
  6.  
  7.  
  8. ISR(TIMER0_COMP_vect) {
  9.     TCCR0=0b00001000;
  10.     PORTD|=(1<<PD5);
  11.    _delay_us(10);
  12.     PORTD&=~(1<<PD5);
  13.     TIMSK=0;
  14.     TCNT0=0;
  15.  
  16.    
  17.    
  18. }
  19. ISR(_VECTOR(1))
  20. {
  21.     OCR0=h;
  22.     TCCR0=0b00001101;
  23.     TIMSK=0b00000010;
  24. }
  25.  
  26. int main(void)
  27. {
  28.     //DECLARACION DE ENTRADAS Y SALIDAS
  29.     DDRD=0b00100001;
  30.     PORTD=0b11101110;
  31.     //Declaración de interrupciones
  32.     MCUCR=0b00000001;
  33.     //Enable external interrupt 0
  34.     GICR=0b01000000;
  35.     asm("SEI");
  36.     while(1)
  37.     {
  38.         //SUMA
  39.         if(!(PIND&(1<<PIND1)))                   // Está en cero el PIN 7 del puerto D????
  40.         {
  41.             _delay_ms(10);                       //Espero 10ms para ver si no es ruido
  42.             if(!(PIND&(1<<PIND1)))             // Está todavía en cero el PIN 7 del puerto D????
  43.             {
  44.                
  45.                 h=h+2;
  46.                 if(h>60)  
  47.                 {
  48.                     h=60;
  49.                 }
  50.                 while(!(PIND&(1<<PIND1)))
  51.                 {
  52.                    
  53.                 }
  54.                
  55.             }
  56.            
  57.         }
  58.         //resta
  59.         if(!(PIND&(1<<PIND3)))
  60.         {
  61.             _delay_ms(10);                       //Espero 10ms para ver si no es ruido
  62.             if(!(PIND&(1<<PIND3)))             // Está todavía en cero el PIN 7 del puerto A????
  63.             {
  64.                 h=h-2;
  65.                 if(h<4)
  66.                 {
  67.                     h=4;  
  68.                 }
  69.                  while(!(PIND&(1<<PIND3)))
  70.                  {
  71.                      
  72.                  }
  73.                
  74.                
  75.             }
  76.            
  77.         }
  78.            
  79.     }
  80.    
  81.    
  82. }


Learn More :