Continue to Site

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.

  • 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.

16F819 my inputs don't work!

Status
Not open for further replies.

masCh

New Member
Hi, I'm trying to get my head around the PIC stuff.

I have written code in MPLAB IDE and been trying to simulate it with PIC Simulator IDE trial from Oshonsoft.

I have even copied some codes from the datasheet but it doesn't help.

Here is my code:

Code:
LIST   		P=PIC16F819
#include 	<P16F819.inc> ;processor specific variable definitions
radix 		hex
errorlevel 	-302, -207
__config 	_INTRC_IO & _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF

DC MACRO N
MOVLW (3*N)
CALL DELAY
ENDM
;
DD MACRO N
MOVLW (1*N)            
			movlw     0x1A         ;AV|PSU|RDY|MB|AMP|xxx=011010  >turn RDY on
			movwf     PORTB
CALL REARDELAY
			movlw     0x13         ;AV|PSU|RDY|MB|AMP|xxx=010010  >turn RDY off
			movwf     PORTB
ENDM
;

CBLOCK H'20'
COUNT1, COUNT2, COUNT3, COUNT4
ENDC
;
ORG  H'00'   
;*******************SETUP CONSTANTS*******************
;* 	Azrudi Mustapha: Shutdown Controller & Rear switcher V2.0
;*      OREN         - RA1 - IGNITION   - HI 5V
;*      KUNING       - RA0 - REAR       - LO 0V
;*      COKLAT       - RB1 - AMP EN     - HI 5V - turns AMP EN on
;*      HIJAU MERAH  - RB2 - MB ON      - HI 5V turns switch on
;*      PUTIH GREY   - RB3 - READY      - HI 5V turns it on
;*      MERAH TEBAL  - RB4 - PSU ON     - HI turns it on
;*	    PURPLE BIRU  - RB5 - AV SW      - HI switches VID		
;*		
;*****************************************************		


CLRF    INTCON
CLRF    PCLATH       ; ensure page 0 is used

GOTO    INITIALIZATION  ; go to beginning of program

ORG H'04'
retfie

;*******************************************************
;**
;**
;**	         DECLARATION & INITIALIZATIONS
;**
;**
;*******************************************************
ORG H'05'
INITIALIZATION
; enable below to enable interrupts
; bsf        INTCON,7      ;GIE – Global interrupt enable (1=enable)
; bsf        INTCON,4      ;INTE - RB0 Interrupt Enable (1=enable)
; bcf        INTCON,1      ;INTF - Clear FLag Bit Just In Case

banksel 	PORTA			;select BANK of PORTA
CLRF		PORTA

banksel		ADCON1
MOVLW		0x06
MOVWF		ADCON1

MOVLW		0xFF
MOVWF		TRISA			;set RA0-RA7 as inputs


bsf		STATUS,5			;switch to bank 1

	movlw   b'01100000' 	;clock is 4 kHz
	movwf   OSCCON 

	movlw	b'00000000'     ;8 bits of PORTB are output
	movwf   TRISB

bcf		STATUS,5			;switch back to bank 0
CLRF	PORTA
CLRF	PORTB

;initialize COUNTx registers, this is used upon program execution only, 
;after this automatically reset to 0xFF upon subtraction from 0x00
movlw		0xFF
movwf		COUNT1
movlw		0xFF
movwf		COUNT2
movlw		0xFF
movwf		COUNT3
movlw		0xFF
movwf		COUNT4







;*******************************************************
;**
;**
;**					START OF PROGRAM
;**
;**
;*******************************************************

;initialize OUTPUT state
movlw        0x00       ;AV|PSU|RDY|MB|AMP|xxx=000000  >turn RDY off
movwf        PORTB

DC			 1

Start
 
TUNGON:
             movlw     0x08         ;AV|PSU|RDY|MB|AMP|xxx=001000  >turn RDY on
             movwf     PORTB
			 DC		   1
	
             BTFSS     PORTA,1
             Goto      TUNGON

             movlw     0x10         ;AV|PSU|RDY|MB|AMP|xxx=010000  >turn PSU on
             movwf     PORTB
             DC         2            ; 2 sec delay
             movlw     0x1C         ;AV|PSU|RDY|MB|AMP|xxx=011100  >touch MB switch
             movwf     PORTB
             DC         1            ; 1 sec delay
             movlw     0x10         ;AV|PSU|RDY|MB|AMP|xxx=010000  >release MB switch
             movwf     PORTB
             DC         3
             movlw     0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >turn AMP on
             movwf     PORTB
             DC         2



TUNGOFF:
             movlw     0x1A         ;AV|PSU|RDY|MB|AMP|xxx=011010  >turn RDY on
             movwf     PORTB
             DC         1

             movlw     0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >turn RDY off
             movwf     PORTB
             DC         1

             BTFSC     PORTA, 1
             Goto      TUNGOFF

             DC         5
             BTFSC     PORTA, 1     ;check if really want to turn off
             Goto      TUNGOFF

             DC         3
             movlw     0x16         ;AV|PSU|RDY|MB|AMP|xxx=010110  >press MB switch
             movwf     PORTB
             DC         1
             movlw     0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >release MB switch
             movwf     PORTB
             DC         5
             movlw     0x10         ;AV|PSU|RDY|MB|AMP|xxx=010000  >turn AMP off
             movwf     PORTB
             DC         5

             goto      Start














;*******************************************************
;**
;**
;**					Subroutines
;**
;**
;*******************************************************

DELAY          ;197ms delay
   decfsz      COUNT1,F
   goto        DELAY
   ;disabled the next 2 lines for faster monitoring in simulator
   ;decfsz      COUNT2,F
   ;goto        DELAY   
   BTFSC       PORTA,0				;if PORTA,0 is 0 then skip next line
   call        REARACTIVE           
   ADDLW       -1
   BTFSS       STATUS,Z
   goto        DELAY
   return

   

REARACTIVE
   movlw       0xFE         ;AV|PSU|RDY|MB|AMP|xxx=1000 0000
   movwf       PORTB
   goto REARACTIVE

   DD          2
   BTFSS       PORTA,0				;if PORTA,0 is 0 then
   return							;return

   DD		   4
   movlw       0x32         ;AV|PSU|RDY|MB|AMP|xxx=110010  >Switch AV on
   movwf       PORTB
   DD          1
   movlw       0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >Switch AV off
   movwf       PORTB
   DD          2
   
   ;wait for rear to go off, meaning RA0=HI
WAITREAR
   DD          2
   BTFSC       PORTA,0
   Goto        WAITREAR
   
   movlw       0x32         ;AV|PSU|RDY|MB|AMP|xxx=110010  >Switch AV on
   movwf       PORTB
   DD          1
   movlw       0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >Switch AV off
   movwf       PORTB
   DD          4
   movlw       0x32         ;AV|PSU|RDY|MB|AMP|xxx=110010  >Switch AV on
   movwf       PORTB
   DD          1
   movlw       0x12         ;AV|PSU|RDY|MB|AMP|xxx=010010  >Switch AV off
   movwf       PORTB
   DD          2
   return      
   
  
REARDELAY      ;197ms delay


   decfsz      COUNT3,F
   goto        REARDELAY
   ;disabled the next 2 lines for faster monitoring in simulator
   ;decfsz      COUNT4,F
   ;goto        REARDELAY
   ADDLW       -1
   BTFSS       STATUS,Z
   goto        REARDELAY
   return
   
end

My chip is supposed to have RA0-RA7 as digital inputs and RB0-RB7 as digital outputs, running with internal clock at 4 Hz (MHz? KHz?)

