Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Below is my output and cod using UART. The output asks me to enter resistance, I

ID: 674638 • Letter: B

Question

Below is my output and cod using UART. The output asks me to enter resistance, I press 5 then enter. The program the enerts a 5 below the one I inputed and then a blank and 5 for voltage and current. I'm trying to find what is wrong with my code that makes it do this. I want the program to prompt me for resistance then voltage then current then output those numbers into the table i created in my output.

/******************************************************************************
* 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;
}

COM OPEN! For Case1, enter the resistance. For Case 2, enter the voltage. For Case 3, enter the current. case (volts) (amps) (ohms) 10 4294967260 2.000000 4294967261 4294967259

Explanation / Answer

This is simple mistake...

firstly you are using...sme variable for reading all values and

for while loop you are using same variable named "ch != ' ' ".

For the first time..you will enter ' ' to get out of first while loop that is to stop entering resitance.

Then for voltage you are again checking with same variable ch...it wont go inside voltage to read user input value..

it simply prints voltage last value you entered..thats why it was printing 5 for voltage as well as current. It will not read input from user.

EXACT ERROR:

while( ch != ' ' ) { ============>HERE IS THE WHILE LOOP TERMINATION CONDITION
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
}
}

SO to avoid this...change variable name or....reassign ch value..before reading voltage.

-----------------------------------------------------------------------------------------

***************Here code after modification************************************(i just marked modifed code as bold)

#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 );

ch = ' '; //just assigning dummy character, rather than ' '
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 );

ch = ' '; //just assigning dummy character, rather than ' '
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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote