Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Shutting PWM off and on based on CTS PAD

Status
Not open for further replies.

PoS080

Junior Member level 3
Joined
Apr 2, 2017
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
243
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.


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);
}
 

Setting the TRISA to 1, to block output

Perhaps a high impedance input is not a good idea, depending on what is connected at the PWM output pin. I would prefer an active low level output.
 

Perhaps a high impedance input is not a good idea, depending on what is connected at the PWM output pin. I would prefer an active low level output.

I see, but im just trying to figure out a way to shut off PWM.
 

I think that just setting duty-cycle to 0% should be pretty enough, anyway if you want to disable PWM module for safety purpose in order to avoid accidental activity caused by error in program, you could turn this pin as Output and then assert its value to zero, as mentioned before.
 

Try this. if your Capacitive Touch Input Pin is digital I/O then you might need to disable ADC for RA0. (RA0 is your capacitive touch input pin ?)

Code:
ANSELA = 0x01;
 

Hmmm none of those worked, I rewrote the code using functions and called the functions in the if and else statements. Still not sure why the original code didn't work, but here is the solution I used.


Code:
if(touch) {
PWMOn();
}
else{
PWMOff();
}


void PWMOn(){
//code for pwm from post
}

void PWMOff(){
//Set dutycycle = 0
//Set lowest 4 of CCPxCON to 0
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top