Subversion Repositories Code-Repo

Rev

Rev 272 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
163 Kevin 1
#include <xc.h>
2
#include "defines.h"
3
#include "base_PWM.h"
4
 
5
void PWM_Init() {
6
    // Output pin initially blocked
7
    PWM_TRIS = 1;
8
 
9
    /* Initialize PWM module */
10
    PR2 = 0xF9; // 4ms @ 16MHz
11
    CCP1CONbits.P1M = 0b00; // Single output, P1A modulated only
12
    CCP1CONbits.CCP1M = 0b1100; // PWM mode, P1A active-high, P1B active-high
13
 
14
    // Idle the output till width is specified
15
    CCPR1L = 0x00;
16
    CCP1CONbits.DC1B = 0b00;
17
 
18
    /* Initialize Timer 2 */
19
    PIR1bits.TMR2IF = 0; // Clear the interrupt flag for Timer 2
20
    T2CONbits.T2CKPS = 0b11; // Set a prescaler of 64
21
    T2CONbits.TMR2ON = 1; // Enable the timer
22
 
23
    // Wait for the timer to overflow before enabling output
24
    while (!PIR1bits.TMR2IF);
25
    PWM_TRIS = 0;
26
}
27
 
28
void PWM_Set_Width(int width_us) {
29
    // Set the pulse duration to the requested width
30
    int value = width_us / 4;
31
    CCPR1L = value >> 2;
32
    CCP1CONbits.DC1B = value;
33
}