Ok this is EXTREMELY odd, I am making a PIC 16F84A programming, and I have it simulating like I wanting it, but when I load it onto my actually pic and test it, a certain part of the program randomly works correctly and randomly doesnt work correctly.
Here is the code:
The program is with the START function. During the 8 clock cycles, it is suppose to send 0000 0001 to the controller (RA1 bit is only suppose to go high the first clock cycle) and simulates as such. However when I turn on the pic, sometimes it incorrectly sends 0000 0011 and sometimes it correctly sends 0000 0001. When I turn it off and turn it on, it incorrectly sends it about 70% of the time and sends it right 30% of the time. Every other part of the program works, its just that part that is random.
It simulates just fine, so I dont have a clue on what could be going wrong. I'm sure my pic is hooked up correctly since everything else works fine and Ive never had this problem with any other programs ive made.
And I'm sure there isnt anything wrong with the actual PIC itself since i've tested this on multiple (brand new) PICs with the same result.
Here is the code:
Code:
errorlevel -302 ;Suppress bank warning
__config 3FFAh ; WDT Off, HS Oscillator, CP off
;****************************************************************************
;* Set Up Varibles *
;****************************************************************************
STATUS equ 0x03
TRISA equ 0x85
TRISB equ 0x86
PORTA equ 0x05
PORTB equ 0x06
COUNT1 equ 0x08
COUNT2 equ 0x09
COUNT3 equ 0x0C
CMDOUT equ 0x0D
DATAIN equ 0x0E
BTLOOP equ 0x0F
BIT equ 0x10
WTEMP equ 0x11
;****************************************************************************
;* Set Up Ports *
;****************************************************************************
bsf STATUS,5
movlw 0x01
movwf TRISA
; RA0 - DATA INPUT
; RA1 - COMMAND OUTPUT
; RA2 - ATT OUTPUT
; RA3 - CLOCK OUTPUT
movlw 0x00
movwf TRISB ; Button Outputs
bcf STATUS,5
;****************************************************************************
;* Set Initial Conditions *
;****************************************************************************
movlw 0x0C
movwf PORTA
movlw 0x00
movwf PORTB
;****************************************************************************
;* Main Program *
;****************************************************************************
call DELAY ; Delay
bcf PORTA,2 ; Bring ATT Low
call DELAY ; Delay
movlw 0x01 ; Set the command to be sent as 0x01
movwf CMDOUT ; Move it into variable
call START ; Send "Start" Command Byte 1
call DELAY
call DELAY
movlw 0x42 ; Set the command to be sent as 0x42
movwf CMDOUT ; Move into variable
call REQUESTDATA ; Request Controller Data Byte 2
bsf PORTA,2 ; Bring ATT High
movf DATAIN,w
movwf PORTB
LOOP
movlw 0x00
goto LOOP
;****************************************************************************
;* START - Send 0x01 to Controller *
;****************************************************************************
START
movlw 0x08 ; Set up 8 Clock Cycles
movwf BTLOOP ;
movlw 0x01 ; Set up Bit Counter
LOOP2
movwf BIT ; Move Bit
andwf CMDOUT,w ; Is CMD high?
skpz ; If not, skip next command
bsf PORTA,1 ; Set CMD bit high
bcf PORTA,3 ; Clock Low
call DELAY ; Delay
bsf PORTA,3 ; Clock High
rlf BIT,w ; Shift for next bit
bcf PORTA,1 ; Clear CMD bit
call DELAY ; Delay
decfsz BTLOOP
goto LOOP2
return
;****************************************************************************
;* REQUESTDATA - Sending 0x42 *
;****************************************************************************
REQUESTDATA
movlw 0x08 ; Set up 8 Clock Cycles
movwf BTLOOP ;
movlw 0x00
movwf DATAIN
movlw 0x01 ; Set up Bit Counter
LOOP3
movwf BIT ; Move Bit
andwf CMDOUT,w ; Is CMD high?
skpz ; If not, skip next command
bsf PORTA,1 ; Set CMD bit high
bcf PORTA,3 ; Clock Low
rrf PORTA,w ; Read bit from DATA in
rrf DATAIN,f ; shift bit
call DELAY ; Delay
bsf PORTA,3 ; Clock High
rlf BIT,w ; Shift for next bit
bcf PORTA,1 ; Clear CMD bit
call DELAY ; Delay
decfsz BTLOOP
goto LOOP3
return
;****************************************************************************
;* Delay Loop 195.84ms (x5 Delay Multiplier) *
;****************************************************************************
DELAY
movwf WTEMP
movlw 0xFF ; Set Up Delay
movwf COUNT1
movlw 0xFF
movwf COUNT2
movlw 0x05
movwf COUNT3
movf WTEMP,w
LOOP1
decfsz COUNT1,1 ; Decrease Count 1 1us x 255 = 255us
goto LOOP1 ; until Count1 is zero 2us x 255 = 510us
decfsz COUNT2,1 ; Repeat this process 1us x 255 = 255us
goto LOOP1 ; 255 times 2us x 255 = 510us
decfsz COUNT3,1 ; Repeat this process
goto LOOP1 ; 5 times
return ; Return (((3us * 255 times) * 255 times) + (3us x 255 times)) * 5
end
The program is with the START function. During the 8 clock cycles, it is suppose to send 0000 0001 to the controller (RA1 bit is only suppose to go high the first clock cycle) and simulates as such. However when I turn on the pic, sometimes it incorrectly sends 0000 0011 and sometimes it correctly sends 0000 0001. When I turn it off and turn it on, it incorrectly sends it about 70% of the time and sends it right 30% of the time. Every other part of the program works, its just that part that is random.
It simulates just fine, so I dont have a clue on what could be going wrong. I'm sure my pic is hooked up correctly since everything else works fine and Ive never had this problem with any other programs ive made.
And I'm sure there isnt anything wrong with the actual PIC itself since i've tested this on multiple (brand new) PICs with the same result.