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:
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:
Now, wierdly, if i change the second last line to:
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!
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: