For a few applications, there is the need to control or monitor the status of the PSOC at the same time, this cant be done with an LCD Display.
The Idea of this code is that the PSOC will Identify specific commands sent trough the serial port and execute each corresponding task in my project I made use of the serial comunication to make data aquisition with Matlab and design a control system afterwards. (I will upload an example soon)
So to begin the code we need to include the libraries (Some of them are required for my program) you can verify which ones you can leave
#include <device.h> #include <stdio.h> // for printf() call #include <stdlib.h> #include <math.h> #include <stdbool.h>
here I'm defining the new line function, since this command will be used a few times it is a first code optimization.
//variables UART #define CR 0x0D #define LF 0x0A #define NewLine() UART_UartPutChar(CR); UART_UartPutChar(LF);
we need to define this two functions Reverse and ITOA, both functions make it possible to change the numerical data to a char array which can be later sent via UART, there is another alternative code sprintf() but the problem it generates is that it requires too much memory to work it nearly filled the available memory space in my psoc.
//Reverse
char reverse(s) char *s;{
char *j;
int c;
j = s + strlen(s) - 1;
while(s < j) {
c = *s;
*s++ = *j;
*j-- = c;
}
}
//ITOA
void itoa(int n, char s[]){
int i, sign;
if ((sign = n) < 0) /* record sign */
n = -n; /* make n positive */
i = 0;
do { /* generate digits in reverse order */
s[i++] = n % 10 + '0'; /* get next digit */
} while ((n /= 10) > 0); /* delete it */
if (sign < 0)
s[i++] = '-';
s[i] = '\0';
reverse(s);
}
Then on the main function we have two parts the first one is used to identify the input commands sent trough the UART port, then there is the normal execution part of the code (when no character is sent trough the UART port) the idea is that on the first part after identifying the commands some flag variables will indicate the actual mode so that the program can run on its own The first part should be put into an interrupt routine to make the program more efficient. but there is the start point
void main()
{
int pitchenc=500,speedP=0,speedY=0,eY,eYold1=0,eYold2,uYold=0,uY,tcompP;
uint16 a=1,msg=0,run=0;
Init();
e=0;
for(;;){
/*Read DATA from sensors*/
ch=UART_UartGetChar(); //READ UART
if (0!=ch){//UART DECODING
switch(ch){
case('$'):{//STOP Command
/*Stop components or reset routine*/
Init();
NewLine();
UART_UartPutString("Stopped");
NewLine();
e=0;
run=0;
msg=0; break;}
case('#'):{//Mode 1
NewLine();
/* MODE 1 Startup Routine*/
e=1;
break;}
case('&'):{//Mode 2
NewLine();
/* MODE 2 Startup Routine*/
e=2;
break;}
case('*'):{//option A (Print message)
/*Activate/Deactivate option*/
if((e==0)||(msg==0)) msg=1;
else msg=0;
break;}
default:{
if((e==2)||(e==3)){//READ Input Data only on mode 1 and 2
if(ch=='p'){//Identify input variable 'p' or 'y'
pitch=1;
i=0;
motorP[0]= '\0';
motorP[1]= '\0';
motorP[2]= '\0';
motorP[3]= '\0';
}
if(ch=='y'){
pitch=0;
i=0;
motorY[0]= '\0';
motorY[1]= '\0';
motorY[2]= '\0';
motorY[3]= '\0';
}
if((pitch==1)&&(ch!='p')&&(ch!='e')){//if input variable 'p' get p value
motorP[i]=ch;
i++;
}
if((pitch==0)&&(ch!='y')&&(ch!='e')){//if input variable 'y' get y value
motorY[i]=ch;
i++;
}
if((ch=='e')&&(i>0)){//End of input Message Character
motP=atoi(motorP); //convert char values to Numerical
motY=atoi(motorY);
run=1;
}
}
}
}
}
t=65000-Timer_ReadCounter();
switch(e){
case(1):{//modo PRBS
/*Mode 1 execute routine*/
break;
}
case(2):{//Modo Control
/*Mode 1 execute routine*/
break;
}
}
}
I use a Message function because every time I print the message I want it to be the same feedback message (thats my case..)
This part requires the ITOA and Reverse functions to work
void message(){
itoa(motP,motorP);
itoa(motY,motorY);
itoa(Pitchang,Pang);
itoa(Yawang,Yang);
itoa(tYaw,Yspeed);
itoa(tPitch,Pspeed);
itoa(t,time);
///FeedBack Message Input
NewLine();
UART_UartPutString("p");
UART_UartPutString(motorP);
UART_UartPutString("y");
UART_UartPutString(motorY);
///FeedBack Message Angle
UART_UartPutString("ap");
UART_UartPutString(Pang);
UART_UartPutString("ay");
UART_UartPutString(Yang);
//Feedback Message Speeds
UART_UartPutString("tp");
UART_UartPutString(Pspeed);
UART_UartPutString("ty");
UART_UartPutString(Yspeed);
UART_UartPutString("t");
UART_UartPutString(time);
UART_UartPutString("f");
}