sp3ctrm5tr

PIC16F877A Timer, PWM Mode

Mar 22nd, 2012
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. /*---------------------------------------------/
  2. |   Pulse Generator using TIMER2          |
  3. |   Date modified : 22 March 2012         |
  4. /---------------------------------------------*/
  5. #include <htc.h>
  6. #include <pic16f877a.h>
  7. #define _XTAL_FREQ 20000000 // Set the oscillator frequency to 20MHz
  8.  
  9. void main( )
  10. {  
  11.     TRISC = 0x00;   //Port C as outputs
  12.    
  13. /*  T2CON = 0b0000000; 
  14.     /*---------------------------------------------/
  15.     | Timer2 control register
  16.     | Timer2 Output Postscale = 1:11 postscale
  17.     | Timer2 On bit(TMR2ON) = Timer2 is off (0)
  18.     | Timer2 Clock Prescale = Prescaler is 1
  19.     /---------------------------------------------*/
  20.  
  21.     T2CON = 0b00000001;
  22.     /*---------------------------------------------/
  23.     | Timer2 control register (for 5kHz pulse)
  24.     | Timer2 Output Postscale = 1:11 postscale
  25.     | Timer2 On bit(TMR2ON) = Timer2 is off (0)
  26.     | Timer2 Clock Prescale = Prescaler is 4
  27.     /---------------------------------------------*/
  28.    
  29.     //PR2 = 0b01111111; //Timer2 increamenr from 00h until 7Fh @ 127
  30.     //PR2 = 0b10100101; // 30kHz
  31.     PR2 = 0b11111001; // 5kHz pulse
  32.    
  33.     //CCP1CON = 0b00111100;
  34.     /*---------------------------------------------/
  35.     | PWM Least Significant bits = 11b
  36.     | CCPx Mode Select bits = PWM mode
  37.     /---------------------------------------------*/
  38.  
  39.     CCP1CON = 0b00001100; // 50% duty cycle
  40.     /*---------------------------------------------/
  41.     | PWM Least Significant bits = 00b
  42.     | CCPx Mode Select bits = PWM mode
  43.     /---------------------------------------------*/
  44.    
  45.     //CCPR1L = 63; // 00111111b
  46.     CCPR1L = 125; // ( 1111101b ) 50% duty cycle
  47.  
  48.     TMR2ON = 1; //Turn on Timer 2
  49.     while(1); //run, run, run
  50. } //End of Program
Advertisement
Add Comment
Please, Sign In to add comment