Advertisement
Guest User

Untitled

a guest
Feb 14th, 2016
7,800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. #define _XTAL_FREQ 20000000 // set crystal oscillator to 20MHz.
  2. #define TMR1PRESCALE 8 // timer1 prescaler is 8.
  3. #define OUT RC2 // use the name OUT for RC2 pin.
  4.  
  5. #include <xc.h>
  6.  
  7. // BEGIN CONFIG
  8. #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
  9. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
  10. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
  11. #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
  12. #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  13. #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
  14. #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
  15. #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
  16. //END CONFIG
  17.  
  18.  
  19.  
  20. // variables and constants declarations
  21. unsigned long CCPR = 0; // holds the value needed to be put in CCP's registers.
  22. unsigned long current_period = 0; // holds the period that timer1 will use.
  23. const unsigned long total_period = 12500; // 20ms for 50hz frequency.
  24.  
  25.  
  26. // interrupt service routine
  27. void interrupt tmr1isr () {
  28. if (CCP1IF == 1) { // if CCP compare interrupt flag is set
  29.  
  30.  
  31. if ((current_period > 0) && (current_period < total_period)){ // if duty is > 0% AND < 100% then:
  32.  
  33. if (OUT == 1) { // if the output was 1 -> was "on-time".
  34. OUT = 0; // set output to 0 in order to achieve "off-time".
  35. CCPR = total_period - current_period; // make it time for "off-time", off-time = full time - on time.
  36. }
  37.  
  38. else { // if the output was 0 -> was "off-time".
  39. OUT = 1; // set output to 1 in order to achieve "on-time"
  40. CCPR = current_period; // make it time for "on-time".
  41. }
  42. }
  43. else {
  44. if (current_period == total_period) { OUT = 1;} // if duty = 100%, then output 1 all the time.
  45. if (current_period == 0) {OUT = 0;} // if duty = 0%, then output 0 all the time.
  46. }
  47.  
  48.  
  49. // now set the value of CCPR into CCP module's registers:
  50.  
  51. CCPR1H = CCPR >> 8; // right-shift CCPR by 8 then load it into CCPR1H register (load higher byte).
  52. CCPR1L = CCPR; // put the lower byte of CCPR in CCPR1L register.
  53. CCP1IF = 0; // reset CCP1 interrupt flag.
  54. }
  55. }
  56.  
  57.  
  58. // main function:
  59. void main() {
  60.  
  61.  
  62. TRISC = 0; // port c is output.
  63. PORTC = 0; // port c = 0.
  64.  
  65. T1CON = 0b00110000; // timer1 uses prescaler value of 8 and it is off.
  66. TMR1H = 0; // timer1 registers have 0 (clear).
  67. TMR1L = 0;
  68.  
  69. CCP1CON = 0x0b; // set CCP module to compare mode and trigger special event when interrupt happens.
  70. CCPR = 0; // load 0 in CCPR.
  71. CCP1IF = 0; // clear CCP1 interrupt flag.
  72. CCP1IE = 1; // enable CCP1 interrupt.
  73. INTCON = 0xC0; // enable global and peripheral interrupt.
  74. T1CON = 0b00110001; // start timer1 with the same settings like before.
  75.  
  76.  
  77.  
  78.  
  79. while (1) { // infinite loop.
  80.  
  81.  
  82. // TEST CODE...
  83.  
  84. current_period = total_period * 0.5; // 50% duty cycle.
  85. __delay_ms(2000); // delay 2s.
  86. current_period = total_period * 0.1; // 10% duty cycle.
  87. __delay_ms(2000); // delay 2s.
  88. current_period = total_period * 1; // 100% duty cycle.
  89. __delay_ms(2000); // delay 2s.
  90. current_period = total_period * 0; // 0% duty cycle.
  91. __delay_ms(2000); // delay 2s.
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement