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.

X minutes have passed by sir. (while Timer 1 is running)

Status
Not open for further replies.

qtommer

Member
hi everyone,
I am new with the Timer modules on the PIC and I have just implemented using 876A with a 4MHz crystal a code which counts up to 60 seconds and then lights and LED on PORTB,0.

Using Timer1, TMR1L and TMR1H combines to overflow when reaching 0xFFFF. Therefore, to get up to a count of 1 second, (using a prescaler of 1:8) I'm adding an outer loop counter (which after calculations) is set as d'114'.

In short, Timer 1 overflows 114 times to equal to 1 minute (60secs)

The code I've done works and is as follows:

Code:
		org		0x00

	
		BANK1
		movlw	0x00
		movwf	PORTA
		movlw	b'10000000'
		movwf	TRISB
		bsf		PIE1,0
		BANK0
		
		clrf	PORTB

		movlw	d'114'
		movwf	timer_count


settime	movlw	b'00110000'
		movwf	T1CON
;		movlw	0xFF		apparently I don't have to insert these 4 lines
;		movwf	TMR1L        of code. Without it, the Timer automatically 
;		movlw	0xFF         counts up to 0xFFFF. If I do insert this 4 lines,
;		movwf	TMR1H       the Timer does not work as intended and the LED
                                                ;lights up upon power on. Any one know why?

		
		bsf		T1CON,0
		

wait	btfss	PIR1,0
		goto	wait
		

		

		bcf		T1CON,0
		bcf		PIR1,0
		decfsz	timer_count,f
		goto	settime

		bsf		PORTB,0
		goto	$+0

end

Apart from the mini question I inserted in the code excerpt, I now would like to be able to add a feature whereby I press a button within this 1 minute interval and from there, read the current time passed at the instant of which the button is pressed. Say I press a button and the program reads the Timer and tells me that X seconds have passed, I would then like to update this value in hex to an entire Port. After 60 seconds, the program will just stop and wait for reset.

Is what I'm trying to achieve suitable for implementation using the Timer module? I actually intend to do a 30 minute timer and with the push of a button, get an update on how much time has passed in minute resolution. If it is possible to use Timer 1, could anyone give me a rough idea of how I can set out to achieve this? thanks=)
 
Hi,

Well am afraid to say the code you posted simply does not work, but you should be able to get it working if you have a look at these 2 tutorials.

See this one for how to start coding your project and read a switch etc.




See this one on how to use the Timers

https://ece.wpi.edu/~wrm/Courses/EE2801/Notes/ee2801c00lec23.pdf


You can do what you want in a simple program loop but once you get well into programming then you will control the timer events from inside the Interrupt Service Routine - don't try it now -its way to complicated at this stage.
 
hi thank you for your reply

Well am afraid to say the code you posted simply does not work, but you should be able to get it working if you have a look at these 2 tutorials.
May I humbly ask which portion of my code is in error? I tried this out on my board and it lit up the LED after 59++ seconds from power up.I'm new and have yet alot to learn. =)

See this one for how to start coding your project and read a switch etc.

Nigel's PIC Tutorial Page


See this one on how to use the Timers

**broken link removed**


You can do what you want in a simple program loop but once you get well into programming then you will control the timer events from inside the Interrupt Service Routine - don't try it now -its way to complicated at this stage.

Thank you for the links. I have given them a read=)

I have another question=)
What I want to actually achieve (as stated in my previous post) is for the timer to run in the background and update to a register each time 1 minute has passed. (The updating to the register also has to run independent of the program code in the background (as my main program code will be polling something else). What I plan to do is to use an external 555 timer with a 1Hz period to control a counter IC. each time the counter reaches 60, a pulse is sent to RA4/TOCK1 to update Timer 0. Hence, at any point of time in my main code, I am able to just read from Tmr0 directly.

This is however what I was thinking. I am open to more practical and wise solutions. Anyone has any thoughts on this?=)
 
Last edited:
Hi,

Assume you are using MPLAB for compiling your code ?
If so your code simply will not build into the .hex program file becasue of the coding errors.

You need to go though the WinPic tutorial Lesson 1 which shows how to properly set up your code.

If you do not learn it for yourself now you will never progress .. give it a try ..
 
Yes I'm using MPLAB and yes the code compiled.. There are no coding errors. .. The above is just an excerpt out of my whole code....I just did not include the "setting up" portion of the code to spare the details...cheerio;)
 
Last edited:
sorry if i caused any misunderstanding. I have a fair knowledge of PIC programming. Im not new to PIC but just new to Timers ;) Any opinions on post #3? ;)
 
Hi,

Think what you want to do is start off your timer0, but also set up the Interrupt system so that when timer0 has completed, its sets its Interupt flag which momemtarily stops your main program code and services the Interupt where you update your counter register and restart Timer0, then return to your main program loop.
https://ww1.microchip.com/downloads/en/devicedoc/33023a.pdf

Have a look at the Timer1 example which shows a RTC using the ISR method - you can use that or readily convert it to work with your Timer0.

Regarding your earlier code, PortA is in Bank0
 
Last edited by a moderator:
thanks again for your reply

Im sorry but Im afraid the link is bad. Could you repost?

Regarding your earlier code, PortA is in Bank0
ahh didn't catch that..that was supposed to be TRISA. Guess I didnt catch that cuz PORTA was not utilized in the code. Thanks!
 
Hi Again,

Was rushing around when I pointed you to that manual, those examples are not the best for using the Interrupt system.

The attached file uses your code but the main loop just sits there waiting for timer1 to Interrupt and then turn on PortB.
Normally the main loop would be busy doing other things while timer 1 and the ISR take care of each other.

Have a look at the code - one thing , do not use a goto or call to go out of the ISR routine or it will get lost, keep your ISR code tight and short, set flags to do more complex things in the main program loop.
 

Attachments

  • ONESEC.asm
    2.2 KB · Views: 134
  • ONESEC.jpg
    ONESEC.jpg
    99.7 KB · Views: 171
hi Wp100



thanks so much..I have it all working it now....I really appreciate you replying in the midst of your busyness...Thanks:):):):):):)
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top