1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

converting uart string to int mikroc

Discussion in 'Microcontrollers' started by Varunme, Mar 20, 2012.

  1. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Code (text):

    void main(){
    char output[7];
    int i,rot;
    trisb=0x00;
    portb=0x00;
     
    UART1_Init(9600);
     
    while(1){
     
         //  Initialize string with all '\0'
            for(i=0; i<7; i++){
                output[i] = '\0';
            }
            if ((!UART1_Data_Ready());        // if there is UART character in buffer
              UART1_Read_Text(output, "m", 255);   // read input from uart
     
            for(rot=0; rot<=atoi(output)-1; ++rot)
            {
                   PORTB=0b00010000; delay_ms(200);
                   PORTB=0b00001000; delay_ms(200);
            }
    }
    }
     
    change to an if statement
     
    • Like Like x 1
  2. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0
    yes,
    its working


    thanks,

    :)
     
  3. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0
    How can i rewrite this so that we can use floats,
    like

    if 0.1 is sent, then once the loop is executed
    0.2 -> two times

    --
    --
    --
    1.0 -> 10 times
    1.1 -> 11 times etc ?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0

    i think , i have to apply atof() and just multiply by 10 and send to the loop isnt it ?

    tried this, but found something wrong
    Code (text):



    void main(){
    char output[7];
    int i,rot,y;
    trisb=0x00;
    portb=0x00;

    UART1_Init(9600);

    while(1){

             //  Initialize string with all '\0'
            for(i=0; i<7; i++){
                output[i] = '\0';
            }
            if (!UART1_Data_Ready());        // if there is UART character in buffer
                  UART1_Read_Text(output, "m", 255);   // read input from uart
             y=  (atof(output)-1);
            for(rot=0; rot<=y*10; ++rot)
            {
                   PORTB=0b00010000; delay_ms(200);
                   PORTB=0b00001000; delay_ms(200);
            }
    }
    }

     
     
    Last edited: Mar 21, 2012
  6. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Why would you want to do that? Doesn't make any sense..
     
    Last edited: Mar 22, 2012
  7. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0
    yes,

    so i have to format the data in PC and sent as integers, isnt it ?
     
  8. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Yes. That is a better idea. Avoid using floats in microcontrollers.
     
    • Like Like x 1
  9. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0
    it works abnormally,
    some times the non decimal part multiplied by 10 and loop that much times,
    sometimes no loop , like when 0.1m is sent....


    actually this is the answer for your second question
     
    Last edited: Mar 22, 2012
  10. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Ok.. sorry I deleted my second question.

    Anyway the problem with your latest code is that the variable "y" is an integer so it cuts off the decimals of the float. Your compiler should at least give a warning when you compile that kind of code.. actually the code should not compile at all.
     
    • Like Like x 1
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    This line of code does nothing. You can delete it.
    Code (text):

    if (!UART1_Data_Ready());        // if there is UART character in buffer
     
     
    • Like Like x 1
  12. Varunme

    Varunme Member

    Joined:
    Sep 25, 2011
    Messages:
    75
    Likes:
    0
    my compiler compiled it with out an issue, but not worked
     
  13. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    Sorry Mr T ... that was my mistake... I left the semi colon in by mistake it should read the RS232 port ONLY when data is ready.
     
  14. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I think the "UART1_Read_Text(output, "m", 255); " waits until it receives the data, so the "data ready" polling is useless (also in my early version of the code).
     
  15. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,148
    Likes:
    907
    Location:
    Rochdale UK
    ONLINE
    That's the point... He doesn't want to wait... He only wants the data when it arrives....
     
  16. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Hmm.. you are right.
     

Share This Page