![]() | ![]() | ![]() |
| | |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
| | LinkBack | Thread Tools | Display Modes |
| | (permalink) |
| 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 | |
| |
| | (permalink) |
| It's so long ago I've no idea now, but it was probably just to show the individual operations?. | |
| |
| | (permalink) | |
| Quote:
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/ | ||
| |
| | (permalink) |
| | |
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) | |
| Quote:
__________________ Eric "Good enough is Perfect" PIC tutorials: Gramo's: www.digital-diy.net/ Bill's: www.blueroomelectronics.com/ | ||
| |
| | (permalink) | |
| Quote:
Code: clrf T2CON bsf T2CON,1 | ||
| |
| | (permalink) | |
| Quote:
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/ | ||
| |
| | (permalink) |
| 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 | |
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) |
| | |
| |
| | (permalink) | |
| Quote:
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. | ||
| |
| | (permalink) |
| 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. | |
| |
| | (permalink) |
| 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! | |
| |
| | (permalink) | |
| Quote:
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 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. | ||
| |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
| |
| | ||||
| 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 |