SX48 Questions (PWM with internal timer)
Hello,
·· I am new to the SX line of chips but very interested in using them in a couple of new projects.
··
· The SX48 proto board running at 50mhz is what I am looking at. I want to use this for PWM control of a solenoid at about 17-18 khz, the question is can I use the onboard timer for PWM and still be able to process some sensors and other input signals?
Also would the SX/B compiler able to use the internal timer or do I have to start learning assembly?
Thanks for any help..
Post Edited By Moderator (Bean (Hitt Consulting)) : 5/23/2007 11:47:50 AM GMT
·· I am new to the SX line of chips but very interested in using them in a couple of new projects.
··
· The SX48 proto board running at 50mhz is what I am looking at. I want to use this for PWM control of a solenoid at about 17-18 khz, the question is can I use the onboard timer for PWM and still be able to process some sensors and other input signals?
Also would the SX/B compiler able to use the internal timer or do I have to start learning assembly?
Thanks for any help..
Post Edited By Moderator (Bean (Hitt Consulting)) : 5/23/2007 11:47:50 AM GMT
Comments
· You can use SX/B. Look up the TIMER command in the help file.
· Basically you will use "TIMER1 PWM, value, 2778" value can be from 0 to 2777.
· 2778 = 50MHz / 18KHz.
· Remember that timer1 uses pin RB.6 for output and timer2 uses pin RC.2 for output.
· Also remember to make the pin an output.
· Once you issue the TIMER1 PWM command you don't have to do anything else. The SX48 will continue to generate the PWM, you can read sensors all day long.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Thanks for the tip is that able to be done with SX/B or do I have to use assembly?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
TIMER1 PWM Spd, Duty
The help file should probably be updated to show the comma right after the PWM since none of the other commands like the prescaler seem to need it and one might expect that the PWM isn't supposed to have it either! If you leave that comma out it will happily compile your program without errors but you'll never get what you expect out of your program.
I just read this post and noticed that there should be a comma after PWM like:
TIMER1 PWM,Spd, Duty
Adding the comma just fixed a problem that I was chasing down on and off all day. I missed the actual example and would have caught it sooner if the compiler had flagged it as an error. I kept overlooking the same thing. Now it works great!
I hope this note saves someone else some time.
Robert
Thanks for the tip I will be learing the SX/B very soon ...
I'll make sure the next release catches the missing comma. Thanks for posting your experience.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Robert
What if I want to run lower frequencys like say 292hz would I use the timer like this?
TIMER1 PWM, value, 170941 where value = 0 to 170941 or do I have scale this value somehow?
For your values you would have to use a prescale of 1:4 and use 42735 for 170941.
TIMER1 PRESCALE, 2 ' 2 = 1:4 prescaler
TIMER1 PWM, value, 42735 ' Value = 0 to 42735
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Thats what I though but the Help file in SX/B was not very clear about using the prescaler.
Thanks again.
One more question though I have a need for three PWM outputs and their is only two Timers would a ISR be better for the third PWM and how would you setup for a 0-100% duty say with a 32hz pulse?
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
The pwmValue is set between 0-8192 right?
Thanks again so If I wanted to have say 10bits I could use and interrupt of 1024*32 =32768 then set the value 0-32768?
For 10 bits the value would be 0 to 1023 (you will have to use WORD variables, and modify the code·too).
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
What if I want to run a couple of PWM channels with different frequencys can I use mutiple ISRs? Also will I still be able to use the serin and serout commands?
The ISR runs every millisecond; LED #1 gets toggled every 100 ms, LED #2 gets toggled every 133 ms, and LED #3 gets toggled every 221 ms. The example is not really practical, but demonstrates that for each of your VPs you may need to keep track of separate dividers.
INTERRUPT 74752
INC pwmCnt32hz
If pwmCnt32hz = 8192 THEN
pwmCnt32hz = 0
ELSE pwmCnt32hz > pwmValue32hz THEN
32hzpwmPin = 0
ELSE
32hzpwmPin = 1
ENDIF
INC pwmCnt61hz
IF pwmCnt61hz = 15616 THEN
pwmCnt61hz = 0
ELSE pwCnt61hz > pwmValue61hz THEN
61hzpwmPin =0
ELSE
61hzpwmPin = 1
ENDIF
INC pwmCnt292hz
IF pwmCnt292hz = 74752 THEN
pwmCnt292hz = 0
ELSE pwCnt292hz > pwmValue292hz THEN
292hzpwmPin =0
ELSE
292hzpwmPin = 1
ENDIF
RETURNINT
· And you need to adjust your values, for higher Hz rates you need LOWER reset values.
32 Hz would be 2336 (not 8192), 61 hz would be 1225 (not 15616) and 292 Hz would be 256 (not 74752).
Just take 74752 / hz to get the reset value.
· And you won't be able to use the SERIN or SEROUT command with an interrupt.j
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
“The United States is a nation of laws -· poorly written and randomly enforced.” - Frank Zappa
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Thanks again as usual I see what you mean about the resets so I was not to far out in left field just went backwards with my timming.
Thanks for the tip about the variable not starting with a digit.
Am I correct about the interrupt running at the 74752 (256*292)?
Thanks Jonny for your help also you got me started in the right direction.
Post Edited (bennettdan) : 6/12/2007 11:13:32 PM GMT
You said I cant use serout and serin commands what about using shiftout and shiftin to read from a ADC chip?
Also do you know of any examples on how to setup a serial link to a PC and still using the ISR?
Thanks again
There are many examples of using interrupts for serial communications in the forums. I found many helpful answers in response to my post about Adding a Background UART to SX/B.
You can Cut-and-Paste UART code from several posted examples. Be sure to call the UART code in your ISR with the right timing for your selected baud rate.
- Sparks