PoS080
Junior Member level 3
Hello EE friends,
MICRO: PIC 16f1829
Complier: XC8
IDE: MPLAB
PIN: RA5
I am trying to write code that will provide a PWM output at a pin while a capacitive touch pad on my board is pressed. On release I would like the signal to be 0.
Currently my code turns on the signal on button press, but the pwm signal does not turn off upon button release, instead the pwm continues.
Things I have tried
1. Setting the TRISA to 1, to block output
2. Setting the dutycycle to 0%
3. Setting all PWM registers to 0x00
Im unsure of what is the proper way of doing this.
MICRO: PIC 16f1829
Complier: XC8
IDE: MPLAB
PIN: RA5
I am trying to write code that will provide a PWM output at a pin while a capacitive touch pad on my board is pressed. On release I would like the signal to be 0.
Currently my code turns on the signal on button press, but the pwm signal does not turn off upon button release, instead the pwm continues.
Things I have tried
1. Setting the TRISA to 1, to block output
2. Setting the dutycycle to 0%
3. Setting all PWM registers to 0x00
Im unsure of what is the proper way of doing this.
Code:
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = OFF // PLL Enable (4x PLL disabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = OFF // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv) {
OSCCON = 0x68; //4 MHZ
//TOUCH VARIABLES
unsigned int Tcount, Threshold;
int i;
INTCON =0; // purpose of disabling the interrupts.
ANSELC = 0; /* all ADC pins to digital I/O */
TRISC = 0; /* set as output */
LATC = 0x00; /* zero out */
OPTION_REG = 0XC5;
T1CON = 0XC1;
T1GCON = 0X81;
CPSCON1 = 0X03;
CPSCON0 = 0X8C;
for (i=0; i< 0x7000; i++) continue; //short delay
Threshold = 0x1150; // Capacitive Threshold
while (1){
// TIMERS for TOUCH
TMR1ON = 0;
TMR0 = 0;
TMR1H = 0;
TMR1L = 0;
TMR1ON = 1;
TMR0IF = 0; //Clear the interrupt flag for Timer 1
TMR0 = 0;
// Now start the touch sensing part and UART output
while (!TMR0IF) continue; //wait here till Timer 0 overflows
Tcount = (TMR1H<<8)+TMR1L; //Save 16 bit count value in timer 1
TMR0IF = 0; //Clear the interrupt flag for Timer 1
// Loop for detection
if(Tcount < Threshold){
APFCON1 = 0X01; //PWM output to PIN RA5;
TRISA = 0; //A to output
PORTA = 0; //CLR A
//PWM SETTINGS
PR2 = 0b00110001 ;
T2CON = 0b00000101 ;
CCPR2L = 0b00011000 ;
CCP2CON = 0b00111100;
for (i=0; i< 0x7000; i++) continue; //short delay
}
else {
TRISA = 1;
}
}
return (EXIT_SUCCESS);
}