Question
a) Write a C program to solve the following problem to be used from a PSoc 4 Pioneer Kit to a UART: Taking user input
a) Write a C program to solve the following problem to be used from a PSoc 4 Pioneer Kit to a UART:
Taking user input for the value of the radius of a circle, calculate the area and circumference of the circle. You should store the value of pi using #define. Your program should prompt the user for the radius with a prompt such as: Please enter the radius in centimeters. After reading the user input, and performing the calculations, display the radius, and the two calculated values as follows: Radius = R cm, Area = A sq. cm, Circumference = C cm.
b) Use the template provided as a starting point:
#include
#include
#include
int main()
{
char rxbuffer[100]; // UART receive buffer
int rxindex; // UART receive buffer index
double number; // floating-point number
uint8 ch; // variable to hold received character
// initialize and clear rxbuffer
for( rxindex = 0; rxindex < 100; rxindex++ ) {
rxbuffer[rxindex] = 0u;
}
rxindex = 0; // initialize rxindex
UART_Start(); // start UART
// printf( "COM OPEN! " );
UART_UartPutString( "COM OPEN! " );// display initialization string
// printf( "Type a number: " );
UART_UartPutString( "Type a number: " );
// read string until the user presses return
// scanf( "%s", rxbuffer );
while( ch != ' ' ) {
ch = UART_UartGetChar(); // get the next character
if( ch != ' ' && ch != 0u ) { // if the character is not a return or null
rxbuffer[rxindex] = ch; // store it in the rxbuffer string
rxindex++; // increment the rxbuffer index
}
}
// output the received string to the UART
// printf( " %s ", rxbuffer );
UART_UartPutChar(' ');
UART_UartPutString(rxbuffer);
UART_UartPutChar(' ');
// convert the received string to a double (floating-point number)
number = strtod( rxbuffer, 0 );
// initialize and clear rxbuffer (good practice to avoid bugs)
for( rxindex = 0; rxindex < 100; rxindex++ ) {
rxbuffer[rxindex] = 0u;
}
rxindex = 0; // initialize rxindex
// output the float as a string and store it in rxbuffer
// NOTE: In order to properly output doubles, you must enable them in the compiler
// See http://www.cypress.com/?id=4&rID=87354 for more details
sprintf( rxbuffer, "Converted from double = %lf ", number );
// output the new string to the UART
// printf( " %s ", rxbuffer );
UART_UartPutChar(' ');
UART_UartPutString(rxbuffer);
UART_UartPutChar(' ');
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started