Arghavan M.hasani Arghavan M.hasani - 3 months ago 16
C Question

Multiplying uint32_t to a constant number

I've written a code for lpc1768 microcontroller in C.
I am trying to multiply a uint32_t number(=2000) to 1000, but not getting the correct result(=2000000).
I get the result equal to 49216. I think the problem is with casting but I don't know how to solve it. Could you please help me? Here is a simple code related to my problem.

void send_AT_Command( uint32_t timeout )
{

int8_t module_answer = 0;

uint32_t timeout_microsec;
timeout_microsec = (timeout*1000);
LCD_Clear();
LCD_Printf( "TimeOut: %" PRIu32 "\n", timeout_microsec );
DELAY_sec( 1 );

return module_answer;

}


void module_power_on_checking(){

int8_t module_answer = 0;
module_answer = send_AT_Command( 2000 );

LCD_Clear();
LCD_Printf( "RCVD: %" PRIu8 "\n", module_answer );
DELAY_sec( 1 );

}


void system_Setup(){

//System Initialization
SystemInit();

LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P1_24,P1_25,P1_26,P1_27);

/* Specify the LCD type(2x16, 4x16 etc) for initialization*/
LCD_Init(2,16);


module_power_on_checking();
}


int main()
{
system_Setup();
while(1);

}


The problem is in getting the wrong result of timeout_microsec in the function send_AT_Comand()

Answer

First test if this works:

uint32_t number = 2000000;
LCD_Clear();
LCD_Printf( "Number is : %" PRIu32 "\n", number );

If not, then problem is in LCD_Printf. Check its documentation to see what format you need to use for uint32_t. With some googling I found one library for lpc1768 which expects non-standard %U, in which case your code would be:

uint32_t number = 2000000;
LCD_Clear();
LCD_Printf( "Number is : %U\n", number );

You also have other error. Since you are returning value from send_AT_Command, it can't be declared void, so change that to e.g. int8_t:

int8_t send_AT_Command( uint32_t timeout )
{ 
  ...
Comments