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.

18F2420 Program Counter Problem

Status
Not open for further replies.
Hello,

Hope all you 'forumers' are ok.

I need some help with my program. My program doesn't jump to where I think it should when I add an offset of 0x18 to PCL, in fact the program hangs.

I add the value 0x18 to 0xE0 (PCL) and expect to go to 0xF8. If you look at the attached screenshot I think you will see what I am trying to do.

The prog does a 'GOTO' ok to all days except Saturday. I can't see what's different about that one.

Code:
   movf   day,w                  ;BCD day of the week number (Sun = 0, Sat = 6)
    andlw   0x0f                    ;clear upper nybble
    rlncf  WREG,W                   ;double it
    rlncf  WREG,w                   ;and again (because of x2 pc increments)
    addwf  PCL                      ;add it (day of week number x4) to prog counter low byte  
     
    goto   Sunday
    goto   Monday
    goto   Tuesday
    goto   Wednesday
    goto   Thursday
    goto   Friday
    goto   Saturday
    
Sunday
    movlw   low dt_sun
    movwf   lowbyte
    movlw   high dt_sun
    movwf   highbyte
    goto    Msg_count
    
Monday
    movlw   low dt_mon
    movwf   lowbyte
    movlw   high dt_mon
    movwf   highbyte
    goto    Msg_count
    
Tuesday
    movlw   low dt_tues
    movwf   lowbyte
    movlw   high dt_tues
    movwf   highbyte
    goto    Msg_count
    
Wednesday
    movlw   low dt_wed
    movwf   lowbyte
    movlw   high dt_wed
    movwf   highbyte
    goto    Msg_count
    
Thursday
    movlw   low dt_thurs
    movwf   lowbyte
    movlw   high dt_thurs
    movwf   highbyte
    goto    Msg_count
    
Friday
    movlw   low dt_fri
    movwf   lowbyte
    movlw   high dt_fri
    movwf   highbyte
    goto    Msg_count
    
Saturday
    movlw   low dt_sat
    movwf   lowbyte
    movlw   high dt_sat
    movwf   highbyte
    ;goto    Msg_count                        
    
        
Msg_count    
    movf    msg_cnt,w
    call    Day_txt             
    tstfsz  WREG
    goto    $+8
    clrf    msg_cnt
    return                  ;leave routine                      
    call    lcd_char
    incf    msg_cnt
    goto    Msg_count


Thanks in advance for any help.
 

Attachments

  • PCL1.jpg
    PCL1.jpg
    624.5 KB · Views: 259
Your code will jump to 0x6F8 as PCLATH contains 0x06. Clear PCLATH and it will work as expected.

Mike.
 
Last edited:
Why, for values from 0-6 are you AND'ing with 15?.

AND with 7 instead, and add a 7th dummy target, just in case.

I'd also tend to use a sequential jump table, rather than multiplying to correct for the multi-line routines.
 
Last edited:
Why, for values from 0-6 are you AND'ing with 15?.

AND with 7 instead, and add a 7th dummy target, just in case.

I'd also tend to use a sequential jump table, rather than multiplying to correct for the multi-line routines.


Hi,

I tried Pommie's suggestion and it appears to work ok. Thanks Pommie. I'm sure I tried clearing PCLATH before but still had trouble, I must have been screwing it up elsewhere as well! No suprise there :)

Nigel,

I'm getting data fom a RTC chip to send to an lcd. I'm still thinking about your AND with 7 suggestion. I made the routine I posted before to save having to make a data table for each day-of-the-week string.

What's a SEQUENTIAL JUMP TABLE?

Code:
movf   day,w                    ;BCD day of the week number (Sun = 0, Sat = 6)
    andlw   0x0f                    ;clear upper nybble
    rlncf  WREG,W                   ;double it
    rlncf  WREG,w                   ;and again (because of x2 pc increments)
    clrf   PCLATH   ;THANKS POMMIE
    addwf  PCL                      ;add it (day of week number x4) to prog counter low byte  
     
    goto   Sunday
    goto   Monday
    goto   Tuesday
    goto   Wednesday
    goto   Thursday
    goto   Friday
    goto   Saturday
    
Sunday
    movlw   low dt_sun
    movwf   lowbyte
    movlw   high dt_sun
    movwf   highbyte
    goto    Msg_count
    
Monday
    movlw   low dt_mon
    movwf   lowbyte
    movlw   high dt_mon
    movwf   highbyte
    goto    Msg_count
    
Tuesday
    movlw   low dt_tues
    movwf   lowbyte
    movlw   high dt_tues
    movwf   highbyte
    goto    Msg_count
    
Wednesday
    movlw   low dt_wed
    movwf   lowbyte
    movlw   high dt_wed
    movwf   highbyte
    goto    Msg_count
    
Thursday
    movlw   low dt_thurs
    movwf   lowbyte
    movlw   high dt_thurs
    movwf   highbyte
    goto    Msg_count
    
Friday
    movlw   low dt_fri
    movwf   lowbyte
    movlw   high dt_fri
    movwf   highbyte
    goto    Msg_count
    
Saturday
    movlw   low dt_sat
    movwf   lowbyte
    movlw   high dt_sat
    movwf   highbyte
    ;goto    Msg_count                        
    
        
Msg_count    
    movf    msg_cnt,w
    call    Day_txt             
    tstfsz  WREG
    goto    $+8
    clrf    msg_cnt
    return                  ;leave routine                      
    call    lcd_char
    incf    msg_cnt
    goto    Msg_count


Code:
Day_txt                                
        rlncf   WREG,W              
        movwf   temp
        movf	highbyte,w              ;USES A VARIABLE HERE   
        movwf	PCLATH              
        movf    lowbyte,w               ;USES A VARIABLE HERE
        addwf   temp,w              
        btfsc   STATUS,C            
        incf    PCLATH              
        movwf   PCL
                             
dt_sun
	    dt      "Sunday \0"                          
dt_mon
	    dt      "Monday \0"	
dt_tues
	    dt      "Tuesday \0"	    
dt_wed
	    dt      "Wednesday \0"
dt_thurs
	    dt      "Thursday \0"
dt_fri
	    dt      "Friday \0"
dt_sat
	    dt      "Saturday \0"
 
Nigel,

I'm getting data fom a RTC chip to send to an lcd. I'm still thinking about your AND with 7 suggestion. I made the routine I posted before to save having to make a data table for each day-of-the-week string.

Why are you AND'ing with 15, when your jump table is only 7 entries long?, it means you have a possible 7 correct jumps, and 9 incorrect ones. Hopefully it shouldn't make any difference, but in that case why bother AND'ing in the first place, as not doing so shouldn't make any difference either.

Personally I always AND to ensure that it can't possible exceed the bounds of the jump table.

What's a SEQUENTIAL JUMP TABLE?

Sorry, I wasn't paying attention, you're already doing that :D
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top