久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 997|回復: 0
收起左側

stm32 串口初始化

[復制鏈接]
ID:417369 發表于 2018-10-29 16:33 | 顯示全部樓層 |閱讀模式
#include "bsp_uart.h"
#include "stm32f4xx.h"
#include "stdio.h"
#include "bsp_delay.h"
#include "bsp_us100.h"
extern char RxCounter4,RxBuffer4[100];
u8 Timeout2;
//重新指向數據流   printf();---> cmd的界面
//                                                                 printf(); ---> usart1

//#pragma import(__use_no_semihosting)   

struct __FILE
{
        int handle;
};

FILE __stdout;  

void _sys_exit(int x)
{
        x = x;
}

int fputc(int ch, FILE *f)        //輸出重定向  printf
{      
        while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) !=SET );
        USART1->DR = (uint8_t)ch;
        return ch;
}

int fgetc(FILE *f)                                //輸入重定向  scanf
{
        while(USART_GetFlagStatus(USART1 , USART_FLAG_RXNE) !=SET );
        return (int)USART1->DR;
}


void UART1_Init(uint32_t baud)
{
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;
               
                RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
               
               
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 |GPIO_Pin_7;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStruct);

                GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);
                GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

                USART_InitStruct.USART_BaudRate = baud;  //波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
                USART_InitStruct.USART_Parity = USART_Parity_No;
                USART_InitStruct.USART_StopBits = USART_StopBits_1;
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;
    USART_Init(USART1, &USART_InitStruct);
               
               
               
                USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接受中斷
                USART_ClearITPendingBit(USART1, USART_FLAG_TC);
               
                NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(USART1, ENABLE);
}
       
uint8_t i=0;

void USART1_IRQHandler(void)     
{
        if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //接受中斷,獲取相應中斷狀態,返回值是SET,說明是串口發送完成中斷發生
       
        {
                        USART_ClearITPendingBit(USART1, USART_FLAG_TC);
                        USART_SendData(USART1, USART_ReceiveData(USART1));
                        while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) == RESET);
        }
}

void UART2_Init(uint32_t baud)
{
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;               
         
          RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
          RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2,  ENABLE);
               
          GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_5 |GPIO_Pin_6;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStruct);
               
               
                GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_USART2);
                GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART2);
         
                USART_InitStruct.USART_BaudRate = baud;  //波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無硬件數據流控制
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//設置收發模式
                USART_InitStruct.USART_Parity = USART_Parity_No;//無奇偶校驗位
                USART_InitStruct.USART_StopBits = USART_StopBits_1;//一個停止位
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
          USART_Init(USART2, &USART_InitStruct);
               
                USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
                USART_ClearITPendingBit(USART2, USART_FLAG_TC);
               
                NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(USART2,ENABLE);
               
}
void USART2_IRQHandler(void)
{
        if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //接受中斷,獲取相應中斷狀態,返回值是SET,說明是串口發送完成中斷發生
       
        {
                        USART_ClearITPendingBit(USART1, USART_FLAG_TC);
                        USART_SendData(USART1, USART_ReceiveData(USART1));
                        while(USART_GetFlagStatus(USART1 , USART_FLAG_TC) == RESET);
        }
}

void UART3_Init(uint32_t baud)
{
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;

                RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
          RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3,  ENABLE);
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 |GPIO_Pin_9;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStruct);
               
                GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_USART3);
                GPIO_PinAFConfig(GPIOB, GPIO_PinSource9, GPIO_AF_USART3);
               
                USART_InitStruct.USART_BaudRate = baud;  //波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //無硬件數據流控制
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//設置收發模式
                USART_InitStruct.USART_Parity = USART_Parity_No;//無奇偶校驗位
                USART_InitStruct.USART_StopBits = USART_StopBits_1;//一個停止位
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
          USART_Init(USART3, &USART_InitStruct);
               
                USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
                USART_ClearITPendingBit(USART3, USART_FLAG_TC);
               
                NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(USART3,ENABLE);
}

void USART3_IRQHandler()
{
        u8 temp;
        if(USART_GetITStatus(USART3, USART_FLAG_RXNE) == SET)
{
   temp = ( u8 )USART_ReceiveData(USART3);//(USART1->DR);//讀取接受到的數據
   USART_ClearFlag(USART3,USART_FLAG_RXNE);
         USART_SendData(USART3,temp);

        }
}


void UART4_Init(uint32_t baud)
{
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;
               
                RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
          RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4,ENABLE);
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10 |GPIO_Pin_11;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOC, &GPIO_InitStruct);
               
                GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4);
                GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4);
               
                USART_InitStruct.USART_BaudRate = baud;//波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //設置收發模式
                USART_InitStruct.USART_Parity = USART_Parity_No; //無奇偶校驗位
                USART_InitStruct.USART_StopBits = USART_StopBits_1;//一個停止位
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
                USART_Init(UART4,&USART_InitStruct);
               
                USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
                USART_ClearITPendingBit(UART4, USART_FLAG_TC);
               
                NVIC_InitStruct.NVIC_IRQChannel = UART4_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(UART4,ENABLE);
}



void UART4_IRQHandler()
{
  char a[100];  
        int distance = 0;
        if(USART_GetITStatus(UART4, USART_IT_RXNE) == SET)
        {
                int i;
                UART4_send_byte(0x55);
                for(i=0;i<100;i++)
                {
                a[i] = USART_ReceiveData(UART4);       
                i++;
                distance = (a[0] << 8 ) | a[1];
                }
        }
       
}


void UART5_Init(uint32_t baud)
{
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;
               
                RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOD, ENABLE);
                RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
          RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5,ENABLE);
               
                GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_UART5);
                GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_UART5);
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOC,&GPIO_InitStruct);
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOD,&GPIO_InitStruct);
               
                USART_InitStruct.USART_BaudRate = baud;//波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //設置收發模式
                USART_InitStruct.USART_Parity = USART_Parity_No; //無奇偶校驗位
                USART_InitStruct.USART_StopBits = USART_StopBits_1;//一個停止位
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
                USART_Init(UART5,&USART_InitStruct);
               
                USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
                USART_ClearITPendingBit(UART5, USART_FLAG_TC);
       
                NVIC_InitStruct.NVIC_IRQChannel = UART5_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(UART5,ENABLE);

                }
void UART5_IRQHandler()
{
        u8 temp;
        if(USART_GetITStatus(UART5, USART_FLAG_RXNE) == SET)
{
   temp = ( u8 )USART_ReceiveData(UART4);//(USART1->DR);//讀取接受到的數據
   USART_ClearFlag(UART5,USART_FLAG_RXNE);
         USART_SendData(UART5,temp);

        }
}
  void UART6_Init(uint32_t baud)
        {
                GPIO_InitTypeDef GPIO_InitStruct;
          USART_InitTypeDef USART_InitStruct;
                NVIC_InitTypeDef NVIC_InitStruct;
               
                RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE);
          RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART6, ENABLE);
               
                GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_USART6); //GPIOG復用為USART
          GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_USART6);
               
                GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
                GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
                GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
                GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
                GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOC,&GPIO_InitStruct);
               
                USART_InitStruct.USART_BaudRate = baud;//波特率
                USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無硬件數據流控制
                USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //設置收發模式
                USART_InitStruct.USART_Parity = USART_Parity_No; //無奇偶校驗位
                USART_InitStruct.USART_StopBits = USART_StopBits_1;//一個停止位
                USART_InitStruct.USART_WordLength = USART_WordLength_8b;//字長為8位數據格式
                USART_Init(USART6,&USART_InitStruct);
               
                USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
                USART_ClearITPendingBit(USART6, USART_FLAG_TC);
               
                NVIC_InitStruct.NVIC_IRQChannel = USART6_IRQn;
                NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
                NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 4;
                NVIC_InitStruct.NVIC_IRQChannelSubPriority = 3;
                NVIC_Init(&NVIC_InitStruct);
               
                USART_Cmd(USART6,ENABLE);
               
               
        }

void USART6_IRQHandler()
{
        u8 temp;
        if(USART_GetITStatus(USART6, USART_FLAG_RXNE) == SET)
{
   temp = ( u8 )USART_ReceiveData(USART6);//(USART1->DR);//讀取接受到的數據
   USART_ClearFlag(USART6,USART_FLAG_RXNE); //清除標志
         USART_SendData(USART6,temp);  //發送數據

        }
}


回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 伊人性伊人情综合网 | 亚洲在线免费 | 99精品国产一区二区青青牛奶 | 成人av激情| 成人在线一区二区三区 | 成人在线精品 | 久久精品亚洲精品国产欧美kt∨ | 国产婷婷精品 | 夜夜爽99久久国产综合精品女不卡 | 中文字幕在线人 | 特级毛片爽www免费版 | 国产精品一区二区视频 | 激情伊人网 | 国产成人99久久亚洲综合精品 | 久久综合九九 | 国产粉嫩尤物极品99综合精品 | 久久精品一区 | 9久久精品 | 在线视频中文字幕 | 国产一级片在线观看视频 | 在线观看的av | 91偷拍精品一区二区三区 | 成人在线视频一区二区三区 | 性色av网站 | 国产精品欧美一区二区 | 国产清纯白嫩初高生在线播放视频 | 在线观看国产视频 | 国产日韩中文字幕 | 亚洲精品成人av | 黄篇网址 | 欧美久久久久久久久 | 国产精品成人一区二区 | 国产999精品久久久久久 | 国产一区二区观看 | 国产精品久久国产精品99 | 亚洲 欧美 综合 | 欧美激情国产日韩精品一区18 | 成人一区在线观看 | 久久在看| 二区中文字幕 | 免费视频成人国产精品网站 |