Timer PIC16F57


Closed Thread
Results 1 to 26 of 26

Thread: Timer PIC16F57

Hybrid View

  1. #1
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79

    Default Timer PIC16F57

    Hello.
    Iīm trying to write a simple example that will make my ledīs flash every 20s.
    I understand timerīs in PIC16F872 because they have interrupts associated (TMR1), but iīm using PIC16F57 which only has one timer, TMR0, and no interrupts...
    TMR0 timer is an 8-bit special function register with working range of 256. Assuming that 4MHz oscillator is used, TMR0 can measure 0-255 microseconds range (at 4MHz, TMR0 increments by one microsecond), but how do i measure 20s with TMR0?
    Regards

  2. #2
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Leonel,

    Iīm trying to write a simple example that will make my ledīs flash every 20s.
    I understand timerīs in PIC16F872 because they have interrupts associated (TMR1), but iīm using PIC16F57 which only has one timer, TMR0, and no interrupts...
    I don't know what you want to do... but how complicated do you want to get????

    psuedo code..(that just may work, but haven't tested it).

    TRISB=%00000000
    Loop:
    Portb.0 = High
    Pause 500
    Portb.0=Low
    Pause 20000 'pause 20 seconds.
    goto loop
    end

    Dwayne
    Last edited by Dwayne; - 29th June 2005 at 17:53.
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  3. #3
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Talking

    Hi, leonel

    At each interrupt, you increment ( or easier decrement !!! ) a counter ...which (inc) rements another counter if the first overflows ... which ( inc ) rements ...

    etc. etc.

    Permits to do something else during blinking ... ( isn't it Dwayne ??? )

    can be done with PBP or directly with assembler ... ( No, ... i didn't tell it ... my doctor said: no shock on the head, ... )

    Alain

    PS : No interrupt ... so you poll for TMR0 overflow !!!
    Last edited by Acetronics2; - 29th June 2005 at 18:05.

  4. #4
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Alain,

    Alain >>Permits to do something else during blinking ... ( isn't it Dwayne ??? )<<

    Don't know <smile>. Simple program to blink a light?

    With change of Chips? With no info on the final project, With no intended purpose but to blink a light, and "Simple example"? I can only assume a Blinky Light...Hopefully He will have it running, and if the info we gave him doesn't pan out, He can give us more info, so that we can become a little more detailed <g>.

    Like What Ralph posted on the FAQ...

    Need chip #, Code, details, Projects purpose, and other things.
    I rememeber a while back, a person asked to change a 4 (separate) byte Hex to Decimal...I gave him the code, then his example had 5 (separate) bytes coming in, and he wondered why it didn't give him the right answer<g>.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  5. #5
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Pause command

    Using pause command i donīt see if any other input. What i want is put my output high during 20s but at the same time see if other input goes high. If it goes high i will start over again, my time goes to 0s, and i will start counting 20s

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Wink

    Hi, leonel ...

    Don't shout at me but I did something like that with a resettable 555 and some CD4000 logic ... it was in one of my prior lives.

    Said honestly, the 16F57 is not really the best Choice here ...

    Alain

  7. #7
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Space problems

    Yes, i know thatīs not the best choice, but itīs what i have i know that with some logic i can do that, but i have 8 inputs and mixing all together i will need "some CD4000" and i have space problems (in my box i have to put 8 relays, and all electronic...), thatīs why i want to use a cheaper PIC...

  8. #8
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,648


    Did you find this post helpful? Yes | No

    Talking

    Your project looks like a quizz box ...

    20 second to answer ... who's the quickest of 8 !!!

    we REALLY need to know a lot more about your top secret weapon. ...

    if you really want help, of course

    Alain

  9. #9
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Code - warning

    Hereīs my code to do what i said (with a little help of Dwayne).
    Compiling my code thereīs some warnings "Crossing page boundary -- ensure page bits are set." I read in PBP manual that i donīt have to worry because PBP take care of this for me. Itīs true?
    Attached Files Attached Files

  10. #10
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Leonel,

    Leonel
    If itīis pressed i turn on the relay and stays on for 20s or 40s (depending the position of a switch). If one button is pressed while one relay is on the time goes to begin and the two relays stays on again 20s or 40s.
    Hummm....I see a 40 second delay.... not a twenty second delay... thus a new variable has been added

    Ok. let me get this straight...(correct me if I am wrong).

    1. You have 8 LED's (or relays).
    2. You have 1 switch that is connected to some kind of software relay that says 20 or 40 seconds. (I am going to assume you have this switch set to the 20 second position for this message)
    3. If #1 button is pressed, it will turn on relay 1 for 20 seconds (or 40, depending upon if the HighLow software switch is on or off) If a second button is pressed BEFORE the first relay resets, Both the first Relay and the NEW relay (The one that you just got through pressing) stays on for at LEAST another 20 seconds.
    4. If another button (lets say relay #3) is pressed BEFORE Button 1 and 2 go off in their allowed time, all 3 relays will be active for at least another 20 seconds..
    5. But at ANY time, there is a 20 second interval of NO button pressing, all the relays will turn off....

    Is this correct?

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  11. #11
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Leonel,

    I am going to assume what I posted was correct...Here is some code to look at...Using my code, I added just a few things to allow for all your switches.

    Code:
    Switching var byte;
    Counter1 var word
      Counter2 Var word
      Loop:
      Counter1=0
      Counter2=0
      Loop2:
       if Counter1< 20
         if Counter2 <1000
         if switching=1 then Pause 1  'Pause 1/1000 of a second to make 20
         if switching=0 then Pause 2  'Pause 1/500 of a second to make 40
    
         if Portb.0=1 then 
                turn on switch.
                goto Loop
          endif
    
          if Portb.1=1 then 
                turn on switch.
                goto Loop
          endif
    
          if Portb.2=1 then 
                turn on switch.
                goto Loop
          endif
    
          ....
          ....    (for each of your eight switches)
    
    
            Counter2=Counter2+1;
         Endif
         
         if(Counter2>=1000)
           Counter1=Counter1+1;
           counter2=0;
         endif
       endif
       if Counter1<20 goto Loop2
    
      Portb=0;  'turn off ALL of the switches!
      Blink light = on
      Pause 200  'pause 1/5 of a second to light up the light.
      Blink Light = off
      goto Loop:
    The above code will take care of your switches, but the problem comes with your Blinky light every 20 seconds. If a person keeps pressing switches, your light will not blink every 20 seconds. Thus some kind of internal timer is needed to take care of your blinky to have it blink every 20 seconds.

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  12. #12
    Join Date
    Feb 2005
    Location
    Portugal
    Posts
    79


    Did you find this post helpful? Yes | No

    Default Correct

    Yes... all your 5 points are correct. If you understand my code thatīs a good beginning!
    Thank you!

  13. #13
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Leonel,

    Yes, I pretty much understood your code...

    Just a couple of suggestions.

    If and then statements...

    If a "IF statement has only one statement, you do not need a "endif".


    The if statement below NEEDS a endif
    Code:
      If a > b then
           c=d
           d=f
           f=g
       endif
    this does not need a endif statement;
    Code:
      if  a=b then c=d
      ...
      ...
      if d=f then d=g
      ....
    This will help you on your code space and readability...(Of course I must look whos speaking too <chuckle> I think my code is some of the hardest to read <g>)

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

  14. #14
    Join Date
    Dec 2003
    Location
    Wichita KS
    Posts
    511


    Did you find this post helpful? Yes | No

    Default

    Hello Leonel,,

    just had a brain fart to take care of your blinky light...


    Code:
    Switching var byte;
      LightCount var Word
      Counter1 var word
      Counter2 Var word
      LightCount=0
      Loop:
      Counter1=0
      Counter2=0
      Loop2:
       if Counter1< 20
         if Counter2 <1000
         if switching=1 then Pause 1  'Pause 1/1000 of a second to make 20
         if switching=0 then Pause 2  'Pause 1/500 of a second to make 40
    
         LightCount=LightCount+1
         If LightCount=20000 then
           LightCount=0
           Blink light = on
           Pause 200  'pause 1/5 of a second to light up the light.
           Blink Light = off
         endif
    
    
         if Portb.0=1 then 
                turn on switch.
                goto Loop
          endif
    
          if Portb.1=1 then 
                turn on switch.
                goto Loop
          endif
    
          if Portb.2=1 then 
                turn on switch.
                goto Loop
          endif
    
          ....
          ....    (for each of your eight switches)
    
    
            Counter2=Counter2+1;
         Endif
         
         if(Counter2>=1000)
           Counter1=Counter1+1;
           counter2=0;
         endif
       endif
       if Counter1<20 goto Loop2
    
      Portb=0;  'turn off ALL of the switches!
      goto Loop:
    I put a separate counter for the blinky... stuck the blinky in the routine so that it will be called all the time, removed it from the end of the routine and since you said it doesn't matter too much on the exact time.... well...

    Dwayne
    Ability to Fly:
    Hurling yourself towards the ground, and missing.

    Engineers that Contribute to flying:
    Both optimists and pessimists contribute to the society. The optimist invents the aeroplane, the pessimist the parachute

    Pilots that are Flying:
    Those who know their limitations, and respect the green side of the grass...

Similar Threads

  1. Elapsed Timer Demo
    By Darrel Taylor in forum Code Examples
    Replies: 111
    Last Post: - 29th October 2012, 17:39
  2. High Resolution Timer & Speed Calculator
    By WOZZY-2010 in forum Code Examples
    Replies: 4
    Last Post: - 7th February 2010, 16:45
  3. Timer + rc5
    By naga in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 19th November 2009, 07:56
  4. Timer interrupt frequency
    By Samoele in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 17th January 2009, 23:49
  5. timer interupt help 16f73
    By EDWARD in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 3rd July 2005, 08:41

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