打印
[开发工具]

【转】STM8L101F3P6查询发送中断接收

[复制链接]
818|6
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
一坨代码|  楼主 | 2016-10-16 14:51 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式


  • /******************************Copyright (c)***********************************/  
  • /*                                                                            */  
  • /*                             老李电子工作                                   */  
  • /*                                                                            */  
  • /*------------------------------File Info-------------------------------------*/  
  • /* File name:           main.c                                                */  
  • /* Last modified Date:  2014-06-23                                            */  
  • /* Last Version:        1.0                                                   */  
  • /* Descriptions:        STM8L103F3P6,内部时钟,16MHz,串口查询发送中断接收。  */  
  • /*                      本程序模拟一小段协议的解析,接收到报文0xc2,0x01,0x7b后*/  
  • /*                      返回报文0xd2,0x64,0x21                                */  
  • /*                      查询发送,中断接收                                    */  
  • /*                                                                            */  
  • /* 硬件连接:                                                                  */  
  • /*                      TX----PC3                                             */  
  • /*                      RX----PC2                                             */  
  • /*                                                                            */  
  • /*----------------------------------------------------------------------------*/  
  • /* Created by:          Li Xiang                                              */  
  • /* Created date:        2014-06-19                                            */  
  • /* Version:             1.0                                                   */  
  • /* Descriptions:        无                                                    */  
  • /*                                                                            */  
  • /******************************************************************************/  
  •   
  • /* Includes ------------------------------------------------------------------*/  
  • #include "stm8l10x.h"  
  • #include "stm8l10x_usart.h"  
  • /* Private typedef -----------------------------------------------------------*/  
  • /* Private define ------------------------------------------------------------*/  
  • #define POWER_BD            GPIO_Pin_0  
  • #define POWER_BT            GPIO_Pin_1  
  • #define MSEL                GPIO_Pin_2  
  • #define NRESET              GPIO_Pin_3  
  • #define BD_NRESET           GPIO_Pin_4  
  • #define RESETB              GPIO_Pin_5  
  • #define SCL2                GPIO_Pin_6  
  • #define SDA2                GPIO_Pin_7  
  •   
  • #define SDA                 GPIO_Pin_0  
  • #define SCL                 GPIO_Pin_1  
  • #define SCREEN_CTRL         GPIO_Pin_4  
  •   
  • #define POWER_WIFI          GPIO_Pin_0  
  • /* Private macro -------------------------------------------------------------*/  
  • /* Private variables ---------------------------------------------------------*/  
  • uint8_t RXBUF[64];  
  • uint8_t rxcounter=0;  
  • uint8_t rxflag=0;  
  • /* Private function prototypes -----------------------------------------------*/  
  • static void BoardInit(void);  
  • static void CLK_Init(void);  
  • static void GPIO_Init_my(void);  
  • static void USART_Config(void);  
  • void USART_SendByte(uint8_t data);  
  • void USART_SendString(uint8_t* Data,uint16_t len);  
  • void Delay_ms(uint32_t nCount);  
  • /* Private functions ---------------------------------------------------------*/  
  •   
  • /******************************************************************************/  
  • /* Function name:        main                                                 */  
  • /* Descriptions:        主函数                                                */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • void main(void)  
  • {  
  •     uint8_t i=0;  
  •       
  •     BoardInit();  
  •       
  •     enableInterrupts();      
  •       
  •     while (1){  
  •         if(rxflag==1){  
  •             switch(RXBUF[0]){  
  •                 case 0xc1:  
  •                     break;  
  •                 case 0xc2:  
  •                     if( RXBUF[1]==0x01 && RXBUF[2]==0x7b ){  
  •                         for(i=0;i<64;i++){  
  •                             RXBUF=0;  
  •                         }  
  •                         rxcounter=0;  
  •                         rxflag=0;  
  •                           
  •                         USART_SendByte(0xD2);  
  •                         USART_SendByte(0x64);  
  •                         USART_SendByte(0x21);  
  •                     }  
  •                     break;  
  •                 case 0xc3:  
  •                     break;  
  •             }  
  •         }  
  •     }  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        BoardInit                                            */  
  • /* Descriptions:        主函数                                                */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • static void BoardInit(void)  
  • {  
  •     CLK_Init();      
  •     GPIO_Init_my();  
  •     USART_Config();  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        CLK_Init                                             */  
  • /* Descriptions:        时钟初始化函数                                        */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • static void CLK_Init(void)  
  • {      
  •     CLK_DeInit();  
  •     CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        GPIO_Init_my                                         */  
  • /* Descriptions:        IO初始化函数                                          */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  


沙发
一坨代码|  楼主 | 2016-10-16 14:52 | 只看该作者
  • static void GPIO_Init_my(void)   
  • {  
  •     GPIO_Init(GPIOA,GPIO_Pin_2|GPIO_Pin_3,GPIO_Mode_Out_PP_Low_Slow);    //悬空未用  
  •       
  •     GPIO_Init(GPIOB,POWER_BD,GPIO_Mode_Out_PP_Low_Slow);                 //默认断电  
  •     GPIO_Init(GPIOB,POWER_BT,GPIO_Mode_Out_PP_Low_Slow);                 //取消未用  
  •     GPIO_Init(GPIOB,MSEL,GPIO_Mode_Out_PP_Low_Slow);                     //取消未用,Wifi模式选择  
  •     GPIO_Init(GPIOB,NRESET,GPIO_Mode_Out_PP_Low_Slow);                   //取消未用,Wifi复位      
  •     GPIO_Init(GPIOB,BD_NRESET,GPIO_Mode_Out_PP_Low_Slow);                //北斗复位信号,默认复位状态  
  •     GPIO_Init(GPIOB,RESETB,GPIO_Mode_Out_PP_Low_Slow);                   //取消未用,蓝牙复位  
  •     GPIO_Init(GPIOB,SDA2|SCL2,GPIO_Mode_Out_OD_HiZ_Slow);                //电池电量用  
  •               
  •     GPIO_Init(GPIOC,SDA|SCL,GPIO_Mode_Out_OD_HiZ_Slow);                  //温度传感器  
  •     GPIO_Init(GPIOC,GPIO_Pin_2,GPIO_Mode_In_PU_No_IT);                   //串口接收  
  •     GPIO_Init(GPIOC,GPIO_Pin_3,GPIO_Mode_Out_PP_High_Slow);              //串口发送  
  •       
  •     GPIO_Init(GPIOD,GPIO_Pin_All,GPIO_Mode_Out_PP_Low_Slow);             //取消未用,Wifi供电  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        USART_Config                                         */  
  • /* Descriptions:        串口初始化函数                                        */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • static void USART_Config(void)  
  • {  
  •     CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);      
  •       
  •     USART_DeInit();  
  •       
  •     USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,  
  •                                             USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));  
  •                                              
  •     USART_Cmd(ENABLE);      
  •       
  •     USART_ITConfig(USART_IT_TXE,DISABLE);//关闭串口发送中断  
  •     USART_ITConfig(USART_IT_TC,DISABLE);//关闭串口发送完中断  
  •     USART_ITConfig(USART_IT_RXNE,ENABLE);//打开串口接收中断  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        UART1_SendByte                                       */  
  • /* Descriptions:        发送单字节                                            */  
  • /* input parameters:    data:待发送数据                                       */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • void USART_SendByte(uint8_t data)  
  • {  
  •     USART_SendData8((unsigned char)data);  
  •     /* Loop until the end of transmission */  
  •     while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        UART1_SendString                                     */  
  • /* Descriptions:        发送字符串                                            */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • void USART_SendString(uint8_t* Data,uint16_t len)  
  • {  
  •   uint16_t i=0;  
  •   for(;i<len;i++)  
  •     USART_SendByte(Data);  
  •    
  • }  
  • /******************************************************************************/  
  • /* Function name:        Delay_ms                                             */  
  • /* Descriptions:        延时函数                                              */  
  • /* input parameters:    延时时间                                              */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • void Delay_ms(uint32_t nCount)  
  • {  
  •     uint16_t i=0,j=0;;  
  •     for(i=0;i<nCount;i++){  
  •             for(j=0;j<1100;j++){  
  •                     ;  
  •             }  
  •     }  
  • }  
  •   
  • #ifdef  USE_FULL_ASSERT  
  •   
  • /**
  •   * @brief  Reports the name of the source file and the source line number
  •   *   where the assert_param error has occurred.
  •   * @param file: pointer to the source file name
  •   * @param line: assert_param error line source number
  •   * @retval : None
  •   */  
  • void assert_failed(uint8_t* file, uint32_t line)  
  • {  
  •     /* User can add his own implementation to report the file name and line number,
  •        ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  
  •   
  •     /* Infinite loop */  
  •     while (1)  
  •     {  
  •     }  
  • }  
  • #endif  
  •    
  • /*********************************END OF FILE**********************************/  

使用特权

评论回复
板凳
一坨代码|  楼主 | 2016-10-16 14:53 | 只看该作者

STM8L101F3P6串口的查询接收

  • /******************************Copyright (c)***********************************/  
  • /*                                                                            */  
  • /*                            老李电子工作                                    */  
  • /*                                                                            */  
  • /*------------------------------File Info-------------------------------------*/  
  • /* File name:            main.c                                               */  
  • /* Last modified Date:   2014-06-19                                           */  
  • /* Last Version:         1.0                                                  */  
  • /* Descriptions:         STM8L103F3P6,内部时钟,16MHz,串口查询接收,接收1个字 */  
  • /*                       符                                                   */  
  • /*                                                                            */  
  • /* 硬件连接:                                                                  */  
  • /*                       TX----PC3                                            */  
  • /*                       RX----PC2                                            */  
  • /*                                                                            */  
  • /*----------------------------------------------------------------------------*/  
  • /* Created by:           Li Xiang                                             */  
  • /* Created date:         2014-06-19                                           */  
  • /* Version:              1.0                                                  */  
  • /* Descriptions:         无                                                   */  
  • /*                                                                            */  
  • /******************************************************************************/  
  •   
  • /* Includes ------------------------------------------------------------------*/  
  • #include "stm8l10x.h"  
  • #include "stm8l10x_usart.h"  
  • /* Private typedef -----------------------------------------------------------*/  
  • /* Private define ------------------------------------------------------------*/  
  • #define POWER_BD            GPIO_Pin_0  
  • #define POWER_BT            GPIO_Pin_1  
  • #define MSEL                GPIO_Pin_2  
  • #define NRESET              GPIO_Pin_3  
  • #define BD_NRESET           GPIO_Pin_4  
  • #define RESETB              GPIO_Pin_5  
  • #define SCL2                GPIO_Pin_6  
  • #define SDA2                GPIO_Pin_7  
  •   
  • #define SDA                 GPIO_Pin_0  
  • #define SCL                 GPIO_Pin_1  
  • #define SCREEN_CTRL         GPIO_Pin_4  
  •   
  • #define POWER_WIFI          GPIO_Pin_0  
  • /* Private macro -------------------------------------------------------------*/  
  • /* Private variables ---------------------------------------------------------*/  
  •   
  • /* Private function prototypes -----------------------------------------------*/  
  • static void BoardInit(void);  
  • static void CLK_Init(void);  
  • static void GPIO_Init_my(void);  
  • static void USART_Config(void);  
  • void USART_SendByte(uint8_t data);  
  • void USART_SendString(uint8_t* Data,uint16_t len);  
  • uint8_t USART_ReceiveByte(void);  
  • void Delay_ms(uint32_t nCount);  
  • /* Private functions ---------------------------------------------------------*/  
  •   
  • /******************************************************************************/  
  • /* Function name:        main                                                 */  
  • /* Descriptions:        主函数                                                */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • void main(void)  
  • {  
  •     volatile uint8_t ch;  
  •       
  •     BoardInit();  
  •   
  •     while (1){  
  •             ch=USART_ReceiveByte();  
  •             Delay_ms(1000);  
  •     }  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        BoardInit                                            */  
  • /* Descriptions:        主函数                                                */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • static void BoardInit(void)  
  • {  
  •     CLK_Init();      
  •     GPIO_Init_my();  
  •     USART_Config();  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        CLK_Init                                             */  
  • /* Descriptions:         时钟初始化函数                                       */  
  • /* input parameters:     无                                                   */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       无                                                   */  
  • /******************************************************************************/  
  • static void CLK_Init(void)  
  • {   
  •     CLK_DeInit();  
  •     CLK_MasterPrescalerConfig(CLK_MasterPrescaler_HSIDiv1);  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        GPIO_Init_my                                         */  
  • /* Descriptions:         IO初始化函数                                         */  
  • /* input parameters:     无                                                   */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       无                                                   */  
  • /******************************************************************************/  
  • static void GPIO_Init_my(void)   
  • {  
  •     GPIO_Init(GPIOA,GPIO_Pin_2|GPIO_Pin_3,GPIO_Mode_Out_PP_Low_Slow);    //悬空未用  
  •       
  •     GPIO_Init(GPIOB,POWER_BD,GPIO_Mode_Out_PP_Low_Slow);                 //默认断电  
  •     GPIO_Init(GPIOB,POWER_BT,GPIO_Mode_Out_PP_Low_Slow);                 //取消未用  
  •     GPIO_Init(GPIOB,MSEL,GPIO_Mode_Out_PP_Low_Slow);                     //取消未用,Wifi模式选择  
  •     GPIO_Init(GPIOB,NRESET,GPIO_Mode_Out_PP_Low_Slow);                   //取消未用,Wifi复位      
  •     GPIO_Init(GPIOB,BD_NRESET,GPIO_Mode_Out_PP_Low_Slow);                //北斗复位信号,默认复位状态  
  •     GPIO_Init(GPIOB,RESETB,GPIO_Mode_Out_PP_Low_Slow);                   //取消未用,蓝牙复位  
  •     GPIO_Init(GPIOB,SDA2|SCL2,GPIO_Mode_Out_OD_HiZ_Slow);                //电池电量用  
  •          
  •     GPIO_Init(GPIOC,SDA|SCL,GPIO_Mode_Out_OD_HiZ_Slow);                  //温度传感器  
  •     GPIO_Init(GPIOC,GPIO_Pin_2,GPIO_Mode_In_PU_No_IT);                   //串口接收  
  •     GPIO_Init(GPIOC,GPIO_Pin_3,GPIO_Mode_Out_PP_High_Slow);              //串口发送  
  •       
  •     GPIO_Init(GPIOD,GPIO_Pin_All,GPIO_Mode_Out_PP_Low_Slow);             //取消未用,Wifi供电  
  • }  

使用特权

评论回复
地板
一坨代码|  楼主 | 2016-10-16 14:55 | 只看该作者
  • /******************************************************************************/  
  • /* Function name:        USART_Config                                         */  
  • /* Descriptions:        串口初始化函数                                        */  
  • /* input parameters:    无                                                    */  
  • /* output parameters:   无                                                    */  
  • /* Returned value:      无                                                    */  
  • /******************************************************************************/  
  • static void USART_Config(void)  
  • {  
  •     CLK_PeripheralClockConfig(CLK_Peripheral_USART, ENABLE);      
  •       
  •     USART_DeInit();  
  •       
  •     USART_Init((uint32_t)9600, USART_WordLength_8D, USART_StopBits_1,  
  •                                             USART_Parity_No, (USART_Mode_TypeDef)(USART_Mode_Rx | USART_Mode_Tx));  
  •                                              
  •     USART_Cmd(ENABLE);      
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        UART1_SendByte                                       */  
  • /* Descriptions:         发送单字节                                           */  
  • /* input parameters:     data:待发送数据                                      */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       无                                                   */  
  • /******************************************************************************/  
  • void USART_SendByte(uint8_t data)  
  • {  
  • USART_SendData8((unsigned char)data);  
  • //查询发送寄存器空标志位,为1,说明寄存器的内容已经发送出去了!  
  • while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        UART1_SendString                                     */  
  • /* Descriptions:         发送字符串                                           */  
  • /* input parameters:     无                                                   */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       无                                                   */  
  • /******************************************************************************/  
  • void USART_SendString(uint8_t* Data,uint16_t len)  
  • {  
  • uint16_t i=0;  
  • for(;i<len;i++)  
  • USART_SendByte(Data);   
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        USART_ReceiveByte                                    */  
  • /* Descriptions:         接收一个字符                                         */  
  • /* input parameters:     无                                                   */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       接收到的字符                                         */  
  • /******************************************************************************/  
  • uint8_t USART_ReceiveByte(void)  
  • {  
  •     volatile uint8_t rxchar;   
  •     //查询接收寄存器非空标志位,为1,说明寄存器收到了数据,把寄存器的内容读走吧!  
  •     while ( USART_GetFlagStatus(USART_FLAG_RXNE) == RESET );  
  •     rxchar=USART_ReceiveData8();  
  •     return rxchar;  
  • }  
  •   
  • /******************************************************************************/  
  • /* Function name:        Delay_ms                                             */  
  • /* Descriptions:         延时函数                                             */  
  • /* input parameters:     延时时间                                             */  
  • /* output parameters:    无                                                   */  
  • /* Returned value:       无                                                   */  
  • /******************************************************************************/  
  • void Delay_ms(uint32_t nCount)  
  • {  
  •     uint16_t i=0u,j=0u;  
  •     for(i=0u;i<nCount;i++){  
  •             for(j=0u;j<1100u;j++){  
  •                     ;  
  •             }  
  •     }  
  • }  
  •   
  • #ifdef USE_FULL_ASSERT  
  •   
  • /**
  • * @brief Reports the name of the source file and the source line number
  • * where the assert_param error has occurred.
  • * @param file: pointer to the source file name
  • * @param line: assert_param error line source number
  • * @retval : None
  • */  
  • void assert_failed(uint8_t* file, uint32_t line)  
  • {  
  •     /* User can add his own implementation to report the file name and line number,
  •     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */  
  •       
  •     /* Infinite loop */  
  •     while (1)  
  •     {  
  •     }  
  • }  
  • #endif  
  •    
  • /*********************************END OF FILE**********************************/   

使用特权

评论回复
5
一坨代码|  楼主 | 2016-10-16 14:56 | 只看该作者
先说几句,一直都没有对STM8的串口应用进行总结,最近小结一下。STM8应用库来开发,在基本的串口的应用上,我自己的理解现在还很不到位。我本以为STM8的串口也会有查询发送、中断发送、查询接收、中断接收这样几种方法呢~!可事实上我现在对于中断发送这路模式还没太搞明白,与51单片机的串口工作还是有区别的。

STM8提供了发送中断和发送完中断,我自己的理解是,在发送时进入中断和发送一个字节完成时进行中断。这与我大脑中理解的串口中断还是不一样的,回头要继续把这里搞明白。

一般情况下,STM8的串口多数是用查询发送,中断接收,这样来做。使用串口前要如下几点:
1.配置TX和RX的GPIO管脚状态
2.初始化串口,这里要打开串口设备的时钟,配置串口工作模式,和关闭USART_IT_TXE、USART_IT_TC中断,打开USART_IT_RXNE中断。然后才可以正常使用串口。
3.发送一个字节要调用库函数USART_SendData8(),但调用之后一定要记着去查询标志位,编写USART_SendByte()如下



  • void USART_SendByte(uint8_t data)  
  • {  
  • <span style="white-space:pre">    </span>USART_SendData8((unsigned char)data);  
  •   <span style="white-space:pre">  </span>/* Loop until the end of transmission */  
  •   <span style="white-space:pre">  </span>while (USART_GetFlagStatus(USART_FLAG_TXE) == RESET);  
  • }  


注意看查询USART_FLAG_TXE标志,即这个标志为0时,一直等待,直到为该标志为1时,发送完成,结束这个函数。USART_FLAG_TXE标志是“发送数据寄存器空标志位”。

使用特权

评论回复
6
robter| | 2016-10-17 14:26 | 只看该作者
很好很详细的资料,值得学习

使用特权

评论回复
7
一坨代码|  楼主 | 2016-10-23 13:17 | 只看该作者
robter 发表于 2016-10-17 14:26
很好很详细的资料,值得学习

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

52

主题

109

帖子

2

粉丝