How to write period in Timer from keyboard

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Hi all, 

   

I would like to be able to write new period to my timer, while the program is running. But when I'm trying to do that:

   

                uint32 period;
                UART_UartPutString("New Timer period");
                UART_UartPutString("\r\n>");
                period = UART_UartGetChar();
                Timer_WritePeriod(period);

   

I'm not able to set it right. For example when I type 10000 the period is only 10 and I'm not sure what I'm doing wrong. I've uploaded my whole project. Thanks for any help.

0 Likes
14 Replies
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

                period = UART_UartGetChar(); will not wait for a character ready, so you should check first if there is something received by checking UART__SpiUartGetRxBufferSize() for not zero.

   

The second issue is: When you type 10000 you will receive 5 characters ´1` and 4 times `0` which you need to set together to a number with the appropriate value. Not quite easy, but doable. (Convert to a null-terminated string, then use atoi() macro)

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Hi Bob,

   

thanks for Your reply. So it should be something like this?

   

uint32 period;
                UART_UartPutString("New Timer period");
                UART_UartPutString("\r\n>");

   

                if(UART_SpiUartGetRxBufferSize != 0){
                    period = UART_UartGetChar();
                }
                Timer_WritePeriod(period);

   

? But now the program says that the comparison made in if is always true. And the second issue is that first I need to make that number saved not as a period but as some string? And after that period = atoi(period)? Am I getting You right?

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

              if(UART_SpiUartGetRxBufferSize != 0) will not work, better is

   

              if(UART_SpiUartGetRxBufferSize() != 0)

   

I (personally) would put the incoming chars into a buffer until a return char is received (`\r` or 0x0d) all characters in the buffer now must be converted. period = atoi(period) is definitively wrong.

   

char Buffer[20];

   

Fill buffer with received chars

   

add a null (0) behind the last char

   

period = atoi(Buffer)

   

 

   

Put the above scheme into a subroutine like

   

uint32 ReadNumber(void) ...

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Thanks again for Your reply. I'm trying to do just like you said but then I have a problem with atoi function it says "incompatible pointer types passing uint32[30] to parameter of type const char*" and' I'm not sure what I'm doing wrong. Here are my functions:

   

uint8 load_period(uint32 *period){
    uint8 ch;
    uint8 check =0;
    uint8 cnt =0;
    if(UART_SpiUartGetRxBufferSize() != 0){
        while(check ==0){
        ch = UART_UartGetChar();
        if(ch !=0u){
            period[cnt]=ch;
            if(cnt<20){
                cnt++;
            }
            if(ch == CR_CHAR_VALUE){
                cnt--;
                period[cnt]=0;
                check=1;
            }
        }
    }
    }
    return *period;
}

   

 

   

 

   

and the other one 

   

uint32 buffer[30];
                uint32 period;
                UART_UartPutString("New period:");
                UART_UartPutString("\r\n>");
                *buffer = load_period(buffer);
                period = atoi(buffer);
                Timer_WritePeriod(period);
                Timer_WriteCounter(0);

   

 

   

I'm also attaching whole project.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

declare buffer as char and not as uint32

   

 

   

Bob

0 Likes
Anonymous
Not applicable

But then the whole function load_period should also be as a char, not uint32? because when I declare buffer as char then there is a problem in line *buffer = load_period(buffer). passing char [32] to parameter of type uint32. 

   

Mariusz

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

I did not suggest you to write a function LoadPeriod(), I advised you to write a ReadNumber().

   

I think you need to understand a bit more about C-language. Here is my preferred manual.

   

Post your complete project, so that we all can have a look at all of your settings. To do so, use
Creator->File->Create Workspace Bundle (minimal)
and attach the resulting file.

   

 

   

Bob

0 Likes
lock attach
Attachments are accessible only for community members.
Anonymous
Not applicable

Yes I know but I tried to combine my function to load characters to bufer with the one you suggestes, because I've already got loading from buffer ready. Here is my project.

0 Likes
lock attach
Attachments are accessible only for community members.
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

See changes in project. I would suggest you to change the longer elseif construct to a switch statement.

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks for Your help, but I think the forever loop shouldn't be used, because then when I type s0 to set new Timer Period, it will never exit that loop so I can't do anything else and other commands just don't work. And because of that also setting new period doesn't work it's still 14 ms as set in the Timer module before the start of the program.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

OK

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Also I don't know exactly why, but the program never enters the 

   

if(ch == CR_CHAR_VALUE)
        {
            UART_UartPutString("load period");
           return atoi(Buffer);
        }

   

loop. I've tried with some putstrings and the problem was Wait() function. After I commented that, now it enters properly the function and the loop but no mather what I type on my keyboard the output of atoi(Buffer) is always <0> sometimes it says "interrupt" near to that. But I don't have any interrupts on my UART set.

0 Likes
Bob_Marlowe
Level 10
Level 10
First like given 50 questions asked 10 questions asked

My fault. Change

   

        while(UART_SpiUartGetRxBufferSize()) Wait();    //    Wait for character received

   


to

   

        while(!UART_SpiUartGetRxBufferSize()) Wait();    //    Wait for character received

   

 

   

 

   

Bob

0 Likes
Anonymous
Not applicable

Thanks man, now it works just as I wanted! 🙂

0 Likes