这是main程序
#include "
STM32f10x.h"
#include "bsp_usart1.h"
#include "bsp_led.h"
int main(void)
{
LED_GPIO_Config();
USART1_Config();
NVIC_Configura
tion();
while(1)
{
char ch;
if(ch=='A')
{
LED1(ON);
}
else if(ch=='B')
{
LED1(OFF);
}
}
}
这是bsp.usart.c程序
#include "bsp_usart1.h"
/**
* @brief USART1 GPIO ÅäÖÃ,¹¤×÷ģʽÅäÖá£9600 8-N-1
* @param ÎÞ
* @retval ÎÞ
*/
void USART1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* config USART1 clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 mode config */
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
USART_Init(USART1, &USART_InitStructure);
/* ʹÄÜ´®¿Ú1½ÓÊÕÖÐ¶Ï */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
// USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
}
/// ÅäÖÃUSART1½ÓÊÕÖжÏ
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/******************
ÖжϷþÎñ³ÌÐò
*****************/
void USART1_IRQHandler(void)
{
uint8_t ch;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
ch = USART_ReceiveData(USART1);
printf( "
%02x
", ch ); //printf·¢ËÍ·½Ê½
USART_SendData(USART1,ch); //32¿âº¯Êý·¢ËÍ·½Ê½
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
}
/******************
¿ÉÒÔ·¢ËÍÒ»¸ö×Ö·û´®
*******************/
void USART1_Send_s(u8 *ch)
{
while(*ch)
{
USART_SendData(USART1,*ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);
USART_ClearFlag(USART1,USART_FLAG_TC);
}
}
/// Öض¨Ïòc¿âº¯Êýprintfµ½USART1
int fputc(int ch, FILE *f)
{
/* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART1 */
USART_SendData(USART1, (uint8_t) ch);
/* µÈ´ý·¢ËÍÍê±Ï */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return (ch);
}
/// Öض¨Ïòc¿âº¯Êýscanfµ½USART1
int fgetc(FILE *f)
{
/* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */
while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);
return (int)USART_ReceiveData(USART1);
}
本人菜鸟,求大神指导
那么第一,你应该先把串口的收和发的功能都实现;
第二步才来实现根据接收的数据实现对应的功能。
一周热门 更多>