Continue to Site

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.

  • 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.

converting uart string to int mikroc

Status
Not open for further replies.
Code:
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
 
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 ?
 
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:
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:
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 ?

Why would you want to do that? Doesn't make any sense..
 
Last edited:
yes,

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

Yes. That is a better idea. Avoid using floats in microcontrollers.
 
Why would you want to do that? Doesn't make any sense..

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:
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

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.
 
This line of code does nothing. You can delete it.
Code:
if (!UART1_Data_Ready());        // if there is UART character in buffer
 
This line of code does nothing. You can delete it.
Code:
if (!UART1_Data_Ready());        // if there is UART character in buffer

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.
 
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.

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).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top