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
- 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
- /*
- 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) {
- TMR2IF = 0; // Clear Timer 2 interrupt flag
- }
- }
- /*
- void interrupt timer2(void) {
- if (PIR1bits.TMR2IF) { //Ha passat 1ms
- ++contador; //Augmentem el contador cada ms
- if (contador == 6) { //Quan el contador == 6, hauràn passat 6ms
- contador = 0; //Reiniciem el contador a 0ms.
- ++i; //Augmentem una variable i.
- if (i <= 124) ++CCPR1L; //duty-cycle entre 0 i 100% [400 vegades]
- if (i > 125) --CCPR1L; //duty-cycle oscil·lant entre 100 i 0% [400 vegades]
- if (i >= 249) { // 1s
- i = 0;
- CCPR1L = 0;
- }
- }
- PIR1bits.TMR2IF = 0;
- }
- }
- */
- void loop(void) {
- }
- void main(void)
- {
- setup();
- while (1) loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment