PIC Inputs On & Off

Thread Starter

solpic

Joined Jul 23, 2009
25
Hello everyone,

I have another strange experience with PIC16Fxx microcontrollers. The chip was supposed to send a 1 to RA1, RA2, and a 0 to RA3 when RA0 was 1 and a 0 to RA1, RA2, and a 1 to RA3 when RA0 was 0. So when I connected RA0 to power RA1 and RA2 (with their LEDs) lit up, and when I connected RA0 to ground, RA3 lit up. But then when I attached RA0 to nothing, just in the air, all three LEDs lit up. I don't have the exact code but it was something like this.

(This was a PIC16F84A 20I/P chip)

Rich (BB code):
PORTA	equ	0x05
TRISA		equ	0x85
STATUS	equ	0x03

main:	bsf	STATUS, 5
	movlw	0x01
	movwf	TRISA
	bcf	STATUS, 5

loop:	btfss	PORTA, 0
	goto	is_off
	goto	is_on

is_off:	bcf	PORTA, 1
	bcf	PORTA, 2
	bsf	PORTA, 3
	goto	loop

is_on:	bsf	PORTA, 1
	bsf	PORTA, 2
	bcf	PORTA, 3
	goto	loop
	
	end
Also, what are the designations for on and off for the inputs on the PIC?

Thanks!
 

blueroomelectronics

Joined Jul 22, 2007
1,757
For started use an include file. No reason to define manually anymore.
Also run it through MPLAB's Simulator and see if PORTA works as you expect it to.
Plus the 16F84 is pretty ancient, the 16F628A is cheaper and better.

Note: your problem might be what is known as the "Read / Modify / Write" bug. Google it.
 

AlexR

Joined Jan 16, 2008
732
What you are seeing is more or less what you would expect.
An input pin must be either pulled high (usually a 10K resistor from pin to Vdd) or low (10K to Vss), it must never be left floating.
If you float an input pin it will be read as either a high, a low, or as probably as in your case, a rapid oscillation between a high and a low. And no, this effect will not show up in a simulation!

As for the PIC16F84, yes its an ancient chip and you would be foolish to use it in any new project but it's a very good chip for learning the basics of PIC programming because you don't have the added complications of analog ports and advanced peripherals.
 

jpanhalt

Joined Jan 18, 2008
11,087
Also, what are the designations for on and off for the inputs on the PIC?

Thanks!
Not sure if this answers your question, but you read the inputs using btfsc and btfss (bit test f, skip if clear; bit test f, skip if set). The set and clear mean high and low, respectively, just as when you write to a port.

John
 

Thread Starter

solpic

Joined Jul 23, 2009
25
I mean what does the PIC read as on and off for a pin. Like is it when the pin is connected to power or ground, or is 5v and 0v.
 

Thread Starter

solpic

Joined Jul 23, 2009
25
What you are seeing is more or less what you would expect.
An input pin must be either pulled high (usually a 10K resistor from pin to Vdd) or low (10K to Vss), it must never be left floating.
If you float an input pin it will be read as either a high, a low, or as probably as in your case, a rapid oscillation between a high and a low. And no, this effect will not show up in a simulation!

As for the PIC16F84, yes its an ancient chip and you would be foolish to use it in any new project but it's a very good chip for learning the basics of PIC programming because you don't have the added complications of analog ports and advanced peripherals.
Thanks, this is what I was looking for! So this means if for instance I wanted to detect whether a SPST switch was on or off I couldn't just check if the pin was on or off, I would have to use some sort analog measurement?
 

AlexR

Joined Jan 16, 2008
732
The usual way I would connect a switch to an input pin is with a 10K resistor from Vdd to the pin and then the switch going from the pin to Vss(earth). When the switch is open the pin sits at 5 volts and when the switch is closed the pin is pulled down to 0 volts.

Your second question about the what voltage levels the PIC sees as high and low, this will vary with supply voltage and the chip type so its best to look it up in the data sheet under "Electrical Specification / DC Characteristics" but as a general rule 0.8 volt and below is taken to be a low and 2 volt and above is taken as a high.
 
Top