Forum Index
HomeZBasic Home   Forum RulesForum Rules   Forum FAQForum FAQ   MemberlistMemberlist   UsergroupsUsergroups   RSS FeedRSS Feed
Site SearchSite Search   LinksLinks   DownloadDownload   Digests and SubscriptionsDigests and Subscriptions
ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in   RegisterRegister
Odd limit of openPWM

 
Post new topic   Reply to topic    Forum Index -> ZBasic Language
Author Message
pjc30943



Joined: 02 Dec 2005
Posts: 220

Posted: 29 August 2007, 0:46 AM    Post subject: Odd limit of openPWM Reply with quote

What's the fastest pwm any of you users have obtained?

Is there anything seperate that might influence maximum PWM frequencies? Whenever I initialize a channel above about 200Hz, the outputs vanish. Lower than (or equal to) 200Hz gives a proper output.

I'm on a 1281e, and tried this on both channels (TMR1 and TMR3), and using the two operating modes (Fast/Correct), with no difference.

As is usually the case, it's probably an obvious user error!Smile
Back to top
mikep



Joined: 24 Sep 2005
Posts: 771
Location: Austin, TX

Posted: 29 August 2007, 8:53 AM    Post subject: Reply with quote

I have been able to recreate this problem on both ZX1281e and ZX128e but not a ZX24.

It is no coincidence that the frequency where the PWM stop working is 225Hz for Fast PWM and 122Hz for Normal PWM. Fast PWM transitions twice as fast as Normal PWM. A quick multiply by 65536 gives the magic number of the ZBasic clock frequency (14.7456 MHz).

Here is my test code:
Code:
Sub Main()
   Call OpenPWM(1, 225.0, zxFastPWM)
   Call PWM(1, 50)
   Debug.Print CStrHex(Register.TCCR1A)
   Debug.Print CStrHex(Register.TCCR1B)
   Debug.Print CStrHex(Register.ICR1)
   Debug.Print CStrHex(Register.OCR1A)   
   Do
   Loop
End Sub

I dumped off some of the internal AVR registers to try to determine what was happening. If you run this testcase with Fast PWM and a 50% duty cycle, you can see that the value of OCR1A is half of ICR1 (as it should be)

The clock divider seems to be set correctly for higher frequencies but I noticed that OCR1A was set to 0. This is effectively a 0% duty cycle and may might point to a compiler problem.

I tried setting OCR1A myself to half of ICR1 but that didn't work. My guess is that the TOV1 interrupt service routine is resetting the value from a private copy. This might explain how successive calls to PWM work and allow you to change frequency or duty cycle on the fly.

There are a few more things I could play with but I think at this point Don needs to take a look.
Back to top
pjc30943



Joined: 02 Dec 2005
Posts: 220

Posted: 29 August 2007, 18:54 PM    Post subject: Reply with quote

Thanks Mike. You are right.
Don sent a VM version last night that appears to have solved the issue [thank you Don].
Back to top
dkinzer
Site Admin


Joined: 03 Sep 2005
Posts: 2593
Location: Portland, OR

Posted: 29 August 2007, 19:48 PM    Post subject: Reply with quote

There is an error in the PWM code that causes the timer prescaler to be configured incorrectly for frequencies above 225Hz. This doesn't occur in the mega32-based ZX devices due to a difference in implementation.

As a temporary workaround, you can avoid this problem for the higher PWM frequencies as shown in the code example below. Note, however, that you only want to use the workaround if the frequency is actually above 225Hz. The prescaler is correctly set for lower frequencies and the workaround code may cause the prescaler to be incorrect.

The prescaler bits are the three least significant bits of the TCCRnB register. If those bits are all zero, the timer is off. An alternate workaround strategy might be to check those bits and only OR in the &H01 value if they are all zero.

Code:
Sub Main()
  Call OpenPWM(1, 226.0, 0)

  ' temporary workaround to correctly set the
  ' prescaler for frequencies above 225Hz
  Register.TCCR1B = Register.TCCR1B Or &H01

  Call PWM(1, 50)
  Debug.Print "TCCR1B=0x"; CStrHex(Register.TCCR1B)
End Sub
Back to top
Display posts from previous:   
Post new topic   Reply to topic    Forum Index -> ZBasic Language Time synchro. with the server - Timezone/DST with your computer
Page 1 of 1

 


All content Copyright © 2005-2012 Elba Corp. All Rights Reserved.
Opinions expressed in posts are those of the author and not necessarily those of Elba Corp.
Powered by phpBB © 2001, 2005 phpBB Group