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
 
Tools
Old 20th April 2004, 04:14 PM   #1
Default PIC 16F877 timer

Hi all,
My name is Patrick. This is my first time in this forum.
I ve just bought a PIC 16F877. Wow, so exciting, and I wanted to build my own first application. Well, I dont know if it is feasible or not, because I am a newbie in this field!

Ok, this is the specification:
This timer is for marital arts, which allows me to select number of rounds, duration of round, and break time. Current time will be shown on a LCD. So I reckond, the max period of time should be around 5 mins. There are 3 buttons, which are stop, reset and start.

I have few questions about this application. Could you please help me out?
1. There are many timers inside the chip such as TMR0,1,2,3 and even Watchdog timer. I dont know which one to use. If the max time is 5 mins, I think I can use Timer ) with 8 bit input.

2. I get an idea to use the crystal, but if so, I wont use teh CHIP I like make use of the chip.

3. How can I implement the button?

Thanks
Have a good day
Patrick
patricktran is offline  
Old 20th April 2004, 05:03 PM   #2
Default

Have a look at my tutorials, it should give you a few ideas.

Timer1 is probably the best for your purposes, you can easily use it to generate regular interrupts which you count for your basic clock - for example, generate 1mS interrupts and count 1000 of them to increment the seconds counter.

Interesting you are building a martial arts timer, which martial art do you do?, I'm a 2nd Dan in Ju Jitsu and a registered instructor and grading examiner.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 20th April 2004, 05:43 PM   #3
Default

easiest way to program a pic i find is in c

you can download a trial version of the ccs compilier at www.ccsinfo.com

you dont have to worry about memory locations. the code is ineffficent but a hell of alot easier
kitedude is offline  
Old 21st April 2004, 03:11 AM   #4
Default

Hi, thanks for your replies. It will take me awhile to read the tutorials. But realy realy I found these tutorials so useful. Thanks again. :wink:
I am abit more confident in high level programming language. I think this timer can be done by using C language, but honestly, I want to take this as a chance to learn more about assembly language. :shock:
Well, I think this timer can be used in Karate, or Kick boxing. The max time would be 5 mins. It sounds so limitted, but I hope I can improve the timer when I get it done for basic functions.
I am on holidays now, so full time working for ...PIC16f877
Thanks
patricktran is offline  
Old 21st April 2004, 03:30 AM   #5
Default

Hi,
I understand prescale of a timer. say timer counts from 256 to 0. If the prescale is 1:16 then the timer will generate an interrupt after 256*16 counts.
How about postscale? is it important?
Thanks
patricktran is offline  
Old 21st April 2004, 03:38 AM   #6
Default

thanks
patricktran is offline  
Old 21st April 2004, 09:06 AM   #7
Default

Quote:
Originally Posted by patricktran
Hi,
I understand prescale of a timer. say timer counts from 256 to 0. If the prescale is 1:16 then the timer will generate an interrupt after 256*16 counts.
How about postscale? is it important?
Thanks
It depends what you want to do, prescale divides the clock down before it enters the timer, postscale divides the output from the timer - so a postscale of 4 would generate an interrupt every 4 timer transitions.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 21st April 2004, 02:09 PM   #8
Default

Thanks very much. Your site is so great.
So basically, they are quite similar? I reckon it it possible to write this program with only prescale. Is it right?
patricktran is offline  
Old 21st April 2004, 02:26 PM   #9
Default

Quote:
Originally Posted by patricktran
Thanks very much. Your site is so great.
So basically, they are quite similar? I reckon it it possible to write this program with only prescale. Is it right?
Yes, you only need to use one - personally I would suggest using Timer1 rather than Timer0, it's probably easier to generate regular interrupts with.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Old 22nd April 2004, 02:32 PM   #10
Default

Hi,
I left the Timer part for latter. I think it is easier to do the push button first.
Here is my code:
Code:
 title  "ledon - Turn on a LED when a Button is Pressed"

	
	include "D:/p16F877.inc" 
	__config (_CP_OFF & _PWRTE_ON & _HS_OSC & _WDT_ON & _BODEN_ON & _LVP_OFF & _DEBUG_ON)
	errorlevel -302  


;;;;;;;;Vectors  ;;
	org	H'000'
	goto	MainLine
	org	H'004'
	goto	IntService
Stop
	goto	Stop


;;;Interrupt Service;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

IntService
    retfie	
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;;;;;;;;;;; Initial Subroutine;;;;
Initial

	bcf		STATUS,RP0	; 
	bcf		STATUS, RP1	; Bank 0 selected
	clrf	PORTC
	clrf	PORTB
	bsf		STATUS,RP0	; Bank 1 selected
	movlw	H'00'		 ; All pins of PORTC outputs
	movwf	TRISC
	movlw	H'11'    
	movwf	TRISB		; All pins of PORTB inputs
  	bcf		STATUS,RP0	; Bank 0 selected 
	return
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

MainLine
	call 	Initial
MainLoop
    btfsc   PORTB,0     ; check Button 1, if it is not pushed, skip the next step
    goto    B1_routine
    btfsc   PORTB,1     ; check Button 2, if it is not pushed, skip the next step
    goto    B2_routine
    goto    TurnOff		;  if bit 0 is 1, goto Turnoff
B1_routine        
    movlw   b'10101010'
    movwf	PORTC  		
    goto  	MainLoop
B2_routine        
    movlw   b'01010101'
    movwf	PORTC  		
    goto  	MainLoop
TurnOff	
    clrf		PORTC	
    goto  	MainLoop
  	end
Basically, I want 2 buttons, Button 1 at PB0, and Button 2 at PB1.
Button 1: shows 10101010 in port C
Button 2: shows 01010101 in port C
I wanted to test it in the MPLAB, but when I tried to force to change the PB0, PB1, the MPLab does not allow (test by stepping). Then I tried to change the config of portB, make them as outputs (I know, this is not correct, and the buttons are attached here!!!) (TRISB with all pins of 0), then MPLAB allows me to change its bits.
This is so strange!!!! Anyone please help me. I reckon we can force the bits of INPUT port when stepping in MPLAB(simulation).
Thanks
patricktran is offline  
Old 22nd April 2004, 02:36 PM   #11
Default

One more thing, this program seems I am polling to check the buttons. Is it good? I reckon I should make the button to generate an interrupt by itself. Can you give me some hints of how to do it?
Thanks
patricktran is offline  
Old 22nd April 2004, 07:37 PM   #12
Default

Quote:
movlw H'00' ; All pins of PORTC outputs
movwf TRISC
movlw H'11'
movwf TRISB ; All pins of PORTB inputs
You cannot use all inputs of portb with this
Code:
movlw h'ff'
movwf trisb
If you move h'11' to trisb, trisb will be equal to b'00010001' that is only bit 0 and bit3 are inputs.
falleafd is offline  
Old 23rd April 2004, 12:33 AM   #13
Default

Oh thanks, Now I know, we cant use all pins in port B for I/O prupose.
Thanks again
patricktran is offline  
Old 23rd April 2004, 12:48 AM   #14
Default

Can someone give me some hints for phush button with interrupts? Not polling ...
Thanks
patricktran is offline  
Old 23rd April 2004, 06:49 AM   #15
Default

Quote:
Originally Posted by patricktran
Can someone give me some hints for phush button with interrupts? Not polling ...
Thanks
Use PortB and the 'Interrupt on change' feature - there's an application note at MicroChip about using it.
__________________
PIC programmer software, and PIC Tutorials at:
http://www.winpicprog.co.uk
Nigel Goodwin is offline  
Reply

Tags
pic, timer

Thread Tools
Display Modes




All times are GMT. The time now is 09:00 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker