Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. /*
  2. uprava 6.2.2016
  3. */
  4.  
  5. #define F_CPU 16000000UL //clock speed 16MHz
  6.  
  7. #include <avr/io.h>
  8. #include <util/delay.h>
  9. #include <avr/interrupt.h>
  10. #include <avr/io.h>
  11.  
  12.  
  13. void delay(int t)
  14. {
  15.     static int elapsedDelay=0;
  16.    
  17.    for(int j=elapsedDelay;j<t;j++)
  18.    {
  19.       if ((bit_is_clear(PINC, 0)) || (bit_is_clear(PINC, 1)) || (bit_is_clear(PINC, 2)) || (bit_is_clear(PINC, 3)) || (bit_is_clear(PINC, 4)) || (bit_is_clear(PINC, 5)))
  20.       {
  21.        elapsedDelay = j;
  22.       return;
  23.       }
  24.       else
  25.       {
  26.       elapsedDelay=0;
  27.       }
  28.       _delay_ms(1);
  29.    }
  30. }
  31.  
  32.  
  33.  
  34. volatile int tot_overflow;  //rozdiel medzi uint8_t a int je, ze int ma o 24 bytov viac
  35.  
  36.  
  37.  
  38.  
  39. int main(void)
  40. {
  41. /* na zaciatku sa musime vzdy rozhodnut, ci budu vystupne registre DDRx ako output alebo input */
  42.  
  43.  
  44. DDRC &= ~(1 << PINC0);  //  -> input (0) - tlacitko 1
  45. PORTC |= 1 << PINC0;        //  -> high level  (1)  - tlacitko 1 je na high urovni
  46.  
  47. DDRC &= ~(1 << PINC1);  //  -> input (0) - tlacitko 2
  48. PORTC |= 1 << PINC1;        //  -> high level  (1)  - tlacitko 2 je na high urovni
  49.  
  50. DDRC &= ~(1 << PINC2);  //  -> input (0) - tlacitko 3
  51. PORTC |= 1 << PINC2;        //  -> high level  (1)  - tlacitko 3 je na high urovni
  52.  
  53. DDRC &= ~(1 << PINC3);  //  -> input (0) - tlacitko 4
  54. PORTC |= 1 << PINC3;        //  -> high level  (1)  - tlacitko 4 je na high urovni
  55.  
  56. DDRC &= ~(1 << PINC4);  //  -> input (0) - tlacitko 5
  57. PORTC |= 1 << PINC4;        //  -> high level  (1)  - tlacitko 5 je na high urovni
  58.  
  59. DDRC &= ~(1 << PINC5);  //  -> input (0) - tlacitko 6
  60. PORTC |= 1 << PINC5;        //  -> high level  (1)  - tlacitko 6 je na high urovni
  61.  
  62.  
  63.  
  64. int Stlacene_off_0percent   = 0;
  65. int Stlacene_100percent     = 0;
  66. int Stlacene_66percent      = 0;
  67. int Stlacene_33percent      = 0;
  68. int Stlacene_pulse          = 0;
  69. int Stlacene_blik           = 0;
  70.  
  71.  
  72. int tlacitko = 0;
  73. int i=0;
  74.  
  75.  
  76.  
  77. DDRD |= 0xFF; //vsetky piny na porte D ako vystup
  78.  
  79.   // prescaler = 256
  80. TCCR2 |= (1 << CS22)|(1 << CS21);
  81.  
  82. // initialize counter
  83. TCNT2 = 0;
  84.  
  85. // enable overflow interrupt
  86. TIMSK |= (1 << TOIE2);
  87.  
  88. // enable global interrupts
  89. sei();
  90.  
  91. // initialize overflow counter variable
  92. tot_overflow = 0;
  93.  
  94.  
  95. TCCR1A |= 1<<WGM11 | 1<<COM1A1 | 1<<COM1A0;                 //Set OC1A/OC1B on compare match, clear OC1A/OC1B at BOTTOM, (inverting mode), PWM, Phase Correct, 9-bit
  96.  
  97. TCCR1B |= 1<<WGM12 | 1<<WGM13 | 1<<CS10; //| 1<<CS11;       //cs10 ON -> prescaler off  cs10, cs11 ON -> prescaler 64, clear on timer compare, top value ICR1
  98.  
  99. ICR1 = 1000;                                                //maximalna hodnota z 65535 ... (16MHz/64) /1000= 250Hz co je nasa PWM frekvencia   fpwm=fclkIO/(N*(1+TOP))
  100.  
  101.  
  102.  
  103. while (1)
  104.     {
  105.  
  106.                 /* PWM 0% */
  107.         if (bit_is_clear(PINC, 0))
  108.             {
  109.  
  110.                 if (Stlacene_off_0percent == 0)
  111.                 {
  112.                 tlacitko = 1;
  113.                 Stlacene_off_0percent = 1;
  114.                 }
  115.             }
  116.            
  117.         else
  118.         {
  119.         Stlacene_off_0percent = 0;
  120.         }
  121.            
  122.                 /* PWM 100% */
  123.         if (bit_is_clear(PINC, 1))
  124.             {
  125.  
  126.                 if (Stlacene_100percent == 0)
  127.                 {
  128.                     tlacitko = 2;
  129.                     Stlacene_100percent = 1;
  130.                 }
  131.             }
  132.         else
  133.         {
  134.         Stlacene_100percent = 0;
  135.         }
  136.            
  137.            
  138.                 /* PWM 66% */
  139.         if (bit_is_clear(PINC, 2))
  140.             {
  141.  
  142.                 if (Stlacene_66percent  == 0)
  143.                 {
  144.                     tlacitko = 3;
  145.                     Stlacene_66percent  = 1;
  146.                 }
  147.             }
  148.         else
  149.         {
  150.         Stlacene_66percent  = 0;
  151.         }
  152.  
  153.                 /* PWM 33% */
  154.         if (bit_is_clear(PINC, 3))
  155.             {
  156.  
  157.                 if (Stlacene_33percent  == 0)
  158.                 {
  159.                 tlacitko = 4;
  160.                 Stlacene_33percent  = 1;
  161.                 }
  162.             }
  163.         else
  164.         {
  165.         Stlacene_33percent  = 0;
  166.         }
  167.            
  168.                 /* pulse na PINC4 tlacitku*/
  169.         if (bit_is_clear(PINC, 4))
  170.             {
  171.                 if (Stlacene_pulse   == 0)
  172.                 {
  173.                 tlacitko = 5;
  174.                 Stlacene_pulse   = 0;
  175.                 }
  176.             }
  177.            
  178.         else
  179.         {
  180.         Stlacene_pulse   = 0;
  181.         }
  182.            
  183.                 /* BLIK */
  184.         if (bit_is_clear(PINC, 5))
  185.             {
  186.  
  187.                 if (Stlacene_blik  == 0)
  188.                 {
  189.                 tlacitko = 6;
  190.                 Stlacene_blik  = 1;
  191.                 }
  192.             }
  193.         else
  194.         {
  195.         Stlacene_blik  = 0;
  196.         }
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.    
  204.         switch (tlacitko)
  205.         {
  206.             case 1:                     //turn off
  207.             OCR1A=ICR1;
  208.             break;
  209.             case 2:                     //100%
  210.             OCR1A=ICR1*0;
  211.             break;
  212.             case 3:                     //66%
  213.             OCR1A=ICR1*0.66;
  214.             break;
  215.             case 4:                     //33%
  216.             OCR1A=ICR1*0.33;
  217.             break;             
  218.             case 5:                     //pulse
  219.  
  220.  
  221.  
  222.                     for(i=0;i<50;i++)
  223.                     {
  224.                         OCR1A = ICR1 - i*20;
  225.                         delay(20);
  226.                        
  227.                     }
  228.                
  229.                
  230.                     for(i=50;i>=0;i--)
  231.                     {  
  232.                         OCR1A = ICR1 - i*20;
  233.                         delay(20);
  234.                     }          
  235.                     break;
  236.                    
  237.             case 6:                     //blik
  238.                
  239.  
  240.                       if (tot_overflow >= 24)
  241.                         {
  242.                                 OCR1A ^= (1 << ICR1);   // toggles the led, to iste ako PORTB ^= (1 << 0);
  243.                                 TCNT2 = 0;            // reset counter
  244.                                 tot_overflow = 0;     // reset overflow counter
  245.                         }                      
  246.  
  247.                
  248.             break;
  249.            
  250.             default:
  251.             OCR1A=ICR1;
  252.         }
  253.     }
  254. }
  255.  
  256. ISR(TIMER2_OVF_vect)
  257. {
  258.     // keep a track of number of overflows
  259.     tot_overflow++;
  260. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement