Hi, please excuse me as i am a beginner.
I am aiming to read an integer from stdin(pointed to the uart) and echo it with the uart interrupt service routine. there is a simple retarget file along with the main code shown below. So far i can read chars (char x[32] but i am struggling with int.
I have gathered that i need to use the scanf function to read an int from the pointer defined in fgets.
My output is giving me weird values, i enter 8 and ill get a random 3 digits back. I have a feeling its a problem with the input buffer.
can someone tell me where im going wrong?
Code:
//------------------------------------------------------------------------------
// Cortex-M0
//------------------------------------------------------------------------------
#include <stdio.h>
#include <time.h>
#include <rt_misc.h>
#include <stdlib.h>
#define AHB_LED_BASE 0x50000000
#define AHB_UART_BASE 0x51000000
void UART_ISR()
{
int answer;
printf("%d", answer);
}
//////////////////////////////////////////////////////////////////
// Main Function
//////////////////////////////////////////////////////////////////
int main(void) {
int i;
char x[32];
int answer;
fgets (x, 32, stdin); /* read in a char */
sscanf (x, "%d", &answer); /* scan for the integer */
while(1)
{
*(unsigned int*) AHB_LED_BASE = 0x55;
for(i=0;i<10;i++);
*(unsigned int*) AHB_LED_BASE = 0xAA;
for(i=0;i<1000;i++);
}
}