void USART2_IRQHandler(void)
{
uint8_t i=0;
if(RESET != USART_GetITStatus(USART2, USART_IT_IDLE))//接收非空
{
/* clear IDLE flag */
rc_tmp=USART2->ISR;
rc_tmp=USART2->RDR;//软件序列清除IDLE标志位
USART2->ICR = 1<<3;
USART2->ICR = 1<<4;
USART_ClearITPendingBit(USART2,USART_IT_IDLE);
for(i=0;i<7;i++)
{
recive_485buf = rxbuf;
}
UART_RX_Proc();
DMA_Cmd(DMA1_Channel5,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel5, 10);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
if(RESET != USART_GetITStatus(USART2, USART_IT_TXE))//发送完成
{
USART_ClearITPendingBit(USART2, USART_IT_TXE);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
}
}
static void usart_dma_init(void)
{
DMA_InitTypeDef DMA_InitStructure;
NVIC_InitTypeDef NVIC_Initstrcuture;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//DMA1时钟使能
/* tx */
DMA_DeInit(DMA1_Channel4);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->TDR); //DMA外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)txbuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
DMA_InitStructure.DMA_BufferSize = 0; //数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //存储器数据长度:8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // 使用普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //中等优先级
DMA_Init(DMA1_Channel4, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel4,DISABLE);
/* rx */
DMA_DeInit(DMA1_Channel5);
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(USART2->RDR); //DMA外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)rxbuf;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 10; //数据传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设非增量模式
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //存储器增量模式
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; //外设数据长度:8位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; //存储器数据长度:8位
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; // 使用普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //中等优先级
DMA_Init(DMA1_Channel5, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel5,ENABLE);
}
void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_Initstructure;
USART_InitTypeDef USART_Initstructure;
NVIC_InitTypeDef NVIC_Initstrcuture;
// RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE );
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA ,ENABLE );
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
//TX
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstructure);
//RX
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstructure);
//485 set
GPIO_Initstructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Initstructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_Initstructure.GPIO_OType = GPIO_OType_PP;
GPIO_Initstructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Initstructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_Initstructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
USART_Initstructure.USART_BaudRate = 115200;
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);
NVIC_Initstrcuture.NVIC_IRQChannel = USART2_IRQn;
NVIC_Initstrcuture.NVIC_IRQChannelPriority=3;
NVIC_Initstrcuture.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_Initstrcuture);
usart_dma_init();
USART_DMACmd(USART2, USART_DMAReq_Rx | USART_DMAReq_Tx, ENABLE);
USART_ITConfig(USART2, USART_IT_IDLE, ENABLE);//开启空闲中断
USART_ClearITPendingBit(USART2,USART_IT_IDLE);
USART_ClearFlag(USART2, USART_FLAG_IDLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_TXE);
USART_ClearFlag(USART2, USART_IT_TXE);
USART_Cmd(USART2, ENABLE);
}
void USART0_DMAsend(uint8_t Len)
{
GPIO_SetBits(GPIOA,GPIO_Pin_4);
DMA_Cmd(DMA1_Channel4,DISABLE);
DMA_SetCurrDataCounter(DMA1_Channel4, Len);
DMA_Cmd(DMA1_Channel4,ENABLE);
while (RESET == DMA_GetFlagStatus(DMA1_FLAG_TC2
|
|