With the above code, RB3 lights up after 2000usecs as it should.
Now I should input 5V to either RA0 or RA1, and RB1-RB4 should light up (LEDs are connected to the RBx's thru NPN BJT configuration. However, nothing changes and RB3 continues to be lit up with no other pin on PORTB showing any change.

I took this code from the Microchip datasheet:
Code:
banksel 	PORTA			;select BANK of PORTA
CLRF		PORTA

banksel		ADCON1
MOVLW		0x06
MOVWF		ADCON1

MOVLW		0xFF
MOVWF		TRISA			;set RA0-RA7 as inputs

Now, wierdly, if i change the second last line to:
Code:
MOVLW           0x00
MOVWF		TRISA

then the inputs start to work.. I have not been able to test whether the whole circuit works with this yet, but shouldn't I set the pins to 1 to signify it as input?

p/s sorry for my english, its not my first language. Hope the meaning gets through!
 
Last edited:
I just tested the PIC on a circuit.. it doesn't work at all like the sim!

Do I need to connect anything at the RA6 and RA7?

The only connections I have made are:
RA0 : from a relay that supplies either 0V or 5V
RA1 : from a relay that supplies either 0V or 5V
Vdd : 5V sourced from 7805
Vss : Gnd
RB1 : to resistor and base of BJT.. RB1=1 makes an LED1 turn on
RB2 : to resistor and base of BJT.. RB2=1 makes an LED2 turn on
RB3 : to resistor and base of BJT.. RB3=1 makes an LED3 turn on
RB4 : to resistor and base of BJT.. RB4=1 makes an LED4 turn on
RB5 : to resistor and base of BJT.. RB5=1 makes an LED5 turn on

All I see is LED3 blinking every second.. its as if the PIC is resetting itself over and over again..
 
It sounds like the watch dog timer is reseting it. Are you sure you have MPLAB set to use the config in the code. Select menu configure->configuration bits and check there is a tick at the top.

Also, do you have A1 tied low? And did you make the delay long again?

Mike.
 
That's the exact code I am compiling on MPLAB, yes there is a tick under the Configuration Bits tab.

A1 is not tied low, it is connected from a relay.. when the relay is off, A1 is connected to gnd and when the relay is energized, A1 is connected to Vdd..

I have tried to simplify the code just to test delays, but even that don't work..

Code:
LIST                P=PIC16F819
#include         <P16F819.inc> ;processor specific variable definitions
radix               hex
errorlevel        -302, -207
__config         _INTRC_IO & _CP_OFF & _DEBUG_OFF & _WRT_ENABLE_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_OFF & _WDT_OFF

DC MACRO N
    MOVLW     (10*N)
    CALL         DELAY
ENDM
;

CBLOCK H'20'
COUNT1, COUNT2, COUNT3, COUNT4
ENDC
;

ORG  H'00'  
;*******************SETUP CONSTANTS*******************
;*      Azrudi Mustapha: Shutdown Controller & Rear switcher V2.0
;*      OREN                 - RA1 - IGNITION  - HI 5V
;*      KUNING           - RA0 - REAR           - LO 0V
;*      COKLAT           - RB1 - AMP EN     - HI 5V - turns AMP EN on
;*      HIJAU MERAH      - RB2 - MB ON          - HI 5V turns switch on
;*      PUTIH GREY       - RB3 - READY          - HI 5V turns it on
;*      MERAH TEBAL      - RB4 - PSU ON    - HI turns it on
;*      PURPLE BIRU      - RB5 - AV SW      - HI switches VID       
;*       
;*****************************************************       


CLRF        INTCON
CLRF        PCLATH               ; ensure page 0 is used

GOTO    INITIALIZATION      ; go to beginning of program

ORG H'04'
retfie

;*******************************************************
;**
;**
;**             DECLARATION & INITIALIZATIONS
;**
;**
;*******************************************************
ORG H'05'
INITIALIZATION
; enable below to enable interrupts
; bsf        INTCON,7      ;GIE – Global interrupt enable (1=enable)
; bsf        INTCON,4      ;INTE - RB0 Interrupt Enable (1=enable)
; bcf        INTCON,1      ;INTF - Clear FLag Bit Just In Case

banksel         PORTA            ;select BANK of PORTA
CLRF            PORTA

banksel            ADCON1
MOVLW      0x06
MOVWF        ADCON1

MOVLW        0xFF
MOVWF        TRISA            ;set RA0-RA7 as inputs


bsf                STATUS,5            ;switch to bank 1

 movlw       b'01100000'     ;clock is 4 kHz
 movwf       OSCCON

 movlw        b'00000000'     ;8 bits of PORTB are output
 movwf       TRISB

bcf                STATUS,5            ;switch back to bank 0
CLRF            PORTA
CLRF            PORTB

;initialize COUNTx registers, this is used upon program execution only,
;after this automatically reset to 0xFF upon subtraction from 0x00
movlw            0xFF
movwf            COUNT1
movlw            0xFF
movwf            COUNT2
movlw            0xFF
movwf            COUNT3
movlw            0xFF
movwf            COUNT4







;*******************************************************
;**
;**
;**                    START OF PROGRAM
;**
;**
;*******************************************************

;initialize OUTPUT state
movlw        0x02
movwf            PORTB

DC                 2

Start

    movlw    0x0C
    movwf     PORTB
    DC        5

    movlw    0x08
    movwf    PORTB
    DC        5

    goto    Start



;*******************************************************
;**
;**
;**                    Subroutines
;**
;**
;*******************************************************

DELAY                                  ;197ms delay
     decfsz          COUNT1,F
     goto            DELAY
     ;disabled the next 2 lines for faster monitoring in simulator
     decfsz         COUNT2,F
     goto            DELAY  

     ADDLW       -1
     BTFSS           STATUS,Z
     goto            DELAY
     return

 

 
end

Its supposed to output PORTB RB1 high for 2 secs,
then PORTB RB2 and RB3 high for 5 secs
then PORTB RB3 high alone for 5 secs.

but on chip, i see this:
RB1 high for 7 secs
RB2 and RB3 high for a few millisecs
and this continues over and over..

RB1 should never be high anymore.. but I did this so I can test if the chip is resetting. It seems it is resetting over and over
 
Last edited:
This is sounding like a supply problem.

Do you have a decent supply or are you using old batteries?
Do you have decoupling capacitors?
Have you got current limiting resistors in the base circuits?
Do you have current limiting resistors in series with the LEDs?
Can you post a complete schematic?

The code works fine in MPSIM.

Mike.
 
Last edited:
Hi there Mike

I'm using a 7805 that takes a 12V source from a computer power supply..
The 7805 powers pretty much everything in the circuit.

I don't have decoupling capacitors, what are they for? Where do I need to use them?

I have current limiting resistors in the base circuits and the LEDs..

There isn't much schematic to post, I can take a photo but i don't think it will explain anything.. the entire circuit is basically as described in post #2.

Thanks for testing the code.
 
Decoupling caps are small capacitors (0.1uF) to absorb high frequency noise and are normally placed close to chip power pins.

Do you have the normal capacitors on the 7805? See 7805 data sheet.

The reason I ask is it sounds like a brownout. That is where the supply drops below a certain level and the chip does a reset. Can you put a biggish capacitor across the supply lines (after the 7805) and see if that helps?

Your LEDs aren't 1W ones are they?

Mike.
 
Wow, you got it right Mike..

I would never in a million years be able to solve this problem. I connected the 5V end of the 7805 directly to a 5V source from a computer power supply, and the circuit works as it should!

So I guess the culprit is the 7805. Do you think adding caps between the 5V and gnd is enough to solve it? Or should I find another chip to supply the 5V?
 
The 7805 is used for lots of circuits and works well. Just add the required capacitors and it will be good. Nice to hear it is working as you intended.

Mike.
 
Hi Mike, just would like to update you.. I've got the entire circuit working just by adding a 220uF capacitor. I also removed some relays for the PIC input. Previously I had the relay to switch to feed either gnd or 5V. Now I tied up the PIC input pin to Vdd and connected to the collector of an npn BJT. I hope that reduces the power consumption of the entire circuit. So far it works fine under testing environment. I hope its good enough in the field.

By the way, I'm a little confused about the banks.
In my code,

Code:
banksel 	PORTA			;select BANK of PORTA
CLRF		PORTA

banksel		ADCON1
MOVLW		0x06
MOVWF		ADCON1

MOVLW		0xFF
MOVWF		TRISA			;set RA0-RA7 as inputs


bsf		STATUS,5			;switch to bank 1

	movlw   b'01100000' 	;clock is 4 kHz
	movwf   OSCCON 

	movlw	b'00000000'     ;8 bits of PORTB are output
	movwf   TRISB

bcf		STATUS,5			;switch back to bank 0
CLRF	PORTA
CLRF	PORTB

It does look like I am just pasting different codes together, I am honestly don't really understand it perfectly. Is there a best-practice way of defining the ports? Should I use bcf/bsf or banksel?

It probably wont matter for this project because you know what they say, don't fix something that aint broken! But the info would be useful in the future I think.

Thanks for all your help!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top