I created a code with C. Now I need it to run on UART. This my C code output: Th
ID: 674575 • Letter: I
Question
I created a code with C. Now I need it to run on UART.
This my C code output:
This is my current code output using UART: The program only prompts me for resistance then does not let me input values for voltage or current. Trying to figure out what I am doing wrong
/******************************************************************************
* Project Name : USB-UART-EGR
* File Name : main.c
* Version : 1.1 (Updated by Dr. Jordan)
* Device Used : CY8C4245AXI-483
* Software Used : PSoC Creator 3.0 SP1
* Compiler : ARMGCC 4.4.1
* Related Hardware : CY8CKIT-042 PSoC 4 Pioneer Kit
*
*
******************************************************************************
* THEORY OF OPERATION
* This project demonstrates UART communication over the kit USB-UART Bridge.
* The project uses the SCB based UART component which stores the users input in an
* array. when the user press the return key the PSoC will send what has be in the
* array, then clears the array and sets the pointer back to o.
*
* UART is configured with
* UART Basic
* Mode: Standard
* Direction: TX+RX
* Baud rate (kbps): 9600
* Data bits: 8 bits
* Parity: None
* Stop bits: 1 bit
* Physical connections needed:
* Jumper between P0[5] and PSoC 5LP Header J8 P12[6]
* UART Advanced
* RX buffer size: 8
* TX buffer size: 8
* Interrupt: None
*
* Physical Connetion
* Connect jumper wire from the UART RX (P0[4]) of PSoC 4 to J8_10 (P12[7]) of PSoC 5LP
* Connect jumper wire from the UART TX (P0[5]) of PSoC 4 to J8_9 (P12[6]) of PSoC 5LP
* ******************************************************************************/
#include <device.h>
#include <stdio.h>
#include <stdlib.h> // contains strtod(), which converts an ASCII string to a double
int main()
{
char rxbuffer[100]; // UART receive buffer
int rxindex; // UART receive buffer index
uint ch; // variable to hold received character
double v1 = 10;
double i1 = 2;
double i2 = 5;
double r2 = 7;
double v3 = 3;
double r3 = 4;
// 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( "Enter Resistance" );
UART_UartPutString( "For Case 1, enter the resistance. " );
// 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
}
}
UART_UartPutChar(' ');
UART_UartPutString(rxbuffer);
UART_UartPutChar(' ');
// printf( "Enter Voltage" );
UART_UartPutString( "For Case 2, enter the voltage. " );
// 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
}
}
UART_UartPutChar(' ');
UART_UartPutString(rxbuffer);
UART_UartPutChar(' ');
// printf( "Enter current" );
UART_UartPutString( "For Case 3, enter the current. " );
// 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
}
}
UART_UartPutChar(' ');
UART_UartPutString(rxbuffer);
UART_UartPutChar(' ');
UART_UartPutString(" ----------------------------------------------- " );
UART_UartPutString( " V I R " );
UART_UartPutString( "Case (volts) (amps) (ohms) " );
UART_UartPutString( "----------------------------------------------- " );
sprintf(rxbuffer,"1 %g %f %u ",v1,i1,ch-'0');
UART_UartPutString(rxbuffer);
sprintf(rxbuffer,"2 %u %g %g ",ch-'1',i2,r2);
UART_UartPutString(rxbuffer);
sprintf(rxbuffer,"3 %g %u %g ",v3,ch-'2',r3);
UART_UartPutString(rxbuffer);
return 0;
}
Explanation / Answer
Clarification:
You are reading your values as character. When you print that character value using '%d", system simply prints its ASCII value, not the actual value entered as character. You should convert it to interger, then print it. A character can be converted to integer simply by subtracting '0' from it.
Example:
char ch='5';
printf("%d ",ch); //This will print 53 - ascii value of symbol 5
printf("%d ",ch-'0'); //This will print actual value 5 as integer.
Try this and your prorgam should work.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.