PDA

View Full Version : Timer PIC16F57



leonel
- 29th June 2005, 17:44
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

Dwayne
- 29th June 2005, 17:50
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

Acetronics2
- 29th June 2005, 17:54
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 !!!

Dwayne
- 29th June 2005, 18:06
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

leonel
- 29th June 2005, 18:08
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

Acetronics2
- 29th June 2005, 18:12
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

leonel
- 29th June 2005, 18:31
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...

Acetronics2
- 29th June 2005, 18:53
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

Dwayne
- 29th June 2005, 19:37
Hello Loenel,

How accurate does it have to be on blinking every 20 seconds?
We now have concluded that there is muching going on, thus, it is not just a simple "Blink" a light.

1. How long of a pause is allowed?

For example: Untested... but the idea is there...




Counter1 var word
Counter2 Var word
Loop:
Counter1=0
Counter2=0
Loop2:
if Counter1< 20
if Counter2 <1000
Pause 1 'Pause 1/1000 of a second
CheckButtons....
Do stuff...
CheckButtons....
Do Stuff
if CheckButtons==Pushed then goto Loop
Counter2=Counter2+1;
Endif

if(Counter2>=1000)
Counter1=Counter1+1;
counter2=0;
endif
endif
if Counter1<20 goto Loop2

Blink light = on
Pause 200 'pause 1/5 of a second to light up the light.
Blink Light = off
goto Loop:


Granted, this is a "oversimplification" of code. But it is a generic program that will allow lights to be blinked and still scan keys once ever 1/1000 of a second (with the exception of a blinky light.

leonel
- 30th June 2005, 08:25
Ok, i'll explain my secret weapon ;)
I have 8 buttons that will control relays. One button corresponds to one relay. 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. On led is on if one relay is on...
Hereīs my secret weapon :)
It was simple if i was using PIC16F872 (thatīs the PIC i know better...)
My pulse itīs 200ms and the time itīs not critical (if i have 17s instead 20s thatīs ok)

Acetronics2
- 30th June 2005, 10:24
ok ..something usable now ...

leonel
- 30th June 2005, 13:44
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?

Dwayne
- 30th June 2005, 14:54
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

Dwayne
- 30th June 2005, 15:15
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.



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

leonel
- 30th June 2005, 15:19
Yes... all your 5 points are correct. If you understand my code thatīs a good beginning!
Thank you!

Dwayne
- 30th June 2005, 15:29
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


If a > b then
c=d
d=f
f=g
endif


this does not need a endif statement;


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

Dwayne
- 30th June 2005, 15:42
Hello Leonel,,

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




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

leonel
- 30th June 2005, 15:57
Iīm sorry to start by a blinky light, but my english itīs not good to explain in detaill all things, so iīve put a simple question ;)
Iīm used to finish with endif, but if itīs better for my code space iīll follow your advise!
Just another question: my code after compiling gives me this warning " "Crossing page boundary -- ensure page bits are set." i donīt have to worry, because PBP takes care of this problem???!!! I donīt have any branch instruction so thatīs ok (pag.13 of PBP manual), my code will work great! Correct?

Dwayne
- 30th June 2005, 16:19
Hello Leonel,

Leonel


Iīm sorry to start by a blinky light, but my english itīs not good to explain in detaill all things, so iīve put a simple question


That is fine Leonel... Even I have problems... so you and I are in the same boat together... We can wave flags at each other, and sink with the ship...at least we will have a little fun.



Iīm used to finish with endif, but if itīs better for my code space iīll follow your advise!


This is not a requirement Leonel... This is all up to you and your preference. You may code anyway that makes you understand it. I only made a suggestion, to show you a different way how to code. I have been corrected many times on my coding!... Sometimes I change, sometimes I don't. As long as the code compiles, and I understand it... its fine with me.



Just another question: my code after compiling gives me this warning " "Crossing page boundary -- ensure page bits are set." i donīt have to worry, because PBP takes care of this problem???!!! I donīt have any branch instruction so thatīs ok (pag.13 of PBP manual), my code will work great! Correct?


You shouldn't have to worry about crossing page boundary... PBP takes care of that... I only question is why??? I don't think you have 2 k of code there... but I could be wrong. But you should not have to worry about this warning.

Now about your code... If you notice my tag, one of the statements is: "Compile something that runs" There is no guarantee on your code (or my code) running to do the job you want it to do. Since I have no way how to test it, I can only give you code that is Close to what I think you are wanting.

Remember, the code I am giving you is a "shell" around your "Secret weapon" That shell is taking care of a blinky light, taking care of your 20 and 40 second timing, and also checking your switches. I noticed on your code, that my routine was used as a kind of subroutine. This may get you into a little trouble...with how your program works. But you can try it!...

Since you said the blinky light can be off by a few seconds, I made the code as a shell. A shell is the part that fits around your program and stuff. What will vary your blinky light, will be the code in my shell. But I am betting that the computer is so fast, that your code will not make that much difference between the blinky lights.

I also noticed you used a Pause 1000 . That is a 1 second delay...That means if you are pushing a button, you may have to hold it for at least 1 second! If this is ok with you, great.

My biggest question is:

Do you fully understand what my code is doing? Do you fully understand that it is a shell, and how it is taking care of the timing, switch checking, and blinky light? Do you know where to insert your code, so that my code will work like it should? I only ask these questions, because it seems you are using it as a kind of Subroutine. (I have not studied your code to fully comprehend it, but I just picked up on a few little things that made me wonder).


Dwayne

leonel
- 30th June 2005, 16:38
Yes i understand your code and your right, iīm using it with a subroutine, and with your explanation i understand what a shell is (an oil pump ;) )
I used your code with a little modifications... now i have to build my hardware and test it.
I know that with pause the program doesnīt check anything, but in this case (only in 2 inputs), thereīs no problem. If you see i'll make some of my outputs high during 20s or 40s with pause. and in this time i donīt want to see any input.

Dwayne
- 30th June 2005, 16:56
Hell Leonel,

Ok! Just wanted to make sure!...

I also noticed you changed my "If" statements to "while" statements.
Since I do not have a testing ground here, I do not know what will happen, if the program "Jumps" out of the middle of a while statement too many times.
I have had no problems jumping out of a "If" statement... but I am not sure about a while statement. So, I did not use while statements, for fear of crashing <g>.

Ralph, Steve, Melanie, Darrel, Bruce... Whats the answer???



Loop
a=1

while a <100
.....
if a==50 goto Loop
...
a=a+1
wend
....
goto Loop.


If a hits 50 a bunch of times, will this give PBP a problem and lose its stack? or run haywire? I know in some older versions of C/C++, it could cause a problem with memory...but I am not sure in PBP.

Dwayne

leonel
- 30th June 2005, 17:02
To be honest i donīt know the answer too! If anyone can help us...
In all my experience (10 months in PBP :) ) i didnīt have problems, but... thereīs always a but!

mister_e
- 30th June 2005, 19:04
personnaly if i want to jump out of a while statement i would prefer to validate the WHILE condition and set a JUMP or FLAG bit to do XYZ task...

well code will talk by itself...



Start:
While AVar!=50
if AVar==20 then
AVar=50
FlagBit=1
endif
Wend
IF FlagBit=1 then Start


BUT i would also prefer another approach... like two WHILE statement one after the other... case by case i guess.

I don't belive that the Stack will be a problem since it's not a GOSUB...

Dwayne
- 30th June 2005, 19:33
Hello Steve,

Steve>>i would prefer to validate the WHILE condition and set a JUMP or FLAG bit to do XYZ task...<<

Great minds think alike <grin>... (Hope you don't take that as a insult)

I am like you... I like to force the while to do its job...

Thanks Bud!... I hope things are well in Canada...

Dwayne

mister_e
- 1st July 2005, 02:36
Great minds think alike ... (Hope you don't take that as a insult)

i don't see why i should ;o]


I hope things are well in Canada...

couldn't be better thanks!!!

leonel
- 1st July 2005, 08:14
What i understand that itīs better to put a flag bit in a while do a goto in an if statement!! Ok...
Thank 's to all of you for your help...