Pseudo Code
Pseudo Code
bool counting
int timeCounter
char *out
void toggleRedLED(void)
-LEDstate -> ^(GPIO_PIN_1)
-Output LEDstate in PF1
CMD_T Table[]
begin
{ "ledr" , &toggleRedLED },
{ "dd" , &distDisplay },
{ "ddcm" , &distDisplayCM},
{ "sm" , &stopMotor },
{ "mf" , &moveForward },
{ "mb" , &moveBackward },
{ "tr" , &turnRight },
{ "tl" , &turnLeft },
{ "a" , &motionON },
{ "s" , &motionOFF },
end
void init_UART()
Enable peripheral GPIO for Port B
Enable peripheral UART1
Configure PB0 -> U1RX (receiver)
Configure PB1 -> U1TX (transmitter)
Configure PB0 and PB1
Configure PB0 and PB1 for use by the UART peripheral
void CommandInterpreter(void)
const int size -> Table size
int i -> 0
while is true
UART gets char input from the user and stores it in command[8]
for each i<size of the Table do
if the comparison between command and CmdName is true
then call the pointer function that corresponds to the i ID
break
end if
end for
if i is more than the size of the table (the command has not been found)
Display in UART "Invalid command! Try again\n"
end if
end while
end
void init_ADC(void)
distR -> 0
distF -> 0
Configures the trigger source and priority of the sample sequence for ADC0 (SS3)
Configure a step of the sample sequencer: ADC0 (SS3)
Enable sample sequence 3 for ADC0
Enable the interrupt for sample sequence 3 of module ADC0
void hwiDistF(void)
Clear sample sequence 3 interrupt source of ADC0
Post software interrupt swiDistT
end
void hwiDistR(void)
Clear sample sequence 3 interrupt source of ADC1
Post software interrupt swiDistT
end
void swiDistTrigger(void)
Get the captured data of ADC0 SS3, and store it in variable distF
Get the captured data of ADC1 SS3, and store it in variable distR
end
void clkDistTrigger(void)
Cause a processor trigger on SS3 of ADC0
Cause a processor trigger on SS3 of ADC1
end
uint16_t Dist_GetF(void)
return distF
end
uint16_t Dist_GetR(void)
return distR
end
void distDisplay(void)
print Right and Left distances in ADC (12-bit)
end
void distDisplayCM(void)
float distF_CM -> 4543.5 * distF^(-0.827)
float distR_CM -> 4543.5 * distR^(-0.827)
Create dynamic char array out
Convert the float right and left distances in CM into a string and store it out
Print out
end
void init_PWM(void)
Configure the rate of the clock provided to the PWM module
Enable peripheral PWM0
Configures pin PC4 for use by the PWM peripheral (used for left motor)
Configures pin PC5 for use by the PWM peripheral (used for right motor)
void stopMotor(void)
Write 0 into PC6
Write 0 into PC7
Disables the PWM output for PC4
Disables the PWM output for PC5
end
void moveForward(void)
Write 0 into PC6 into PC6 to set forward direction in left motor
Write 0 into PC7 into PC7 to set forward direction in right motor
Enable the PWM output for PC4 (left motor)
Enable the PWM output for PC5 (right motor)
end
void moveBackward(void)
Write GPIO_PIN_6 into PC6 to set reverse direction in left motor
Write GPIO_PIN_7 into PC7 to set reverse direction in right motor
Enable the PWM output for PC4
Enable the PWM output for PC5
Enable the PWM output for PC4 (left motor)
Enable the PWM output for PC5 (right motor)
end
void turnLeft(void)
Write GPIO_PIN_6 into PC6 to set reverse direction in left motor
Write 0 into PC7 to set forward direction in right motor
Enable the PWM output for PC4 (left motor)
Enable the PWM output for PC5 (right motor)
end
void turnRight(void)
Write 0 into PC6 to set forward direction in left motor
Write GPIO_PIN_7 into PC7 to set reverse direction in right motor
Enable the PWM output for PC4 (left motor)
Enable the PWM output for PC5 (right motor)
end
void checkFrontWall(void)
frontD = Dist_GetF();
void prepPID(void)
Clears timer interrupt for TIMER0
if GO is true
Post software interrupt swiPID (PID)
end if
else
Call stopMotor function
return
end else
void motionON(void)
GO -> true
counting -> true
end
void motionOFF(void)
GO -> false
counting -> false
end
void init_LightSensor()
Enable GPIO peripheral for Port F
Configure PF4 as output
readData-> false
sendData-> false
end
void LightSensorCalculation()
uint32_t lightSensorValue -> 0
uint32_t lightCounter -> 0
else do
else if blkLineCounter >= 40 and blkLineCounter <= 100 and readData == 1 then
Reset lightCounter to 0
end
=== Milestone 9: Acquire Data & Send Data ===
struct MODBUSFRAME
begin
char colon
char addr
char data[20]
char checksum
char cr
char lf
end struct
void init_PingPong()
Point dataCurr to dataPing
dataPing.colon = ':';
dataPing.addr = '5';
dataPing.checksum = '5';
dataPing.cr = '\r';
dataPing.lf = '\n';
dataPong.colon = ':';
dataPong.addr = '5';
dataPong.checksum = '5';
dataPong.cr = '\r';
dataPong.lf = '\n';
end
void PingPong(void)
Clears timer interrupt for TIMER0
else do
Turn Green LED
Print dataCurr->colon
Print dataCurr->addr
Print dataCurr->checksum
Print dataCurr->cr
Print dataCurr->lf
Reset dataCount to 0
end else
end
void init_StopWatch()
counting -> false;
timeCounter -> 0
Create dynamic char array out
end
void StopWatch()
if counting is true then
timeCounter -> timeCounter + 1;
end if
end
=== Main ===
int main(void)
Call init_LEDS function
Call init_UART function
Call init_ADC function
Call init_PWM function
Call init_LightSensor function
Call init_PingPong function
Print “Bluetooth Connected” to verify that Bluetooth/UART works properly