PIC16F84A using pulsout and TMR0


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Posts
    33

    Post PIC16F84A using pulsout and TMR0

    Hi All

    I had so touble on the Pulsout and tmr0 interrupt.
    Can I send a pulse in 4.32 second to the stepper motor driver (UCN5804B)and at the same time doing tmr0 interrupt to do the time and date.
    For example: 8am start run the stepper motor in period=4.32second and 5pm the stepper motor back to its initial position.

    Below are my code: Please give advice thanks you very very much
    The code is ok i think ,but the period is NOT what I want.

    ' The program is doing the solar tracker using stepper motor 1.8degree(5V,1A)
    ' ANd it must be track in 15 degree per hour
    ' The clock using TMR0 is to activate the tracker to start and stop.
    ' I will also need to display the time on the LCD
    '
    ' For Example: When 8 am the stepper motor start run in 15 degree per hour.
    ' gearbox 100:1 RATIO
    '
    ' This code that I write just run in period 65.53 milisecond.
    ' The code is NOT what i need.

    'WHAT I NEED IS TO SEND PULSE IN 4.32 second.
    'BELOW PULSOUT 0,6553 WHICH IS ONLY 65.53MILISECOND.
    ' Code below can run but the pulsout time period is not what I want (period=4.32 second)

    hour VAR BYTE
    bm VAR BYTE
    bs VAR BYTE
    day VAR WORD
    month VAR BYTE
    year VAR WORD
    s VAR BYTE
    t VAR BYTE

    hour=0:minute=0:second=0:

    'TMR0 will interrupt every 65,536 milliseconds

    OPTION_REG=%01010111 ' prescaler= 1:256
    INTCON=%10100000

    ON INTERRUPT GoTo start
    TRISB=0 ' all port b set to output
    High 1 ' stepper motor stop (Enable=high)
    High 2 ' CW direction

    '************************************************* ************************************
    main:
    PulsOut 0, 6553 ' clock pulse to the stepper motor driver UCN5804B
    ' 65.53 milisecond
    GoTo main
    ************************************************** ************************************

    ' Interrupt service routine (ISR)

    Disable ' Disable interrupts during interrupt handler
    start : t = t + 1 ' Count pieces of seconds
    IF t < 15 Then exit ' 15 ticks per second (65.536ms pertick)

    '**********Time elasped = 1 second ***********
    t = 0
    bs = bs + 1
    IF bs >= 60 Then
    bs = 0
    bm= bm + 1
    IF bm >= 60 Then
    bm = 0
    hour = hour + 1
    IF hour >= 24 Then
    hour = 0

    EndIF
    EndIF
    EndIF

    exit: INTCON.2 = 0 ' Reset timer interrupt flag
    Resume
    End

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I guess what you want is one pulse every 4.32s, in other words a frequency of 0.2315Hz.

    Code:
    'Produce squarewave on PortB.0 period~4.32s, 50% dutycycle.
    
    Loop var Word
    
    Main:
     High PortB.0
     For Loop = 0 to 2160       'Loop here for ~2.16s, tweak to match.   
       PauseUs 1000
     Next
     Low PortB.0
     For Loop = 0 to 2160       'Loop here for ~2.16s, tweak to match. 
       PauseUs 1000
     Next
    Goto Main
    Since the Pause statement in the For-Next loop is shorter (1mS) than what your interrupt period is (65mS) the ISR will still run.

    However, your time-keeping will not be very accurate. You're only getting 983mS each second, give or take, 65536uS * 15 = 983040uS.

    /Henrik Olsson.

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    too bad PIC16F84a don't have 2 timers... The Henrik solution will work indeed, but i'll suggest to use shorter PAUSEUS, let's say 10uSec... OR a loop of @ nop.

    If you need accuracy, you may decide to fine-tune it using MPLAB stopwatch OR measure it using an extra i/o and a scope.

    Another solution... Use 2 variables, flags and 1 Timer.

    There's many different ways.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Nov 2006
    Posts
    33


    Did you find this post helpful? Yes | No

    Post Thanks for the reply....some question below....

    Hi all

    Thanks to HenrikOlsson and mister_e that give me the marvelous advice.
    I had tested it on the lab but the period is 4.6 and something 4.5. After I change it to For 0 To 2020 it will work 4.3 and something 4.2.
    I just measure it using stopwatch.
    For accuracy of the clock I will assign TMR0 = 12 {((fosc/4)*(256-12)*256)=0.9994}. Is that OK????

    Mister_e has suggest me to use 10us pause. Which mean that the for loop have to increase to 216000 rite???
    Sorry..I don't undestand the statement below:-

    *****If you need accuracy, you may decide to fine-tune it using MPLAB stopwatch OR measure it using an extra i/o and a scope.
    Another solution... Use 2 variables, flags and 1 Timer.*****************

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I don't follow you on the math....
    If you reload TMR0 with 12 each interrupt you'll get (256-12) * 256 = 62464uS between each interrupt.

    What kind of accuracy do you need? With the prescaler set to 1:256 and a TMR0 reload value of 178 you'll get (256-178)*256 = 19968uS between each interrupt plus a little for the time it takes to reload.

    Another way would be to use an external xtal for TMR0, with a standard 32.768 watch xtal and the prescaler set to 1:128, you'll get one interrupt each second.

    MPLAB have a simulator where you can run the code, it has a stopwatch function that can be used to "measure" how long a certain piece of code takes. I've never used it myself.

    /Henrik Olsson.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Instead of using StopWatch, you may measure it in real-world. Use a scope and measure the time using an extra I/O.

    Example:

    High AextraIO
    ' code you need to evaluate
    LOW AextraIO

    You place your scope (or frequency meter, etc etc) on the AextraIO pin and you measure it.

    something handy for your Timer Calc
    http://mister-e.org/pages/utilitiespag.html
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  7. #7
    Join Date
    Nov 2006
    Posts
    33


    Did you find this post helpful? Yes | No

    Post Some LCD stuff

    Hi all

    One question to ask...

    I actually want to display "Time: 00:00:00"
    The LCD is working but the problem is it is only happen when i use crystal 2.4576Mhz.

    When i change my crystal to 4Mhz it will just display "00:00:00"
    without seeing the word "Time"..
    I had add the pull up esistor to 1k ohm and DEFINE LCD_COMMANDUS 1000. Is that ok???

    Why??
    Does it need to pause more longer???


    Thanks
    Last edited by DragonBall_6; - 7th December 2006 at 13:38.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,605


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Do you have DEFINE LCD_COMMANDUS 1000 in your code? If that's the case try increasing that to 2000 to start with. If that doesn't help put a PAUSE 500 right at the beginning of the program, that will let the LCD startup for sure.

    If that doesn't help either, increase the COMMANDUS and DATAUS defines. Test with 10000 for COMMANDUS and 500 for DATAUS just to see if it helps. If it works, try to find the sweetspot just over where it stops working.

    /Henrik Olsson.

Similar Threads

  1. Pin won't stay high during Repeat...Until loop??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th August 2009, 23:57
  2. Won't go back to SLEEP after 1st Interrupt
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 32
    Last Post: - 29th June 2009, 09:00
  3. Battery powered applications
    By NavMicroSystems in forum Off Topic
    Replies: 7
    Last Post: - 22nd June 2009, 07:12
  4. COUNT is not counting again
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 33
    Last Post: - 19th June 2009, 04:52
  5. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts