daily pastebin goal
2%
SHARE
TWEET

PWM Test

a guest Mar 21st, 2015 22 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #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
  2.  
  3. #include <avr/io.h>
  4. #include <avr/interrupt.h>
  5. #include <util/delay.h>
  6.  
  7. //Timer 0 initialisation
  8. void init_timer0 ()
  9. {
  10.         //Set registry bits for timer
  11.         TCCR0B          = 0b00000001;   //Set up timer 0 with no prescaler
  12.         TCNT0           = 0b00000000;   //Initialise counter
  13. }
  14.  
  15. int main()
  16. {
  17.         //Variables
  18.         char ch_R_on_dur        = 1;    //Number of counts channel R should remain on for
  19.         char ch_R_off_dur       = 254;  //Number of counts channel R should remain off for
  20.         char ch_R_count         = 0;    //Counter tracking amount of time channel R has remained on/off
  21.  
  22.         char ch_G_on_dur        = 0;    //Number of counts channel G should remain on for
  23.         char ch_G_off_dur       = 0;    //Number of counts channel G should remain off for
  24.         char ch_G_count         = 0;    //Counter tracking amount of time channel G has remained on/off
  25.  
  26.         char ch_B_on_dur        = 0;    //Number of counts channel B should remain on for
  27.         char ch_B_off_dur       = 0;    //Number of counts channel B should remain off for
  28.         char ch_B_count         = 0;    //Counter tracking amount of time channel B has remained on/off
  29.  
  30.         char ch_W_on_dur        = 0;    //Number of counts channel W should remain on for
  31.         char ch_W_off_dur       = 0;    //Number of counts channel W should remain off for
  32.         char ch_W_count         = 0;    //Counter tracking amount of time channel W has remained on/off
  33.        
  34.         //Initialise Timer
  35.         init_timer0();
  36.        
  37.         //Initialise LED output
  38.         DDRC    = 0b10000000;   //Sets the direction of the PC7 to output
  39.         PORTC   |= (1<<PC7);    //Sets PC7 high
  40.  
  41.         //Keep LED on at 100% for 2 seconds to show difference in output when PWM starts
  42.         _delay_ms(2000);
  43.  
  44.         while(1)
  45.         {
  46.                 //Update counters
  47.                 //If timer has ticked, update counters
  48.                 if (TCNT0 >= 1)
  49.                 {
  50.                         //Increment counters
  51.                         ch_R_count++;
  52.                         //Increment Green counter
  53.                         //Increment Blue counter
  54.                         //Increment White counter
  55.  
  56.                         //Reset timer
  57.                         TCNT0 = 0;
  58.                 }
  59.                
  60.                 //Cycle red led on and off at high speed
  61.                 //If channel R has been off for max duration
  62.                 if (PORTC == 0b00000000 && ch_R_count >= ch_R_off_dur)
  63.                 {
  64.                         //Turn channel R on
  65.                         PORTC |= (1<<PC7);
  66.                        
  67.                         //Reset channel R overflow counter
  68.                         ch_R_count = 0;
  69.                 } else {
  70.                 //If channel R has been on for max duration
  71.                 if (PORTC == 0b10000000 && ch_R_count >= ch_R_on_dur)
  72.                 {
  73.                         //Turn channel R off
  74.                         PORTC &= ~(1<<PC7);
  75.        
  76.                         //Reset channel R overflow counter
  77.                         ch_R_count = 0;
  78.                 }}
  79.  
  80.                 //Cycle Green LED
  81.                
  82.                 //Cycle Blue LED
  83.                
  84.                 //Cycle White LED
  85.         }
  86.  
  87.         //End program
  88.         return 0;
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top