# Pic Programming question

Status
Not open for further replies.

#### skeeterb

##### Member
I've got a question in regards to programming a pic. What do I have to do to setup the ports as Input and Output and read the inputs to activate and deactivate the output. Currently if I try to use TRISA and TRISB in my code, the programing program throws me out with and error message.

here is a bit of the code I have now

ORG 0x000
GOTO Start

Init

bsf 03h,5
movlw b'11111111'
movwf 05h
movlw b'00000000'
movwf 06h
bcf 03h,5
goto Main
Start
call Init

Main
btfsc PORTA,1
bsf PORTB,3
goto Main

END

#### birdman0_o

##### Active Member
Code:
cblock 0x20   ; will change depending on chip
d1
d2
d3
endc

ORG 0x000
GOTO Init

ORG 0x004
GOTO Main

Init

bsf STATUS,RP0
movlw b'11111111'
movwf PORTA  ; porta all input
movlw b'00000000'
movwf PORTB; portb all output
bcf  STATUS, RPO
CLRF PORTB  ; port b all low

goto Main

Main

btfsc PORTA,1  ;if port a,1  = high
bsf PORTB,3 ; set  portb, 3

Call delay

bcf PORTB,3 ; if PORTA,1 is no longer pressed this will turn off that led

goto Main

delay1	   ; half a millisecond delay
MOVLW	0x0B0
MOVWF	d1
MOVLW	17h
MOVWF	d2
MOVLW	0x02
MOVWF	d3
delay2
DECFSZ	d1,F
GOTO	$+2 DECFSZ d2,F GOTO$+2
DECFSZ	d3,F
GOTO	delay2
GOTO	$+1 GOTO$+1
GOTO	\$+1
RETLW	D'00'			; 499994 cycles

END

Try out this code, i wrote it up quick but it should help you figure some stuff out, write back if something doens't work

Mike

#### skeeterb

##### Member
I'm using multisim to simulate my circuit as it stands, and it only has PIC16F84 and PIC16F84A MCU modules. I had to clean up the code that you put on here, and I've got it down to one error, and that's symbol not previously defined (RP0)

Edit:
I fixed the errors and put a copy of the circuit that I have been working on. The LEDs represent the different connectors that will be on my primary circuit when I build it.

#### Attachments

• 16.2 KB Views: 151
Last edited:

#### birdman0_o

##### Active Member
Thats weird, well put BSF STATUS,5 instead then , and BCF STATUS,5
you sure your doing a zero and not an "oh"?

Mike

#### skeeterb

##### Member
I declared the RPO variable and it cleared that error message. most of the errors were that the lines were in column 1 and I just bumped them over 5 spaces, and that got rid of those errors. it still doesn't do anything right now, that's frustrating to the max I've posted a logic description of how it should work in another thread here that shows how it should work.

Edit:
I found the Logic description

Upper + Lower Sensor = 1 Then Operation = 1; Warn = 0; Refill = 0
Upper Sensor = 0 + Lower Sensor = 1 Then Operation = 1; Warn = 1; Refill = 0
Upper + Lower Sensor = 0 Then Operation = 0; Warn = Flashing; Refill = 1

I've been playing with the circuit in Multisim, and it still just sits there even if i do hit the switches representing the two sensors. I've temporarily put LEDs in the place to the connector to my secondary circuit which contains the indicator LEDs. The Flashing of the Warn LED will be handled on my indicator circuit with a 555. I've pretty much finalized the look of the indicator circuit, so I don't need help there, because it operates as I desire.

Last edited:

#### birdman0_o

##### Active Member
Use the command i put in my previous post since RP0 is not working. It is very important as it chooses the tristate register. It's very unclear what is not working...post your full code please.

Mike

#### skeeterb

##### Member
here's the code I've got so far

Code:
 Processor 16f84
#include "p16f84.inc"    ; This includes PIC16F84 definitions for the MPASM assembler

status   equ    0x03
porta    equ    0x05
portb   equ 0x06
trisa    equ    0x85
trisb    equ    0x86
RPO     equ 5

ORG 0x000
GOTO Init

ORG 0x004
GOTO Main
Init

BSF STATUS,RP0             ; go to bank 1
movlw 1FH
movwf PORTA                ; porta all input
movlw 00H
movwf PORTB                ; portb all output
goto Main

Main

btfss PORTA,0              ;if port a,0  = low
bsf PORTB,1           ;set port b,0 = high
btfsc PORTA,0              ;if port a,0 = high
bcf PORTB,1           ;set port b,0 = low
goto Main

END
I've simplified the circuit to use only the RB0 Pin on the MCU for the output and RA0as the input.

Last edited:

#### birdman0_o

##### Active Member
I see three things that might be wrong

#1 you are missing this line of code (it should be the third line)
__CONFIG _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

#2 255 = 0xFF not 0x1F

#3 bsf and bcf should not be indented

#4 get right of RPO and just put a 5

#5 you should not need to define all the ports and tris registers if you are using MPLAB

#6
PORTB,1 ;set port b,0 = low
are you checking pin 1 or 0?

Try some of this out,
Mike

Last edited:

#### skeeterb

##### Member
I'm using Multisim to simulate my circuit and MPLab to build the HEX file

#### birdman0_o

##### Active Member
Are the modifications working?

#### skeeterb

##### Member
No, not really. I'm getting a little frustrated

#### birdman0_o

##### Active Member
Is it not compiling?

#### skeeterb

##### Member
oh, it compiles, but it doesn't do anything when I try to use it in Multisim, it just sits there doing nothing.

Edit:

Here's what it shows when I build it in multisim

Multisim - 2009-05-20 15:59:31
--------------------------Building: Project: project1---------------------------
control.asm
Note: Due to path length limitations in the Microchip MPASM for PIC16, the path C:\Documents and Settings\Robin\My Documents\National Instruments\Circuit Design Suite 10.0\MCU Workspaces\controlcircuit\project1\ has been mapped to the drive Z: temporarily for this operation.
Assembler results: 0 - Errors, 0 - Warnings

Last edited:

#### birdman0_o

##### Active Member
Have u build the actual hardware yet?
Try it on that

#### skeeterb

##### Member
I don't have the stuff to build the circuit yet, I'm trying to simulate the circuit and get it working before I build it.

#### blueroomelectronics

##### Well-Known Member
Try running it through MPLAB or Oshonsofts simulator.
PS the transistor won't do much, use it as a current sink instead.
Also no need to redefine your SFRs as you're using an include file.

Thought you were going to switch to Swordfish BASIC for this HHO project?

Last edited:

#### skeeterb

##### Member
I have designed another secondary board to control the final stage of the circuit. I'm using 12V Relays to switch the system around from run to refill and back. I need the 12V coming through the transistor to activate the relay in order to switch from run to refill.

#### blueroomelectronics

##### Well-Known Member
Your relay should be driven like this.

Last edited:

#### skeeterb

##### Member
ok, my system is a relay driving a relay. The first relay switches from run to refill, and the second relay turns on the refill system which consists of a refill control valve and pump.

Edit:

How do you emulate the high/low of the inputs to activate the outputs in MPlab? I have MPlab and Multisim and have been working on the circuit using Multisim.

Last edited: