Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- //define cpu clock speed if not defined
- #define F_CPU 1600000
- void init(void)
- {
- /* setup pwm outputs
- ATmega328p OC ATmega328p avr-gcc
- Timer Register 28-DIP Pin Port Pin
- --------------------------------------------------
- Timer 0 OC0A 12 PD6
- Timer 0 OC0B 11 PD5
- Timer 1 OC1A 15 PB1
- Timer 1 OC1B 16 PB2
- Timer 2 OC2A 17 PB3
- Timer 2 OC2B 5 PD3
- LED SETUP
- RED - OC1A (PB1) - Timer 1
- GRN - OC0B (PD5) - Timer 0
- BLU - OC0A (PD6) - Timer 0
- */
- DDRB = (1 << 1);
- DDRD = (1 << 5) | (1 << 6);
- //Set Initial Timer value
- TCNT0 = 0;
- TCNT1 = 0;
- //Place compare values to Output compare registers
- OCR0A = 20;
- OCR0B = 20;
- OCR1A = 20;
- //Set fast PWM mode
- //and make clear OC0A and set OC0B on compare match
- TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << COM0B0) | (1 << WGM01) | (1 << WGM00);
- TCCR1C |= (1 << COM1A1) | (1 << COM1B1) | (1 << COM1B0) | (1 << WGM11) | (1 << WGM10);
- }
- void startTimer0(void)
- {
- //Set prescaller 256 and start timer
- TCCR0B &= ~(0x07 << CS00);
- TCCR0B |= (1 << CS02) | (1 << CS00);// | (0 << CS01) | (0 << CS00);
- TCCR1B &= ~(0x07 << CS10);
- TCCR1B |= (1 << CS12) | (1 << CS10);// | (0 << CS01) | (0 << CS00);
- }
- int main(void)
- {
- init();
- startTimer0();
- // enter infinite loop
- while(1) {
- }
- // we'll never get here because of infinite loop
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement