2012-10-09 02:15 PM
Hi,
I'm trying to connect together two USARTs. First is set as a transmitter and second as a receiver. I connect together two pins on the board PA9 - USART1_TX and PC7 USART6_RX. I have problem with receving data by USART6 from USART1. My program won't exit the loop -while
(
!
(
USART6->
SR&
USART_SR_RXNE)
).
My
code looks like this:int main(void)
{
RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOCEN;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_USART6EN; //USART clock enable
GPIO_Port_Init (GPIOA, USART1_TX, GPIO_Mode_AF, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);
GPIO_Port_Init (GPIOC, USART6_RX, GPIO_Mode_AF, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);
GPIO_Port_Init (GPIOD, LED1_Green | LED2_Orange, GPIO_Mode_OUT, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);
GPIO_Port_AF_Init(GPIOA, USART1_TX, GPIO_AF_USART1);
GPIO_Port_AF_Init(GPIOC, USART6_RX, GPIO_AF_USART6);
USART1->CR1 = 0x0000;
USART1->CR2 = 0x0000;
USART6->CR1 = 0x0000;
USART6->CR2 = 0x0000;
USART1->CR1 |= USART_CR1_UE; //USART enable
USART6->CR1 |= USART_CR1_UE; //USART enable
USART1->BRR = 84000000/9600;
USART6->BRR = 84000000/9600;
USART1->CR1 |= USART_CR1_TE; //transmitter enable
USART6->CR1 |= USART_CR1_RE; //receiver enable
while( !( USART1->SR & USART_SR_TXE )); //wait until DR register will be empty
USART1->DR = 'o'; //send data
while(!(USART1->SR & USART_SR_TC)); //wait for end of transmission
while(!(USART6->SR & USART_SR_RXNE)); //wait for data in DR register
uint16_t a = USART6->DR;
if (a=='o')
{
LED1_Green_on;
}
while (1)
{
}
}
Thanks in advance!
2012-10-09 06:33 PM
What library are you using, and where can the documentation be found?
What are the defines behind the values used? Be aware that the ST libraries AF Init equivalent takes GPIO_PinSource# values not GPIO_Pin_# ones.2012-10-10 12:08 AM
''My program won't exit the loop -
while
(
!
(
USART6->
SR&
USART_SR_RXNE)
)''
So think about what could cause that to happen.
You code comment says it:while( !(USART6->SR & USART_SR_RXNE) ); //wait for data in DR register
If it doesn't exit that loop, it's not receiving anything - is it?! So work back through the chain to find where it's broken... eg, is anything actually being transmitted?2012-10-10 07:46 AM
I'm not using any library, functions in code above are my. They work good, because I checked value of registers and GPIOA->AFRH was equal to 0x70 and GPIOC->AFRL 0x80000000.
I think that it is problem with receiver. When I'm measuring voltage with low-cost multimeter between PA9 and GND in transmission time, it is about 2V and before or after the transmision PA9 is at high level 3V.2012-10-10 07:46 AM
2012-10-10 11:18 AM
Well with no visibility into your code, I can't offer any input on that.
Your problem is mostly likely C49, get an oscilliscope it should be evident.2012-10-11 02:59 PM
I have changed the role of USART1 and USART6 and the communication is working good. I think that problem was a capacitor which is connected to PA9 pin (USART1 transmitter).
2012-10-12 01:58 AM
I think that problem was a capacitor which is connected to PA9 pin (USART1 transmitter).
Without doubt, sending a continuous stream at 9600 baud looks profoundly bad on a scope, 2400 might be passable.