Thread: Reading an integer from stdin

  1. #1
    Registered User
    Join Date
    Mar 2014
    Posts
    7

    Reading an integer from stdin

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

  2. #2
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    Well I am not sure what UART is, but this is wrong:

    Code:
    *(unsigned int*) AHB_LED_BASE = 0x55;   //wrong
    ...
    *(unsigned int*) AHB_LED_BASE = 0xAA;  //wrong
    The define preprocessor command will replace any occurance of "AHB_LED_BASE" with whatever you define it as, in this case a string seemingly representing an address. This would be equivalent to doing:

    Code:
    (some dereferencing/casting stuff)0x5000000 = 0x55;
    See the problem?
    "Simplicity is the ultimate sophistication." - Leonardo da Vinci

  3. #3
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,784
    Quote Originally Posted by jwroblewski44 View Post
    See the problem?
    I don't.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,907
    Quote Originally Posted by jwroblewski44 View Post
    Well I am not sure what UART is, but this is wrong:

    Code:
    *(unsigned int*) AHB_LED_BASE = 0x55;   //wrong
    ...
    *(unsigned int*) AHB_LED_BASE = 0xAA;  //wrong
    The define preprocessor command will replace any occurance of "AHB_LED_BASE" with whatever you define it as, in this case a string seemingly representing an address. This would be equivalent to doing:

    Code:
    (some dereferencing/casting stuff)0x5000000 = 0x55;
    See the problem?
    That's actually valid. It's a common practice in embedded systems, where you have direct access to registers or memory-mapped IO like a UART (which is for serial communications). Basically, AHB_LED_BASE contains an address that is used to communicate with some device. That address is cast to a pointer to unsigned int, then that pointer dereferenced to assign 0x55 or 0xAA to that address. Sort of like:
    Code:
    unsigned int *pointer_to_thing_i_want_to_change = get_pointer_to_thing_i_want_to_change();
    *pointer_to_thing_i_want_to_change = 0x55;
    ...
    unsigned int *get_pointer_to_thing_i_want_to_change(void) {
        return AHB_LED_BASE;
    }

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    I don't see where you have set up the UART connection

    I'm guessing that you have the wrong baudrate.

    [edit]
    I found this link for a ARM Cortex-M0
    Getting Started with NXP&#39;s LPC11XX Cortex-M0 ARM Microcontrollers - Microcontroller - eewiki

    I notice that you are returning 0 at the end of main.
    [/edit]
    Last edited by Click_here; 03-26-2014 at 11:24 PM.
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Mar 2012
    Location
    the c - side
    Posts
    373
    In normal c, the answer variable in the ISR routine and the one in the main function have different scope, ie they are not the same variable.
    If that's the problem a quick way to check would be to declare answer as a global variable and see what happens.

  7. #7
    Registered User
    Join Date
    Mar 2014
    Posts
    7
    Hi Click_here, the Uart is set up in the IP blocks of the hardware, as is the interrupt service routines ARM Information Center

    Gemara, I'll try this later, thanks.

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    What baud rate is it set up as?

    Are there any links for working examples that you could put up here for us to look at?
    Fact - Beethoven wrote his first symphony in C

  9. #9
    Registered User
    Join Date
    Mar 2014
    Posts
    7
    19200

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,875
    What is your frequency - UARTCLK?

    What value do you have in your UARTFBRD?

    Can you at least confirm that your baudrate is 19200 with a digital oscilloscope?

    The reason why I am suspicious of your baud rate is that I've seen this problem a few times, and you are not controlling any of the settings in your code.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading from stdin
    By windingprince in forum C Programming
    Replies: 2
    Last Post: 08-27-2009, 05:07 AM
  2. reading sentence from stdin
    By -EquinoX- in forum C Programming
    Replies: 7
    Last Post: 03-29-2009, 10:12 PM
  3. reading from stdin
    By carlorfeo in forum C++ Programming
    Replies: 18
    Last Post: 02-06-2008, 08:50 AM
  4. help reading from stdin
    By asdffa in forum C Programming
    Replies: 8
    Last Post: 11-15-2006, 03:53 PM
  5. reading from stdin
    By AngKar in forum C Programming
    Replies: 4
    Last Post: 05-03-2006, 12:14 PM

Tags for this Thread