Questions about use of SX48 16-bit timers
![RobotWorkshop](https://forums.parallax.com/uploads/userpics/915/nCTRZ4MIBWKDB.jpg)
Hello,
I have a quick question about the Internal 16-bit timers on the SX48 chip. I can see that the
output of the timers (when running) will normally drive RB5 (Timer 1) and RC2 (Timer 2) if
the timer is active. So, the question is what it the best way to shut off the timer when you
are done with it and then let the normal SX48 port control the pin? Is this just done by
using the TIMER1 CLEAR command? Or, does it just need to be redefined as an input or
output.
Robert
I have a quick question about the Internal 16-bit timers on the SX48 chip. I can see that the
output of the timers (when running) will normally drive RB5 (Timer 1) and RC2 (Timer 2) if
the timer is active. So, the question is what it the best way to shut off the timer when you
are done with it and then let the normal SX48 port control the pin? Is this just done by
using the TIMER1 CLEAR command? Or, does it just need to be redefined as an input or
output.
Robert
Comments
TIMER1 CLEAR just clears the counter, you need to put the timer into "software" timer mode by using the "TIMER1 TIMER" command.
P.S. Great question. I actually had to search because I didn't know myself.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
Is this something that can get added to the next revision of the SX/B Help file?
Robert
Does anyone happen to know the default behavior of the TIMER when in TIMER mode? I've read in the datasheet that this can generate an Interrupt.
The reason I am asking is that i've been using the ISR based serial routines that Jon Williams has posted to this forum. So far they had been working great (just using the receive portion) but this evening they seem to be off and I no longer receive the characters as expected. Just wondering if perhaps the TIMER1 TIMER is generating extra interrupts which is now throwing off the ISR timing and trashing the Serial data coming in.
If that is the case then that goes back to my original question on how to turn off a TIMER and put it back in the state it was before you started it.
I'm going to sleep on it and see if I can take a fresh look at this in the morning.
Robert
Post Edited (RobotWorkshop) : 2/21/2008 3:37:01 PM GMT
They are intended mainly to do other things, like generate PWM for motor control in parallel with the main program running other tasks. Generally, the ISR routine is independent of the timer routines, though I can imagine more creative and exotic programing with the timers sharing the ISRs interrupt. I don't know if it would be good or useful. If one does enable the two additional timers, then it is possible to enable an internal interupt or use a pin to provide an external interrupt.
I've looked at the interrupt possiblities and really wondered about all the choices. I suspect that if you want to use the WTD with postscaller, it may block using the RTCC for ISRs. If that is true, it appears that you can use one of the 16bit timers to take over the RTCCs functions. This is all speculation at this point.
NONE of this applies to SX28 as it doesn't have the extra two timers.
I suppose that you want to know that the DEFAULT behavior is simply·that the two additional timers are not in play.· You can check listings·of the defaults for all the·Port·B and C mode registers to be sure.
IN SXAsm, the timers are selected by choosing the proper MODE register to reach two Timer Control Registers A & B for each timer and then toggling the right enable bits. Each of the Timers is associated with·one port, Port B and Port C for inputs or outputs and control. I am not really clear on how SX/B handles all that. I suspect it is all does pretty close to the way the SXasm handles it unless someone has written specialized uses, like Timer PWM or Capture and Compare.
Take a look at the diagram in the Ubicom SX48/52 PDF for the interupt logic on page 23. The main - page one - block diagram seems wrong as it doesn't show a possilbe·internal interupt connection between the two additional timers and the interupts that use the interupt stack.
If you are going to work with these additional timers, you may need to read and fully understand that PDF anyway.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Post Edited (Kramer) : 2/21/2008 4:15:48 PM GMT
I am using one of the 16-bit timers in PWM mode to control the speed of a single motor. The program is written in SX/B and the problem now appears to be that some of the TIMER modes may (or may not) generate interrupts. If they are I suspect it is breaking the ISR serial receive code i've been using. If that is the case then I need to find a way to configure them to stop generating an interrupt. From the datasheet it looks like it may be possible to change a config register to handle this. I just wanted to see if anyone else has run into this problem before. If so perhaps adding some inline assembly to set the config register to disable the interrupt on the timer is all that is needed.
I'm going to go over the datasheet again and am going to try and see if I missed anything that may help.
Robert
Post Edited (RobotWorkshop) : 2/21/2008 7:01:16 PM GMT
So, I pulled opened up the assembly code for my program and went over all the code generated for working with TIMER1. It appears that it is always working with the T1CNTB register or the capture registers. I did a search through all the code for a $17 and not once did I find that it tried setting the T1CNTA register. Assuming that is the case and the default setting is bit 0 is on then as soon as I specified TIMER 1 TIMER the program would start generating extra Interrupts which would then in turn trash the incoming Serial data that is based on ISR code.....
I had used the TIMER1 TIMER as a way to shut off TIMER1 PWM mode. This is something that needed to be done since it is controlling the speed of a motor and there are many times for wanting to turn off the motor.
So, at the moment I am looking at alternative ways to shut down a TIMER until I need it again. An alternative may be to try and add some inline assembly at the beginning of the program to set the option register for TIMER1 to clear out bit 0. Since nothing else in the program sets it that should work and prevent the timer from generating undesired interrupts.
Unless I am way off here I think that adding something like this at the start of my program might work:
' Code to disable Interrupts on TIMER1 (assumes wrkVar in current BANK)
ASM
MOV W,#$07
MOV M,W
MOV !RB,W
AND W,#$FE
MOV wrkVar,W
MOV W,#$17
MOV M,W
MOV W,wrkVar
MOV !RB,W
ENDASM
If all this works then perhaps some notes can be added to the next SX/B help file about this (or maybe Jon's new book)
Robert
As a very temporary work around I did the following:
To stop the PWM timer I used the following line instead of TIMER1 TIMER
TIMER1 PWM, 1, dutyCycle
Once I did that my program is behaving as I would expect and the ISR based serial code seems to receive everything sent to it perfectly!
I really, really don't like my work around since it it sending out a tiny pulse to the H-Bridge all the time. Not enough to move the motor but wasting battery power all the same. It just isn't the right way to do it but at the moment it is the only thing I have that works. Help! Anyone have a better solution???
Can someone else using the ISR based Serial code on an SX48 try an experiment or two using the timers with PWM or trying the timer mode to confirm this behavior? There has got to be some way to shut off the PWM and put the timer truly back into the state it was before you start using the timers.
Robert
It confirms what I suspected. The Timer1 interrupts are associated with Port B· [noparse][[/noparse]I guess there is a Timer2 that is tied to Port C that does the same] and can be fed into the Interrupt pathway that triggers the ISRs interrupt stack. So that is the source of your Serial I/O failure - two timers providing different ISR interrrupts.
I am not sure how SX/B presents this or if there is currently adequate isolation features in the software. It is really easier to study in assembly language and it appears that you have gotten the hang of it. Something needs to be considered and written about avoiding ISR conflicts when using these two timers.
I presume that Jon's ISR and the approach in his book is SX28 oriented: almost nothing has been explored and published about the SX48/52 timers.
You are doing pioneering with this topic.· I find it both interesting and wierd that the an ISR can be created with 3 interrupts or that the RTCC might be eliminated and one of the other Timers can generate a more complex IRS timer.· As I said about, this is exotic - but may not be useful.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Post Edited (Kramer) : 2/22/2008 6:08:18 AM GMT
At the moment I think the only EXTRA interrupts are coming from TIMER1 as that is the one I am using. As long as you don't touch them they are dormant and don't seem to generate any Interrupts at all. Also, in regular PWM they don't generate an Interrupt either. It only showed up when I switched to the TIMER mode which can generate Interrupts. I've tried changing the small section of inline assembly code to set T1CNTA register to $DA instead which should prevent any Interrupts from coming from TIMER1 but it didn't seem to fix it.
I expected that other Interrupts would all go to the same ISR. That may be desired in some cases and it would be up to the ISR code to determine what generated the Interrupt. I think that it would also be possible to add a check to the beginning of the ISR to check to see if the Interrupt was due to the TIMER and if so just exit the ISR without hitting the Serial code. At the moment the Serial code only expects to get executed at the specific any Interval defined at the beginning of the Interrupt code. If other sources generate an Interrupt it just assumes that it is time to look for another bit coming down the Serial line.....
If I needed both Interrupts to be handled by the ISR then I suppose it could be rewritten to handle that. I'd make the code at the very beginning of the ISR check to see what generated the Interrupt then branch accordingly to the appropriate section of code within the ISR to deal with it.
The part that is really bugging me about this problem is that the timers don't normally start generating Interrupts unless you use them in certain modes. I just want to find a way to properly shut off a TIMER running in PWM mode so that it ends up in the same state it was before I started it. There has got to be some way to do it but I haven't found the best method yet.
I don't think any of this is a problem with the SX48 chip. We just need to get all the behavior documented so that there isn't any surprises. At the very least the next update of the SX/B help file should probably have a note regarding the TIMER mode that by default it WILL generate Interrupts ad that when they are used you'll have to add code to your ISR to handle them.
Another thing that would be cool for a future upgrade of SX/B would be an extra command like TIMER1 OFF to reset the state of the timer to put it back in it's initial state. Another possible addition would be some command to easily set the timer configuration registers like TIMER1 CONFIG XXXX where XXXX could be the value to assign to the config registers for the timer specified. Just throwing out a couple ideas for later.
So, I am still looking for a better way to turn off the PWM mode and will be watching the forum to see if anyone else may have something to add.
Robert
But, I still suspect that when you use the Watch Dog Timer, you may need to use one of the Multi-use Timers to run your ISR interrupts. Since the 16-bit layout uses writes to two registers, it is nearly the same as an 8-bit Timer with pre-scaler set up of the RTCC. You just have to include a few more steps in assembly language to provide the Timer with an interrupt value as the register is not Globally available.
In fact, now that I think of it, it could be best to do nothing to ISR and RTCC setup if you could find a way to have a multi-use Timer provide an alternative Watch Dog Timer.
I fear this may all be too complicated for most SX/B users as they may want Basic to avoid all this low level stuff and just want a command that provides a function that works everytime and in every situation.
Sorry that I cannot help with an SX/B solution. You really don't have to reinstate the Timers two registers in entirety to have them be off line. But you do have to go through MODE to reach the right register, then toggle the bit on the right Port. Guenther's book has details on pages 267 and 268.
I do see that there is a lack of info about the original settings before you started messing with it. It looks like one needs to get a factory fresh SX48 chip and read all these before they begin to use it. I suspect going to PWM mode and then stopping the output is adequate. You can do the same thing is Software Timer mode, but also turn off any active interrupt bits.
In other words, some bits are Enable bits and some bits are informational flag bits. One has to identify all the Enable bits to get things sorted out. In this case, the names don't clearly identify which is what.· Also, it appears that to stop things, the Enable bits are not enough.· The free-running counter may be always going.· So you have to enter values in the other registers [noparse][[/noparse]depending on which function you want to make quite] that stop generating either a capture interrupt, a compare interrupt, or a roll-over interrupt.
So, stopping the darned thing is not simple.· Getting back to before you started is not documented.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Post Edited (Kramer) : 2/22/2008 3:58:20 PM GMT
Thanks for your comments! Hopefully this thread will help unravel what is going on and how to handle this issue.
From everything i've read on the datasheet and G
I suspect that you might have to write to as many as four Timer registers to fully and cleanly stop either Timer.
1. Turning off the timers so that they won't interfer with ISRs may merely require you turn off all 3 of the interrupts.
2. If that doesn't work, I suspect that switching PWM or Software Timer may stop the ISR problem. [noparse][[/noparse]There are four choices of functional state, there isn't a 5th option of OFF]
3. If the PWM is continuing to output to its PIN, you may need to Zero the count in the two registers that generate the PWM high and low [noparse][[/noparse]That is both registers should have %0000_0000}.
4. If PWM does not work for shut down, Software Timer with the same registeres changed to Zero may get you complete quiet.· [noparse][[/noparse]Bean seems to indicate above that Software Timer is the right place for shut down.]
5. The Flags seem to be trival unless you are actually using one of the four features that they support. So reseting them may be unnecessary unless you intend to restart the timer and want to minimize initialization time. Of course, there is no harm in doing so and it may just be good housekeeping.
So, I suspect there is a 'quick and dirty' shut down, a longer 'ready for the next operation' shut down, and a longer 'clean' shut down that is useful for people that are using the SX48 in a tutorial fashion. It seems to me that SX/B would prefer to offer only the last choice as a particular command.
Thus, shutting down only the enables, might be considered a 'quick and dirty' shut down. It is still a valid choice for something like an emergency stop feature for a PWM driven motor.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Peter V's prize winning RTO.scr might be adapted to benefit from this extended range.· See the First SX Contest winners for the code.
Also, while the ISR can have 3 sources of interrupts involved; the 3 source of interrupts in the multi-use timers make it possilbe to have 3 interrupts occuring from each in a variety of ways.· So, you ISR could be hit by as many as 7 different kinds of interrupt events.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Post Edited (Kramer) : 2/23/2008 1:11:38 PM GMT
Although just shutting down the PWM in a way that doesn't generate Interrupts will get around the issue I have in my particular program I think it is an excellent idea to review and document all the functionality of the 16-bit timers and their behavior. There seems to be a ton written on the RTCC and Watchdog timers but examples for the 16-bit timers are few and far between. My guess is that a lot of people haven't been taking full advantage of them in their programs yet. I have some results for the five points you brought up.
1. I think that the assembly code I had posted will turn off all Interrupts for TIMER1 but still need to confirm it. I was thinking about making a new program from scratch to help test this. It could have an ISR defined (without a specified interval) so that it would have a handler that wound normally not be called. It could just light up an LED if it is ever executed. First program wouldn't touch the timers at all. With that the LED should never light. Next change, enable the PWM mode. LED should still not light. Next version, Set timer to TIMER Mode. LED will probably light. next, keep tweaking the option registers at beginning of program to see is any can prevent the LED from lighting (which can only happen if the ISR is activated). The same simple ISR could be used to see if anything else generates an Interrupt too.
2. Switching to Software timer mode is what brought this issue to light. Normal PWM mode doesn't generate any Interrupts. The software timer mode can and appears that by default, does generate Interrupts. That default behavior might be fine for some programs but is causing problems with mine. I just want to make sure that those Interrupts are masked (which should be possible) so that it won't affect my code.
3. When in software timer mode is stops sending out pulses on the output pin. Right after the TIMER1 TIMER instruction I follow it with a LOW Spd1 to ensure the motor is off. That stops the motor but I get unwanted Interrupts. My work around was to send 1 pulse out of 1111 in PWM mode which keeps the Timer in PWM mode (no Interrups) and is small enough not to move the motor. I really dislike that solution as I still am sending a pulse to the motor. It's just not the right way to do it and I am not happy with that method.
4. See comments for #2
5. From looking at the assembly generated by SX/B it appears that the BASIC commands already set the B config register for the appropriate Timer. None of the commands seem to reference the A config register which (from the datasheet) is supposed to be able to mask Interrupts for the timer. Setting these directly may work but more tests need to be done.
I just hope that once all this is figured out that in the end we'll have some best practices for working with the 16-bit timers on the SX48 and some solid exampled for their use. Once they are done perhaps they can get rolled into the next release of SX/B (and help file).
Best Regards,
Robert
Yes, I started reading about the timers years ago and very little has been done with them. I have wanted to at least have two PWM so that my Toddler Robot could be driven by the SX-48 while running other routines. I suspect the lack was due to two factors. First there is the fact that you have to learn to insert a Macro in order to use the larger SX-48's double memory capacity before you even get to the extra timers. Second, is that they were only in surface mount packages and until Parallex wisely produced functional boards, most hobbyist and students just avoided them. There was plenty of things to do and learn with either the BasicStamp or the SX28.
I am wondering if the software examples for using the SXes to connect to a LAN exploit them in order to get speed. I presume that there was a basis for including them.
SX/B may be creating the impression that each of the four different timer uses has defaults when there may not be any. After all, one usually initializes before activating a function. SX/B may be doing the initialization for you.
Extra interrupts when turning off could be generated by the rollover interrupt if you are changing values in the registers before you shut down the interrrupts.
Can you try using a LOW Spd0 ? And is there a High Spd # that should be 0 too?
The only mask for interrupts are for the Port B which can enable 1 or more pins to accept interrupts.
You might try to dig up one of Mike Predko's books for more general hardware timer info. He published one that compared 8051s, PICs, AVRs, and it might have good examples of 16 bit timer uses. Sadly, it appears he was force to stop publishing it as the different manufacturers didn't really enjoy being so easily compared. PIC code is relatively similar to the SX code on the smaller, more generalized devices. The last time I tried to get it, it was over $100USD out-of-print. Your public library may have a copy.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
I think you are spot on in regards to that people are probably using the SX28 since it is available in a DIP format. That is one of the reasons I made up the module at the link below:
http://forums.parallax.com/forums/default.aspx?f=7&p=1&m=166781
It is a nice way to put the SX48 on a breadboard. Actually since the blank PCB's for this are about $7.95 and Parallax is out of stock on the SX48 protoboards this is a great way for people to mount and use the SX48 in the meantime. I still have a few full kits on hand as well. To keep this thread on topic anyone interested in these should just send me a PM...
I've looked at the assembly code that the SX/B compiler generates and it does setup some of the timer registers. It just doesn't seem to touch the ones related to Interrupt. I don't see that as a problem but there just needs to be a bit more documentation on their default behavior so there are no surprises.
Sorry about the name Spd1. That is just the name I assigned to the pin which TIMER1 is using to send out PWM. After setting this to TIMER mode I used to LOW Spd1 to force that pin to a low state.
As long as I put the code to mask off the TIMER Interrupts at the very beginning of my program (before touching the timers) I wouldn't expect any interrupts from them.
I think the operation of the timers themselves is pretty straightforward and that the issues here are specific to the SX48.
I'm sure we'll get to the bottom of it!
Robert
ASM
MOV W,#$07 ' Select read register T1CNTA
MOV M,W
MOV !RB,W ' (Use !RC for T2)
AND W,#$DA ' Clear bits 5, 2, and 0
MOV wrkVar,W ' move to temp variable
MOV W,#$17 ' Select write register T1CNTA
MOV M,W
MOV W,wrkVar
MOV !RB,W ' Write out the new value to config register (Use !RC for T2)
ENDASM
I need to do some more testing but it appears that this issue may be fixed.
One thing that was causing me problems during testing is that I would download new code and just RESET the board. Now, I always power cycle the board after sending down new code.
Now, I can start working on the rest of the program.
It would be cool to hear if anyone else can confirm these results.
Robert
The question of uses remains. Ther are four features, but each can adapt to several uses.
1. PWM can also do PPM. And I suspect two PWMs can do FSK
2. Software timer can provide an alternative RTCC, but it could provide a more complex, feature rich RTCC.
3. Capture/compare is really capture or compare
4. External can be directing to the RTCC interrupt and maybe to an output pin to put the MCLR pin low for another watchdog timer format.· Also External can be clocked by a second RC Timer, TTL oscillator, or Crystal driven externally.· In some cases, this might be useful for reference.
You might investigate Microchip's Application Notes for PICs as they published tons of stuff that can be mined for ideas to present as tutorials. Unlike Senix/Ubicom, they had lots of extra profit to spend on promo.
PLEASE NOTE.
I've been reading the SX-Key manual Version 2.0 and both Chapter 10 and Chapter 15 really do a less that adequate job of presenting these timers and their interrupt options.· Some of the information appears to an afterthought by an author that didn't understand them, and the counts for the number of interrupt choices internal and external seem wrong for the SX-48/52.
A lot of·users could benefit from a revision of the material.· Adding a Heading as Sec.15.5.5 entitiled Multi-use Timer Interrupts and moving the last paragraph of 15.5.3 to it as an intro would be a good start.· Sec. 10.4 All About Interrupts· really need a careful revision of the first paragraph to get the total interrupt source count right, the count and source of internal interrupts, and the count and source of external interrupts.· Good definition of what is considered an internal versus external interrupt would be quite·useful.
And, I believe that Guenther might have stayed with the SX-28 and merely copied what he felt was reliable Multi-use Timer info because he had more than enough good useful information for a book without·looking into what the SX-48/52·Multi-use timer features might offer.· He certain has published the most reliable reference, but this area·remains to be explored and published.
I suspect that the problem got passed along from Senix to Ubiicom to Parallax as everyone had other areas of the technology to develop and document.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
PLEASE CONSIDER the following:
Do you want a quickly operational black box solution or the knowledge included therein?······
Post Edited (Kramer) : 2/25/2008 3:35:22 PM GMT