注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
一二三亖的个人空间 https://home.eeworld.com.cn/space-uid-770570.html [收藏] [复制] [分享] [RSS]
日志

stm32双路USART,自己写的一个Tx函数

已有 1052 次阅读2017-8-2 14:40 |个人分类:stm32f103

由于printf只能被一个串口调用,所以就没有用它了,自己写了一个UART_out函数,就学学吧。



#include "uart.h"
#include "stdarg.h"
#include "stm32f10x_usart.h"


/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
uint8_t RxBuffer1[],rec_f,tx_flag;
uint8_t TxBuffer1[];
__IO uint8_t TxCounter1 = 0x00;//·¢Ëí»o3åÇø
__IO uint8_t RxCounter1 = 0x00;//½óêÕ»o3åÇø
__IO uint8_t TxCounter2 = 0x00;//·¢Ëí»o3åÇø
__IO uint8_t RxCounter2 = 0x00;//½óêÕ»o3åÇø  

uint32_t Rec_Len;

u8 USART1_RX_BUF[USART_REC_LEN]; 
u8 USART2_RX_BUF[USART_REC_LEN];
u16 USART_RX_STA=0;       //½óêÕ×′쬱ê¼Ç
/* Private function prototypes -----------------------------------------------*/




/****************************************************************************
* Ãû    3Æ£oUSART_Config(USART_TypeDef* USARTx)
* 1|    Äü£oÅäÖÃ′®¿ú
* èë¿ú2Îêy£o
* 3ö¿ú2Îêy£oÎT
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£oàyèç: USART_Config(USART1)
****************************************************************************/
void USART_Config(void)
{

  USART_InitTypeDef USART_InitStruct;
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();

  USART_InitStructure.USART_BaudRate = 9600; //ËùÂê115200bps
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy¾Yλ8λ
  USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£Ö1λ1λ
  USART_InitStructure.USART_Parity = USART_Parity_No; //ÎTD£Ñéλ
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //ÎTó2¼tá÷¿Ø
  USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //êÕ·¢Ä£ê½
  USART_Init(USART1, &USART_InitStructure); //ÅäÖÃ′®¿ú2Îêyoˉêy
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);                    //ê1Äü½óêÕÖD¶Ï
  USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //ê1Äü·¢Ëí»o3å¿ÕÖD¶Ï
  USART_Cmd(USART1, ENABLE);
   
   //′®2
  USART_InitStructure.USART_BaudRate = 9600; //ËùÂê115200bps
  USART_InitStructure.USART_WordLength = USART_WordLength_8b; //êy¾Yλ8λ
  USART_InitStructure.USART_StopBits = USART_StopBits_1; //í£Ö1λ1λ
  USART_InitStructure.USART_Parity = USART_Parity_No; //ÎTD£Ñéλ
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;   //ÎTó2¼tá÷¿Ø
  USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; //êÕ·¢Ä£ê½USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART2, &USART_InitStructure); //ÅäÖÃ′®¿ú2Îêyoˉêy
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);                    //ê1Äü½óêÕÖD¶Ï
  USART_ITConfig(USART2, USART_IT_TXE, ENABLE); //ê1Äü·¢Ëí»o3å¿ÕÖD¶Ï   
  USART_Cmd(USART2, ENABLE); //′ò¿a′®¿ú¶tÖD¶Ï
}
/****************************************************************************
* Ãû    3Æ£ovoid RCC_Configuration(void)
* 1|    Äü£oÏμí3ê±ÖóÅäÖÃÎa72MHZ£¬ íaéèê±ÖóÅäÖÃ
* èë¿ú2Îêy£oÎT
* 3ö¿ú2Îêy£oÎT
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£oÎT 
****************************************************************************/ 
void RCC_Configuration(void)
{
   SystemInit(); 
   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO , ENABLE);
   RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 , ENABLE);

   //′®2
   RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE);   

