• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

PIC16F631 and Port not going low again

Status
Not open for further replies.

Nayth

New Member
Hi all

I have a problem with my PIC16F631 chip and PORTA.4

First of I set it's state to low and then set it to be an input port:

Code:
PORTA = 0b101100;
TRISA = 0b011011;
Further down the code I check if the port it high or not.

If the port is high I show this by turning on some lights and if it's low I turn them off.

Ok, so far so good, but here's were it goes bad.

If the input on the port goes high the program does what it should, but if it goes low again, at any given time after that, the port stays high and therefore my program think the input on the port is high when in fact it's the port that's high.

PORTA.4 is the only port doing this.

I hope some of you clever Pic coders can help me figure this one out.

Thanks in advance
 

Nayth

New Member
Open-collector?

There's nothing about an open-collector in the datasheet

This is what's in the Datasheet about this pin

Code:
I/O   Pin   Analog   Comparators   Timers   Interrupt   Pull-up      Basic
RA4    3      —           —          T1G       IOC        Y       OSC2/CLKOUT
I just want to know why it won't go low again when the input on the port HAS been high.
 

be80be

Well-Known Member
RA4 is normally an open-collector pin on most PIC's.
Not on that chip it the same as 16f690 even has wpu on portA . Do you have more code then that hard to tell by that whats changing it state
did you set it up something like this
Code:
BCF STATUS,RP0 ;Bank 0
BCF STATUS,RP1 ;
CLRF PORTA ;Init PORTA
BSF STATUS,RP1 ;Bank 2
CLRF ANSEL ;digital I/O
BSF STATUS,RP0 ;Bank 1
BCF STATUS,RP1 ;
MOVLW 0Ch ;Set RA<3:2> as inputs
MOVWF TRISA ;and set RA<5:4,1:0>
;as outputs
BCF STATUS,RP0 ;Bank 0
 
Last edited:

be80be

Well-Known Member
Do you have your config set to INTOSCIO.
I say that's it i have set the config wrong use hex for the word and porta,4 would read wrong on a 16f690 the same chip just more ram and adc on RA4
 

Nayth

New Member
I'm using CC5X

I have set the config to 32D4 as so

Code:
#pragma config = 0x32D4
Oscillator = Internal RC No Clock
Watchdog Timer = Off
Power Up Timer = Off
Master Clear Enable = Internal
Code Protect = Off
Data EE Read Protect = Off
Brown Out Detect = BOD enabled in run, disabled in sleep, SBOREN disabled
Internal External Switch Over Mode = Disabled
Monitor Clock Fail-safe = Disabled
 

3v0

Coop Build Coordinator
Forum Supporter
You sound like you know what you are doing. But to find the problem we need more then what you have provided.

First what is connected to RA4 ? Are there pull up or down resistors? A schematic would be helpful.

It could be a problem with the code. It is impossible to rule that out without seeing it. Maybe a few more eyes on the code could find the problem.

Post it.

3v0
 

be80be

Well-Known Member
I would set my config like this just to see if it may be it.
Code:
__CONFIG    _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
 

Nayth

New Member
First what is connected to RA4 ?
An input that can either be high (~5V) or low (~1V)
Are there pull up or down resistors?
There is a resistor (47K), but that's only there as to not fry the PIC. I previously used PORTC.7 but I had to change to PORTA.4 because C.7 has not interrupt =(
I had not problems with C.7 other than the missing interrupt.
A schematic would be helpful.
I can't show you the schematic, unfortunately.

It could be a problem with the code. It is impossible to rule that out without seeing it. Maybe a few more eyes on the code could find the problem.

Post it.
All I do is run around in a loop and check whether PORTA.4 is high or low

If it's high I call a function that turns on a series of lights and if it's low I turn the lights off.

My problem is that once PORTA.4 has been high it won't go low again even though the input on the port is low, so it would seem like what is really happening is that the port, once having been high has gone from an input port to an output port, or something like it.

I would set my config like this just to see if it may be it.
Code:
__CONFIG    _FCMEN_ON & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
I can't use that ASM code with CC5X, getting some strange error.

I set my config to this value
11001011010100
which is the same as
32D4


I seems as if the port has some SCHMITT TRIGGER or something like it that's keeping it high once triggered, I just can't see how to change it if that's the case
 

Nayth

New Member
No code + No schematic + No new information. = no help.

My crystal ball is broken.
Code:
#include <INT16CXX.H>

