新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2016-10-21
- 在线时间
- 12 小时
|
3金钱
采用485通信,在这USART2的基础上,是不是只需要控制PG8为RX或者TX就可以了?
不知道我程序里面少了哪一步,而实现不了与PC简单的通信.所以请教各位前辈.
#include "stm32f4xx.h"
#define TX_485 GPIO_SetBits(GPIOG,GPIO_Pin_8)
#define RX_485 GPIO_ResetBits(GPIOG,GPIO_Pin_8)
void HW_USART2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//′®¿úê±Öóê1Äü
//GPIOê±Öóê1Äü
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
//
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);//PA2
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);//PA3
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;//A2,PA3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//
GPIO_Init(GPIOA, &GPIO_InitStructure);//PA2,3
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;//
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;//
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//
GPIO_Init(GPIOG, &GPIO_InitStructure);//
//
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;//
USART_InitStructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//
USART_InitStructure.USART_Parity=USART_Parity_No;//
USART_InitStructure.USART_StopBits=USART_StopBits_1;//
USART_InitStructure.USART_WordLength=USART_WordLength_8b;//
USART_Init(USART2,&USART_InitStructure);//
//
USART_Cmd(USART2,ENABLE);
USART_ClearFlag(USART2,USART_FLAG_TC);
//
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;//
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;//
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//
NVIC_Init(&NVIC_InitStructure);//
}
void delay_ms(u16 time)
{
u16 i=0;
while(time--)
{
i=25000;
while(i--);
}
}
void USART2_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART2,USART_IT_RXNE)!=RESET)
{
res=USART_ReceiveData(USART2);
delay_ms(10);
TX_485;
USART_SendData(USART2,res);
delay_ms(20);
RX_485;
}
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
HW_USART2_Init();
RX_485;
while(1);
}
|
|