|
最近調試485,我想把上位機發送的數據再返回到上位機串口調試助手顯示:usart3串口,pB2控制收發,pb10發送,Pb11接收,程序只能收發一個字符,請教各位網友如何收發字符串,我把我的代碼寫上。
#include "stm32f10x.h"
#include
#include "delay.h"
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus; //¶¨òåía2¿¸ßËù¾§ìåÆô¶ˉ×′ì¬Ã¶¾ù±äá¿
RCC_DeInit(); //¸′λRCCía2¿é豸¼Ä′æÆ÷μ½Ä¬èÏÖμ
RCC_HSEConfig(RCC_HSE_ON); //′ò¿aía2¿¸ßËù¾§Õñ
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //μè′yía2¿¸ßËùê±Öó×¼±¸oÃ
if(HSEStartUpStatus == SUCCESS) //ía2¿¸ßËùê±ÖóòѾ-×¼±eoÃ
{
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); //¿aÆôFLASHÔ¤¶á»o3å1|Äü£¬¼óËùFLASHμĶáè¡¡£ËùóD3ìDòÖD±ØDëμÄó÷¨.λÖãoRCC3õê¼»ˉ×óoˉêyàïÃ棬ê±ÖóÆeÕñÖ®oó
FLASH_SetLatency(FLASH_Latency_2); //flash2ù×÷μÄÑóê±
RCC_HCLKConfig(RCC_SYSCLK_Div1); //ÅäÖÃAHB(HCLK)ê±Öóμèóú==SYSCLK
RCC_PCLK2Config(RCC_HCLK_Div1); //ÅäÖÃAPB2(PCLK2)Öó==AHBê±Öó
RCC_PCLK1Config(RCC_HCLK_Div2); //ÅäÖÃAPB1(PCLK1)Öó==AHB1/2ê±Öó
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9); //ÅäÖÃLLê±Öó == ía2¿¸ßËù¾§ìåê±Öó * 9 = 72MHz
RCC_PLLCmd(ENABLE); //ê1ÄüPLLê±Öó
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //μè′yPLLê±Öó¾íD÷
{
}
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); //ÅäÖÃÏμí3ê±Öó = PLLê±Öó
while(RCC_GetSYSCLKSource() != 0x08) //¼ì2éPLLê±ÖóêÇ·ñ×÷ÎaÏμí3ê±Öó
{
}
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART3_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ClearFlag(USART3, USART_FLAG_TC);
}
void Uart3_PutChar(u8 ch)
{
GPIO_SetBits(GPIOB,GPIO_Pin_2);
delay_ms(1);
USART_SendData(USART3, (u8) ch);
while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
delay_ms(2);
GPIO_ResetBits(GPIOB,GPIO_Pin_2);
}
void USART3_IRQHandler(void)
{
u8 dat;
while(USART_GetITStatus(USART3, USART_IT_RXNE) !=SET);
dat = USART_ReceiveData(USART3);
Uart3_PutChar(dat);
//USART_ClearITPendingBit(USART3,USART_IT_RXNE );
//USART_ClearFlag(USART3,USART_FLAG_RXNE);
}
int main(void)
{
//u8 dat;
delay_init(72);
RCC_Configuration();
NVIC_Configuration();
USART3_Configuration();
while(1)
{
// GPIO_ResetBits(GPIOB,GPIO_Pin_12);
// while(USART_GetFlagStatus(USART3,USART_FLAG_RXNE)!=SET);
//dat = USART_ReceiveData(USART3);
//Uart3_PutChar(dat);
}
}
|
|