Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Alex Rodríguez Navas, David Marín Gutiérrez, Carlos Sansón Martín
- #include <xc.h>
- #include "config.h"
- #define _XTAL_FREQ 8000000
- int watchdog = 0;
- int updown = 0;
- void setup(void) {
- TRISCBits.RC2 = 0; // Pin RC2 a output
- // Interrupts
- IPEN = 0; // Todas las interrutps son high priority
- GIE = 1; // Habilitar interrupts de high priority y las unmasked
- PEIE = 1; // Habilitar todas las interrupts perifericas unmasked
- // Timer 2
- TMR2IF = 0; // Timer 2 interrupt flag
- TMR2IE = 1; // Timer 2 enable interrupt flag
- TMR2IP = 1; // Timer 2 interrupt priority
- T2CKPS0 = 0; // Timer 2 prescaler 1:16
- T2CKPS1 = 1; // Timer 2 prescaler 1:16
- PR2 = 124; // Timer 2 period register a 124 pq PRx = period*(Fosc/4*prescaler*postcaler)-1
- // period = 1ms, prescaler = 16, postcaler = 1 ==> PRx = 124 (postcaler is 1 for PWM because ignored)
- // CCP1
- CCP1M0 = 0; // CCP Mode select bits
- CCP1M1 = 0; // 11xx PWM mode
- CCP1M2 = 1;
- CCP1M3 = 1;
- DC1B0 = 0; // Bits bajos del PWM Duty Cycle del CCP1
- DC1B1 = 0;
- CCPR1L = 0; // Bits altos del PWM Duty Cycle del CCP1
- /*
- 1 = CCP1/4*(124+1)
- CCP1 = 500 ==> CCP1L = 125 (0x7D) DC1B = 0 (0x0)
- Timer2 tick cada 1ms, en 1000ms 0%->100% ==> cada 8 ms CCP1L++
- pulseWidth = CCPRxL:DCxB0~1 * TOSC * prescaler
- dutyCycleRatio = (CCPRxL:DCxB0~1)/4*(PRx+1)
- */
- // Timer 2-bis
- TMR2ON = 1; // Timer 2 enable
- }
- void interrupt rutina_AP(void) {
- if (TMR2IF && TMR2IE) {
- watchdog++;
- if (watchdog == 8) { // 8ms
- if (updown <= 125) CCPR1L++; // 1s 0->125
- else CCPR1L--; // 1s 125->0
- updown++;
- watchdog = 0;
- }
- TMR2IF = 0; // Clear Timer 2 interrupt flag
- }
- }
- void loop(void) {
- }
- void main(void)
- {
- setup();
- while (1) loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement