PIC Controlled clock movement hack?

Thread Starter

Tigthwad

Joined Oct 27, 2009
31
Thanks! I had written it that way for read/understand ability...this is for my 12 year old son to understand and it helps when it is more English like, but still full control (no Lego Mindstorm/Arduino drag/drop etc).

Stuck on how to run this for a set period of time, I haven't ever used timer0 etc but this seems like the right kind of application? The whole code needs to run for 6 mins and 30 seconds. I know with an interrupt we can make it do the "fast run" section (might not be as fast as desired but will work). Any direction on a timing tutorial?
 

THE_RB

Joined Feb 11, 2008
5,438
You can use similar code to the one I mentioned in post #6 and #7, but instead of making an exact second, make a period that will be one full step of the stepper motor.

The length of that period sets an exact motor speed. Let's assume you pick a period of 5mS, here is the code for one step every 5mS period (with average exact timing);

Rich (BB code):
// C code for a 5mS period with a 1MHz timer (4MHz xtal); 
// gets here every TMR0 int (every 256 ticks)

bres += 256;         // add 256 ticks to bresenham total

if(bres >= 5000)     // if reached 5mS
{
  bres -= 5000;      // subtract 5mS, retain error
  move_motor();      // move steppermotor to new step
  iterations++;       // count one more step done!
}
Then you just put that in a loop, and every iteration through the loop will be 5mS, so to run the motor for 6 mins 30 seconds (390 secs) you perform the loop X times;
390 seconds / 0.005 secs per step = 78000 iterations.

So you just need an unsigned long variable to count iterations (as it is a big number), then when it gets >=78000 you are done.

The final result is the motor runs at an exact average speed for an exact total time, and also doing an exact total number of steps.
 

Thread Starter

Tigthwad

Joined Oct 27, 2009
31
I am back to not really understanding how to user TMR0...

How do I initialize and set TMR0? I know this is a very novice question but we just kind of jumped into this with no experience in coding or microchips...I can use any pointers/examples anyone has.
 

THE_RB

Joined Feb 11, 2008
5,438
The datasheet is your friend there. TMR0 is the easiest timer and is a pretty simple chapter to read.

For the code above you set TMR0 prescaler to 1:1 (check datasheet for OPTION register).

To use the TMR0 interrupt you enable GIE bit in INTCON, and TMR0IE bit in INTCON, then the interrupt will occur every 256 ticks when TMR0 tolls over.

At the end of the interrupt you need to clear the TMR0IF bit, (the interrupt flag).

You should also have a google there should be tons of hits for "C code for using PIC TMR0". :)
 

John P

Joined Oct 14, 2008
2,026
I think it's time you divulged which compiler you have for writing the code. The different compilers have their own ways to deal with interrupts, and you'll need to look through the manual to find out how to do it. But be assured that it's a common requirement that they've found ways (more or less successfully) to help the user to deal with.
 

takao21203

Joined Apr 28, 2012
3,702
I am back to not really understanding how to user TMR0...

How do I initialize and set TMR0? I know this is a very novice question but we just kind of jumped into this with no experience in coding or microchips...I can use any pointers/examples anyone has.
When I was in that age I did read fairly complex cartoon stories, for instance some of the carefree one's including an old crook with a big treasure, and various family members. Basically each story was a variations of a gang wannabe gangsters plotting to break into his treasure.

Donald Duck, Mickey Mouse, and Co. by Walt Disney.

Setting up TMR0 is not much more difficult or complex than these cartoon stories.

Cartoon books however are subject of criticism and debate, some parents think they are capable of alienating their children from parental authority.

Instead of becoming obsessed with details and get locked into solving mechanical problems your son should be able to explore the technical documentation and to understand it at least on the margin.

When I was 13 or so, there were just some heavy IBM manuals, and no teacher at all. At first I learned about MSDOS within a day or two, and then I wrote my first BASIC programs.

I did read them like I would read a cartoon book- not taking the details too seriously, and trying to have some fun with it. If I did not like or did not understand something, there would be other cartoons to read.
 

Thread Starter

Tigthwad

Joined Oct 27, 2009
31
Thanks guys, sorry for so many questions. I am still exploring the data sheet and getting a feel for TMR0. I will take a shot at the code and see how it goes.
 
Top