EmbeddedRelated.com
Forums

writing bits to PORTA on p16f84

Started by Unknown February 5, 2004
Hello folks,

I'm writing a simple program for the 16f84 pic to learn how it works. I
would like to simulate a switchbutton on pin RA0, that when pressed,
causes an internal counter to decrement.   My problem seems to be when I
try to turn on the bits on PORTA RA0 to simulate the pin going high, it
never gets done.

I am using the gpasm assembler version 0.10.0 and the gpsim simulator
version 0.21.1

Here is the program 

include p16f84.inc
        list    p=16f84
        radix   hex
        __fuses _CP_OFF & _WDT_OFF

        org     0x00            
        goto    init

	cblock  0x0c
		tmp
	endc

init
	bsf STATUS,RP0          ;select bank 1
        movlw 0x01              ;set PORTA RA0 pin to input
        movwf TRISA
        bcf STATUS,RP0          ;select bank 0

        movlw 0x10              ;set counter to 10
        movwf tmp
        bsf PORTA,0             ;simulate input on port RA0
				;this is where I believe I'm
				;having probelms

main
        btfsc PORTA,0	
        decfsz tmp,1            ;countdown from 20 
				;this never gets executed as PORTA bit 0 is
				;always zero
        goto main
end


Here is the snippet of my session with the debugger.  The important
part is:

gpsim> 0x0000000000000008  p16f84  0x0007  0x1405  bsf  porta,0 
   read: 0x00 from porta
  wrote: 0x00 to porta

According to the debugger, PORTA never gets bit 0 set to 1. Am I perhaps
missing something, or do I misunderstand how this works.  If I want to
configure a pin as an input I should turn on the appropriate bits in
TRISA  and set them to 1, no?

I have read the faq and looked at other peoples code, and they seem to
be doing the same thing I am.  So what am I missing?

The entire debugging sessiong step by step is included below in case
any of it is relevant.

Thanks!


Found pic16f84
 f84 construct
TMRO::start
Cycle break point was ignored because cycle 0 has already gone by
current cycle is 0
Hex file "tmp.hex"
** SETTING CONFIG address = 0x2007  value = 0x3ffb
 --- Reset
gpsim> 0x0000000000000001  p16f84  0x0000  0x2801  goto 0x0001
gpsim> 0x0000000000000002  p16f84  0x0001  0x1683  bsf  status,5
   read: 0x18 from status
  wrote: 0x38 to status
gpsim> 0x0000000000000003  p16f84  0x0002  0x3001  movlw        0x01
  wrote: 0x01 to W
gpsim> 0x0000000000000004  p16f84  0x0003  0x0085  movwf        porta
   read: 0x01 from W
  wrote: 0x00 to porta
  wrote: 0x01 to trisa
gpsim> 0x0000000000000005  p16f84  0x0004  0x1283  bcf  status,5
   read: 0x38 from status
  wrote: 0x18 to status
gpsim> 0x0000000000000006  p16f84  0x0005  0x3010  movlw        0x10
  wrote: 0x10 to W
gpsim> 0x0000000000000007  p16f84  0x0006  0x008C  movwf        0x0c
   read: 0x10 from W
  wrote: 0x10 to 0x0c
gpsim> 0x0000000000000008  p16f84  0x0007  0x1405  bsf  porta,0
   read: 0x00 from porta
  wrote: 0x00 to porta
gpsim> 0x0000000000000009  p16f84  0x0008  0x1805  btfsc        porta,0
   read: 0x00 from porta
  skipped: 000a goto    0x0008
gpsim> 0x000000000000000B  p16f84  0x000A  0x2808  goto 0x0008
gpsim> 0x000000000000000D  p16f84  0x0008  0x1805  btfsc        porta,0
   read: 0x00 from porta
  skipped: 000a goto    0x0008
gpsim> 0x000000000000000F  p16f84  0x000A  0x2808  goto 0x0008
gpsim> 0x0000000000000011  p16f84  0x0008  0x1805  btfsc        porta,0
   read: 0x00 from porta
  skipped: 000a goto    0x0008
I know this is true of some PICs, maybe all. It catches just about everyone. 
:-)

By default, the port A pins are set to be inputs to the analog to digital
converters, not digital I/O. Looks at the documentation on the ADCON
register(s) to see how to do that.


