- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
declare buffer as char and not as uint32
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
OK
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My fault. Change
while(UART_SpiUartGetRxBufferSize()) Wait(); // Wait for character received
to
while(!UART_SpiUartGetRxBufferSize()) Wait(); // Wait for character received
Bob
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks man, now it works just as I wanted! 🙂