#pragma origin 4
interrupt wakeup(void)
{
	int_save_registers

	//Disable Interrupt on PORTA pin 1 and 2
	IOCA.0 = 0;
	IOCA.1 = 0;
	IOCA.4 = 0;

	if(RABIF == 1)
	{
		PORTB.6 = 1; // Lamp 4
		PORTB.4 = 1; // Lamp 3
		PORTC.1 = 1; // Lamp 2
		PORTA.2 = 1; // Lamp 1

		PORTA.5 = 1; // Lamp Supply
	}

	RABIF = 0;    // Reset the RABIF-flag before leaving
	int_restore_registers
}

#pragma chip PIC16F631
#pragma config = 0x00D4 //0b11001011010100

void init(void)
{
	OSCCON = 0b01100101; // Oscillator Control Register

	CM1CON0 = 0; //Disable Comparator 1
	CM2CON0 = 0; //Disable Comparator 2
	CM2CON1 = 0; //Disable Comparator 3

	//OPTION_REG = 0b00001111;

	//T1CON = 0;
	
	ANSEL = 0; //All analogue ports to digital

	PORTA = 0b101100;
	TRISA = 0b011011;
	IOCA.0 = 0;
	IOCA.1 = 0;
	IOCA.4 = 0;

	PORTB = 0b1111;
	TRISB = 0b0000;
	IOCB = 0b0000;

	PORTC = 0b01111111;
	TRISC = 0b10000000;

	GIE = 1;
	RABIE = 1;

	PORTA.5 = 1;

	// 0=On / 1=Off
	PORTC.5 = 0; // Led 1
	/*PORTC.4 = 0; // Led 2
	PORTC.3 = 0; // Led 3
	PORTC.6 = 0; // Led 4*/
	
	// 0=Off / 1=On
	PORTB.6 = 1; // Lamp 4
	PORTB.4 = 1; // Lamp 3
	PORTC.1 = 1; // Lamp 2
	PORTA.2 = 1; // Lamp 1
}
void main(void)
{
	init();
	while(1)
	{
		[I]if(PORTA.4 == 1) // Charging?
		{
			Running_Led();
		}else{
			PORTC.5 = 1;
			PORTC.4 = 1;
			PORTC.3 = 1;
			PORTC.6 = 1;
		}[/I]
	}
}
Everything works except the portion in italic.

There's a lot more code, but I'm not allowed to show it and the schematics
 

be80be

Well-Known Member
The 16f631 and 16f690 are the same chip just more ram in the 16f690 and adc on portA4 What's this for
Code:
GIE = 1;
RABIE = 1;
I can't tell that your using interrupts from that code so why is it in there?
 

Nayth

New Member
The 16f631 and 16f690 are the same chip just more ram in the 16f690 and adc on portA4
Ahh ok, didn't know that
What's this for
Code:
GIE = 1;
RABIE = 1;
I can't tell that your using interrupts from that code so why is it in there?
I am using interrupts, I'm using them to wake up the chip once in a sleep

Code:
#pragma origin 4
interrupt wakeup(void)
{
	int_save_registers

	//Disable Interrupt on PORTA pin 1 and 2
	IOCA.0 = 0;
	IOCA.1 = 0;
	IOCA.4 = 0;

	if(RABIF == 1)
	{
		PORTB.6 = 1; // Lamp 4
		PORTB.4 = 1; // Lamp 3
		PORTC.1 = 1; // Lamp 2
		PORTA.2 = 1; // Lamp 1

		PORTA.5 = 1; // Lamp Supply
	}

	RABIF = 0;    // Reset the RABIF-flag before leaving
	int_restore_registers
}
I posted that with the rest of the code


Code:
GIE: Global Interrupt Enable bit
1 = Enables all unmasked interrupts
0 = Disables all interrupts

RABIE: PORTA/PORTB Change Interrupt Enable bit
1 = Enables the PORTA/PORTB change interrupt
0 = Disables the PORTA/PORTB change interrupt
 
Last edited:

be80be

Well-Known Member
Bill said that's what you have
Wonder if it's the read modify write problem on the 12 & 14bit cores.
I no what it is but I don't see it doing any thing in what you posted and if some where you used it on RA4 the pin state can change if not restored after the interrupt
 
Last edited:

Nayth

New Member
Im not enabling interrupt untill just before I call the sleep command, and the chip wakes fine if its the first time PORTA.4 has been high.
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top