Gary Kato wrote:
> I know this is true of some PICs, maybe all. It catches just about everyone. > :-) > > By default, the port A pins are set to be inputs to the analog to digital > converters, not digital I/O. Looks at the documentation on the ADCON > register(s) to see how to do that.
The 16F84 doesn't have an ADC. Leon -- Leon Heller, G1HSM Email: aqzf13@dsl.pipex.com My low-cost Philips LPC210x ARM development system: http://www.geocities.com/leon_heller/lpc2104.html
When set as an input, a read on an I/O line on PICs always gives the
actual (external) logic state on the pin, and not at all whatever you
write in the output register.
>When set as an input, a read on an I/O line on PICs always gives the >actual (external) logic state on the pin, and not at all whatever you >write in the output register. >
If this were a real PIC and RA0 was not connected to anything, would it read 1 when RA0 is set as an input?
I don't use gpsim, but the standard MPLAB SIM. MPLAB SIM seems to assume that
input pins are tied low unless told otherwise by the Pin Stimulator. I tried
setting RA0 as an output and setting the latch, then setting RA0 as an input
and RA0 turned off at that point.

I don't know if gpsim has some way for you to force the value of RA0 to what
you want when it's configured as an input. MPLAB SIM refuses to do that, but
then it has the Pin Stimulator.

<foobear@nospam.foobear.net> wrote in message news:<0wnUb.151$FF5.41618@petpeeve.ziplink.net>...
> Hello folks, > > I'm writing a simple program for the 16f84 pic to learn how it works. I > would like to simulate a switchbutton on pin RA0, that when pressed, > causes an internal counter to decrement. My problem seems to be when I > try to turn on the bits on PORTA RA0 to simulate the pin going high, it > never gets done. > > I am using the gpasm assembler version 0.10.0 and the gpsim simulator > version 0.21.1 > > Here is the program > > include p16f84.inc > list p=16f84 > radix hex > __fuses _CP_OFF & _WDT_OFF > > org 0x00 > goto init > > cblock 0x0c > tmp > endc > > init > bsf STATUS,RP0 ;select bank 1 >[1] movlw 0x01 ;set PORTA RA0 pin to input >[2] movwf TRISA
^^^^^^^^^^^^^ here you set it to be an input. i.e. you can only intelligbly(sp?) *read* from it, writing to it will not have any effect.
> bcf STATUS,RP0 ;select bank 0 > > movlw 0x10 ;set counter to 10 > movwf tmp >[3] bsf PORTA,0 ;simulate input on port RA0 > ;this is where I believe I'm > ;having probelms
there is no problem. you cannot turn an input on and off (high and low) from *within* the chip, you need to do it from outside. try this: 1. bridge RA0 and RA1. 2. replace lines [1] and [2] above with this: bsf TRISA, 1 ; RA0 is now input bcf TRISA, 0 ; RA1 is now output 3. replace line [3] with bsf PORTA, 1 ; this will turn on RA1, which will ; make RA0 high, which you can then read hth goose, hand
> If this were a real PIC and RA0 was not connected to anything, would it read 1 > when RA0 is set as an input?
It would read an undefined value. Might be 1 or 0, who knows?
> main > btfsc PORTA,0 > decfsz tmp,1 ;countdown from 20 > ;this never gets executed as PORTA bit 0 is > ;always zero > goto main > end > >
Just for future reference, when 'goto main' is skipped, the microcontroller will *not* just stop. Instead it will keep executing the rest of (unwritten) program memory and roll back to the beginning of the program. At the and, you should put a stop goto stop end Al
goose <ruse@webmail.co.za> wrote:
>> init >> bsf STATUS,RP0 ;select bank 1 >>[1] movlw 0x01 ;set PORTA RA0 pin to input >>[2] movwf TRISA > ^^^^^^^^^^^^^ > > here you set it to be an input. i.e. you can only intelligbly(sp?) > *read* from it, writing to it will not have any effect. > >> bcf STATUS,RP0 ;select bank 0 >> >> movlw 0x10 ;set counter to 10 >> movwf tmp >>[3] bsf PORTA,0 ;simulate input on port RA0 >> ;this is where I believe I'm >> ;having probelms > > there is no problem. you cannot turn an input on and > off (high and low) from *within* the chip, you need to > do it from outside. > > try this: > 1. bridge RA0 and RA1. > 2. replace lines [1] and [2] above with this: > bsf TRISA, 1 ; RA0 is now input > bcf TRISA, 0 ; RA1 is now output > 3. replace line [3] with > bsf PORTA, 1 ; this will turn on RA1, which will > ; make RA0 high, which you can then read
Hi goose, Thanks for the information. I thought that might have been the cause but I was uncertain, as I'm still somewhat new to PIC software development I don't have breadboard components at the moment, and the simulator I am using (gpsim) is still under development and its features are somewhat limited. As per your suggestion, is bridging RA0 and RA1 possible via some instructions on the PIC? Or were you thinking about doing this via a simulator? Thanks -v