Professional Documents
Culture Documents
Module
SRPWM.c
Description
This service keeps track of each shift register output and its PWM signal
Notes
History
When
Who
What/Why
-------------- ---------11/12/16 02:03 rdb
created it for ME218A project
****************************************************************************/
#include "SRPWM.h"
/*---------------------------- Module Variables ---------------------------*/
// with the introduction of Gen2, we need a module level Priority variable
static uint8_t MyPriority;
// Array holding information for the shift register "strings" we are using for
PWMs
static uint8_t SRStrings[] = { SRPWM_SR_STRING_0, SRPWM_SR_STRING_1,
SRPWM_SR_STRING_2 }; // up to 2 strings in current implementation
// Array holding the current pulse width value of every output pin on our shift
registers
static uint8_t CurrPulseWidth[] = { 0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, 0 }; // Array to keep track of the pulse
width in each LED
// Delay time constraining our PWM frequency
static uint16_t DelayTime = 200;
/****************************************************************************
Function
Init_SRPWMService
Parameters
uint8_t Priotity the priority of this service
Returns
Description
Author
R. D. Born 11/29/16
****************************************************************************/
bool Init_SRPWMService(uint8_t Priority)
{
printf("Initializing SRPWM\n\r");
// Initialize shift register hardware
SRPWM_HWInit();
// Set priority of this service
MyPriority = Priority;
// Initialize short timer and start it off counting
ES_ShortTimerInit(MyPriority, SHORT_TIMER_UNUSED );
ES_ShortTimerStart(TIMER_A, DelayTime);
// Return success
return true;
Description
Handles all the events posted to the SRPWM queue
Author
R. D. Born 11/29/16
****************************************************************************/
ES_Event Run_SRPWMService(ES_Event ThisEvent)
{
ES_Event ReturnEvent;
ReturnEvent.EventType = ES_NO_EVENT; // assume no errors
static int PW_ctr = 0;
uint8_t val = 0;
uint8_t PW_idx = 0;
uint8_t NewPW = 0;
switch(ThisEvent.EventType)
{
case ES_SHORT_TIMEOUT:
// DO FOR EACH shift register string
for(uint8_t SRStr = 0; SRStr < NUM_SR_STRINGS_PWM; SRStr++)
{
// DO FOR EACH shift register in this string
for(uint8_t ShiftReg = NUM_SR_PER_STRING; ShiftReg > 0 ;
ShiftReg--)
{
// DO FOR EACH output in this shift register
for(uint8_t output = NUM_OUTPUTS_PER_SR; output >
0 ; output--)
{
// IF we've been high for the whole pulse
width
//
Pull output low
// ELSE
//
Pull output high
CurrSRString
- 1)
*
*
CurrOutput
+
//
idx
NUM_OUTPUTS_PER_SR
output
= CurrSR
CurrOutput
{
}
/****************************************************************************
Function
SR_SetPWM
Parameters
uint8_t PulseWidth new pulse width to set OutputPin to
uint8_t OutputPin pin whose pulse width we are changing
Returns
Description
Author
R. D. Born 11/29/16
****************************************************************************/
void SR_SetPWM(uint8_t PulseWidth, uint8_t OutputPin)
{
// Set the current pulse width of OutputPin to PulseWidth
CurrPulseWidth[OutputPin] = PulseWidth;
}
/****************************************************************************
Function
SRPWM_HWInit
Parameters
Returns
Description
Initializes hardware associated with SRPWM module
Author
R. D. Born 11/29/16
****************************************************************************/
void SRPWM_HWInit(void)
{
SR_Init();
}
/****************************************************************************
Function
GetPW
Parameters
uint8_t PW_idx the index of the pin whose pulse width we want
Returns
Description
Polls the requested pin for its pulse width (i.e. duty cycle)
Author
R. D. Born 11/29/16
****************************************************************************/
uint8_t GetPW(uint8_t PW_idx)
{
// IF PW_idx is not valid
if(PW_idx > (NUM_SR_STRINGS_PWM*NUM_SR_PER_STRING*NUM_OUTPUTS_PER_SR))
{
// Do nothing
printf("WARNING: requested PWM is out of range, cannot retrieve a
value, returning 0\n\r");
return 0;
}
// Return the current pulse width at PW_idx
return CurrPulseWidth[PW_idx];
}
/****************************************************************************
Function
Encode_Idx_PW
Parameters
parameter
Returns
uint16_t encoded event parameter
Description
that
both the pulse width and the output pin's index can be included as
information with a single event.
Author
R. D. Born 11/29/16
****************************************************************************/
uint16_t Encode_Idx_PW(uint8_t PW, uint8_t PW_idx)
{
// Shift PW_idx left 8 bits
return (PW | (PW_idx << 8));
}
/****************************************************************************
Function
ExtractIdx
Parameters
uint16_t Param event parameter from which to extract the output pin
index
Returns
Description
Decodes an event parameter to retrieve the index of the output pin
whose
width
Returns
uint8_t the pulse width encoded in Param
Description