Page 1 of 1

PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 7:40 am
by cosmodrome
I'm new to PIC controllers and have a question that will probably be simple, but I cannot find the answer anywhere. I've got a 16F527 that I'm controlling a 3 digit 7 segment LED display. The outputs to the display are on PORTC and the transistor driver outputs are on PORTB. I've got 3 input switches, one that increments the display, one that decrements it and one that clears it to '000'. I've got the + and - working on RA0 and RA2 respectively, but I cannot get the third one working on RA4 (or any other RAx for that matter. I've turned off analog inputs, both comparitors and the opamps. Everything works until I add in the code for the third button. When I do three things happen: 1) when the clear button is pressed the LED sometimes resets to '000', and sometimes displays a random value. 2) when the clear button is pressed the display blinks really quickly. (I don't have a debounce per say, I disable the button until it detects that it has been released). 3) the PIC will randomly not work on power on, none of the buttons will work. I could just make RB7 an input and be done with it, but as I'm new to all this I'd really like to know what I'm doing wrong (or simply that what I'm trying is not possible due to xxx).

Thanks in advance
mike

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 7:46 am
by phalanx
Hi Mike,

Can you post your source code and configuration bits so I can have a look at what's going on?

Cheers!

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 7:50 am
by cosmodrome
/*
* File: main.c
* Author: Mike
*
* Created on January 23, 2019, 8:03 PM
*/


// PIC16F527 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = INTRC_IO // Oscillator Selection (INTRC with I/O function on OSC2/CLKOUT and 10 us startup time)
#pragma config WDTE = OFF // Watchdog Timer Enable (WDT Disabled)
#pragma config CP = OFF // Code Protection - User Program Memory (Code protection off)
#pragma config MCLRE = ON // Master Clear Enable (MCLR pin functions as MCLR)
#pragma config IOSCFS = 4MHz // Internal Oscillator Frequency Select (4 MHz INTOSC Speed)
#pragma config CPSW = OFF // Code Protection - Self Writable Memory (Code protection off)
#pragma config BOREN = ON // Brown-out Reset Enable (BOR Enabled)
#pragma config DRTEN = ON // Device Reset Timer Enable (DRT Enabled (18 ms))

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

#define _XTAL_FREQ 4000000

unsigned char digit[10] = {0x3F,0x09,0x75,0x5D,0x4B,0x5E,0x7E,0x0D,0x7F,0x4F};
int i;
int dig1, dig2;
int buttonPress;

void main(void) {
ANSEL = 0; //Turn off analog inputs
CM1CON0 = 0; // Turn off comparator 1
CM2CON0 = 0; // Turn off comparator 2
OPACON = 0;

TRISA=0x01; //PORTA pins input.
TRISB=0x00; //PORTB pins output.
TRISC=0x00; //PORTC pins output.
PORTB=0x00;

i=0;
buttonPress=0;

while(1) {
if (buttonPress==0){
i = checkButtonPress(i);
}
if (buttonPress==1){
checkButtonClear();
}

//if count is less than 0, make it equal to zero
//do not allow negative numbers
if(i==-1){
i=0;
}

//parse the count into individual digits
dig2 = i % 10;
dig1 = (i /10) % 10;

PORTC=digit[dig1];
PORTB=0x10;
__delay_ms(5);
PORTC=digit[dig2];
PORTB=0x20;
__delay_ms(5);
PORTC=digit[0];
PORTB=0x40;
__delay_ms(5);
PORTC=0x00;
}
}

int checkButtonPress(int a){
if(RA0==1){
a=a+1;
buttonPress=1;
}
if(RA2==1){
a=a-1;
buttonPress=1;
}
// if(RA4==1){
// a=0;
// buttonPress=1;
// }
return a;
}

int checkButtonClear(){
if((RA0==0) && (RA2==0)){
buttonPress=0;
}
}

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 7:58 am
by phalanx
I think I found your problem.
#pragma config FOSC = INTRC_IO // Oscillator Selection (INTRC with I/O function on OSC2/CLKOUT and 10 us startup time)
OSC2/CLKOUT uses pin RA4 and overrides the TRIS registers. Change your config to the internal oscillator without I/O and see how your project responds.

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 8:18 am
by cosmodrome
I set it to:
#pragma config FOSC = INTRC_CLKOUT
That doesn't seen to have worked, however, I'm now seeing some unusual operations with the buttons. I've commented out the clear button and disconnected it from the PIC and when I press it the LED dims.. Clearly there is something wrong with my circuit. I may be in error with my debounce method. Let me do some troubleshooting on my end and see if I can get this working.

thanks
mike

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 8:23 am
by phalanx
My apologies. I/O is the correct setting. I'm not sure what I was thinking but that's what happens when you write a response on your phone while out getting coffee.

While you look at your circuit, I'll look through your code once I'm back home to see if anything stands out.

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:06 am
by phalanx
TRISA=0x01; //PORTA pins input.
This would set only RA0 as an input.

I would expect TRISA=0x15 in order to get RA0, RA2, and RA4 as inputs.

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:18 am
by cosmodrome
Solved!
1) Changed my pulldown resistors from 1k to 10k (not sure if this was a problem, but I think the display looks better, plus less current pull).
2) needed to add the standard check-wait-check debounce. My method is good for only clocking one push if the button is held, not so much for debounce.
3) I have a loose connection in my breadboard that was causing me to loose ground. (%&#@&).

I was so focused on the programming I didn't see the other problems.
thanks again, mike

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:24 am
by cosmodrome
Huh, good point.
TRISA=0x01 works, but now I'm not sure why it does
TRISA=0x15 does not work, but it should?
TRISA=1 works, this one makes sense.

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:28 am
by phalanx
Do you have a debugger of any sort? You can set a breakpoint after the TRISA register is updated and then inspect its actual contents.

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:30 am
by phalanx
How do you have your switches connected to the RAx pins?

-Bill

Re: PIC16F527 RA4 not working

Posted: Sun Jan 27, 2019 9:42 am
by cosmodrome
I'm using a PICKIT 3.5 but I haven't looked into debugging yet. I'll look into that tonight.
Here's my schematic (RA5 was moved to RA0). If I'm feeling lucky I will move RA0 back to RA5, will help in making PCB.