经过调试在stm32F101CBT6中实现通信。注意还要配一个TTL转usb接口,才能进行通信。实现字符串回发 #include"stm32f10x_it.h" #include "stm32f10x_conf.h" void RCC_Configuration(void); //时钟配置 void USART_Configuration(void); //定义串口初始化函数 void NVIC_Configuration( void); void Uart1_PutChar(u8 ch); void Uart1_PutString(u8* buf , u8 len); void GPIO1_Configuration(void); void GPIO_Configuration(void); int USART_RX_BUF[64]=0; int USART_RX_STA=0; void RCC_Configuration(void) { ErrorStatusHSEStartUpStatus;
//使能外部晶振 RCC_HSEConfig(RCC_HSE_ON); //等待外部晶振稳定 HSEStartUpStatus =RCC_WaitForHSEStartUp(); //如果外部晶振启动成功,则进行下一步操作 if(HSEStartUpStatus==SUCCESS) { //设置HCLK(AHB时钟)=SYSCLK RCC_HCLKConfig(RCC_SYSCLK_Div1); //PCLK1(APB1) = HCLK/2 RCC_PCLK1Config(RCC_HCLK_Div2); //PCLK2(APB2) = HCLK RCC_PCLK2Config(RCC_HCLK_Div1); RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_4); //启动PLL RCC_PLLCmd(ENABLE); //等待PLL稳定 while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) ==RESET); //系统时钟SYSCLK来自PLL输出 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //切换时钟后等待系统时钟稳定 while(RCC_GetSYSCLKSource()!=0x08); }
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE); } int main(void) { int i; u8 len; RCC_Configuration(); RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//使能各部分时钟 //NVIC_Configuration(); //串口中断配置 //RCC中打开相应串口 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 ,ENABLE); GPIO_Configuration(); //串口1的管脚初始化 GPIO1_Configuration(); NVIC_Configuration(); USART_Configuration(); while (1) { GPIO_SetBits(GPIOA,GPIO_Pin_8) ; GPIO_SetBits(GPIOA,GPIO_Pin_11) ; GPIO_SetBits(GPIOA,GPIO_Pin_15) ; if(USART_RX_STA&0x80)
{ len=USART_RX_STA&0x3f;//得到此次接收到的数据长度 USART2->DR=len; while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET); for(i=0;i {USART2->DR=USART_RX_BUF[i]; while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET);//等待发送结束} } USART_RX_STA=0; } for(i=0;i<1000000;i++); GPIO_ResetBits(GPIOA,GPIO_Pin_11); GPIO_ResetBits(GPIOA,GPIO_Pin_15); for(i=0;i<1000000;i++);
} } void GPIO_Configuration(void) {
GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_11|GPIO_Pin_15; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOA,&GPIO_InitStructure); } voidGPIO1_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //管脚2 GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA,&GPIO_InitStructure); //TX初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //管脚3 GPIO_InitStructure.GPIO_Speed =GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA,&GPIO_InitStructure); //RX初始化
}
void USART_Configuration(void)//串口初始化函数
{ USART_ClockInitTypeDefUSART_ClockInitStructure; //串口参数初始化 USART_InitTypeDef USART_InitStructure;//串口设置恢复默认参数 //初始化参数设置 USART_InitStructure.USART_BaudRate = 9600;//波特率9600 USART_InitStructure.USART_WordLength =USART_WordLength_8b; //字长8位 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;//打开Rx接收和Tx发送功能 USART_Init(USART2,&USART_InitStructure); //初始化 //USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//使能接受中断,在接受移位 寄存器中有数据是产生 USART_ClockInitStructure.USART_Clock =USART_Clock_Disable; USART_ClockInitStructure.USART_CPOL =USART_CPOL_Low; USART_ClockInitStructure.USART_CPHA =USART_CPHA_2Edge; USART_ClockInitStructure.USART_LastBit =USART_LastBit_Disable; USART_ClockInit(USART2,&USART_ClockInitStructure); USART_Cmd(USART2,ENABLE); //启动串口 USART_ITConfig(USART2,USART_IT_RXNE, ENABLE); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//选择分组方式0
NVIC_InitStructure.NVIC_IRQChannel =USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE; NVIC_Init(&NVIC_InitStructure); }
在stm32f10x_it.c void USART2_IRQHandler(void); //放在头文件里面也可以 extern intUSART_RX_BUF[64];//声明全局变量,此处不能够赋值 extern int USART_RX_STA;//声明全局变量,此处不能够赋值
void USART2_IRQHandler(void)
{
unsigned intres; if(USART_GetFlagStatus(USART2,USART_IT_RXNE)==SET) { res =USART_ReceiveData(USART2); if((USART_RX_STA&0x80)==0)//接收未完成 { USART_RX_BUF[USART_RX_STA]=res; USART_RX_STA++; if(USART_RX_STA>63) USART_RX_STA=0; if(res=='#') USART_RX_STA|=0x80; } }
if(USART_GetITStatus(USART2, USART_IT_RXNE) !=RESET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
}
}
|