您的位置:首页 > 编程语言

串口配置的寄存器被写成一个结构体的代码

2017-11-07 10:31 323 查看
typedef struct

{

  uint32_t USART_BaudRate;    //串口波特率

  uint32_t USART_WordLength; //数据位宽

  uint32_t USART_StopBits;    //停止位宽                                                

  uint32_t USART_Parity;      //效验位宽   

  uint32_t USART_Mode;         //工作模式      

  uint32_t USART_HardwareFlowControl; //硬件流控制

} USART_InitTypeDef;

开发板与PC通信程序:

#include \"stm32f0xx.h\"

#include \"stdio.h\"

#define count(a) (sizeof(a)/sizeof(*(a)))

uint8_t Tx_Buffer[] = \"EEWORLD-[晒心得]STM32F03--USART实验\";

/*串口初始化配置 */

void USART_Config(void)

{

        USART_InitTypeDef USART_InitStructure;

        /*初始化USART1时钟*/

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

        USART_InitStructure.USART_BaudRate = 9600;//设置串口波特率

        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//设置流控制

        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//设置工作模式

        USART_InitStructure.USART_Parity = USART_Parity_No;//设置效验位

        USART_InitStructure.USART_StopBits = USART_StopBits_1;//设置停止位

        USART_InitStructure.USART_WordLength = USART_WordLength_8b;//设置数据位

        USART_Init(USART1, &USART_InitStructure);

        USART_Cmd(USART1, ENABLE);//使能串口 1

}

/*GPIOA初始化配置 */

void GPIOA_Config()

{

        GPIO_InitTypeDef GPIO_InitStructure;

        /*初始化GPIOA时钟*/

        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);

        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);

        /* 配置PA9 ,PA10*/

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;

        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_Level_3;

        GPIO_Init(GPIOA, &GPIO_InitStructure);

}

/*发送 1 字节数据*/

void UART_send_byte(uint8_t byte)

{

        while(!((USART1->ISR)&(1<<7)));//等待发送完

        USART1->TDR=byte; //发送一个字节

}

/*发送字符串*/

void UART_Send(uint8_t *Buffer, uint32_t Length)

{

        while(Length != 0)

        {

                while(!((USART1->ISR)&(1<<7)));//等待发送完

                USART1->TDR= *Buffer;

                Buffer++;

                Length--;

        }

}

uint8_t UART_Recive(void)

{

        while(!(USART1->ISR & (1<<5)));

        return(USART1->RDR);

}

void Delay(uint32_t temp)

{

        for(; temp!= 0; temp--);

}

int main(void)

{

        GPIOA_Config();

        USART_Config();

    while(1)

    {

            UART_Se
9e38
nd( Tx_Buffer, count(Tx_Buffer)-1);

            //USART_SendData(USART1, 0xff);

            Delay(500000);

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