Guest User

PWM.c

a guest
Jan 26th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.88 KB | None | 0 0
  1. /*
  2.  * pwm.c
  3.  *
  4.  *  Created on: 2 sty 2014
  5.  *      Author: Kamil
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <avr/interrupt.h>
  10. #include <avr/pgmspace.h>
  11.  
  12. #include "pwm.h"
  13.  
  14. // ------ gamma  GREEN = 2,5
  15. uint8_t gamma_correction[] PROGMEM = {
  16.   0,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,
  17.   1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  1,  2,  2,  2,  2,
  18.   2,  2,  2,  2,  3,  3,  3,  3,  3,  3,  4,  4,  4,  4,  5,  5,  5,  5,
  19.   5,  6,  6,  6,  7,  7,  7,  7,  8,  8,  8,  9,  9,  9, 10, 10, 10, 11,
  20.  11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19,
  21.  19, 20, 20, 21, 21, 22, 23, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29,
  22.  30, 31, 32, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43,
  23.  44, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61,
  24.  62, 63, 64, 65, 66, 67, 68, 69, 70, 72, 73, 74, 75, 76, 78, 79, 80, 81,
  25.  82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, 97, 99,100,101,103,104,106,
  26. 107,109,110,112,113,115,116,118,119,121,123,124,126,127,129,131,132,134,
  27. 136,138,139,141,143,145,146,148,150,152,154,155,157,159,161,163,165,167,
  28. 169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,202,204,
  29. 206,208,210,213,215,217,219,222,224,226,228,231,233,236,238,240,243,245,
  30. 248,250,253,255
  31. };
  32.  
  33. // ***PORTY
  34. #define PORT(x) SPORT(x)
  35. #define SPORT(x) (PORT##x)
  36. // ***PINY
  37. #define PIN(x) SPIN(x)
  38. #define SPIN(x) (PIN##x)
  39. // ***DDR
  40. #define DDR(x) SDDR(x)
  41. #define SDDR(x) (DDR##x)
  42.  
  43. #define GAMMA(a) (pgm_read_byte(&gamma_correction[a]))  // makrodefinicja korekcji gamma
  44.  
  45. void pwm_init( void ){
  46. // *******************KONFIGURACJA TIMERA**************************
  47.     TCCR2 |= (1<<WGM21);    // tryb CTC
  48.     TCCR2 |= (1<<CS20);     // preskaler 1
  49.     OCR2 = 200;             // podział przez 200
  50.     TIMSK |= (1<<OCIE2);    //zezwolenie na przerwanie ComapreMatch
  51. // ****************************************************************
  52.  
  53. #if PWM_CHANNEL > 0
  54.         DDR(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
  55. #endif
  56.  
  57. #if PWM_CHANNEL > 1
  58.         DDR(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
  59. #endif
  60.  
  61. #if PWM_CHANNEL > 2
  62.         DDR(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
  63. #endif
  64.  
  65. #if PWM_CHANNEL > 3
  66.         DDR(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
  67. #endif
  68.  
  69. #if PWM_CHANNEL > 4
  70.         DDR(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
  71. #endif
  72.  
  73. #if PWM_CHANNEL > 5
  74.         DDR(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
  75. #endif
  76.  
  77. #if PWM_CHANNEL > 6
  78.         DDR(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
  79. #endif
  80.  
  81. #if PWM_CHANNEL > 7
  82.         DDR(PWM7_PORT) |= (1<<PIN(PWM8_PIN));
  83. #endif
  84.  
  85.  
  86. #if PWM_TYPE == ANODA
  87.  
  88.  
  89. #if PWM_CHANNEL > 0
  90.         PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
  91. #endif
  92.  
  93. #if PWM_CHANNEL > 1
  94.         PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
  95. #endif
  96.  
  97. #if PWM_CHANNEL > 2
  98.         PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
  99. #endif
  100.  
  101. #if PWM_CHANNEL > 3
  102.         PORT(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
  103. #endif
  104.  
  105. #if PWM_CHANNEL > 4
  106.         PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
  107. #endif
  108.  
  109. #if PWM_CHANNEL > 5
  110.         PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
  111. #endif
  112.  
  113. #if PWM_CHANNEL > 6
  114.         PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
  115. #endif
  116.  
  117. #if PWM_CHANNEL > 7
  118.         PORT(PWM7_PORT) |= (1<<PIN(PWM8_PIN));
  119. #endif
  120.  
  121.  
  122. #else // jeli wspólna katoda
  123.  
  124.  
  125. #if PWM_CHANNEL > 0
  126.         PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
  127. #endif
  128.  
  129. #if PWM_CHANNEL > 1
  130.         PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
  131. #endif
  132.  
  133. #if PWM_CHANNEL > 2
  134.         PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
  135. #endif
  136.  
  137. #if PWM_CHANNEL > 3
  138.         PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
  139. #endif
  140.  
  141. #if PWM_CHANNEL > 4
  142.         PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
  143. #endif
  144.  
  145. #if PWM_CHANNEL > 5
  146.         PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
  147. #endif
  148.  
  149. #if PWM_CHANNEL > 6
  150.         PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
  151. #endif
  152.  
  153. #if PWM_CHANNEL > 7
  154.         PORT(PWM7_PORT) &= ~(1<<PIN(PWM8_PIN));
  155. #endif
  156.  
  157. #endif
  158. }
  159.  
  160. ISR( TIMER2_COMP_vect ){
  161.  
  162.     static uint8_t cnt;
  163.  
  164. #if PWM_TYPE == ANODA
  165.  
  166.     if(PWM_CHANNEL){
  167.         if(cnt>=GAMMA( pwm1 )) PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
  168.         else PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
  169.     }
  170.  
  171.     if(PWM_CHANNEL > 1){
  172.         if(cnt>=GAMMA( pwm2 )) PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
  173.         else PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
  174.     }
  175.  
  176.     if(PWM_CHANNEL > 2){
  177.         if(cnt>=pwm3) PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
  178.         else PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
  179.     }
  180.  
  181.     if(PWM_CHANNEL > 3){
  182.         if(cnt>=pwm4) PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
  183.         else PORT(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
  184.     }
  185.  
  186.     if(PWM_CHANNEL > 4){
  187.         if(cnt>=pwm5) PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
  188.         else PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
  189.     }
  190.  
  191.     if(PWM_CHANNEL > 5){
  192.         if(cnt>=pwm6) PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
  193.         else PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
  194.     }
  195.  
  196.     if(PWM_CHANNEL > 6){
  197.         if(cnt>=pwm7) PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
  198.         else PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
  199.     }
  200.  
  201.     if(PWM_CHANNEL >7){
  202.         if(cnt>=pwm8) PORT(PWM8_PORT) &= ~(1<<PIN(PWM8_PIN));
  203.         else PORT(PWM8_PORT) |= (1<<PIN(PWM8_PIN));
  204.     }
  205.  
  206. #else
  207.  
  208.     if(PWM_CHANNEL == 1){
  209.         if(cnt>=GAMMA( pwm1 )) PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
  210.         else PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
  211.     }
  212.  
  213.     if(PWM_CHANNEL == 2){
  214.         if(cnt>=pwm2) PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
  215.         else PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
  216.     }
  217.  
  218.     if(PWM_CHANNEL == 3){
  219.         if(cnt>=pwm3) PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
  220.         else PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
  221.     }
  222.  
  223.     if(PWM_CHANNEL == 4){
  224.         if(cnt>=pwm4) PORT(PWM4_PORT) &= (1<<PIN(PWM4_PIN));
  225.         else PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
  226.     }
  227.  
  228.     if(PWM_CHANNEL == 5){
  229.         if(cnt>=pwm5) PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
  230.         else PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
  231.     }
  232.  
  233.     if(PWM_CHANNEL == 6){
  234.         if(cnt>=pwm6) PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
  235.         else PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
  236.     }
  237.  
  238.     if(PWM_CHANNEL == 7){
  239.         if(cnt>=pwm7) PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
  240.         else PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
  241.     }
  242.  
  243.     if(PWM_CHANNEL == 8){
  244.         if(cnt>=pwm8) PORT(PWM8_PORT) |= (1<<PIN(PWM8_PIN));
  245.         else PORT(PWM8_PORT) &= ~(1<<PIN(PWM8_PIN));
  246.     }
  247.  
  248.  
  249. #endif
  250.     cnt++;
  251. }
Advertisement
Add Comment
Please, Sign In to add comment