2012-03-20 04:03 AM
Hello,
I´m a newbie in microcontrollers and I'm just getting started with stm32f4 discovery. I have this code below to send data through a wifi modulebut i want to send data every 10 seconds. How can i do that? Does anyone can help me?Thank´s in advance!int
main(
void
)
{
USART3Init();
char
str[] =
''Testing...''
;
char
*s;
while
(1)
{
s=str;
while
(*s){
while
(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
USART_SendData(USART3, *s++);
}
}
return
1;
}
void
USART3Init(
void
)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable USART3 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
/* Connect PD8 and PD9 pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
/* Configure USART3 Tx (PD8) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//speed
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
//
GPIO_Init( GPIOD, &GPIO_InitStructure );
/* Configure USART3 Rx (PD9) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init( GPIOD, &GPIO_InitStructure );
/* Initialise Structure USART3 */
USART_InitStructure.USART_BaudRate = 9600;
//9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//1
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Initialise USART3 */
USART_Init(USART3,&USART_InitStructure);
/* Enable the interrupt Rx USART3 */
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
/* Enable the UART3 */
USART_Cmd( USART3, ENABLE );
}
2012-03-20 08:48 AM
The SysTick Timer is best suited for this purpose.
Init it like this: if (SysTick_Config (SystemCoreClock / 1000)) // init to 1ms clock while (1); In the SysTick interrupt handler, just count the events and set a flag if the desired time is up. In main, send the USART data in the main loop when the flag is set, and reset the flag. A simple method, which should work in your case. You should find plenty of other SysTick code in example applications, usually delivered with the DevTool of your choice, or from ST.