/****************************************************************************
* Ãû    3Æ£ovoid GPIO_Configuration(void)
* 1|    Äü£oí¨óÃIO¿úÅäÖÃ
* èë¿ú2Îêy£oÎT
* 3ö¿ú2Îêy£oÎT
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£o
****************************************************************************/  
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;     //LED1¿ØÖÆ--PB5
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //íÆíìêä3ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;         //USART1 TX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //¸′óÃíÆíìêä3ö
  GPIO_Init(GPIOA, &GPIO_InitStructure);     //A¶Ë¿ú 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;         //USART1 RX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //¸′óø¡¿Õêäèë
  GPIO_Init(GPIOA, &GPIO_InitStructure);         //A¶Ë¿ú
  
   //′®2
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;         //USART2 TX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;     //¸′óÃíÆíìêä3ö
  GPIO_Init(GPIOA, &GPIO_InitStructure);     //A¶Ë¿ú 

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;         //USART2 RX
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;   //¸′óÿa©êäèë
  GPIO_Init(GPIOA, &GPIO_InitStructure);         //A¶Ë¿ú 


/****************************************************************************
* Ãû    3Æ£ovoid NVIC_Configuration(void)
* 1|    Äü£oÖD¶ÏÔ′ÅäÖÃ
* èë¿ú2Îêy£oÎT
* 3ö¿ú2Îêy£oÎT
* Ëμ    Ã÷£o
* μ÷ó÷½·¨£oÎT 
****************************************************************************/
void NVIC_Configuration(void)
{
  /*  ½á11éùÃ÷*/
  NVIC_InitTypeDef NVIC_InitStructure;

  /* Configure the NVIC Preemption Priority Bits */  
  /* Configure one bit for preemption priority */
  /* óÅÏ輶×é ËμÃ÷áËÇàÕ¼óÅÏ輶ËùóÃμÄλêy£¬oíÏìó|óÅÏ輶ËùóÃμÄλêy   ÔúÕaàïêÇ0£¬ 4 
  0×é£o  ÇàÕ¼óÅÏ輶ռ0룬 Ïìó|óÅÏ輶ռ4λ
  1×é£o  ÇàÕ¼óÅÏ輶ռ1룬 Ïìó|óÅÏ輶ռ3λ
  2×é£o  ÇàÕ¼óÅÏ輶ռ2룬 Ïìó|óÅÏ輶ռ2λ
  3×é£o  ÇàÕ¼óÅÏ輶ռ3룬 Ïìó|óÅÏ輶ռ1λ
  4×é£o  ÇàÕ¼óÅÏ輶ռ4룬 Ïìó|óÅÏ輶ռ0λ  
  */       
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);    

  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;     //éèÖÃ′®¿ú1ÖD¶Ï
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;     //ÇàÕ¼óÅÏ輶 0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //×óóÅÏ輶Îa0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äü
  NVIC_Init(&NVIC_InitStructure);  
 
   //′®2
  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;     //éèÖÃ′®¿ú2ÖD¶Ï
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;     //ÇàÕ¼óÅÏ輶 0
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //×óóÅÏ輶Îa0
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //ê1Äü
  NVIC_Init(&NVIC_InitStructure);
}

