stm32F030串口接收與發送


void USART_Configuration(void)//串口初始化函數
  {  

        GPIO_InitTypeDef  GPIO_InitStructure;
        USART_InitTypeDef USART_InitStructure;
                NVIC_InitTypeDef NVIC_InitStructure;
        
        RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE );
                
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource14,GPIO_AF_1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource15,GPIO_AF_1);        
        /*
        *  USART1_TX -> PA14 , USART1_RX ->        PA15
        */                                
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;                 
        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);        
        
        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_HardwareFlowControl = USART_HardwareFlowControl_None;//設置流控制
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//設置工作模式
        USART_Init(USART2, &USART_InitStructure); //配置入結構體
                
                NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
                NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
                NVIC_Init(&NVIC_InitStructure);
                
                USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
                
        USART_Cmd(USART2, ENABLE);//使能串口1

    }
void UART_send_byte(uint8_t byte) //發送1字節數據
{
 while(!((USART2->ISR)&(1<<7)));
 USART2->TDR=byte;    
}        

void UART_Send(uint8_t *Buffer, uint32_t Length)
{
    while(Length != 0)
    {
        while(!((USART2->ISR)&(1<<7)));//等待發送完
        USART2->TDR= *Buffer;
        Buffer++;
        Length--;
    }
}

void UART_Rec(uint8_t *Buffer, uint32_t Length)
{
    if(USART2->ISR & (1<<5))
    {
        while(Length !=0)
        {
            while(!(USART2->ISR & (1<<5)));
            *Buffer = USART2->RDR;
            Buffer++;
          Length--;
        }
    }
}

uint8_t UART_Recive(void)
{    
    while(!(USART2->ISR & (1<<5)));//等待接收到數據
    return(USART2->RDR);             //讀出數據
}


PUTCHAR_PROTOTYPE 
{
/* 將Printf內容發往串口 */
  USART_SendData(USART2,(uint8_t)  ch);
  while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
    {}
 
  return (ch);
}
void USART2_IRQHandler(void)
{
    if(USART2->ISR & (1<<5))
    {
        //USART_ClearITPendingBit(USART1, USART_IT_RXNE);
        code[cnum] =  USART2->RDR;
        cnum++;
        if(cnum >= 4)
        {
            cnum = 0;
            cflg = 1;
        }    
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM