新手上路
- 积分
- 27
- 金钱
- 27
- 注册时间
- 2016-2-12
- 在线时间
- 6 小时
|

楼主 |
发表于 2016-3-7 00:15:05
|
显示全部楼层
void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
uint8_t com_correct = 8;
uint8_t TxBuffer1[2] = {1, 2};
uint8_t TxBuffer2[2] = {10, 20};
uint8_t RxBuffer1[2];
uint8_t RxBuffer2[2];
int main(void)
{
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//éèÖÃÖD¶ÏóÅÏè¼¶·Ö×éÎa×é2£o2λÇàÕ¼óÅÏè¼¶£¬2λÏìó|óÅÏè¼¶
delay_init(); //Ñóê±oˉêy3õê¼»ˉ
//uart_init(115200); //′®¿ú3õê¼»ˉÎa115200
LED_Init();
// USART1_init(); //PA3 TX; PA9 TX
RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
while(1)
{
if(com_correct == 8)
{
//com_correct = USART1_ReadByte();
delay_ms(500);//500ms
/* Wait until end of transmit */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
/* Write one byte in the USARTy Transmit Data Register */
USART_SendData(USART1, TxBuffer1[0]);
/* Wait until end of transmit */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
{
}
/* Wait the byte is entirely received by USARTz */
while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
{
}
/* Store the received byte in the RxBuffer2 */
RxBuffer2[0] = USART_ReceiveData(USART2);
/* Clear the USART1 Data Register */
USART_ReceiveData(USART1);
/* Wait until end of transmit */
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)== RESET)
{
}
/* Write one byte in the USARTz Transmit Data Register */
USART_SendData(USART2, TxBuffer2[0]);
/* Wait the byte is entirely received by USARTy */
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE) == RESET)
{
}
/* Store the received byte in the RxBuffer1 */
RxBuffer1[0] = USART_ReceiveData(USART1);
}
delay_ms(500);//500ms
com_correct = 0;
}
}
/**
* @brief Configures the different system clocks.
* @param None
* @retval None
*/
void RCC_Configuration(void)
{
/* Enable GPIO clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
/**
* @brief Configures the different USART.
* @param None
* @retval None
*/
void USART_Configuration(void)
{
/* USARTy and USARTz configuration -------------------------------------------*/
/* USARTy and USARTz configured as follow:
- BaudRate = 230400 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate = 230400;
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;
/* Configure USARTy */
USART_Init(USART1, &USART_InitStructure);
/* Configure USARTz */
USART_Init(USART2, &USART_InitStructure);
/* Enable the USARTy */
USART_Cmd(USART1, ENABLE);
/* Enable the USARTz */
USART_Cmd(USART2, ENABLE);
/* Enable USARTy Half Duplex Mode*/
USART_HalfDuplexCmd(USART1, ENABLE);
/* Enable USARTz Half Duplex Mode*/
USART_HalfDuplexCmd(USART2, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable the USART3 Pins Software Remapping */
//GPIO_PinRemapConfig(GPIO_Remap_USART1 | GPIO_Remap_USART2, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
/* Configure USARTy Tx as alternate function open-drain */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USARTz Tx as alternate function open-drain */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
|
|