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.

PIC 16f877A interrupt code problem - HELP!!!

Status
Not open for further replies.

burgerfeet

New Member
Hi everybody,

I am in desperate need of help with my interrput routine. Any pointers would be sincerely appreciated.

The code below reads the RX buffer and then executes a call based on the value of the character stored in the buffer.

org 0x0004
bsf PORTC,0
movf RCREG,0
movwf rx_command

; A - Realtime acquistion mode
movlw 0x41
subwf rx_command,0
btfss STATUS,Z
goto $+3
call adc_routine
goto exit_interrupt

; U - Upload Memory
movlw 0x55
subwf rx_command,0
btfss STATUS,Z
goto exit_interrupt
call sequential_read

exit_interrupt
retfie

The code works perfectly using OSHON PIC simulator. When I run the code in the PIC, it appears to hang, with no execution of the interrupt code. If I just call either routine from the interrupt it works fine.

The full code compiles to a 3K hex file with 400 out of the 8000 code memory addresses used. I was thinking that I am having paging errors. I will post the whole code if necessary but ir appears all messy when I paste it into the post editing box.

Many thanks in advance.

Andrew
 
Last edited:
You don't appear to be saving and restoring any registers?, I suggest you check the requirements for this.

I would also suggest you reconsider your method, ISR's should be as short and fast as possible - it's also bad practice to call subroutines from them, due to the small stack size.
 
Last edited:
Hello Nigel,

The code is too big to paste as whole (limit 20000 characters). If I replace the rx_command with a literal value it works ok.

Is there enough code here. - have just managed to attach the .ASM file.

#INCLUDE<P16F877A.INC>
__CONFIG _DEBUG_OFF&_CP_ALL&_WRT_HALF&_CPD_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_OFF&_WDT_OFF&_HS_OSC
errorlevel -305,-207,-302

;****** GPR's **************************************************************************

datai equ 20h ;Data input byte buffer
datao equ 21h ;Data output byte buffer
bytecount equ 22h ;Counter for byte loops
pollcnt equ 23h ;Counter for polling loops
loops equ 24h ;Delay loop counter
loops2 equ 25h ;Delay loop counter
hbc equ 26h ;High Byte address register
lbc equ 27h ;Low Byte address register
max_hb equ 28h ;Max High Byte address register
count_del equ 29h
count_ms equ 30h
count equ 31h ;Counter for ADC setup delay
ADhi equ 32h ;Binary input high byte
ADlo equ 33h ;Binary input low byte
thos equ 34h ;Thousands digit in decimal
huns equ 35h ;Hundreds digit in decimal value
tens equ 36h ;Tens digit in decimal value
ones equ 37h ;Ones digit in decimal value
i2c_eeprom equ 38h ;EEPROM address
rx_command equ 39h ;Hex value in USART rx register
val_us equ .249 ;short delay parameter
val_ms equ .100 ;long delay parameter

;****** Macro definitions***************************************************************

write_addr equ b'10100000' ;Control byte for write operations
read_addr equ b'10100001' ;Control byte for read operations

;***************************************************************************************

org 0x0000 ;Main program start address
nop ;Required for ICD
goto init

;****** Interrupt Routine **************************************************************

org 0x0004
bsf PORTC,0
movf RCREG,0
movwf rx_command

; A - Realtime acquistion mode
movlw 0x41
subwf rx_command,0
btfss STATUS,Z
goto $+3
call adc_routine
goto exit_interrupt

; U - Upload Memory
movlw 0x55
subwf rx_command,0
btfss STATUS,Z
goto exit_interrupt
call sequential_read

exit_interrupt
retfie
 

Attachments

  • BGS Analogue Input A2D + RS232 Test.asm
    23.3 KB · Views: 174
Last edited:
As I've already said, you're not saving and restoring any registers in the ISR - if you don't it WILL crash - read the datasheets, and check my tutorials that use interrupts. Try my seven segment LCD tutorial, which uses timer interrupts.
 
Sorry Nigel. I am about to look at your tutorial. What seems to be confusing me is that it works in the simulator so my simplistic view is should work in reality but as I have now discovered, the theories and the realities differ greatly.

....will update shortly. Thanks again
 
Sorry Nigel. I am about to look at your tutorial. What seems to be confusing me is that it works in the simulator so my simplistic view is should work in reality but as I have now discovered, the theories and the realities differ greatly.

I never use simulators, there seems little point, as they are only partially accurate.
 
Hello Nigel,

Still no joy. Saving and restoring the registers as shown in your tutorial but still hangs. Have hooked it up to MPLAB ICD2 but cannot get the ICD2 to see the character arrive at the UART. I am at a complete loss.

I can execute simple math routines within the interrupt, turn leds on and off at the ports with no problem. As soon as I try to use the rx_command value into the w register and try an subtract a literal....all stops. Use a literal inplace of the rx_command file, no problems.

I have to solve this as I am totally at a stop but certainly at the moment I do not know how. Have spent hours and no further forward.

Thanks,
Andrew
 
hi Andrew,
You can attach an *.asm file to your posts by using the 'manage attachments' button lower down the REPLY window.

Attach your complete listing and I'll run in thru my Oshonsoft and MPLAB.
 
Last edited:
Hi Eric.....have attached, did it work. Thanks
 

Attachments

  • BGS Analogue Input A2D + RS232 BACKUP Test.asm
    8.6 KB · Views: 178
"The code below reads the RX buffer and then executes a call based on the value of the character stored in the buffer."

I am just pointing out the obvious here I know but are you sure you're sending the REQUIRED character (HEX - ASCII- etc) at the CORRECT BAUD RATE?
 
Last edited:
Hi shingadaddy,

Sending at 19200, individual characters. PC software sends one character (ASCII) and then waits before sending other. Although no handshaking is set up it works well as the pic only transmits on receipt of a character.

The software worked before the character differentition was added. I even looked at the line with a logical analyser just in case. The scope was certainly showing the correct character. Not sure if you have tried it yet with OSHON, but I found it worked great.
 
Last edited:
one thing after another :eek:

I have attached a zip with everything I have associated with the project. Fingers crossed!!! Thanks
 

Attachments

  • BGS Data Logger.zip
    9 KB · Views: 126
Your ISR doesn't save W or STATUS. What does the EXIT statement do? (If it's a label it should be in the left column)

The labels are in the right place, it is just when I pasted the code in the box to post it, it just reformated the layout. Yes, the EXIT you see is just a label.

I have tried saving the W, STATUS, PCLATH etc and it still does not work

Appreciate you looking.
 
hi Andrew,
After much fiddling I managed to get it assembled in MPLAB.
It will not assemble in Oshonsoft.

Its not the same listing as you posted in your original posts..:confused:
 
From a more basic point of view, why are you making it far more complicated than it needs to be?, there seems no reason to be using interrupts at all on such a simple program.
 
hi Andrew,
After much fiddling I managed to get it assembled in MPLAB.
It will not assemble in Oshonsoft.

Its not the same listing as you posted in your original posts..:confused:
Sorry Eric, I have been fiddling. Doing other bits and pieces inbetween. The ASM attached now I have just tested and I will leave alone until I hear from you. I have never tried to assemble in with OSHON, I used MPLAB. I just test the .HEX file with the simulator. :eek:
 

Attachments

  • BGS Analogue Input A2D + RS232 Test.asm
    23.4 KB · Views: 127
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top