You are on page 1of 4

C:\Users\jon33\Documents\Jon-ECE341-DCM.

Sunday, May 1, 2016 12:36 PM

#include <plib.h>
#include <stdio.h>
#include <string.h>
#include
#include
#include
#include
#include

"CerebotMX7cK.h"
"Project10.h"
"btnlib.h"
"lcdlib.h"
"dcmlib.h"

int main()
{
systemInit();

lcdPutString("PWM = 0% (init)");
// main() loop
while(1)
{
updateScreenSpd();
msDelay(100);
}

return 1;

void systemInit()
{
// Config Bit Variable
int t2ConfigBits = ( T2_ON | T2_SOURCE_INT | T2_PS_1_1 );
int t2IntConfigBits = ( T2_INT_ON | T2_INT_PRIOR_2 | T2_INT_SUB_PRIOR_1 );
int t3ConfigBits = ( T3_ON | T3_SOURCE_INT | T3_PS_1_256 );
int t3IntConfigBits = ( T3_INT_ON | T3_INT_PRIOR_2 | T3_INT_SUB_PRIOR_2 );

int ic5ConfigBits = ( IC_ON | IC_CAP_16BIT | IC_IDLE_STOP | IC_FEDGE_FALL |


IC_TIMER3_SRC | IC_INT_1CAPTURE | IC_EVERY_FALL_EDGE );
int ic5IntConfigBits = ( IC_INT_ON | IC_INT_PRIOR_3 | IC_INT_SUB_PRIOR_0 );
// Basic Board Setup
Cerebot_mx7cK_setup();

// Timer 2 Open w/ Interrupts


OpenTimer2( t2ConfigBits , PWM_CYCLE_COUNT - 1 );
ConfigIntTimer2( t2IntConfigBits );
// Timer 3 Open w/ Interrupts
OpenTimer3( t3ConfigBits , 0xFFFF );
ConfigIntTimer3( t3IntConfigBits );

// IC 5 Open w/ Interrupts
OpenCapture5( ic5ConfigBits );
ConfigIntCapture5( ic5IntConfigBits );

// Change Notice Open w/ BTN1|BTN2 and interrupts


mCNOpen(CN_ON,(CN8_ENABLE|CN9_ENABLE),0);
mCNSetIntPriority(1);
mCNSetIntSubPriority(0);
PORTReadBits(IOPORT_G,(BTN1|BTN2));
mCNClearIntFlag();
mCNIntEnable(1);

// Set SM LED Digital Pins for testing


PORTSetPinsDigitalOut(IOPORT_B,(LEDA|LEDB|LEDC|LEDD)); // SM LEDS A-D
-1-

C:\Users\jon33\Documents\Jon-ECE341-DCM.c

Sunday, May 1, 2016 12:36 PM

LATBCLR = (LEDA|LEDB|LEDC|LEDD);
// Initialize the LCD peripheral
lcdInit(CORE_MS_TICK_RATE);
// Initialize the BTN peripheral
btnInit(CORE_MS_TICK_RATE);

// Initialize the DCM peripheral


dcmInit( 0 , PWM_CYCLE_FREQ , FPB );
// Enable System Interrupts
INTEnableSystemMultiVectoredInt();
INTEnableInterrupts();
}

return;

void __ISR(_TIMER_2_VECTOR, IPL2) T2IntHandler(void)


{
// Toggle LEDA every ms
LATBINV = LEDA;
}

// Clear the Interrupt Flag


mT2ClearIntFlag();

void __ISR(_TIMER_3_VECTOR, IPL2) T3IntHandler(void)


{
// Toggle LEDA when T3 rolls over
LATBINV = LEDC;
}

// Clear the Interrupt Flag


mT3ClearIntFlag();

void __ISR(_INPUT_CAPTURE_5_VECTOR, IPL3) IC5IntHandler(void)


{
// Declare Variables
unsigned int buffer[8];
// IC int buffer

unsigned short int tNew = 0;


// New timestamp
static unsigned short int tOld = 0; // Old timestamp
unsigned short int tDif = 0;
// Difference between stamps

static unsigned short int accumLog[IC_LOG_SIZE] = {0}; // tDif log


static unsigned int accumSum = 0;
// Running sum for average tDif
static unsigned int accumIdx = 0;
// Index for running sum
float accumAvg = 0;
// Running average for tDif
// Toggle LEDD
LATBINV = LEDD;

// Read IC5 FIFO as tNew, calc tDif


ReadCapture5(buffer);
tNew = buffer[0];
tDif = tNew-tOld;

// Manipulate the sum for the average


accumSum += tDif;
accumSum -= accumLog[(accumIdx&(IC_LOG_SIZE-1))];
accumLog[(accumIdx&(IC_LOG_SIZE-1))] = tDif;
accumIdx++;
// Calculate running average tDif

-2-

C:\Users\jon33\Documents\Jon-ECE341-DCM.c

Sunday, May 1, 2016 12:36 PM

accumAvg = (float)accumSum/IC_LOG_SIZE;

// Calculate motor speed from tDif average


if(accumAvg != 0 )
spd = (float)1/(accumAvg*256/FPB);
else
spd = 0;
// Set the current timestamp as the old
tOld = tNew;
}

// Clear the int flag


mIC5ClearIntFlag();

void __ISR(_CHANGE_NOTICE_VECTOR, IPL1) CNIntHandler(void)


{
// Set LEDB for Timing
LATBSET = LEDB;
// Declare Variables
int pwmDuty = 0;
char pwmDutyStr[8];

// Debounce the Button with simple delay


btnmsDelay(BTN_DEBOUNCE_MS);
// Read the Buttons
btnState = btnRead();

// Decode the buttons into a Duty Cycle Value


pwmDuty = dcmDecodeBtns(btnState);
// Execute PWM Control of motor
dcmControl(pwmDuty);

// Create a string from the duty cycle value


sprintf(pwmDutyStr,"%d",pwmDuty);
// Put the Speed to the LCD
lcdClearLine(1);
lcdPutString("PWM = ");
lcdPutString(pwmDutyStr);
lcdPutChar('%');

// Clear LEDB and Interrupt Flag


LATBCLR = LEDB;
mCNClearIntFlag();

void msDelay( int ms )


{
unsigned int tStart;

while(ms--)
{
tStart = ReadCoreTimer();

while((ReadCoreTimer() - tStart < CORE_MS_TICK_RATE ))


/* do nothing! */;

void updateScreenSpd()
{
// Declare a string buffer
-3-

C:\Users\jon33\Documents\Jon-ECE341-DCM.c

Sunday, May 1, 2016 12:36 PM

char spdStr[16];

// Print Global spd to string


sprintf(spdStr,"%.2f",spd);

// Disable CN interrupt to presserve


mCNIntEnable(0);
// Set SPD on line 2
lcdClearLine(2);
lcdPutString("SPD = ");
lcdPutString(spdStr);
lcdPutString(" RPS");
}

// Re-Enable CN Interrupt
mCNIntEnable(1);

-4-

You might also like