Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
163 Kevin 1
#include <xc.h>
2
#include "defines.h"
3
#include "base_CPS.h"
4
 
5
static CPS_DATA *cps_data_p;
6
 
7
void CPS_Init(CPS_DATA* data) {
8
    cps_data_p = data;
9
    for (char i = 0; i < 4; i++) {
10
        cps_data_p->btn_pressed[i] = 0;
11
        cps_data_p->btn_last_value[i] = 0;
12
        cps_data_p->btn_avg_value[i] = 0;
13
        cps_data_p->btn_pct_value[i] = 0;
14
    }
15
 
16
    /* Initialize port direction */
17
    CPS_0_TRIS = 1;
18
    CPS_1_TRIS = 1;
19
 
20
    /* Initialize FVR for the upper threshold (Ref+) */
21
    FVRCONbits.CDAFVR = 0b01; // Gain of 1x (1.024V)
22
    FVRCONbits.FVREN = 1; // Enable FVR module
23
 
24
    /* Initialize DAC for the lower threshold (Ref-) */
25
    DACCON0bits.DACEN = 0; // Disable DAC
26
    DACCON0bits.DACLPS = 0; // Negative reference source selected
27
    DACCON0bits.DACOE = 0; // Output not routed to DACOUT pin
28
    DACCON0bits.DACPSS = 0b00; // Vdd used as positive source
29
//    DACCON0bits.DACNSS = 0; // Vss used as negative source
30
    DACCON1bits.DACR = 0b00000; // Voltage output set to 0
31
 
32
    /* Initialize Timer 0 */
33
    OPTION_REGbits.TMR0CS = 0; // Clock source is FOSC/4
34
    OPTION_REGbits.PSA = 0; // Prescaler enabled
35
    OPTION_REGbits.PS = 0b111; // Prescaler of 1:256
36
 
37
    /* Initialize Timer 1 */
38
    T1CONbits.TMR1CS = 0b11; // Clock source is Capacitive Sensing Oscillator
39
    T1CONbits.T1CKPS = 0b00; // 1:1 Prescale value
40
    T1GCONbits.TMR1GE = 1; // Counting is controlled by the gate function
41
    T1GCONbits.T1GPOL = 1; // Gate is active high
42
    T1GCONbits.T1GTM = 1; // Gate toggle mode is enabled
43
    T1GCONbits.T1GSPM = 0; // Gate single-pulse mode is disabled
44
    T1GCONbits.T1GSS = 0b01; // Gate source is Timer 0 overflow
45
    T1CONbits.TMR1ON = 1; // Enables timer 1
46
 
47
    /* Initialize CPS Module */
48
    CPSCON0bits.CPSRM = 1; // DAC and FVR used for Vref- and Vref+
49
    CPSCON0bits.CPSRNG = 0b11; // Osc in high range (100uA)
50
    CPSCON0bits.T0XCS = 0; // Timer 0 clock runs at FOSC/4
51
    CPSCON1bits.CPSCH = 0b00; // Channel 0 (CPS0)
52
    cps_data_p->channel = 0;
53
    CPSCON0bits.CPSON = 1; // CPS module is enabled
54
 
55
    /* Initialize timer interrupts and clear timers */
56
    INTCONbits.TMR0IE = 1; // Timer 0 interrupt enabled
57
    PIE1bits.TMR1IE = 0; // Timer 1 interrupt disabled
58
    CPS_Reset();
59
}
60
 
61
void CPS_Timer_0_Interrupt_Handler() {
62
    unsigned int value = TMR1;
63
    long percent;
64
 
65
    if (value < 10) {
66
        return;
67
    }
68
 
69
    // Calculate percentage change
70
    percent = (long)cps_data_p->btn_avg_value[cps_data_p->channel]-(long)value;
71
    if (percent < 0)
72
        percent = 0;
73
    else {
74
        percent *= 100;
75
        percent /= cps_data_p->btn_avg_value[cps_data_p->channel];
76
    }
77
 
78
    cps_data_p->btn_last_value[cps_data_p->channel] = value;
79
    cps_data_p->btn_pct_value[cps_data_p->channel] = percent;
80
 
81
    if (percent < CPS_PCT_OFF) {
82
        // Calculate average
83
        cps_data_p->btn_avg_value[cps_data_p->channel] =
84
            cps_data_p->btn_avg_value[cps_data_p->channel] +
85
            ((long)value - (long)cps_data_p->btn_avg_value[cps_data_p->channel])
86
            /CPS_AVG_COUNT;
87
        // Set flag to indicate that button is not pressed
88
        cps_data_p->btn_pressed[cps_data_p->channel] = 0;
89
    } else if (percent > CPS_PCT_ON)
90
        // Set flag to indicate that button was pressed
91
        cps_data_p->btn_pressed[cps_data_p->channel] = 1;
92
 
93
    cps_data_p->channel = cps_data_p->channel + 1;
94
    if (cps_data_p->channel == CPS_NUM_CHANNELS)
95
        cps_data_p->channel = 0;
96
 
97
    CPSCON1bits.CPSCH = cps_data_p->channel;
98
 
99
    CPS_Reset();
100
}
101
 
102
void CPS_Reset() {
103
    TMR1 = 0;
104
    TMR0 = 0;
105
}
106
 
107
void CPS_Enable() {
108
    INTCONbits.TMR0IE = 1; // Timer 0 interrupt enabled
109
    T1CONbits.TMR1ON = 1;
110
    CPSCON0bits.CPSON = 1;
111
    CPS_Reset();
112
}
113
 
114
void CPS_Disable() {
115
    INTCONbits.TMR0IE = 0;
116
    CPSCON0bits.CPSON = 0;
117
    T1CONbits.TMR1ON = 0;
118
}