Setting up PWM on a CY8C9560A

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Anonymous
Not applicable

 Hi, I'm helping to develop a device driver for a module that uses the CY8C9560A.  I'm having a problem getting PWM to send a signal.  The data sheet isn't very clear to me regarding the exact steps or sequence that needs to be taken to activate PWM for a pin.  The code I'm using is below but all I am getting is a solid 1.63V output from PWM8.  Can you help point out what I might be missing?  

   

From some of the other posts, I gathered that it might be necessary to reset the device before the PWM settings take effect.  Is this the case?  Is there a way to soft reset this device?  If this is necessary then is there a way to reset just the one pin so that others are not interrupted?  Thanks!

   

   
io60p16.SetPwm(7, 0, 0x5e, 0x2f);  // Port 7, pin 0 = PWM8
   

   

   
        public void SetPwm(byte port, byte pin, byte period, byte pulseWidth)        {            WriteRegister(0x18, port);          // Select port              var b = ReadRegister(0x1a);                      b &= (byte)(~(1 << pin));                        WriteRegister(0x1a, b);             // select PWM for port output              b = ReadRegister(0x1C);                          b &= (byte)(~(1 << pin));                        WriteRegister(0x1C, b);             // Set pin for output.              WriteRegister(0x28, (byte)(0x08 + pin));          // Select the PWM pin to configure.            WriteRegister(0x29, 0x00);          // Config PWM (select 32kHz clock source)            WriteRegister(0x2a, period);        // set the period (0-256)            WriteRegister(0x2b, pulseWidth);    // set the pulse width (0-(period-1))        } 
0 Likes
1 Solution
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Can it be that you write a 0 (zero) into the PWM-Register 0x1a instead of a 1 (one) as the datasheet tells?

   

 

   

Bob

View solution in original post

0 Likes
5 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Can it be that you write a 0 (zero) into the PWM-Register 0x1a instead of a 1 (one) as the datasheet tells?

   

 

   

Bob

0 Likes
Anonymous
Not applicable

 I think you're right, Bob.  Copy & paste error.  I'll give that a try tonight.  Thanks!

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Ian,

   

if it's that easy to help, you're always welcome!

   

 

   

Bob

0 Likes
Anonymous
Not applicable

If you're intending to write a 1 bit, then

   
 b &= (byte)(~(1 << pin));  
   

is certainly wrong. It writes a 0 on the selected pin. Try this code if you want to set the selected bit to 1:

   
b |= (byte)(1 << pin);  
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

This is another case where I would strongly suggest to use macros for such simple (but easy targets for typos) jobs.

   

#define BitMask(BitNumber) (0x01 << BitNumber)

   

#define SetBit(Variable,BitNumber) (Variable |= BitMask(BitNumber)) 

   

#define ClearBit(Variable,BitNumber) (Variable &= ~BitMask(BitNumber))

   

#define GetBit(Variable,BitNumber) (Variable & BitMask(BitNumber))

   

 

   

The original line

   

b &= (byte)(~(1 << pin));  

   

would then read

   

ClearBit(b,pin);

   

and the line

   

b |= (byte)(1 << pin);

   

would become

   

SetBit(b,pin);

   

which (in my opinion) is far more readable than the original and thus (even if you cut & paste) would have avoided an error like this inherently.

   

 

   

Bob

   

(hope I made no Typos)

0 Likes