2017-06-19 11:12 AM
I am trying to interface USART1 with the stm8l. However I am getting a problem that whatever the value I assign to the USART1_DR , it always takes the value of 0xff no matte what. my code is as follows:
(I am working with a 16 MHz internal clock and have already done the initialisation)
USART1_CR1 = 0x00;
USART1_CR1 = 0x0C;
USART1_CR3 = 0x0F;
USART1_BRR2 = 0x0A;
USART1_BRR1 = 0x08;
USART1_DR = 'HELLO'; (still shows the value 0xff in the DR register)
2017-06-21 01:29 AM
Hello Abhishek,
I didn't make a full review of your code, but at least one line looks wrong to me. USART1_DR register has only one byte, so it is not possible to write to it five bytes in one time, like you did. In your case if you try to write 'HELLO' to this register, I would expect you will send just the last byte ('O', so hexadecimal 0x4F).
Proper way to send five bytes is to send them byte by byte and you need to make sure, that you start sending next byte after previous one is already sent, so for example like this:
USART1->DR = 'H';
while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'E'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'L'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'L'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'O'; while(!(USART1->SR & USART_SR_TC));If you would like to evalaute complete application, you can use the minimum code for UART configuration, which I attach below + the code for data transmission. I checked it on my side and it is working fine. Pin PC3 is used here as a UART_TX line.
#include 'stm8l15x.h'
main()
{ CLK->PCKENR1 = 0x20; //enable clock for USART1 GPIOC->DDR = 0x08; //PC3 as OUT_PP for USART_TX GPIOC->CR1 = 0x08; GPIOC->CR2 = 0x08; USART1->BRR2 = 0x01; //UART configuration USART1->BRR1 = 0x01; USART1->CR2 = 0x0C;while (1)
{ USART1->DR = 'H'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'E'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'L'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'L'; while(!(USART1->SR & USART_SR_TC)); USART1->DR = 'O'; while(!(USART1->SR & USART_SR_TC)); }}Best regards
Szymon
2017-06-21 04:35 AM
Hello Szymon,
I tried compiling your code but unfortunately the problem still persists
that the USART_DR register takes only 0xff value whatever the character.
I'm attaching a screenshot. Also I didn't understand the purpose of the
line while(!(USART1->SR & USART_SR_TC)) . As in what's the difference
between the condition I had written and yours.
Regards,
Abhishek
On Wed, Jun 21, 2017 at 1:59 PM, Szymon Panecki <
________________ Attachments : ss.PNG : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006Hy0K&d=%2Fa%2F0X0000000b9y%2FUCiQQaYHE9l7HuGa8Yfn20QiRxgJpDk53bnJETA2Ul8&asPdf=false2017-06-21 04:47 AM
What about the clock configuration(for setting the baud rate) for the above code ?
2017-06-21 05:23 AM
HelloAbhishek,
Regarding
while(!(USART1->SR & USART_SR_TC)) : this line checks if USART transfer is complete. It is recommended to use it, because with this condition you are sure, that previous byte was sent and you can write new data to register, so you will not face the situation that new data overwrites old one before it is sent by USART.
Regarding clock configuration in my application: as you can see I don't configure the clock domain, which means that MCU uses default settings: system clock is equal toHSI / 8 = 16 MHz / 8 = 2 MHz.
Please find attached a STVD project with my code. Could you plese try it? I tested it on STM8L Discovery kit (STM8L152C6T6).
Regards
Szymon
________________ Attachments : stm8l_test.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HyK2&d=%2Fa%2F0X0000000b9x%2FO5SIcrs.041GvtMq9jwoV7y8uLXxE9G5kpECbHXzoV8&asPdf=falseSTVD_screen.png : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HybH&d=%2Fa%2F0X0000000b9w%2FFFOHfgr9lR7riDHK5yFtmdRE71BJe9ySrym4N9EbkaQ&asPdf=false2017-06-21 05:32 AM
Okay that's the reason but I'm still getting the wrong value in the
USART_DR register
On Wed, Jun 21, 2017 at 3:55 PM, Szymon Panecki <
2017-06-22 03:10 AM
This is debugging of your own code. Why is it working for you and not for me?
2017-06-22 08:10 AM
You can't inspect the DR like that, it doesn't act like a memory cell. Unless you loop-back the output externally, the RECEIVE register is not going to reflect what you put in the TRANSMIT register.
Wait for the register to be empty, and send a 'U' character, do this in a loop, attach a scope probe to the TX pin and review the signalling, check bit timing, etc.
2017-06-22 11:57 PM
Hello Clive, I'm a bit of an amateur so couldn't really follow the last part? Could you please elaborate?