0
\$\begingroup\$

New learner on Microchip. I'm now trying to make the PIC16LF1829 to make the four LEDs blink in a frequency of 1 time/second. My intention was modify the oscillator frequency at different positions of the code with fixed amount of instruction cycles to approximate that '1 scond'.

At first, all I have is the command with 197,376 instruction cycles. Since those amount of cycles running in 500KHz of oscillation freq would last four about 1.5 seconds, I intended to use several higher frequencies such as 1MHz and 4MHz to approximate that '1 second'.

This makes me to modify the oscillator frequency at different positions of the code. However things turned out that when adding the same command like:

    movlw   0x5A
    movwf   OSCCON

the LEDs takes twice as long as before.

Can anyone help me figure out what's wrong with this?

Below is the code I have:

#include <p16f1829.inc>
 __CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF);
 __CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF);
    ORG     0
    errorlevel -302     ; suppress the 'not in bank0' warning
    cblock  0x70
      Delay1
      Delay2
      Count
    endc

Start:
    banksel OSCCON      ; Select bank1
    movlw   0x5A        ; running at 1MHz
;   movlw   b'00111000' ; set CPU clock speed
    movwf   OSCCON      ; set the clock frequency of 2MHz
    bcf     TRISC,0
    bcf     TRISC,1
    bcf     TRISC,2
    bcf     TRISC,3
    banksel LATC
    clrf    LATC        ; set all the LEDs off
Timelag1:
    movlw   0x5A
    movwf   OSCCON
    decfsz  Dleay1,F
    bra     Timelag1
    decfsz  Dleay2,F
    bra     Timelag1
Timelag2:
    movlw   0x6A        ; 4M
    movwf   OSCCON
    decfsz  Dleay1,F
    bra     Timelag2
    decfsz  Dleay2,F
    bra     Timelag2
Main:
    bsf     LATC,0      ; turn on the LED-0,with 4MHz
    bcf     LATC,1
    bcf     LATC,2
    bcf     LATC,3
    decfsz  Dleay1,F
    bra     Main
    decfsz  Dleay2,F
    bra     Main
Main_1:
    movlw   0x5A        ; 1M
    movwf   OSCCON
    decfsz  Dleay1,F    ; prolong the time with another 1MHz
    bra     Main_1
    decfsz  Dleay2,F
    bra     Main_1
    end

Best Regards,

Simon

\$\endgroup\$
  • \$\begingroup\$ Could you please indicate where you think the error lies and what you have already tried in solving this problem? \$\endgroup\$ – loudnoises Mar 31 '18 at 12:42
0
\$\begingroup\$

The posted code is failing because the writes to OSCCON change the DACCON1 register in BANK2, the bank where the LATC register is located.

To correct this problem change your code to select BANK1 before updating to the OSCCON register.

I think this code should work as you intended:

#include <p16f1829.inc>
 __CONFIG _CONFIG1, (_FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF);
 __CONFIG _CONFIG2, (_WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _LVP_OFF);
    ORG     0
    errorlevel -302     ; suppress the 'not in bank0' warning
    cblock  0x70
      Delay1
      Delay2
      Count
    endc

Start:
    banksel OSCCON      ; Select bank1
    movlw   0x5A        ; running at 1MHz
;   movlw   b'00111000' ; set CPU clock speed
    movwf   OSCCON      ; set the clock frequency of 2MHz
    bcf     TRISC,0
    bcf     TRISC,1
    bcf     TRISC,2
    bcf     TRISC,3
    banksel LATC        ; Select bank2
    clrf    LATC        ; set all the LEDs off
Timelag1:
    banksel OSCCON      ; Select bank1
    movlw   0x5A
    movwf   OSCCON
    decfsz  Dleay1,F
    bra     Timelag1
    decfsz  Dleay2,F
    bra     Timelag1
Timelag2:
    banksel OSCCON      ; Select bank1
    movlw   0x6A        ; 4M
    movwf   OSCCON
    decfsz  Dleay1,F
    bra     Timelag2
    decfsz  Dleay2,F
    bra     Timelag2
Main:
    banksel LATC        ; Select bank2
    bsf     LATC,0      ; turn on the LED-0,with 4MHz
    bcf     LATC,1
    bcf     LATC,2
    bcf     LATC,3
    decfsz  Dleay1,F
    bra     Main
    decfsz  Dleay2,F
    bra     Main
Main_1:
    banksel OSCCON      ; Select bank1
    movlw   0x5A        ; 1M
    movwf   OSCCON
    decfsz  Dleay1,F    ; prolong the time with another 1MHz
    bra     Main_1
    decfsz  Dleay2,F
    bra     Main_1
    end
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.