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.

Quick questions about timer

Status
Not open for further replies.

bananasiong

New Member
Hi,
I have some quick questions about timer:

1. When watchdog timer overflows, the PIC will reset, so it goes to org 0x0000 or somewhere else desired by the user? I think it will be at 0x0000 because it resets, just to confirm.

2. A timer (either 0, 1, 2 or 3) will keep running from 0 to full then back to 0 again without doing any setting in the program? For example, I need a random 8 bit number, so I simply take the value of tmr0 in anywhere of the program will do?

Thanks
 
1. Yes, the pic will reset. You can check the value of various bits to establish if it was a WDT reset. Refer to table 12-2 in the 16f88 data sheet.

2. Yes, however the values you get will not be very random. If you wait for a key press or some other external trigger then the value will be pretty random. Which pic has timer 3?

Mike.
 
Pommie said:
1. Yes, the pic will reset. You can check the value of various bits to establish if it was a WDT reset. Refer to table 12-2 in the 16f88 data sheet.

2. Yes, however the values you get will not be very random. If you wait for a key press or some other external trigger then the value will be pretty random. Which pic has timer 3?

Mike.

Why the 18F and above often have timer 3
 
Yes, thanks :)
But you mean that the value will not be very random because the timer increase one for every instruction cycle; if I get the value of tmr0 in very begging of the program, I will properly get small value, am I right?
And yes, I've seen timer 3 in 18f series, but I have not started touching them because I'm still in the process of learning the 16f series :)
 
bananasiong said:
Hi,
1. When watchdog timer overflows, the PIC will reset, so it goes to org 0x0000 or somewhere else desired by the user? I think it will be at 0x0000 because it resets, just to confirm.

Except when the PIC is sleeping. In that case a WatchDog Timer time-out causes the PIC to wake up and continue executing the code after the SLEEP instruction.


bananasiong said:
if I get the value of tmr0 in very begging of the program, I will properly get small value, am I right?

You'll very likely get the same number every time :eek:

I've used TMR2 running as fast as possible to generate random numbers. The trick was to read the value of TMR2 when a button was pressed. If you don't have external events, consider specific routines like - if it's worth trying for you application.
 
Yes, I agree that the same value will be got in this case.
The loop of getting the value of tmr0 in my program is not called all the time, so I can get random number when I call the loop.
 
Hi,
Do I need to do anything to reset TMR0 when it overflows? I got 0xff all the time when I tried to read from it. I read the value of TMR0 when a button is pressed, so I think I can get 8 bit random number, but couldn't.

Thanks
 
Oh god, I found it. I forgot to clear the T0CS bit of the OPTION_REG. The default setting of this bit is 1. Now I can get random numbers :D
 
This was just Timer0 and will be more nitpiky once you use the other timers.

INTCON
OPTION_REG
PIR
PIE
T1CON
T2CON

I start a project with a full template similar to the partially full one below. Once I reach the point of near completion, I tiddy up by remove all uneccessary Defaults in the Start section and set bits and flag by Masking a byte as opposed to bcf/bsf each individual bit.
Code:
START:	;-----PORT I/O CONFIGURATION-------------------------------
        NOP
        BANK1
	BSF	CMCON0, CM2		;DISABLE COMPARATOR.
	BSF	CMCON0, CM1		
	BSF	CMCON0, CM0
	BANK1
        LOADF    TRISA, B'11011111'
        LOADF    TRISB, B'00000000'
	LOADF	TRISC, B'00000000'
        BSF	OPTION_REG, NOT_RBPU	;BCF/BSF = ENABLE/DISABLE PULL-UP.
	BCF	OPTION_REG, INTEDG 	;BSF/BCF = HIGH/LOW - RB0/INT EDGE TRIGGER
	BANK2
	CLRF	LCDCON
	BANK1	;------POWER ON RESET CONFIGURATION--------------------------
	BSF	OSCCON, IRCF2	;INTERNAL OSCILLATOR FREQUENCY.
	BSF	OSCCON, IRCF1	;110=4MHZ	
	BCF	OSCCON, IRCF0
	BSF	OSCCON, SCS	;C/S = INTERNAL/FOSC  SYSTEM CLOCK
	BANK1	;------ANALOG CONFIGURATION----------------------------------
	BCF	ANSEL, ANS4	;C/S=DIGITAL/ANALOG PIN
	BCF	ANSEL, ANS3	;C/S=DIGITAL/ANALOG PIN
	BCF	ANSEL, ANS2	;C/S=DIGITAL/ANALOG PIN
	BCF	ANSEL, ANS1	;C/S=DIGITAL/ANALOG PIN
	BCF	ANSEL, ANS0	;C/S=DIGITAL/ANALOG PIN
	BANK1	;------INTERRUPT CONFIGURATION-------------------------------
	BSF	INTCON, PEIE	;C/S=DIS/ENABLE    PERIPHERAL INTERRUPTS
	BSF	PIE1, TMR1IE  	;C/S=DIS/ENABLE    TIMER1     INTERRUPT
	BCF	PIE1, CCP1IE	;C/S=DIS/ENABLE	   CAPTURE    INTERRUPT
	BCF	PIE2, C2IE	;C/S=DIS/ENABLE	   COMPARATOR INTERRUPT
	BCF	PIE1, EEIE	;C/S=DIS/ENABLE	   EEPROM     INTERRUPT
	BSF	INTCON, GIE  	;C/S=DIS/ENABLE    GLOBAL     INTERRUPT
	BCF	INTCON, T0IE	;C/S=DIS/ENABLE    TIMER0     INTERRUPT
        BANK0	;----TIMER1 CONFIGURATION-----------------------------------
	BSF	T1CON, TMR1ON 	;C/S=DIS/ENABLE     	      TIMER1 ON
	BCF	T1CON, TMR1CS 	;C/S=IN/EXTERNAL(RA6)    	  CLOCK
        BANK1	;----TIMER0 CONFIGURATION-----------------------------------
	BCF	OPTION_REG, T0CS	;C/S=IN/EXTERNAL(RA4)   	       CLOCK
	BCF	OPTION_REG, PSA	;C/S=TIMER0/WDT    PRESCALER ASSIGNMENT
	BSF	OPTION_REG, .2	;PRESCALER
	BSF	OPTION_REG, .1	;000=1:2,  001=1:4,  010=1:8,   011=1:16
	BSF	OPTION_REG, .0	;100=1:32, 101=1:64, 110=1:128, 111=1:256
	BANK1	;----SERIAL TRANSMIT CONFIGURATION------------------------
	BSF	TXSTA, TXEN	;C/S=DIS/ENABLE             TRANSMISSION
	BSF	TXSTA, BRGH	;C/S=LO/HI SPEED        BAUD RATE SELECT
	LOADF	SPBRG, .10	;BAUD RATE: 21= 57.6K BPS  10=115.2 KBPS
	BANK0	;----SERIAL RECEIVE CONFIGURATION-------------------------
	BSF	RCSTA, SPEN	;C/S=DIS/ENABLE              SERIAL PORT
	BSF	RCSTA, SREN	;C/S=DIS/ENABLE           SINGLE RECEIVE	
	BSF	RCSTA, CREN	;C/S=DIS/ENABLE        CONTINUOS RECEIVE
 	BANK0	;---------------------------------------------------------


I still keep a copy of the pdf bible running in the background, alt-tabbing, ctrl-F'ing, and F3'ing like a made man.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top