csansoon

Untitled

Nov 18th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 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. void setup(void) {
  8. TRISCBits.RC2 = 0; // Pin RC2 a output
  9.  
  10. // Interrupts
  11. IPEN = 0; // Todas las interrutps son high priority
  12. GIE = 1; // Habilitar interrupts de high priority y las unmasked
  13. PEIE = 1; // Habilitar todas las interrupts perifericas unmasked
  14.  
  15. // Timer 2
  16. TMR2IF = 0; // Timer 2 interrupt flag
  17. TMR2IE = 1; // Timer 2 enable interrupt flag
  18. TMR2IP = 1; // Timer 2 interrupt priority
  19. T2CKPS0 = 0; // Timer 2 prescaler 1:16
  20. T2CKPS1 = 1; // Timer 2 prescaler 1:16
  21. PR2 = 124; // Timer 2 period register a 124 pq PRx = period*(Fosc/4*prescaler*postcaler)-1
  22. // period = 1ms, prescaler = 16, postcaler = 1 ==> PRx = 124 (postcaler is 1 for PWM because ignored)
  23. // CCP1
  24. CCP1M0 = 0; // CCP Mode select bits
  25. CCP1M1 = 0; // 11xx PWM mode
  26. CCP1M2 = 1;
  27. CCP1M3 = 1;
  28. DC1B0 = 0; // Bits bajos del PWM Duty Cycle del CCP1
  29. DC1B1 = 0;
  30. CCPR1L = 0; // Bits altos del PWM Duty Cycle del CCP1
  31. /*
  32. pulseWidth = CCPRxL:DCxB0~1 * TOSC * prescaler
  33. dutyCycleRatio = (CCPRxL:DCxB0~1)/4*(PRx+1)
  34. */
  35.  
  36. // Timer 2 bis
  37. TMR2ON = 1; // Timer 2 enable
  38.  
  39.  
  40.  
  41. }
  42.  
  43. void interrupt rutina_AP(void) {
  44. if (TMR2IF && TMR2IE) {
  45.  
  46.  
  47. TMR2IF = 0; // Clear Timer 2 interrupt flag
  48. }
  49. }
  50.  
  51. /*
  52. void interrupt timer2(void) {
  53. if (PIR1bits.TMR2IF) { //Ha passat 1ms
  54. ++contador; //Augmentem el contador cada ms
  55. if (contador == 6) { //Quan el contador == 6, hauràn passat 6ms
  56. contador = 0; //Reiniciem el contador a 0ms.
  57. ++i; //Augmentem una variable i.
  58. if (i <= 124) ++CCPR1L; //duty-cycle entre 0 i 100% [400 vegades]
  59. if (i > 125) --CCPR1L; //duty-cycle oscil·lant entre 100 i 0% [400 vegades]
  60. if (i >= 249) { // 1s
  61. i = 0;
  62. CCPR1L = 0;
  63. }
  64. }
  65. PIR1bits.TMR2IF = 0;
  66. }
  67. }
  68. */
  69.  
  70. void loop(void) {
  71. }
  72.  
  73. void main(void)
  74. {
  75. setup();
  76. while (1) loop();
  77. }
Advertisement
Add Comment
Please, Sign In to add comment