usart.c(参考匿名的)

来源:互联网 发布:cetv4 网络回看 编辑:程序博客网 时间:2024/06/03 21:39
/*此文件由HC05和Uart1调用*/#include "stm32f10x.h"    #include "usart.h"#define BYTE0(dwTemp)       (*(char *)(&dwTemp))#define BYTE1(dwTemp)       (*((char *)(&dwTemp) + 1))#define BYTE2(dwTemp)       (*((char *)(&dwTemp) + 2))#define BYTE3(dwTemp)       (*((char *)(&dwTemp) + 3))typedef union {unsigned char byte[4];float num;}t_floattobyte;t_floattobyte floattobyte;void Uart1_Init(u32 br_num)//时钟低电平活动,禁用了同步通讯,因此只有Uart{    USART_InitTypeDef USART_InitStructure;    USART_ClockInitTypeDef USART_ClockInitStruct;    GPIO_InitTypeDef GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); //开启USART1时钟    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);        //配置PA9作为USART1 Tx    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9;    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;    GPIO_Init(GPIOA , &GPIO_InitStructure);    //配置PA10作为USART1 Rx    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_10;    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;    GPIO_Init(GPIOA , &GPIO_InitStructure);    //配置USART1    //中断被屏蔽了    USART_InitStructure.USART_BaudRate = br_num;       //波特率可以通过地面站配置    USART_InitStructure.USART_WordLength = USART_WordLength_8b;  //8位数据    USART_InitStructure.USART_StopBits = USART_StopBits_1;   //在帧结尾传输1个停止位    USART_InitStructure.USART_Parity = USART_Parity_No;    //禁用奇偶校验    USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //硬件流控制失能    USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;  //发送、接收使能    //配置USART1时钟    USART_ClockInitStruct.USART_Clock = USART_Clock_Disable;  //时钟低电平活动    USART_ClockInitStruct.USART_CPOL = USART_CPOL_Low;  //SLCK引脚上时钟输出的极性->低电平    USART_ClockInitStruct.USART_CPHA = USART_CPHA_2Edge;  //时钟第二个边沿进行数据捕获    USART_ClockInitStruct.USART_LastBit = USART_LastBit_Disable; //最后一位数据的时钟脉冲不从SCLK输出    USART_Init(USART1, &USART_InitStructure);    USART_ClockInit(USART1, &USART_ClockInitStruct);    //使能USART1接收中断    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);    //使能USART1    USART_Cmd(USART1, ENABLE); }uint8_t TxBuffer[0xff];u8 TxCounter=0;u8 count=; u8 Rx_Buf[2][32];   //两个32字节的串口接收缓存u8 Rx_Act=0;        //正在使用的buf号u8 Rx_Adr=0;        //正在接收第几字节u8 Rx_Ok0 = 0;u8 Rx_Ok1 = 0;void Uart1_IRQ(void)//与PC上位机的通讯{    if(USART1->SR & USART_IT_ORE)//有数据过载了,赶进读    {        USART1->SR;//没什么用    }               /*******************发送中断***********************/    if((USART1->SR & (1<<7))&&(USART1->CR1 & USART_CR1_TXEIE))//if(USART_GetITStatus(USART1,USART_IT_TXE)!=RESET)    {                                                          //如果打开了写中断使能        USART1->DR = TxBuffer[TxCounter++]; //写数据到DR                  if(TxCounter == count)//如果TxCounter达到了最大量count        {            USART1->CR1 &= ~USART_CR1_TXEIE;       //关闭TXE写入中断            //USART_ITConfig(USART1,USART_IT_TXE,DISABLE);        }    }              /****************接收中断 (接收寄存器非空)************/     if(USART1->SR & (1<<5))//if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)        {                       //接收中断为1代表接收数据寄存器已满        u8 com_data = USART1->DR;//赶进读        //寻找接收的数据中帧头0XAAAF            if(Rx_Adr==0)//检验Rx_Buf[Rx_Act][0,1,2,3]                        {            //对应0xAA,AF,FUN,LEN                    if(com_data==0xAA)                  {                    Rx_Buf[Rx_Act][0] = com_data;                    Rx_Adr = 1;                }            }            else if(Rx_Adr==1)            {                if(com_data==0xAF)                  {                    Rx_Buf[Rx_Act][1] = com_data;                    Rx_Adr = 2;                }                else                    Rx_Adr = 0;            }            else if(Rx_Adr==2)      //FUN            {                Rx_Buf[Rx_Act][2] = com_data;                Rx_Adr = 3;            }            else if(Rx_Adr==3)      //LEN            {                Rx_Buf[Rx_Act][3] = com_data;                Rx_Adr = 4;            }            else            {                Rx_Buf[Rx_Act][Rx_Adr] = com_data;                Rx_Adr ++;            }                                          if(Rx_Adr==Rx_Buf[Rx_Act][3]+5)//如果adr等于了前面接收到的LEN            {                              //那么这次act=0的数据接收完成,切换缓存到act=1;                Rx_Adr = 0;                if(Rx_Act)                  {                     Rx_Act = 0;             //切换缓存                    Rx_Ok1 = 1;                }                else                                {                    Rx_Act = 1;                    Rx_Ok0 = 1;                }        }    }}/**********************************串口接收的缓存数据分析*********************************/void Uart_DataAnl(u8 buf_num)       //{    if((Rx_Buf[buf_num][1]==0xAA)&&(Rx_Buf[buf_num][2]==0xAF))//如果串口收到的是AA,AF                                                                 //是PC上位机发送给飞控的数据    {    }}void Uart_CheckEvent(void){    if(Rx_Ok0)    {        Rx_Ok0 = 0;        Uart_DataAnl(0);    }    if(Rx_Ok1)    {        Rx_Ok1 = 0;        Uart_DataAnl(1);    }}/******************************************************************************************//**************************实现函数***************************************************************************************************************************/uint8_t Uart1_Put_Char(unsigned char DataToSend){    TxBuffer[count++] = DataToSend;    USART_ITConfig(USART1, USART_IT_TXE, ENABLE);     return DataToSend;}uint8_t Uart1_Put_Int16(uint16_t DataToSend){    uint8_t sum = 0;    TxBuffer[count++] = BYTE1(DataToSend);    TxBuffer[count++] = BYTE0(DataToSend);  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);    sum += BYTE1(DataToSend);    sum += BYTE0(DataToSend);    return sum;}uint8_t Uart1_Put_Float(float DataToSend){    uint8_t sum = 0;    floattobyte.num=DataToSend;    TxBuffer[count++] = floattobyte.byte[3];      TxBuffer[count++] = floattobyte.byte[2];      TxBuffer[count++] = floattobyte.byte[1];      TxBuffer[count++] = floattobyte.byte[0];      USART_ITConfig(USART1, USART_IT_TXE, ENABLE);    sum += BYTE3(DataToSend);    sum += BYTE2(DataToSend);    sum += BYTE1(DataToSend);    sum += BYTE0(DataToSend);    return sum; }void Uart1_Put_String(unsigned char *Str){    //判断Str指向的数据是否有效.    while(*Str)    {    //是否是回车字符 如果是,则发送相应的回车 0x0d 0x0a    if(*Str=='\r')Uart1_Put_Char(0x0d);        else if(*Str=='\n')Uart1_Put_Char(0x0a);            else Uart1_Put_Char(*Str);    //指针++ 指向下一个字节.    Str++;    }}
原创粉丝点击