新手上路
- 积分
- 22
- 金钱
- 22
- 注册时间
- 2016-9-19
- 在线时间
- 5 小时
|
代码如下:
/* Enable GPIOs clocks */
RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB |
RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD |
RCC_AHB1Periph_GPIOE | RCC_AHB1Periph_GPIOF |
RCC_AHB1Periph_GPIOG|SD_DETECT_GPIO_CLK, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2 , ENABLE); /* 使能串口外设的时钟 */
GPIO_InitTypeDef GPIO_InitStructure;
/* Connect PXx to USARTx_Tx*/
GPIO_PinAFConfig(PORT_UARTTX,PINSOURCE_UARTTX,GPIO_AF_USART2);
/* Connect PXx to USARTx_Rx*/
GPIO_PinAFConfig(PORT_UARTRX,PINSOURCE_UARTRX,GPIO_AF_USART2);
/* Configure USART2 Rx (PD.06) as input floating */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = PIN_UARTRX;
GPIO_Init(PORT_UARTRX, &GPIO_InitStructure);
/* Configure USART2 Tx (PD.05) as alternate function push-pull */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin = PIN_UARTTX;
GPIO_Init(PORT_UARTTX, &GPIO_InitStructure);
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// Enable the USART2 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitTypeDef USART_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_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStructure);
/* Enable USART2 Receive and Transmit interrupts */
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
/* Enable USART2 */
USART_Cmd(USART2, ENABLE);
void USART2_IRQHandler(void)
{
/* if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) //判断发生接收中断
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE); //清除中断标志
uart2_recv_fifo[uart2_recv_fifo_in++]=USART_ReceiveData(USART2);
if(uart2_recv_fifo_in>=UART2_FIFO_SIZE)uart2_recv_fifo_in=0;
}
*/
#if 1
//接收中断
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
if(((((uart2_recv_fifo_in + 1) == UART2_FIFO_SIZE) ? 0 : (uart2_recv_fifo_in + 1)) == uart2_recv_fifo_out))
{
USART_ReceiveData(USART2);
return;
}
/* Read one byte from the receive data register */
uart2_recv_fifo[uart2_recv_fifo_in++] = USART_ReceiveData(USART2);
if(uart2_recv_fifo_in == UART2_FIFO_SIZE)
{
uart2_recv_fifo_in=0;
}
}
#endif
//发送中断
if(USART_GetITStatus(USART2, USART_IT_TC) != RESET)
{
USART_ClearITPendingBit(USART2, USART_IT_TC);
uart2_send_fifo_out++;
if(uart2_send_fifo_out == UART2_FIFO_SIZE)
{
uart2_send_fifo_out = 0;
}
/* Write one byte to the transmit data register */
if(uart2_send_fifo_out != uart2_send_fifo_in)
{
USART_SendData(USART2, uart2_send_fifo[uart2_send_fifo_out]);
//USART_SendData(USART2,0x11);
}
else
{
uart2_status = 0;
USART_ITConfig(USART2, USART_IT_TC, DISABLE);
}
}
}
uint8 uart2_write_buf(uint8 *buf, uint16 length)
{
uint16 send_fifo_in_backup=uart2_send_fifo_in;
while(length--)
{
//测试缓冲区是否满
uint16 fifo_in_test=uart2_send_fifo_in;
fifo_in_test++;
if(fifo_in_test>=UART2_FIFO_SIZE)fifo_in_test=0;
if(fifo_in_test==uart2_send_fifo_out)
{
//缓冲区满恢复发送之前的指针
uart2_send_fifo_in=send_fifo_in_backup;
USART_ITConfig(USART2, USART_IT_TXE,ENABLE);
return 0;
}
//向FIFO填充数据
uart2_send_fifo[uart2_send_fifo_in++]=*buf++;
if(uart2_send_fifo_in>=UART2_FIFO_SIZE)uart2_send_fifo_in=0;
}
//开发送中断 利用中断发送 如果这时候硬件发送FIFO有空位置(UART2_SR&0x02) 就会进入中断自动发送
//UART2_MAX485_CTRL_REG|=(0x0001<<UART2_MAX485_CTRL_BIT);//MAX485发送状态
USART_ITConfig(USART2, USART_IT_TXE,ENABLE);
return 1;
}
uint8 uart2_read_char(uint8 *ch)
{
if(uart2_recv_fifo_out==uart2_recv_fifo_in)return 0;
*ch=uart2_recv_fifo[uart2_recv_fifo_out];//得到当前要读取的字符
uart2_recv_fifo_out++;//指向下一个要读取的字符
if(uart2_recv_fifo_out>=UART2_FIFO_SIZE) uart2_recv_fifo_out=0;//如果到了fifo末尾 则重从头开始
return 1;
}
|
|