Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * pwm.c
- *
- * Created on: 2 sty 2014
- * Author: Kamil
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <avr/pgmspace.h>
- #include "pwm.h"
- // ------ gamma GREEN = 2,5
- uint8_t gamma_correction[] PROGMEM = {
- 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2,
- 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5,
- 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11,
- 11, 11, 12, 12, 13, 13, 13, 14, 14, 15, 15, 16, 16, 17, 17, 18, 18, 19,
- 19, 20, 20, 21, 21, 22, 23, 23, 24, 24, 25, 26, 26, 27, 27, 28, 29, 29,
- 30, 31, 32, 32, 33, 34, 34, 35, 36, 37, 38, 38, 39, 40, 41, 42, 42, 43,
- 44, 45, 46, 47, 48, 49, 50, 51, 51, 52, 53, 54, 55, 56, 57, 58, 59, 61,
- 62, 63, 64, 65, 66, 67, 68, 69, 70, 72, 73, 74, 75, 76, 78, 79, 80, 81,
- 82, 84, 85, 86, 88, 89, 90, 92, 93, 94, 96, 97, 99,100,101,103,104,106,
- 107,109,110,112,113,115,116,118,119,121,123,124,126,127,129,131,132,134,
- 136,138,139,141,143,145,146,148,150,152,154,155,157,159,161,163,165,167,
- 169,171,173,175,177,179,181,183,185,187,189,191,193,195,197,199,202,204,
- 206,208,210,213,215,217,219,222,224,226,228,231,233,236,238,240,243,245,
- 248,250,253,255
- };
- // ***PORTY
- #define PORT(x) SPORT(x)
- #define SPORT(x) (PORT##x)
- // ***PINY
- #define PIN(x) SPIN(x)
- #define SPIN(x) (PIN##x)
- // ***DDR
- #define DDR(x) SDDR(x)
- #define SDDR(x) (DDR##x)
- #define GAMMA(a) (pgm_read_byte(&gamma_correction[a])) // makrodefinicja korekcji gamma
- void pwm_init( void ){
- // *******************KONFIGURACJA TIMERA**************************
- TCCR2 |= (1<<WGM21); // tryb CTC
- TCCR2 |= (1<<CS20); // preskaler 1
- OCR2 = 200; // podział przez 200
- TIMSK |= (1<<OCIE2); //zezwolenie na przerwanie ComapreMatch
- // ****************************************************************
- #if PWM_CHANNEL > 0
- DDR(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
- #endif
- #if PWM_CHANNEL > 1
- DDR(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
- #endif
- #if PWM_CHANNEL > 2
- DDR(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
- #endif
- #if PWM_CHANNEL > 3
- DDR(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
- #endif
- #if PWM_CHANNEL > 4
- DDR(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
- #endif
- #if PWM_CHANNEL > 5
- DDR(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
- #endif
- #if PWM_CHANNEL > 6
- DDR(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
- #endif
- #if PWM_CHANNEL > 7
- DDR(PWM7_PORT) |= (1<<PIN(PWM8_PIN));
- #endif
- #if PWM_TYPE == ANODA
- #if PWM_CHANNEL > 0
- PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
- #endif
- #if PWM_CHANNEL > 1
- PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
- #endif
- #if PWM_CHANNEL > 2
- PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
- #endif
- #if PWM_CHANNEL > 3
- PORT(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
- #endif
- #if PWM_CHANNEL > 4
- PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
- #endif
- #if PWM_CHANNEL > 5
- PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
- #endif
- #if PWM_CHANNEL > 6
- PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
- #endif
- #if PWM_CHANNEL > 7
- PORT(PWM7_PORT) |= (1<<PIN(PWM8_PIN));
- #endif
- #else // jeli wspólna katoda
- #if PWM_CHANNEL > 0
- PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
- #endif
- #if PWM_CHANNEL > 1
- PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
- #endif
- #if PWM_CHANNEL > 2
- PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
- #endif
- #if PWM_CHANNEL > 3
- PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
- #endif
- #if PWM_CHANNEL > 4
- PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
- #endif
- #if PWM_CHANNEL > 5
- PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
- #endif
- #if PWM_CHANNEL > 6
- PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
- #endif
- #if PWM_CHANNEL > 7
- PORT(PWM7_PORT) &= ~(1<<PIN(PWM8_PIN));
- #endif
- #endif
- }
- ISR( TIMER2_COMP_vect ){
- static uint8_t cnt;
- #if PWM_TYPE == ANODA
- if(PWM_CHANNEL){
- if(cnt>=GAMMA( pwm1 )) PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
- else PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
- }
- if(PWM_CHANNEL > 1){
- if(cnt>=GAMMA( pwm2 )) PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
- else PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
- }
- if(PWM_CHANNEL > 2){
- if(cnt>=pwm3) PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
- else PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
- }
- if(PWM_CHANNEL > 3){
- if(cnt>=pwm4) PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
- else PORT(PWM4_PORT) |= (1<<PIN(PWM4_PIN));
- }
- if(PWM_CHANNEL > 4){
- if(cnt>=pwm5) PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
- else PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
- }
- if(PWM_CHANNEL > 5){
- if(cnt>=pwm6) PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
- else PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
- }
- if(PWM_CHANNEL > 6){
- if(cnt>=pwm7) PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
- else PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
- }
- if(PWM_CHANNEL >7){
- if(cnt>=pwm8) PORT(PWM8_PORT) &= ~(1<<PIN(PWM8_PIN));
- else PORT(PWM8_PORT) |= (1<<PIN(PWM8_PIN));
- }
- #else
- if(PWM_CHANNEL == 1){
- if(cnt>=GAMMA( pwm1 )) PORT(PWM1_PORT) |= (1<<PIN(PWM1_PIN));
- else PORT(PWM1_PORT) &= ~(1<<PIN(PWM1_PIN));
- }
- if(PWM_CHANNEL == 2){
- if(cnt>=pwm2) PORT(PWM2_PORT) |= (1<<PIN(PWM2_PIN));
- else PORT(PWM2_PORT) &= ~(1<<PIN(PWM2_PIN));
- }
- if(PWM_CHANNEL == 3){
- if(cnt>=pwm3) PORT(PWM3_PORT) |= (1<<PIN(PWM3_PIN));
- else PORT(PWM3_PORT) &= ~(1<<PIN(PWM3_PIN));
- }
- if(PWM_CHANNEL == 4){
- if(cnt>=pwm4) PORT(PWM4_PORT) &= (1<<PIN(PWM4_PIN));
- else PORT(PWM4_PORT) &= ~(1<<PIN(PWM4_PIN));
- }
- if(PWM_CHANNEL == 5){
- if(cnt>=pwm5) PORT(PWM5_PORT) |= (1<<PIN(PWM5_PIN));
- else PORT(PWM5_PORT) &= ~(1<<PIN(PWM5_PIN));
- }
- if(PWM_CHANNEL == 6){
- if(cnt>=pwm6) PORT(PWM6_PORT) |= (1<<PIN(PWM6_PIN));
- else PORT(PWM6_PORT) &= ~(1<<PIN(PWM6_PIN));
- }
- if(PWM_CHANNEL == 7){
- if(cnt>=pwm7) PORT(PWM7_PORT) |= (1<<PIN(PWM7_PIN));
- else PORT(PWM7_PORT) &= ~(1<<PIN(PWM7_PIN));
- }
- if(PWM_CHANNEL == 8){
- if(cnt>=pwm8) PORT(PWM8_PORT) |= (1<<PIN(PWM8_PIN));
- else PORT(PWM8_PORT) &= ~(1<<PIN(PWM8_PIN));
- }
- #endif
- cnt++;
- }
Advertisement
Add Comment
Please, Sign In to add comment