/****************************************************************************
* Ãû    3Æ£ovoid USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...)
* 1|    Äü£o¸ñê½»ˉ′®¿úêä3öoˉêy
* èë¿ú2Îêy£oUSARTx:  Ö¸¶¨′®¿ú
Data£o   ·¢Ëíêy×é
...:     2»¶¨2Îêy
* 3ö¿ú2Îêy£oÎT
* Ëμ    Ã÷£o¸ñê½»ˉ′®¿úêä3öoˉêy
        "\r" »Ø3μ·û   USART_OUT(USART1, "abcdefg\r")   
"\n" »»DD·û   USART_OUT(USART1, "abcdefg\r\n")
"%s" ×Ö·û′®   USART_OUT(USART1, "×Ö·û′®êÇ£o%s","abcdefg")
"%d" ꮽøÖÆ   USART_OUT(USART1, "a=%d",10)
* μ÷ó÷½·¨£oÎT 
****************************************************************************/
void USART_OUT(USART_TypeDef* USARTx, uint8_t *Data,...){ 
const char *s;
    int d;
    char buf[16];
    va_list ap;
    va_start(ap, Data);

while(*Data!=0){                          //ÅD¶ÏêÇ·ñμ½′ï×Ö·û′®½áêø·û
if(*Data==0x5c){  //'\'
switch (*++Data){
case 'r':          //»Ø3μ·û
USART_SendData(USARTx, 0x0d);   

Data++;
break;
case 'n':          //»»DD·û
USART_SendData(USARTx, 0x0a);
Data++;
break;
default:
Data++;
   break;
}
 
}
else if(*Data=='%'){  //
switch (*++Data){
case 's':  //×Ö·û′®
                s = va_arg(ap, const char *);
                for ( ; *s; s++) {
                    USART_SendData(USARTx,*s);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
                }
Data++;
                break;
            case 'd':  //ꮽøÖÆ
                d = va_arg(ap, int);
                itoa(d, buf, 10);
                for (s = buf; *s; s++) {
                    USART_SendData(USARTx,*s);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
                }
Data++;
                break;
default:
Data++;
   break;
}  
}
else USART_SendData(USARTx, *Data++);
while(USART_GetFlagStatus(USARTx, USART_FLAG_TC)==RESET);
}
}

/******************************************************
ÕûDÎêy¾Y×a×Ö·û′®oˉêy
        char *itoa(int value, char *string, int radix)
radix=10 ±êê¾êÇ10½øÖÆ ·ÇꮽøÖÆ£¬×a»»½á1ûÎa0;  

   ày£od=-379;
Ö′DD itoa(d, buf, 10); oó
buf="-379"    
**********************************************************/
char *itoa(int value, char *string, int radix)
{
    int     i, d;
    int     flag = 0;
    char    *ptr = string;

    /* This implementation only works for decimal numbers. */
    if (radix != 10)
    {
        *ptr = 0;
        return string;
    }

    if (!value)
    {
        *ptr++ = 0x30;
        *ptr = 0;
        return string;
    }

    /* if this is a negative value insert the minus sign. */
    if (value < 0)
    {
        *ptr++ = '-';

        /* Make the value positive. */
        value *= -1;
    }

    for (i = 10000; i > 0; i /= 10)
    {
        d = value / i;

        if (d || flag)
        {
            *ptr++ = (char)(d + 0x30);
            value -= (d * i);
            flag = 1;
        }
    }
    *ptr = 0;

    return string;

void USART1_IRQHandler(void)                 //′®¿ú2ÖD¶Ï·tÎñ3ìDò
{
u8 Res;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)  //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART1);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË 
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART1_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ  
}  
}
}    
     }  
}
void USART2_IRQHandler(void)             
{
u8 Res;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)  //½óêÕÖD¶Ï(½óêÕμ½μÄêy¾Y±ØDëêÇ0x0d 0x0a½áÎ2)
{
Res =USART_ReceiveData(USART2);//(USART1->DR); //¶á衽óêÕμ½μÄêy¾Y
if((USART_RX_STA&0x8000)==0)//½óêÕÎ′íê3é
{
if(USART_RX_STA&0x4000)//½óêÕμ½áË0x0d
{
if(Res!=0x0a)USART_RX_STA=0;//½óêÕ′íÎó,ÖØD¿aê¼
else USART_RX_STA|=0x8000; //½óêÕíê3éáË 
}
else //»1ûêÕμ½0X0D
{
if(Res==0x0d)USART_RX_STA|=0x4000;
else
{
USART2_RX_BUF[USART_RX_STA&0X3FFF]=Res ;
USART_RX_STA++;
if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//½óêÕêy¾Y′íÎó,ÖØD¿aê¼½óêÕ  
}  
}
}    
     }  
}

评论 (0 个评论)

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章