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.

Writing to Tmr2 prescaler+ postscaler

Status
Not open for further replies.

asp1987

New Member
Hi,
I was going through Nigel's tutorial on PWM. He has set the prescaler and postscaler as 16 and 1 resp. by writing to T2CON in 'two' steps. Is that any standard way of writing to the same register? Couldn't find any such statements in 16F877A datasheet.
Code:
    	MOVF     T2CON,W	;set prescaler to 16
    	ANDLW    0xF8		;PWM at 2500HZ
    	IORLW    0x02
    	MOVWF    T2CON

    	MOVF     T2CON,W	;set postscaler to 1
    	ANDLW    0x07
    	IORLW    0x00
    	MOVWF    T2CON
 
Hi,
I was going through Nigel's tutorial on PWM. He has set the prescaler and postscaler as 16 and 1 resp. by writing to T2CON in 'two' steps. Is that any standard way of writing to the same register? Couldn't find any such statements in 16F877A datasheet.
Code:
    	MOVF     T2CON,W	;set prescaler to 16
    	ANDLW    0xF8		;PWM at 2500HZ
    	IORLW    0x02
    	MOVWF    T2CON

    	MOVF     T2CON,W	;set postscaler to 1
    	ANDLW    0x07
    	IORLW    0x00
    	MOVWF    T2CON

hi,
I would have written

CLRF T2CON
bsf T2CON,1

; remember to enable the TMR with bsf T2CON,2

Is this what you mean.?
 
It was done that way so you only change the bits you want to - so it could be done during program execution, and not just during initialisation.

Yes, I do follow the logic of that.:)
 
It was done that way so you only change the bits you want to - so it could be done during program execution, and not just during initialisation.

I didn't get you. Both the codes can be written anywhere, right? The one using
Code:
clrf T2CON
bsf T2CON,1
is smaller too.
 
I didn't get you. Both the codes can be written anywhere, right? The one using
Code:
clrf T2CON
bsf T2CON,1
is smaller too.

hi,
My preference is to bsf/bcf bits, IMO its more clear in what you are doing.
 
Pick a style or method that works for you. Use meaningful comments. And please don't get into the bad habit of using short-cut config' fuse settings found in some tutorials. Use meaningful __config statements in your source code.

Mike

Code:
;
;  setup UART module, 57600 baud (0.8% bit rate error, 8 MHz)
;
        movlw   low(brgval)     ;                                 |B1
        movwf   SPBRG           ;                                 |B1
        movlw   high(brgval)    ;                                 |B1
        movwf   SPBRGH          ;                                 |B1
        bsf     BAUDCTL,BRG16   ; select 16 bit BRG               |B1
        movlw   b'00100100'     ; '0-------' CSRC, n/a (async)    |B1
                                ; '-0------' TX9 off, 8 bits      |B1
                                ; '--1-----' TXEN, tx enabled     |B1
                                ; '---0----' SYNC, async mode     |B1
                                ; '----0---' SENDB, send brk      |B1
                                ; '-----1--' BRGH, high speed     |B1
                                ; '------00' TRMT, TX9D           |B1
        movwf   TXSTA           ;                                 |B1
        bcf     STATUS,RP0      ; bank 0                          |B0
        movlw   b'10010000'     ; '1-------' SPEN, port enabled   |B0
                                ; '-0------' RX9 off, 8 bits      |B0
                                ; '--0-----' SREN, n/a (async)    |B0
                                ; '---1----' CREN, rx enabled     |B0
                                ; '----0---' ADDEN off            |B0
                                ; '-----000' FERR, OERR, RX9D     |B0
        movwf   RCSTA           ;                                 |B0
        movf    RCREG,W         ; flush Rx Buffer                 |B0
        movf    RCREG,W         ;                                 |B0
;
;  setup timer 2 for 1 msec interrupts (8 MHz clock)
;
        clrf    TMR2            ;                                 |B0
        movlw   b'00000010'     ; '-0000---' TOUTPS, postscale 1  |B0
                                ; '-----0--' TMR2ON, timer off    |B0
                                ; '------10' T2CKPS, prescale 16  |B0
        movwf   T2CON           ; 8.0 usec 'ticks', 8 MHz clock   |B0
        bsf     STATUS,RP0      ; bank 1                          |B1
        movlw   125-1           ; 125 x 8 usec ticks = 1 msec     |B1
        movwf   PR2             ;                                 |B1
        bsf     PIE1,TMR2IE     ; enable timer 2 interrupts       |B1
        bcf     STATUS,RP0      ; bank 0                          |B0
 
I didn't get you. Both the codes can be written anywhere, right? The one using
Code:
clrf T2CON
bsf T2CON,1
is smaller too.

Yes, you can write it how you like.

The method I used allows you to set, or clear, any bits you want without disturbing the others. A few lines of BSF and BCF would do the same, but is less elegant :D

A couple of bytes though isn't generally a problem.
 
Hey Mike!

Could you please explain this a bit more? An example if it is possible?

He means write a confusing line that's so wide you need to scroll your screen to read it all! :D

I've always used just the hexadecimal value because it's short and simple, and my programmer software displays the hex value for you - so it's far less trouble.
 
Here's what I meant.

The first example below is similar to the "short-cut" used in some tutorials which is considered by many as poor programming practice. You would have to put that hex value into programming software to list the actual fuse settings or put that value into an MPASM source file to list the actual fuse settings.

The second example shows you the actual fuse settings at-a-glance. No extra work and no guessing. You don't need to include "default" settings (where all fuse bits for that setting are 1s) in the config' line. This reduces the length of the line.

Sure, it's much easier to come up with a config' hex value by using the drop down fuse settings window in your programming software. For that reason I've considered writing a Config' Builder program in BASIC more than once. It would allow selecting fuse settings from drop downs just like your favourite programming software and place the correct MPASM formatted lines into the clipboard. Once in the clipboard you could paste them into your source file.

Mike

Code:
        processor PIC16F887
        include    "p16f887.inc"

 __CONFIG _CONFIG1  0x27E4
 __CONFIG _CONFIG2  0x3DFF
Code:
        processor PIC16F887
        include    "p16f887.inc"
    
;--< config settings >---------------------------------------------

  __CONFIG _CONFIG1, _LVP_OFF&_FCMEN_OFF&_PWRTE_ON&_WDT_OFF&_INTOSCIO
  __CONFIG _CONFIG2, _WRT_256

;
;       _DEBUG_OFF      ; default, debug mode off
;       _LVP_OFF        ; -- low voltage programming off
;       _FCMEN_OFF      ; -- fail safe clock monitor off
;       _IESO_ON        ; default, internal/external switchover on
;       _BOR_ON         ; default, brown out reset on
;       _CPD_OFF        ; default, code protect eeprom off
;       _CP_OFF         ; default, code protect off
;       _MCLR_ON        ; default, mclr 'reset' function
;       _PWRTE_ON       ; -- power up timer enable on
;       _WDT_OFF        ; -- watch dog timer off
;       _INTOSCIO       ; -- internal oscillator with I/O
;
;       _WRT_256        ; -- 0000..00FF write protected
;       _BOR40V         ; default, brown out reset at 4.0 volts
 
Last edited:
Ah I see what you are saying. Thanks.

Are the _XXXXXX defined in the .inc file?

They software you suggested it would be pretty easy to implement and also very useful!
 
Ah I see what you are saying. Thanks.

Are the _XXXXXX defined in the .inc file?
This is my folder::: C:\Program Files\Microchip\MPASM Suite\Template\Code

They software you suggested it would be pretty easy to implement and also very useful!

hi,
I would use the method that Mike demonstrates.
Once you have a proven CONFIG and header for that PIC type, save it as a 'header1.asm' and use the header1.asm as a starting point for all your future programs using that type of PIC.

Its all very well writing program that is 'elegant' but I would choose 'clarity'.

If you use the register and bit names, to set/reset bits, you [ and others] can far more easily understand and debug your code.

IMHO
;this is far easier to follow
Code:
CLRF T2CON
bsf T2CON,1; prescaler 16, postscaler  1

;than this
Code:
MOVF     T2CON,W	;set prescaler to 16
    	ANDLW    0xF8		;PWM at 2500HZ
    	IORLW    0x02
    	MOVWF    T2CON

    	MOVF     T2CON,W	;set postscaler to 1
    	ANDLW    0x07
    	IORLW    0x00
    	MOVWF    T2CON
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top