#include "RS485.h"
#include "sys.h"
void RS485_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; //GPIOA2與GPIOA3
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;//復(fù)用功能
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; //推挽復(fù)用輸出
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOA,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_OUT;
GPIO_Init(GPIOG,&GPIO_InitStruct);
//USART2 初始化設(shè)置
USART_InitStructure.USART_BaudRate = 9600;//波特率設(shè)置
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字長為8位數(shù)據(jù)格式
USART_InitStructure.USART_StopBits = USART_StopBits_1;//一個停止位
USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶校驗位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數(shù)據(jù)流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //收發(fā)模式
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
USART_ClearFlag(USART2, USART_FLAG_TC);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//開啟接受中斷
//Usart2 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//搶占優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //子優(yōu)先級3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器、
RS485_TX_EN =0; //默認(rèn)為接收模式
}u8 res;
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)//接收到數(shù)據(jù)
{
res =USART_ReceiveData(USART2);//;讀取接收到的數(shù)據(jù)USART2->DR
}
}
void RS485_Sendbyte(u8 d)//485發(fā)送一個字節(jié)
{
RS485_TX_EN =1; //發(fā)送模式
USART_SendData(USART2,d);
while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
USART_ClearFlag(USART2,USART_FLAG_TC);
RS485_TX_EN =0;
}
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//設(shè)置系統(tǒng)中斷優(yōu)先級分組2
RS485_Init();
while(1)
{
RS485_Sendbyte('A');
}
}
按理應(yīng)該在主循環(huán)中,串口不斷的發(fā)送字符A,但是沒有反應(yīng),請大家看看怎么回事。謝謝。
|