element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Cypress Kits
  • Products
  • Dev Tools
  • Cypress Kits
  • More
  • Cancel
Cypress Kits
Forum UART Interface (commands menu)
  • Blog
  • Forum
  • Documents
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Cypress Kits to participate - click to join for free!
Actions
  • Share
  • More
  • Cancel
Forum Thread Details
  • Replies 0 replies
  • Subscribers 24 subscribers
  • Views 843 views
  • Users 0 members are here
  • monitor
  • command
  • uart
  • menu
  • comunication
Related

UART Interface (commands menu)

juanes.pazj
juanes.pazj over 12 years ago

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");
}

 

 

   

  • Sign in to reply
  • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube