Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define F_CPU 1000000 // The chip runs at 1 MHz as default (even if you are using a 8MHz crystal), this line is necessary for delay function
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- //Timer 0 initialisation
- void init_timer0 ()
- {
- //Set registry bits for timer
- TCCR0B = 0b00000001; //Set up timer 0 with no prescaler
- TCNT0 = 0b00000000; //Initialise counter
- }
- int main()
- {
- //Variables
- char ch_R_on_dur = 1; //Number of counts channel R should remain on for
- char ch_R_off_dur = 254; //Number of counts channel R should remain off for
- char ch_R_count = 0; //Counter tracking amount of time channel R has remained on/off
- char ch_G_on_dur = 0; //Number of counts channel G should remain on for
- char ch_G_off_dur = 0; //Number of counts channel G should remain off for
- char ch_G_count = 0; //Counter tracking amount of time channel G has remained on/off
- char ch_B_on_dur = 0; //Number of counts channel B should remain on for
- char ch_B_off_dur = 0; //Number of counts channel B should remain off for
- char ch_B_count = 0; //Counter tracking amount of time channel B has remained on/off
- char ch_W_on_dur = 0; //Number of counts channel W should remain on for
- char ch_W_off_dur = 0; //Number of counts channel W should remain off for
- char ch_W_count = 0; //Counter tracking amount of time channel W has remained on/off
- //Initialise Timer
- init_timer0();
- //Initialise LED output
- DDRC = 0b10000000; //Sets the direction of the PC7 to output
- PORTC |= (1<<PC7); //Sets PC7 high
- //Keep LED on at 100% for 2 seconds to show difference in output when PWM starts
- _delay_ms(2000);
- while(1)
- {
- //Update counters
- //If timer has ticked, update counters
- if (TCNT0 >= 1)
- {
- //Increment counters
- ch_R_count++;
- //Increment Green counter
- //Increment Blue counter
- //Increment White counter
- //Reset timer
- TCNT0 = 0;
- }
- //Cycle red led on and off at high speed
- //If channel R has been off for max duration
- if (PORTC == 0b00000000 && ch_R_count >= ch_R_off_dur)
- {
- //Turn channel R on
- PORTC |= (1<<PC7);
- //Reset channel R overflow counter
- ch_R_count = 0;
- } else {
- //If channel R has been on for max duration
- if (PORTC == 0b10000000 && ch_R_count >= ch_R_on_dur)
- {
- //Turn channel R off
- PORTC &= ~(1<<PC7);
- //Reset channel R overflow counter
- ch_R_count = 0;
- }}
- //Cycle Green LED
- //Cycle Blue LED
- //Cycle White LED
- }
- //End program
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement