+ Reply to Thread
Results 1 to 15 of 15

Thread: Driving a servo motor- Timer0

  1. #1
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    Default Driving a servo motor- Timer0

    This is continuous from "Driving a servo motor". This time we'll use Timer0 in P16f84A to generate the pulse width and the period of the pulse train.

    The program sets up the Timer0 function, set the first value in the Timer0 register and wait for it to overflow. This value times the postscale is the pulse high duration. When Timer0 register overflows we set the pulse low and enter a second value (longer duration) and wait for another overflow. This value is to set the pulse period. When this overflow occurs, we set the
    pulse high again and repeats the whole process.

    The program doesn't use the interrupt routine.

    servo motor_timer0_C.txt

    Please refer codes in the main program to setup Timer0 function.
    Because we set 16 mS as the pulse period, we have to use prescale of 1:64 so that Timer0 register will take more than 16mS before overflows.

    This program will lead to the next program, which uses Timer0 and Timer1 and Timer1 interrupt routine........
    May the force be with you.


    My project: Simple White Line Follower

    http://au.youtube.com/watch?v=8Z_MmrdH4oc

    http://i271.photobucket.com/albums/j...nefollower.jpg


  2. #2
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,797

    Default

    Perhaps you should bear in mind this is the 21st century?, why are you posting about an antique from the last century?. Dump the long past it's sell by date F84, and use a modern device and a decent timer - TMR0 is the most useless of the PIC timers, and is only included for backwards compatibilty.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  3. #3
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,711
    Blog Entries
    5

    Default

    I like TMR0, it's limited but handy for a jiffy timer if you use the right crystal. I'm using it on one of my prototypes (the Ethernet one) for the RTC timebase.
    9.8304MHz crystal on the 18F4620 is one of those 'magic' frequencies.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  4. #4
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,797

    Default

    Why use TMR0?, and not one of the better timers?, are you correcting for the latency of the timer, or isn't accuracy a concern?.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  5. #5
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,711
    Blog Entries
    5

    Default

    Me?

    9.8304MHz
    • /65536 (TMR0) to create a very handy interrupt driven 150 Hz jiffy clock
    • it also multiplies by the 4 PLL in the 18F series for a 39.3MHz clock
    • works with any revision of the ENC28J60 (had an SPI speed bug)
    • perfect for a baud clock 0% error
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  6. #6
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,797

    Default

    Quote Originally Posted by blueroomelectronics
    Me?

    9.8304MHz
    • /65536 (TMR0) to create a very handy interrupt driven 150 Hz jiffy clock
    • it also multiplies by the 4 PLL in the 18F series for a 39.3MHz clock
    • works with any revision of the ENC28J60 (had an SPI speed bug)
    • perfect for a baud clock 0% error
    But doesn't TMR0 need reseting in your program everytime it times out?, it's not self reseting like others? - this makes it require special consideration to maintain an accurate clock - check the real time clock application notes back from when TMR0 was all you got.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  7. #7
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,711
    Blog Entries
    5

    Default

    Nope it's free running, and as accurate as the crystal. Just set the prescaler to 256:1 if you want a 150Hz interrupt. Easy
    Opps: It's would need a 64:1 prescaler for 150Hz, or 128:1 for 75Hz.

    http://www.mikroe.com/en/books/picbook/2_08chapter.htm
    Last edited by blueroomelectronics; 25th November 2007 at 08:51 PM.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  8. #8
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,797

    Default

    Quote Originally Posted by blueroomelectronics
    Nope it's free running, and as accurate as the crystal. Just set the prescaler to 256:1 if you want a 150Hz interrupt. Easy
    Opps: It's would need a 64:1 prescaler for 150Hz, or 128:1 for 75Hz.
    Right, I see - you're selecting the crystal to compensate for the limitations of TMR0 - get your point now!.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  9. #9
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,711
    Blog Entries
    5

    Default

    The magic is it's also a perfect UART timebase, and a standandard crystal frequency.
    Here they are... All common crystals
    2.4576MHz
    4.9152MHz
    9.8304MHz
    19.6608MHz

    And it's spares the very useful TMR1 & TMR2
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  10. #10
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    Default

    Quote Originally Posted by Nigel Goodwin
    Perhaps you should bear in mind this is the 21st century?, why are you posting about an antique from the last century?. Dump the long past it's sell by date F84, and use a modern device and a decent timer - TMR0 is the most useless of the PIC timers, and is only included for backwards compatibilty.
    Hi Nigel,

    Perhaps you didn't read the end of the post. I will use different method in my next post, and I will use different PIC. This is just a begining of a discussion of using different method of producing pulses. I know the F84 is old and outdated, but do you know there are many people out there still using it. And for some beginners F84 is a good PIC to start. Don't forget, there are always someone new in this topic.

    Also, as pointed out by Bill, TMR0 is not a bad feature.
    May the force be with you.


    My project: Simple White Line Follower

    http://au.youtube.com/watch?v=8Z_MmrdH4oc

    http://i271.photobucket.com/albums/j...nefollower.jpg

  11. #11
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    Default

    Quote Originally Posted by Nigel Goodwin
    But doesn't TMR0 need reseting in your program everytime it times out?, it's not self reseting like others? - this makes it require special consideration to maintain an accurate clock - check the real time clock application notes back from when TMR0 was all you got.

    I don't think so. TMR0 will reset itself and increment from zero after it times out.
    May the force be with you.


    My project: Simple White Line Follower

    http://au.youtube.com/watch?v=8Z_MmrdH4oc

    http://i271.photobucket.com/albums/j...nefollower.jpg

  12. #12
    Help us help you blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent blueroomelectronics Excellent
    Join Date
    Jan 2007
    Location
    Toronto, Canada
    Posts
    10,711
    Blog Entries
    5

    Default

    Quote Originally Posted by Wingmax
    I don't think so. TMR0 will reset itself and increment from zero after it times out.
    It's called free running, doesn't actually reset but overflows.
    Bill
    Smart Kits build Smart People

    http://www.blueroomelectronics.com/

  13. #13
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    Default

    Quote Originally Posted by blueroomelectronics
    It's called free running, doesn't actually reset but overflows.
    I used the wrong phrasing.
    What I meant was Timer0 will start from zero and increment after overflows and doesn't need to be trigered to restart.
    May the force be with you.


    My project: Simple White Line Follower

    http://au.youtube.com/watch?v=8Z_MmrdH4oc

    http://i271.photobucket.com/albums/j...nefollower.jpg

  14. #14
    Super Moderator Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent Nigel Goodwin Excellent
    Join Date
    Nov 2003
    Location
    Derbyshire, UK
    Posts
    29,797

    Default

    Quote Originally Posted by Wingmax
    I know the F84 is old and outdated, but do you know there are many people out there still using it. And for some beginners F84 is a good PIC to start. Don't forget, there are always someone new in this topic.
    No, it's not a good place to start! - it's a really bad place to start, why choose a long obselete, overly expensive device, with a low specification?.

    By doing so you're just encouraging more people to use such outdated chips, I can see the point that there are ten year old tutorials out there on the net using it, but to write one in 2007 is just beyond belief!.

    I would suggest you at least remove all references to the 84 and do the simple changes to use the 16F628 instead - the device that replaced the 84 last century.
    PIC programmer software, and PIC Tutorials at:
    http://www.winpicprog.co.uk

  15. #15
    Wingmax Excellent Wingmax Excellent Wingmax Excellent Wingmax Excellent
    Join Date
    Sep 2007
    Location
    Australia
    Posts
    163

    Default

    Quote Originally Posted by Nigel Goodwin
    No, it's not a good place to start! - it's a really bad place to start, why choose a long obselete, overly expensive device, with a low specification?.

    By doing so you're just encouraging more people to use such outdated chips, I can see the point that there are ten year old tutorials out there on the net using it, but to write one in 2007 is just beyond belief!.

    I would suggest you at least remove all references to the 84 and do the simple changes to use the 16F628 instead - the device that replaced the 84 last century.
    I never thought using the F84 would cause so many hassels. It was not my intension to resurrect the F84. I was going to use the 628 next, and the 684 for ECCP function.
    But my next one will be using Timer0 and Timer1( you might disagree again). So just thought may be started with Timer0 first. May be there are people out there wanted to know how to use Timer0. But anyway, using Timer0 is not my final goal, just an introduction.
    May the force be with you.


    My project: Simple White Line Follower

    http://au.youtube.com/watch?v=8Z_MmrdH4oc

    http://i271.photobucket.com/albums/j...nefollower.jpg

+ Reply to Thread

Similar Threads

  1. Driving a servo motor
    By Wingmax in forum Robotics Chat
    Replies: 26
    Latest: 11th February 2008, 09:56 PM
  2. Servo problems
    By bananasiong in forum Micro Controllers
    Replies: 0
    Latest: 16th September 2007, 02:16 AM
  3. PIC Hardware PWM for servo control
    By 1krr in forum Micro Controllers
    Replies: 9
    Latest: 2nd December 2006, 12:47 PM
  4. Servo Controll... In reverse?
    By savage in forum Micro Controllers
    Replies: 9
    Latest: 1st December 2006, 03:11 PM
  5. Help required with servo motor controller
    By mayhem in forum Robotics Chat
    Replies: 3
    Latest: 26th May 2006, 04:21 PM

Tags for this Thread