Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.65 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4.  
  5. //define cpu clock speed if not defined  
  6. #define F_CPU 1600000
  7.  
  8.  
  9. void init(void)
  10. {
  11.     /* setup pwm outputs
  12.      
  13.      ATmega328p       OC        ATmega328p     avr-gcc
  14.      Timer        Register    28-DIP Pin     Port Pin
  15.      --------------------------------------------------
  16.      Timer 0        OC0A           12           PD6
  17.      Timer 0        OC0B           11           PD5
  18.      
  19.      Timer 1        OC1A           15           PB1
  20.      Timer 1        OC1B           16           PB2
  21.      
  22.      Timer 2        OC2A           17           PB3
  23.      Timer 2        OC2B            5           PD3
  24.      
  25.      LED SETUP
  26.      RED - OC1A (PB1) - Timer 1
  27.      GRN - OC0B (PD5) - Timer 0
  28.      BLU - OC0A (PD6) - Timer 0
  29.      
  30.      */
  31.     DDRB = (1 << 1);
  32.     DDRD = (1 << 5) | (1 << 6);
  33.        
  34.     //Set Initial Timer value
  35.     TCNT0 = 0;
  36.     TCNT1 = 0;
  37.    
  38.     //Place compare values to Output compare registers
  39.     OCR0A = 20;
  40.     OCR0B = 20;
  41.     OCR1A = 20;
  42.    
  43.     //Set fast PWM mode
  44.     //and make clear OC0A and set OC0B on compare match
  45.     TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << COM0B0) | (1 << WGM01) | (1 << WGM00);
  46.     TCCR1C |= (1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10);
  47.    
  48. }
  49.  
  50. void startTimer0(void)
  51. {
  52.     //Set prescaller 256 and start timer
  53.     TCCR0B &= ~(0x07 << CS00);
  54.     TCCR0B |= (1 << CS02) | (1 << CS00);// | (0 << CS01) | (0 << CS00);
  55.    
  56.     TCCR1B &= ~(0x07 << CS10);
  57.     TCCR1B |= (1 << CS12) | (1 << CS10);// | (0 << CS01) | (0 << CS00);
  58.  
  59. }
  60.  
  61. int main(void)
  62. {
  63.     init();
  64.     startTimer0();
  65.     // enter infinite loop
  66.     while(1) {
  67.        
  68.     }  
  69.     // we'll never get here because of infinite loop
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement