测试一下咯。。。 写一个串口驱动。。很简单void uart_init(void)
{
u32 baudRate = 115200;//波特率
float temp;
u16 BRR_Value;
u16 BRR_Mantissa;
u16 BRR_Fraction;
//定义时钟选择结构体
RCC_ClocksTypeDef RCC_ClocksStatus;
RCC_GetClocksFreq(&RCC_ClocksStatus);
//选择PCLK2 为时钟 RCC设置里未分频,即72Mhz
temp = (float)(RCC_ClocksStatus.PCLK2_Frequency)/(baudRate*16);
BRR_Mantissa = (u32)temp; //得到BRR[15:4]整数部分
BRR_Fraction = (u32)(temp - BRR_Mantissa)*16; //得到BRR[3:0]小数部分
BRR_Mantissa<<=4;
BRR_Value = BRR_Mantissa + BRR_Fraction; //拼接整数和小数部分
USART1->BRR = BRR_Value; //设置波特比率
USART1->CR1 |= 1<<13; // Enable the USART by writing the UE bit in USART_CR1 register to 1
USART1->CR1 |= 1<<2; // Set the TE bit in USART_CR1 to send an idle frame as first transmission
USART1->CR1 |= 1<<3; // Set the RE bit USART_CR1. This enables the receiver which begins searching for a start bit.
}
//我这个用库了。。不用库也一样。。没找到不用库的版本
void USART1_Puts(char * str)
{
//USART_ClearFlag(USART1,USART_FLAG_TC); 清零TC用的,网上说法:“清除标志位,否则第1位数据会丢失”
while(*str)
{
GPIO_ResetBits(GPIOF, GPIO_Pin_8);
USART_SendData(USART1, *str++);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{
}
GPIO_SetBits(GPIOF, GPIO_Pin_8);
}
}
主程序来一下~
int main (void)
{
OSInit();
OSTaskCreateExt(AppStartTask,
(void *)0,
(OS_STK *)&AppStartTaskStk[TASK_STK_SIZE-1],
TASK_START_PRIO,
TASK_START_PRIO,
(OS_STK *)&AppStartTaskStk[0],
TASK_STK_SIZE,
(void *)0,
OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR);
OSStart();
}
再来个task任务~别忘了声明啊static void AppStartTask (void *p_arg);
static void AppStartTask (void *p_arg)
{
(void)p_arg;
USART1_Puts("aaaaa!\r\n");
while(1) {
delay();
}
}
|