Advertisement
Guest User

Untitled

a guest
Nov 18th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. // Alex Rodríguez Navas, David Marín Gutiérrez, Carlos Sansón Martín
  2.  
  3. #include <xc.h>
  4. #include "config.h"
  5. #define _XTAL_FREQ 8000000
  6.  
  7. int watchdog = 0;
  8. int updown = 0;
  9.  
  10. void setup(void) {
  11. TRISCBits.RC2 = 0; // Pin RC2 a output
  12.  
  13. // Interrupts
  14. IPEN = 0; // Todas las interrutps son high priority
  15. GIE = 1; // Habilitar interrupts de high priority y las unmasked
  16. PEIE = 1; // Habilitar todas las interrupts perifericas unmasked
  17.  
  18. // Timer 2
  19. TMR2IF = 0; // Timer 2 interrupt flag
  20. TMR2IE = 1; // Timer 2 enable interrupt flag
  21. TMR2IP = 1; // Timer 2 interrupt priority
  22. T2CKPS0 = 0; // Timer 2 prescaler 1:16
  23. T2CKPS1 = 1; // Timer 2 prescaler 1:16
  24. PR2 = 124; // Timer 2 period register a 124 pq PRx = period*(Fosc/4*prescaler*postcaler)-1
  25. // period = 1ms, prescaler = 16, postcaler = 1 ==> PRx = 124 (postcaler is 1 for PWM because ignored)
  26. // CCP1
  27. CCP1M0 = 0; // CCP Mode select bits
  28. CCP1M1 = 0; // 11xx PWM mode
  29. CCP1M2 = 1;
  30. CCP1M3 = 1;
  31. DC1B0 = 0; // Bits bajos del PWM Duty Cycle del CCP1
  32. DC1B1 = 0;
  33. CCPR1L = 0; // Bits altos del PWM Duty Cycle del CCP1
  34. /*
  35. 1 = CCP1/4*(124+1)
  36. CCP1 = 500 ==> CCP1L = 125 (0x7D) DC1B = 0 (0x0)
  37. Timer2 tick cada 1ms, en 1000ms 0%->100% ==> cada 8 ms CCP1L++
  38.  
  39. pulseWidth = CCPRxL:DCxB0~1 * TOSC * prescaler
  40. dutyCycleRatio = (CCPRxL:DCxB0~1)/4*(PRx+1)
  41. */
  42.  
  43. // Timer 2-bis
  44. TMR2ON = 1; // Timer 2 enable
  45. }
  46.  
  47. void interrupt rutina_AP(void) {
  48. if (TMR2IF && TMR2IE) {
  49. watchdog++;
  50. if (watchdog == 8) { // 8ms
  51. if (updown <= 125) CCPR1L++; // 1s 0->125
  52. else CCPR1L--; // 1s 125->0
  53. updown++;
  54. watchdog = 0;
  55. }
  56.  
  57. TMR2IF = 0; // Clear Timer 2 interrupt flag
  58. }
  59. }
  60.  
  61. void loop(void) {
  62. }
  63.  
  64. void main(void)
  65. {
  66. setup();
  67. while (1) loop();
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement