PDA

View Full Version : Performing a loop every 10ms



wasssup1990
- 8th February 2008, 11:22
Hello. I am not sure how to setup my PIC16F877A to run a looping section of my code every 10ms. A rate gyro must be read every 10ms consistently so that my integeration calculations don't go crazy. I've read the Timer0 section of the datasheet but I just don't understand it well enough to achieve what I want. I am using PIC Basic PRO.
If I have missed an important piece of information here please ask me for it.
Thankyou.

ria_river
- 8th February 2008, 12:06
Can you display your code so that I will have an idea on what's going on.

Acetronics2
- 8th February 2008, 14:06
Hi, Wassup

As Timer 0 is a 8 bits timer ... and we suppose clock is 4Mhz ( ... )

I will be necessary to have a counting loop in the interrupt stub , because interrupts will occur every 256 µs as a maximum

The other solution is to use Timer 1 that has a 16 bits counting range ... and won't need any calculations.

Have a SEARCH for "PicMultiCalc" from Mr E. it's a nice tool to help for Timer designs ...

Alain

sayzer
- 8th February 2008, 14:59
...256 µs as a maximum

??

Minimum?

Maximum?

I must be on the other side of the mirror.

Acetronics2
- 8th February 2008, 15:44
??

Minimum?

Maximum?

I must be on the other side of the mirror.



The other side of the Bosphor, certainly !!!

1x 1µs = 1µs ... 256x 1µs = 256µs ...

so, the maximum period is 256 µs ...

what was requested for, is a period duration of 10 ms ... or a 10,000 timer count !

Tell me if I'm wrong !!!

Alain

skimask
- 8th February 2008, 15:45
Hello. I am not sure how to setup my PIC16F877A to run a looping section of my code every 10ms. A rate gyro must be read every 10ms consistently so that my integeration calculations don't go crazy. I've read the Timer0 section of the datasheet but I just don't understand it well enough to achieve what I want. I am using PIC Basic PRO.
If I have missed an important piece of information here please ask me for it.
Thankyou.

Use an LED for initial testing, make blinky first, otherwise, I'll bet you'll drive yourself crazy trying to get it to work right.
One 'easy-ish' way to get it to work...
Set up timer 0 to interrupt on overflow, get an 'On Interrupt' stub to work blinking an LED. Change the Tmr0 prescaler to 1:64. You'll get an interrupt every 16.384ms instead of 10ms. Yes, it's a bit longer than what you want, but you can easily scale the results from the 16.384ms interrupt to make them look like the results from a 10ms interrupt.
16384/10000 = 1.6384.
No, PBP can't divide by a fractional number...but, you multiply it up by a large number and divide it back down by a whole number, keeping the ratio the same to keep the numbers correct...i.e.
16384/10000 = 1.6384 which is the same as
32768/20000 = 1.6384 which is the same as...and so on and so on...
Or, set up Tmr1 as a 16 bit timer that kicks off every 10ms...just like the other guys said...
But $20 says you're biggest problem will be understanding how to get the interrupt to work in the first place...

sayzer
- 8th February 2008, 19:07
The other side of the Bosphor, certainly !!!

Alain

Depends on what side you are standing at.



1x 1µs = 1µs ... 256x 1µs = 256µs ...

so, the maximum period is 256 µs ...

what was requested for, is a period duration of 10 ms ... or a 10,000 timer count !

Tell me if I'm wrong !!!

Alain

The "maximum" means fastest then.

Also, what do we get from TMR0 at 4Mhz at 1:4 prescaler ?
I get roughly 1.03ms.

I may be doing a wrong math here.

wasssup1990
- 9th February 2008, 00:17
Wow, thankyou for all of your replies.

I forgot to mention that my crystal is 20MHz and both CCP1 and CCP2 are being used for HPWM.

I found PicMultiCalc and it is saying that I can use the 8-bit timer (Timer0) for an error of 0.371%. It says that if I use the 16-bit timer I will get an error of 0%. Now I have decided that I will use the 16-bit timer only if it won't interupt my HPWM. The HPWM is driving two motors. I am not at all familure with these timers and I don't know how to set them up or know when to start my 10ms loop so that the gyro can be read at the start of every loop at 100Hz (10ms intervals). Integration calculations and other processes are carried out after the gyro has been read. If all calculations and procesess have been done before the next loop should start, the processor should wait until the timer flag or whatever signals the loop to start over.

FYI I am making a balancing robot.

skimask
- 9th February 2008, 07:00
Wow, thankyou for all of your replies.
Said it before, I'll say it again...
Start simple...
Get the LED blinking at a known rate under an interrupt drive (or just get an LED blinking :) ), get the HPWM working on all channels in all situations, get the interfaces working, THEN tie them all together.
Otherwise you'll be shooting yourself in the foot wondering why...no more...just wondering WHY!!!!

wasssup1990
- 9th February 2008, 07:17
Well ok; so it's just a trial and error thing then? I'll just experiment with the PICMULTICALC values by plugging them into the right registers and i'll see how I go. BTW I have no idea of how to use an interupt. Anyhelp in using interupts in PBP relating to what I need will be very much appriciated.
I was hoping someone here has done what I want done before. Or perhaps someone could link me somewhere, but thanks for all of your help.

skimask
- 9th February 2008, 08:01
Well ok; so it's just a trial and error thing then? I'll just experiment with the PICMULTICALC values by plugging them into the right registers and i'll see how I go. BTW I have no idea of how to use an interupt. Anyhelp in using interupts in PBP relating to what I need will be very much appriciated.
I was hoping someone here has done what I want done before. Or perhaps someone could link me somewhere, but thanks for all of your help.

The old 'inverted pendulum' trick has been done a lot by various people using various processors...but in the end, it's all up to the guy behind the keyboard.
Read the book, work your way up to it...otherwise, again, your foot will end up with holes in it...

Acetronics2
- 9th February 2008, 12:33
FYI I am making a balancing robot.

You should browse the parallax site ...

They already did it ( and they share it !!! ) ... and moreover their basic is PbP compatible !!!

... at least TONS of understandable solutions to your headaches ...

Alain

wasssup1990
- 10th February 2008, 05:54
I saw that balancing robot on the paralax website. It doesn't use a gyro and an accelerometer. Useless on a sloped surface.

skimask
- 10th February 2008, 05:56
I saw that balancing robot on the paralax website. It doesn't use a gyro and an accelerometer. Useless on a sloped surface.

Differential GPS, one on each corner of a 4 cornered base... Solves everything! :D

wasssup1990
- 11th February 2008, 11:22
Ok I got the timer going, but brace yourself for a possible kalman filter question. I havn't found a single website that can explain kalman filters in laymens terms.

Acetronics2
- 11th February 2008, 12:45
I saw that balancing robot on the paralax website. It doesn't use a gyro and an accelerometer. Useless on a sloped surface.


... ROFL !!

THEY show you how to interface with the sensors, YOU write the program for your own requirements and dreams, with YOUR sensors.

Not just a cut and paste party ...

Alain

skimask
- 12th February 2008, 05:10
Ok I got the timer going, but brace yourself for a possible kalman filter question. I havn't found a single website that can explain kalman filters in laymens terms.

And you probably won't find one.
Best explanation I can offer is that a Kalman filter makes a best 'future guess' based on the accuracy of it's past 'future guesses'.
If the Kalman filter isn't sure of itself, it's 'future guess' goes out the window.
I remember this from working the INU's on -130's. Ring Laser Gyro based INU's. The aircrews wouldn't believe the INU's position, so they'd update the things fairly often from TACAN or RADAR or whatever, even handheld GPS's. Then they'd whine when they got back because the INU would start drifting really badly inflight. The crews would come back and whine, we'd explain to them that they had ruined the INU's confidence by telling the INU it wasn't where it thought it was even though the difference between where the INU thought it was and where the crew told it that it was was very small. But of course, the aircrews knew better, because they're officers, and have degrees right?
We'd have to go out and run a ground align on the INU about a half a dozen times and each time, just let the INU cook by itself for awhile to build up it's confidence. At first it would sit there and drift away from the spot the aircraft was on. After a couple of time, the drifting would get less and less and eventually, it wouldn't drift at all.
But all that explanation doesn't help a person write any code now does it? :D

wasssup1990
- 13th February 2008, 05:00
This post above and the post about the PicMultiCalc have been the most usefull to me in this thread. PicMultiCalc taught me a little bit of how timer0 works, and the post above taught me in laymens terms what the kalman filter does. The post two poitions up by Acetronics is not usefull and this person has probably no life. (:

skimask
- 13th February 2008, 05:09
This post above and the post about the PicMultiCalc have been the most usefull to me in this thread. PicMultiCalc taught me a little bit of how timer0 works, and the post above taught me in laymens terms what the kalman filter does. The post two poitions up by Acetronics is not usefull and this person has probably no life. (:

Well, then let me not help you out a bit more...
If I could edit my Kalman filter post down to nothing but agreement with post #16, I would, but I can't.
And a Kalman filter is a helluva lot more complicated than the 'layman's terms' I laid out...and probably beyond your level of comprehension by a few orders of magnitude...
So, when you get a chance, here's what you do....
Step outside and go play a one handed game of hide and go #@$^&^@ yourself...

Archangel
- 13th February 2008, 10:05
East Tonto, You got leftover DoNuts older than this Kid . . .But ... LMAO

skimask
- 13th February 2008, 16:51
East Tonto, You got leftover DoNuts older than this Kid . . .

East Tonto? Huh? Where am I? Too rough?

Acetronics2
- 13th February 2008, 17:12
If that page doesn't give you enough to understand those filters ...

http://www.cs.unc.edu/~welch/kalman/

There's no more to do for you than sell hotdogs, bretzels or pizzas ( even donuts ) in the street ...

I also remember having seen some of these filters used in UAV's ...

So, I suspect more a SEARCHing problem than an Understanding problem ...

But it's me ...

Alain

Archangel
- 13th February 2008, 21:01
If that page doesn't give you enough to understand those filters ...

http://www.cs.unc.edu/~welch/kalman/

There's no more to do for you than <font color=red>sell hotdogs, pretzels or pizzas ( even donuts ) in the street ...</font color>

I also remember having seen some of these filters used in UAV's ...

So, I suspect more a SEARCHing problem than an Understanding problem ...

But it's me ...

Alain
Hi Alain,
I looked at the whitepaper on that website . . . is that really math or pop art?
You wanna buy a hot dog? :o

Acetronics2
- 14th February 2008, 09:25
Hi, Joe

Was somewhat like that when I first discovered Fuzzy Logic ...

You know what ???

I DO HATE Maths ... But will surely enjoy a good Hotdog !!! _ Making Hoddogs is a full job. Programming Pic too.

LOL !!!

Alain

wasssup1990
- 14th February 2008, 09:29
What a group of people. This forum is sad. It seems like we have a serious case of arrogant and negatively biased people. It's sad that such a potentially useful topic had to end like this. I only wanted some simple help and it seems some people have different reasons for being here. Whether it be belittleing people or actually giving them a hand in solving a problem. I certainly won't be recommending people come here. I certainly hope you idiots stick to this corner of the website and don't infect anywhere else. I've never encountered such a negative group of people like this on any other forum. Well done. ):
For those people who know they did no wrong, are not responsible.
I hope the site admin read this.

mackrackit
- 14th February 2008, 10:49
Whatever happened to people that want to learn.. Seems like all anyone wants these days is their problems solved for them with as little effort as possible on their part.

That is what is really sad.

Here we have a group of people willing to help others in a fairly complicated field, for no compensation whatsoever. From all walks of life and technical backgrounds, willing to share their knowledge.

I do hope that people like wasssup will wise up. If they do not, and more of the world is filled with people like that... I fear for the future.

skimask
- 14th February 2008, 17:22
What a group of people. This forum is sad. It seems like we have a serious case of arrogant and negatively biased people.
You're in the minority that thinks this way.


It's sad that such a potentially useful topic had to end like this.
Useful to...YOU. If it wasn't useful to...YOU...then you wouldn't be whining.


I only wanted some simple help and it seems some people have different reasons for being here.
You wanted it handed to you on a platter so you could modify it and call it your own and impress your teachers or your buddies...whatever...


Whether it be belittleing people or actually giving them a hand in solving a problem. I certainly won't be recommending people come here.
If you couldn't figure this out, what makes you think your recommendations will hold any water in the first place?


I certainly hope you idiots stick to this corner of the website and don't infect anywhere else. I've never encountered such a negative group of people like this on any other forum. Well done. ):
Negative, maybe...but if it'll trip you off into trying to make your own solutions work rather than getting somebody else to do it, then you're probably better off in the long run...ya think?


I hope the site admin read this.
Probably will, might laugh, might cry...you think they read everything? Probably do...not a LOT of traffic here to filter thru.

skimask
- 14th February 2008, 17:24
I do hope that people like wasssup will wise up. If they do not, and more of the world is filled with people like that... I fear for the future.
I had a guy in my shop this morning, 32 years old, and I thought he was a smart guy, messes with electronics, etc.etc. Had a flat tire and didn't know how to change it so he called a tow truck. $200USD for the call!!! And he was here at the shop. He could've walked 15 feet and asked me to help him out...
That's the kind of crap the world is facing sooner more than later...

mackrackit
- 14th February 2008, 17:35
I had a guy in my shop this morning, 32 years old, and I thought he was a smart guy, messes with electronics, etc.etc. Had a flat tire and didn't know how to change it so he called a tow truck. $200USD for the call!!! And he was here at the shop. He could've walked 15 feet and asked me to help him out...
That's the kind of crap the world is facing sooner more than later...

That fellow could have been just plain LAZY. Either way, he has a problem.

Does this all boil down to the way kids are raised now days?

skimask
- 14th February 2008, 17:46
That fellow could have been just plain LAZY. Either way, he has a problem.
Does this all boil down to the way kids are raised now days?

I don't think so... I acted up, my parents/relatives/adult around the block/etc, they all let me know it up-side the head.
Might be the way those morons in 'high places' on their high-horses are telling us we have to raise kids nowadays...
Remember getting slapped up-side the head in the grocery store by Mom 'cause you were messing around? Try and do that these days... Amazing how much a guy CAN'T get away with these days...

Oh....you're talking about the guy in my shop!!!
Ya, I've just figured out he's an idiot and/or Lazy...
So we're all rubbing it in that the guy can't change a flat tire...and we'll probably continue to do so for another few weeks/months. (BTW - I took a look at his vehicle, the instructions, with pictures, are right there in his owner's manual in the glove box and a picture diagram in plain view on the trunk lid (boot cover?)...this guy is now in my book as useless...)

mackrackit
- 14th February 2008, 17:52
When I was growing up and Mom said that she was getting a SWITCH...She was not referring to an electrical component :eek:

skimask
- 14th February 2008, 17:59
When I was growing up and Mom said that she was getting a SWITCH...She was not referring to an electrical component :eek:

Ya, mine too...it think it was an OKMU switch...One kid, multiple uses...for some reason, I begin to think that I was always the one that turned that switch on........and it seems like it never turned off! :D

wasssup1990
- 15th February 2008, 03:18
There you go people, my point has been backed up by these last few replies. I notice there are alot of assumptions being made.
E.g. 1 From skimask: "You wanted it handed to you on a platter so you could modify it and call it your own and impress your teachers or your buddies...whatever..."

Now that is some assumption. You jumped to that conclusion skimask, I know some other people on this form may do that, but all I needed was a somewhere to start. Go and look at my first post. See how innocent that looks? I guess the assumptions started right there. (:

E.g. 2 From mackrackit: "Whatever happened to people that want to learn.. Seems like all anyone wants these days is their problems solved for them with as little effort as possible on their part."
This person made the same assumption. What made you think I just wanted the answer straight up? It would have helped if someone could step me through how to set up Timer0.

Acetronics thought this was a cut and paste party. All I needed was some guidance.

Hmm I wonder what's going to be said next?

skimask
- 15th February 2008, 03:55
Hmm I wonder what's going to be said next?

Allow me.... ('cause most will :) )
Post #1 = +
Post #2 = +
and so on...
Post #15 = +
Post #16 = +
Post #17 = NEGATIVE - and it'll all downhill from there...thanks to...anyone...anyone...yep, you guessed it...

Darrel Taylor
- 15th February 2008, 04:19
It's suppoased to be a loop every 10ms.
Not an insult every 10 ms.

It's like a "Pack of Wolves".
And wasssup didn't start anything.

Y'all need to mellow out, and write some code.

Here's mine...


ReadingTaken VAR BIT
TRM0IF VAR INTCON.2
TMR0IE VAR INTCON.5
TimerReload CON 60

OPTION_REG = %11010111 ; Assign Prescaler to TMR0, 1:256, internal clock
TMR0 = TimerReload ; Load timer for first delay
TMR0IF = 0 ; Clear the interrupt flag
ReadingTaken = 0 ; indicate reading hasn't been taken yet

TMR0IE = 1 ; Enable TMR0 interrupt
ON INTERRUPT GOTO TakeReading

Main:
If ReadingTaken then ; Wait for reading to be taken
ReadingTaken = 0 ; Indicate the reading has been handled

; -- Reading has been taken, do something with it here --
endif

GOTO Main

DISABLE
TakeReading:
TMR0 = TimerReload ; reload timer for next period
TMR0IF = 0 ; Clear the interrupt flag

; -- this is called every ~10.036ms -- (with 20mhz OSC)
; Take your reading

ReadingTaken = 1 ; Tell main loop that a reading has been taken
RESUME
ENABLE
HTH,

wasssup1990
- 15th February 2008, 08:40
Thankyou Darrel
Admin has come to the rescue.

Thankyou for that usefull code. If I ever use any of it I will put credit to you in my source code and whoever else you think deserves credit for it.

I hope I never run into trouble like this again on this forum. I'm just a electronics and programming hobbiest needing a little programming help. I didn't need any of that rubbish above.

Acetronics2
- 15th February 2008, 09:38
MHhhhhh,

Thinking to it, Darrel is perfectly right ... ( as most of time ! )

We are here for programming pics tricks ... and Everything else is just "optionnal". May be this thought could avoid further fighting parties ...

Here are My two cents !

Alain

sayzer
- 15th February 2008, 09:44
It's suppoased to be a loop every 10ms.
Not an insult every 10 ms.

It's like a "Pack of Wolves".
And wasssup didn't start anything.

Y'all need to mellow out, and write some code.

Here's mine...

[code]ReadingTaken VAR BIT
TRM0IF VAR INTCON.2
TMR0IE VAR INTCON.5
TimerReload CON 60

....



Minor correction to DT's code above:
TRM0IF VAR INTCON.2 should be TMR0IF VAR INTCON.2

Also, I agree with DT. Here is my code thingie with my approach:

1.03mS interrupt at 4 MHz, 1:4 Prescaler Assigned to TMR0.
This works the opposite way of what DT does.
DT's code interrupts every 10mS, my code interrupts every 1mS.
Work on either one.



<font color="#008000">@ DEVICE PIC16F628A, INTRC_OSC_NOCLKOUT, WDT_OFF, PWRT_OFF, MCLR_OFF,PROTECT_ON,CPD_ON, BOD_OFF

</font>SetAll:
<font color="#000080"><b>CLEAR
</b></font>TRISA = <font color="#FF0000"><b>%00000000
</b></font>TRISB = <font color="#FF0000"><b>%00000000
</b></font>CMCON = <font color="#FF0000"><b>7
</b></font>VRCON.<font color="#FF0000"><b>7 </b></font>= <font color="#FF0000"><b>0
</b></font>VRCON.<font color="#FF0000"><b>6 </b></font>= <font color="#FF0000"><b>0

</b></font>PORTA = <font color="#FF0000"><b>0
</b></font>PORTB = <font color="#FF0000"><b>0

</b></font>TimeAdder <font color="#000080"><b>VAR BYTE

</b></font>OPTION_REG.<font color="#FF0000"><b>5 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' TMR0 internal clk select bit.
</i></font>OPTION_REG.<font color="#FF0000"><b>3 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Prescaler Assigned to TMR0.
</i></font>OPTION_REG.<font color="#FF0000"><b>2 </b></font>= <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Bit 2,1,0 : 1:4 prescaler.
</i></font>OPTION_REG.<font color="#FF0000"><b>1 </b></font>= <font color="#FF0000"><b>0
</b></font>OPTION_REG.<font color="#FF0000"><b>0 </b></font>= <font color="#FF0000"><b>1

</b></font>T0IE <font color="#000080"><b>VAR </b></font>INTCON.<font color="#FF0000"><b>5 </b></font><font color="#000080"><i>'INTCON.5 = 1 ' Enable TMR0 interrupt.
</i></font>T0IF <font color="#000080"><b>VAR </b></font>INTCON.<font color="#FF0000"><b>2 </b></font><font color="#000080"><i>'INTCON.2 = 0 ' TMR0 interrupt overflow bit. Must be cleared in software.

</i></font>T0IF = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Cleared.
</i></font>T0IE = <font color="#FF0000"><b>1 </b></font><font color="#000080"><i>' Enable TMR0 interrupt.
</i><b>ON INTERRUPT GOTO </b></font>TMR0_Int

Begin:
<font color="#000080"><b>IF </b></font>TimeAdder = <font color="#FF0000"><b>10 </b></font><font color="#000080"><b>THEN </b><i>' We have 10mS cycle.
</i></font>TimeAdder = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Clear time adder.
'Do something here.
'.
'..
'...
</i><b>ENDIF


GOTO </b></font>Begin



<font color="#000080"><b>DISABLE
</b></font>TMR0_Int: <font color="#000080"><i>'This routine is called every 1.03mS. Act Fast here.
</i></font>TimeAdder = TimeAdder + <font color="#FF0000"><b>1
</b></font>T0IF = <font color="#FF0000"><b>0 </b></font><font color="#000080"><i>' Clear interrupt flag bit.
</i><b>RESUME
ENABLE


</b></font>

Darrel Taylor
- 15th February 2008, 09:54
Minor correction to DT's code above:
TRM0IF VAR INTCON.2 should be TMR0IF VAR INTCON.2
WooHoo!, Somebody's paying attention. I love it!
Dang dyslexia.

Thanks Sayzer!
<br>

sayzer
- 15th February 2008, 10:16
WooHoo!, Somebody's paying attention. I love it!
Dang dyslexia.

Thanks Sayzer!
<br>

Attention is in my heart,
Reading and posting is never hard,
Let be friends, don't post assault,
Share the knowledge, you won't lose a byte.


P.S. - I am in finals going toward award.

Darrel Taylor
- 15th February 2008, 10:31
P.S. - I am in finals going toward award.

At poetry.com?
That's great!

May the judges "Let it be". :)
<br>