Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
LinkBack Thread Tools Display Modes
Old 14th August 2008, 07:14 PM   (permalink)
Default Writing to Tmr2 prescaler+ postscaler

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
asp1987 is offline  
Old 14th August 2008, 07:25 PM   (permalink)
Default

It's so long ago I've no idea now, but it was probably just to show the individual operations?.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th August 2008, 07:26 PM   (permalink)
Default

Quote:
Originally Posted by asp1987 View Post
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.?
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/
ericgibbs is online now  
Old 14th August 2008, 07:32 PM   (permalink)
Default

Quote:
Originally Posted by ericgibbs View Post
hi,
I would have written

CLRF T2CON
bsf T2CON,1

; remember to enable the TMR with bsf T2CON,2

Is this what you mean.?
Yes. Thanks to both of you.
asp1987 is offline  
Old 14th August 2008, 07:36 PM   (permalink)
Default

Quote:
Originally Posted by ericgibbs View Post
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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th August 2008, 07:42 PM   (permalink)
Default

Quote:
Originally Posted by Nigel Goodwin View Post
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.
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/
ericgibbs is online now  
Old 14th August 2008, 07:43 PM   (permalink)
Default

Quote:
Originally Posted by Nigel Goodwin View Post
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.
asp1987 is offline  
Old 14th August 2008, 07:46 PM   (permalink)
Default

Quote:
Originally Posted by asp1987 View Post
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.
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/
ericgibbs is online now  
Old 14th August 2008, 08:09 PM   (permalink)
Default

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
Mike, K8LH is offline  
Old 14th August 2008, 08:12 PM   (permalink)
Default

Quote:
Originally Posted by asp1987 View Post
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

A couple of bytes though isn't generally a problem.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th August 2008, 09:41 PM   (permalink)
Default

Quote:
Originally Posted by Mike, K8LH View Post
Use meaningful __config statements in your source code.
Hey Mike!

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

Thanks.
demestav is offline  
Old 14th August 2008, 09:48 PM   (permalink)
Default

Quote:
Originally Posted by demestav View Post
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!

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.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 14th August 2008, 11:16 PM   (permalink)
Default

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 by Mike, K8LH; 14th August 2008 at 11:19 PM.
Mike, K8LH is offline  
Old 15th August 2008, 07:19 AM   (permalink)
Default

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!
demestav is offline  
Old 15th August 2008, 07:48 AM   (permalink)
Default

Quote:
Originally Posted by demestav View Post
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
__________________
Eric
"Good enough is Perfect"

PIC tutorials:
Gramo's: www.digital-diy.net/
Bill's: www.blueroomelectronics.com/

Last edited by ericgibbs; 15th August 2008 at 07:50 AM.
ericgibbs is online now  
Reply

Bookmarks

Thread Tools
Display Modes



Similar Threads
Title Starter Forum Replies Latest
Advice with ADC (TMR2) working with PWM. MrNobody Micro Controllers 4 18th May 2008 03:43 PM
60 Hz PWM for RC Servo using postscaler? ClydeCrashKop Micro Controllers 25 5th December 2007 02:44 AM
How to Reset TMR2? Suraj143 Micro Controllers 6 16th November 2007 10:18 AM
TMR0 Prescaler YAN-1 Micro Controllers 1 19th July 2005 10:54 AM
question on using tmr2 to generate interrupt every 5ms ryan_ryan Micro Controllers 2 7th February 2005 01:47 PM



All times are GMT. The time now is 07:43 PM.


Electronic Circuits  |  Learning Electronics
Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.

eXTReMe Tracker