b. Bridging PWM

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 20 - CleO Bridging >

b. Bridging PWM

This tutorial shows how to use bridging commands to access PWM interface.

 

Command Prototype

 

int16_t DeviceOpen(uint8_t Slot,  uint8_t Interface,  int16_t bytestowrite,  uint8_t* Buffer)

 

Parameters

Description

Slot

Currently only one Slot is supported.Slot value should be equal to 0.

Interface

Value of this parameter should be BRIDGE_TYPE_PWM for PWM interface access.

bytestowrite

Value of this parameter is the total number of bytes to be written through this function which in this case is sizeof pwm_options structure.

Buffer

Value of this parameter should be a pointer to pwm_options structure type in Bridge.h.

 

typedef struct PACK {

   union PACK {

       struct PACK {

           uint32_t Frequency; /* in Hz */

           uint8_t Shots; // shot = 0, means continuous mode

           uint8_t Channels; // bits[0:1] for channel 1 and 2

           uint8_t DutyCycle1;

           uint8_t DutyCycle2;

           uint8_t InitState1; // 0= low, 1= high

           uint8_t InitState2; // 0= low, 1= high

           };

       uint8_t b[10];

   };

}pwm_options;

 

Structure member description:

 

Frequency: Value of this parameter should be the desired clock frequency which is common for all channels. Internally the system computes prescaler and comparator values to best suit the PWM target requested frequency. Valid range: 6Hz to 50MHz

 

Shots: The value of 0 in this parameter will enable PWM in continuous output mode and non-zero number in multi- shot mode. A non-zero number indicates the number of shots that will be output.

 

Channels: This value should indicate the PWM channel  number. It can take values as below which is defined in Bridge.h. CLICK1_PWM_CHANNEL, CLICK2_PWM_CHANNEL or both with bitwise ORed.

 

DutyCycle1: This value should indicate the duty cycle for CLICK1_PWM_CHANNEL. Valid range: 0<= duty cycle <= 100

 

DutyCycle2: This value should indicate the duty cycle for CLICK2_PWM_CHANNEL. Valid range: 0<= duty cycle <= 100

 

InitState1: This value indicates the initial state value of CLICK1_PWM_CHANNEL. 0 is low and 1 is high.

 

InitState2: This value indicates the initial state value of CLICK2_PWM_CHANNEL.0 is low and 1 is high.

 

 

Return Value

Description

int16_t

On success, the function returns a valid handle that can be used in subsequent Device*() calls.

On failure, the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceClose( uint8_t DevHandle)

 

Parameters

Description

DevHandle

A PWM handle that is obtained using DeviceOpen() call.

 

Return Value

Description

int16_t

On success, the function returns 1 and on failure the function returns a negative value of error code with blue screen.

 

 

Code

 

 

int16_t HandlePWM = -1;

 

void setup()

{    

 CleO.begin();

 Serial.begin(115200);

 Bridge::pwm_options PwmOpts;

/* set the system clock frequency which is same for all channels */

 PwmOpts.Frequency = 1000;

/* Value 0 is for continuous generation

  * 1 for one-shot, 2 to 255 are for so many shots

  */

 PwmOpts.Shots = 0;

/* 2 PWM channels are available */

 PwmOpts.Channels = CLICK2_PWM_CHANNEL | CLICK1_PWM_CHANNEL;

/* Set duty cycle for PWM1 (CLICK1_PWM_CHANNEL) */

 PwmOpts.DutyCycle1 = 25;

 /* Set duty cycle for PWM2 (CLICK2_PWM_CHANNEL) */

 PwmOpts.DutyCycle2 = 50;

/* InitState1 and 2 for PWM1 and PWM2 channels respectively , 0= low, 1= high */

 PwmOpts.InitState1 = 1;

 PwmOpts.InitState2 = 0;

 HandlePWM = CleO.DeviceOpen(0, BRIDGE_TYPE_PWM, sizeof(PwmOpts), (uint8_t*)&PwmOpts);

 if(HandlePWM < 0)

 {

 /* Failed to open a handle */

   Serial.print("DeviceOpen failed");

 }  

   

 delay(100000);

 CleO.DeviceClose(HandlePWM);

}

 

void loop()

{

}

 

 

Description

 

This tutorial opens a handle to both PWM channels in one DeviceOpen() call. CLICK1_PWM_CHANNEL is set to 25% duty cycle with initial state of 1. Whereas, the CLICK2_PWM_CHANNEL is set to 50% duty cycle and initial state of 0. By verifying the PWM1 and PWM2 channels using oscilloscope, the generated signal can be observed.

 

 

Output

 

Upon verifying the PWM1 and PWM2 channels using oscilloscope to observe the generated signal, if the DeviceOpen() fails, an error message will be displayed on the serial monitor window.