PWM period and pulse width updating

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

cross mob
Anonymous
Not applicable

Hello everyone,

   

 

   

Straight to the point,

   

I'm new for programming in C

   

I'm trying to provide a PWM signal which constantly changing it's periode or pulse width until a certain value, a delay time is used in each iteraion to see the diffeences.

   

I'm using a simple for loop, but I still can't see the pwm signal changing in the oscilloscope.

   

This is the code

   

 

   

--------------------------------------------

   

PWM_start;

   
                for     (i=     0     ; i<     255     ; i++);   
   
     {   
   
         
   
      PWM_WritePulseWidth(i);   
   
        
   
      Delay10msTimes(     100     );   
   
         
   
        
   
           }   
   
     ----------------------------------------   
   
     Can you please informe with what's wrong.   
   
        
   
     Thank you    
0 Likes
7 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Welcome in the fascinating world of PSoCs!

   

As usual: Can you post your complete project, so that we all can have a look at all of your settings? To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

You have probably forgotten to program an infinite loop so that your PSoC gets re-initialized everytime your loop ends which is probably not what you want.

   

 

   

Bob
 

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Some observations -

   

 

   

1) When you typed PWM_start; that should be typed as PWM_Start();

   

 

   

2) Your pulse width value should not exceed the period value you have set for the PWM,

   

otherwise the PWM output will simply stay fixed at a logic high. Since I see no code for

   

the period value check its global properties by right clcking component.

   

 

   

3) You are updating at a rate of 1 sec roughly, so clock to PWM / Period value,

   

should be << 1 hz, otherwise you would see erratic behaviour on scope.

   

 

   

4) Since you are writing a uint8 (or so it appears) to PWM, I assume its an 8 bit

   

PWM, otherwise in a 16 bit PWM, just changing the lower 8 bits of the PW, if the

   

period value uses most of the 16 bits, would lead to very little observable pulse

   

width change on scope.

   

 

   

Regards, Dana.

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi guys,

   

Sorry fro being late in coming back to you.

   

I tried to change the clock but i still can't find where's the mistake.

   

I attached the project.

   

I palpitate it

   

Thank you

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable
0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

Two errors I can see from the .bmp

   

1st: the infinitive loop is missing.

   

2nd: Your for-loop does not execute, there is a semi-colon which finishes the loop. after executing the rest of the program once, main() exits which will reset your PSoC

   

 

   

Bob

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Hi, you declared i as an int, but are trying to write it to

   

an 8 bit PWM, I would change it to unsigned char. Or

   

cast it to an 8 bit value in the function argument for the

   

PWM write.

   

 

   

Second you called a delay function without any code base for it.

   

Add the #include "Delay.h" as shown, then use URL to get the

   

delay code, .h and .asm file,  and add into project lib directory.

   

 

   

    

   

          http://www.cypress.com/?id=4&rID=47960

   

 

   

Regards, Dana.

   

 

   

 

   

 

   

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "Delay.h"

unsigned char i;

void main(void)
{
    // M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts
    // Insert your main routine code here.
    PWM_Start();
    for( i = 0; i < 50; i++ ) {

         PWM_WritePeriod(i);
         Delay10msTimes(100);
    }
 }

0 Likes
ETRO_SSN583
Level 9
Level 9
250 likes received 100 sign-ins 5 likes given

Forgot Bobs infinite loop to repeat the PWM loop,

   

\so code should look like -

   

 

   

#include <m8c.h>        // part specific constants and macros
#include "PSoCAPI.h"    // PSoC API definitions for all User Modules
#include "Delay.h"

unsigned char i;

void main(void)
{
    // M8C_EnableGInt ; // Uncomment this line to enable Global Interrupts
    // Insert your main routine code here.
    PWM_Start();
   
    for( ; ; ) {
   
        for( i = 0; i < 50; i++ ) {

             PWM_WritePeriod(i);
             Delay10msTimes(100);
        }
    }
 }

   

 

   

One last, you set the PWM up with a period of 100, compare value of 10, so writing period values < 11

   

will give you a fixed output on compare pin. So either write period > 10, which will give you a changing

   

frequency and duty cycle output. Or write the compare register if what you are trying to do is change

   

the duty cycle and not the frequency of the output.

   

 

   

Also you set the CPU clock as /8 in the global properties. That will save power but also CPU will

   

operate slower, is that what you wanted ? Additionally in global properties you set the Op-Amp Bias and

   

A-Buff_Power low, that will inhibit the CM range of the PGA and its output as seen at an analog pin.

   

Is that also what you wanted ?

   

 

   

Regards, Dana.

0 Likes