STM32/STM8资源节约主义编程方式

news2024/12/24 11:30:07

STM32/STM8资源节约主义编程方式

在小资源芯片进行代码设计时,如STM32C0系列,STM8系列,因为官方库本身要包含各种场景应用特征的支持,所以会有一些冗余的代码占用更多FLASH空间。当需要实现资源占用最简化设计方式时,可以采用如下方式:

  1. 启动一个1ms周期定时器,每个中断进行32位计数值的累加计数
  2. 实现指令方式微秒延时函数,原理参考《STM32 HAL us delay(微秒延时)的指令延时实现方式及优化》,此方法适用于所有MCU的指令方式微秒延时实现,不限于STM32/STM8。
  3. 关闭1ms周期定时器。
  4. 用微秒延时函数进行各种接口GPIO时序模拟,如I2C, SPI等等。从而不需要用到官方接口库,实现最简化代码设计。一旦实现了微秒延时函数,用微秒延时函数设计实现的STM32接口协议函数,可以移过来用于STM8。
  5. 当需要实现某种更准确的时序要求时,可以将一个或多个TIM启动,配合微秒延时函数实现各种时序设计。

这里以STM8S103F3P6为例,介绍
*模拟UART TX发送的代码,不用到TIM资源
*模拟UART TX/RX环回测试代码,使用TIM资源

以及例程下载

STM8S103F3P6 UART TX ONLY模拟协议代码

基于微秒延时函数不采用定时器的UART时序模拟原理介绍参考:《STM32 GPIO模拟UART串口:最简延时方式》,这里的逻辑为:

  1. 用TIM4实现1ms周期计数
  2. 实现微秒延时函数
  3. 关闭TIM4
  4. 用微秒延时函数实现9600波特率UART的TX发送功能,循环发送出增加的数值
  5. 这个代码在微秒延时函数实现后关闭了1ms周期计数的TIM4,因此后续运行没有用到任何TIM,也没有用到其它接口库。
    完整的实现代码在两个文件内。这里PB5为LED管脚,PC7为UART TX管脚。
    stm8s_it.c里的代码,主要是实现1ms周期中断的计数功能:
/**
  ******************************************************************************
  * @file     stm8s_it.c
  * @author   MCD Application Team
  * @version  V2.1.0
  * @date     18-November-2011
  * @brief    Main Interrupt Service Routines.
  *           This file provides template for all peripherals interrupt service 
  *           routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s_it.h"
extern uint8_t i;
/** @addtogroup Template_Project
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

#ifdef _COSMIC_
/**
  * @brief Dummy Interrupt routine
  * @par Parameters:
  * None
  * @retval
  * None
*/
INTERRUPT_HANDLER(NonHandledInterrupt, 25)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*_COSMIC_*/

/**
  * @brief TRAP Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Top Level Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TLI_IRQHandler, 0)

{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Auto Wake Up Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(AWU_IRQHandler, 1)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Clock Controller Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(CLK_IRQHandler, 2)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTA Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTB Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTC Interrupt routine.
  * @param  None
  * @retval None
  */


INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
   
}

/**
  * @brief External Interrupt PORTD Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTE Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#ifdef STM8S903
/**
  * @brief External Interrupt PORTF Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S903*/

#if defined (STM8S208) || defined (STM8AF52Ax)
/**
  * @brief CAN RX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief CAN TX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S208 || STM8AF52Ax */

/**
  * @brief SPI Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(SPI_IRQHandler, 10)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Timer1 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#ifdef STM8S903
/**
  * @brief Timer5 Update/Overflow/Break/Trigger Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
 
/**
  * @brief Timer5 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
/**
  * @brief Timer2 Update/Overflow/Break Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief Timer2 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S903*/

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
    defined(STM8S005) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
/**
  * @brief Timer3 Update/Overflow/Break Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief Timer3 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
    defined(STM8S003) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
/**
  * @brief UART1 TX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART1 RX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax */

/**
  * @brief I2C Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#if defined(STM8S105) || defined(STM8S005) ||  defined (STM8AF626x)
/**
  * @brief UART2 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART2 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /* STM8S105 or STM8AF626x */

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief UART3 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART3 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief ADC2 interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(ADC2_IRQHandler, 22)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#else /*STM8S105, STM8S103 or STM8S903 or STM8AF626x */
/**
  * @brief ADC1 interrupt routine.
  * @par Parameters:
  * None
  * @retval 
  * None
  */
 INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#ifdef STM8S903
/**
  * @brief Timer6 Update/Overflow/Trigger Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x */
/**
  * @brief Timer4 Update/Overflow Interrupt routine.
  * @param  None
  * @retval None
  */

 __IO uint32_t uwTick = 0;
 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 
     uwTick++;   
     
     TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
 }
#endif /*STM8S903*/

/**
  * @brief Eeprom EEC Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

main.c里的代码:

/**
  ******************************************************************************
  * @file    Project/main.c 
  * @author  MCD Application Team
  * @version V2.1.0
  * @date    18-November-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stm8s_clk.h"

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Init_GPIO(void)
{
	    GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);//LED       
     
        
        GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST);//UART_TX
        GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);     //UART_RX    
        GPIO_WriteHigh(GPIOC, GPIO_PIN_7);
	
}
void Init_Timer4(void)
{

	TIM4_TimeBaseInit(TIM4_PRESCALER_128, 125);
    /* Clear TIM4 update flag */
    TIM4_ClearFlag(TIM4_FLAG_UPDATE);
	
	/* Enable update interrupt */
	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
	TIM4_Cmd(ENABLE);
}

/*Start of Pegasus Segment*/
extern  __IO uint32_t uwTick;

__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = uwTick+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = uwTick;
  PY_Delay_us_t(1000000) ;
  secondms = uwTick;

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}

void PY_Delay_us(uint32_t Delay)
{
  __IO uint32_t delayReg;

  __IO uint32_t msNum = Delay/1000;
  __IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);

  if(msNum>0) HAL_Delay(msNum);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}







#define GPIO_OUTPUT_EXEC_DELAY_us 40

#define BaudRate_us 104 //for 9600bps

#define RD (GPIO_ReadInputPin(GPIOC, GPIO_PIN_6)==GPIO_PIN_6)?1:0
#define TDL GPIO_WriteLow(GPIOC, GPIO_PIN_7)
#define TDH GPIO_WriteHigh(GPIOC, GPIO_PIN_7)

#define buff_len 128
__IO uint8_t tx_status = 0;
__IO uint8_t rx_status = 0;

__IO uint8_t txd[buff_len];
__IO uint8_t rxd[buff_len];

__IO uint32_t txd_index=0;
__IO uint32_t rxd_index=0;


__IO uint8_t bytebit=0;
__IO uint8_t bytebit_s=0;

__IO uint8_t data=0;
/*Enf of Pegasus Segment*/
void main(void)
{
  
    /* Config internal high speed clock as 16MHz main clock */ 
            //CLK_HSICmd(ENABLE);
            CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
            CLK_HSICmd(ENABLE);
            CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
	        CLK_ClockSwitchCmd(ENABLE);
	    
   
   
        Init_GPIO();
	    Init_Timer4();
	    enableInterrupts();
        
        for(uint32_t j=0; j<0xffff; j++) ;  //for clock stability
        
        PY_usDelayTest();
        PY_usDelayOptimize();  //get us delay function
        
        disableInterrupts();
        TIM4_Cmd(DISABLE);  //no use of TIM4 now
  
  
  while (1)
  {
    
    PY_Delay_us_t(1000000);
    
    GPIO_WriteReverse(GPIOB, GPIO_PIN_5); //Flash LED    
  
          tx_status = 1;
          data++;
          
	  switch(tx_status)
	  {

	  case 1:{//start bit
             bytebit = data;
             TDL;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             tx_status ++;
	         }
	  case 2:{//bit 1
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 3:{//bit 2
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 4:{//bit 3
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 5:{//bit 4
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 6:{//bit 5
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 7:{//bit 6
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 8:{//bit 7
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             bytebit >>= 1;
             tx_status ++;
	         }
	  case 9:{//bit 8
             if((bytebit&0x01)==0) TDL;
             else TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             tx_status ++;
	         }
	  case 10:{//stop bit
             TDH;
             PY_Delay_us_t(BaudRate_us-GPIO_OUTPUT_EXEC_DELAY_us);
             tx_status = 0;
             
             rx_status = 0;
	         }
	  default:break;
	  }

  
  }
  
}

#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(u8* file, u32 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

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/


代码下载后测试效果:
在这里插入图片描述

STM8S103F3P6 UART TX/RX 模拟协议代码及环回测试

基于采用定时器的UART时序模拟介绍参考:《STM32 GPIO模拟UART串口:外部时钟及TIM方式》,注意STM8应用场景主要采用内部时钟模式,所以STM8的内部时钟稳定度尚可,配合对接收时序采样时刻的调整,可以不采用外部时钟而实现9600波特率UART稳定收发。这里的逻辑为:

  1. 用TIM4实现1ms周期计数
  2. 实现微秒延时函数
  3. 关闭TIM4
  4. 重配TIM4和启用为52us周期中断(9600波特率对应104us位宽)
  5. 实现UART接收到发送的环回功能
    完整的实现代码在两个文件内。这里PB5为LED管脚,PC7为UART TX管脚,PC6为RX管脚。

stm8s_it.c里的代码:

/**
  ******************************************************************************
  * @file     stm8s_it.c
  * @author   MCD Application Team
  * @version  V2.1.0
  * @date     18-November-2011
  * @brief    Main Interrupt Service Routines.
  *           This file provides template for all peripherals interrupt service 
  *           routine.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s_it.h"
extern uint8_t i;
/** @addtogroup Template_Project
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

#ifdef _COSMIC_
/**
  * @brief Dummy Interrupt routine
  * @par Parameters:
  * None
  * @retval
  * None
*/
INTERRUPT_HANDLER(NonHandledInterrupt, 25)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}
#endif /*_COSMIC_*/

/**
  * @brief TRAP Interrupt routine
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER_TRAP(TRAP_IRQHandler)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Top Level Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TLI_IRQHandler, 0)

{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Auto Wake Up Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(AWU_IRQHandler, 1)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Clock Controller Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(CLK_IRQHandler, 2)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTA Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTA_IRQHandler, 3)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTB Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTB_IRQHandler, 4)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTC Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTC_IRQHandler, 5)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTD Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTD_IRQHandler, 6)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief External Interrupt PORTE Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EXTI_PORTE_IRQHandler, 7)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#ifdef STM8S903
/**
  * @brief External Interrupt PORTF Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(EXTI_PORTF_IRQHandler, 8)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S903*/

#if defined (STM8S208) || defined (STM8AF52Ax)
/**
  * @brief CAN RX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_RX_IRQHandler, 8)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief CAN TX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(CAN_TX_IRQHandler, 9)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S208 || STM8AF52Ax */

/**
  * @brief SPI Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(SPI_IRQHandler, 10)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Timer1 Update/Overflow/Trigger/Break Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_UPD_OVF_TRG_BRK_IRQHandler, 11)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @brief Timer1 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM1_CAP_COM_IRQHandler, 12)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#ifdef STM8S903
/**
  * @brief Timer5 Update/Overflow/Break/Trigger Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_UPD_OVF_BRK_TRG_IRQHandler, 13)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
 
/**
  * @brief Timer5 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM5_CAP_COM_IRQHandler, 14)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */
/**
  * @brief Timer2 Update/Overflow/Break Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_UPD_OVF_BRK_IRQHandler, 13)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief Timer2 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM2_CAP_COM_IRQHandler, 14)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S903*/

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
    defined(STM8S005) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8AF626x)
/**
  * @brief Timer3 Update/Overflow/Break Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_UPD_OVF_BRK_IRQHandler, 15)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }

/**
  * @brief Timer3 Capture/Compare Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(TIM3_CAP_COM_IRQHandler, 16)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#endif /*STM8S208, STM8S207 or STM8S105 or STM8AF62Ax or STM8AF52Ax or STM8AF626x */

#if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S103) || \
    defined(STM8S003) ||  defined (STM8AF62Ax) || defined (STM8AF52Ax) || defined (STM8S903)
/**
  * @brief UART1 TX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_TX_IRQHandler, 17)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART1 RX Interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART1_RX_IRQHandler, 18)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8S103 or STM8S903 or STM8AF62Ax or STM8AF52Ax */

/**
  * @brief I2C Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(I2C_IRQHandler, 19)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

#if defined(STM8S105) || defined(STM8S005) ||  defined (STM8AF626x)
/**
  * @brief UART2 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_TX_IRQHandler, 20)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART2 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART2_RX_IRQHandler, 21)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /* STM8S105 or STM8AF626x */

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief UART3 TX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_TX_IRQHandler, 20)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }

/**
  * @brief UART3 RX interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(UART3_RX_IRQHandler, 21)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#if defined(STM8S207) || defined(STM8S007) || defined(STM8S208) || defined (STM8AF52Ax) || defined (STM8AF62Ax)
/**
  * @brief ADC2 interrupt routine.
  * @param  None
  * @retval None
  */
 INTERRUPT_HANDLER(ADC2_IRQHandler, 22)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#else /*STM8S105, STM8S103 or STM8S903 or STM8AF626x */
/**
  * @brief ADC1 interrupt routine.
  * @par Parameters:
  * None
  * @retval 
  * None
  */
 INTERRUPT_HANDLER(ADC1_IRQHandler, 22)
 {
    /* In order to detect unexpected events during development,
       it is recommended to set a breakpoint on the following instruction.
    */
 }
#endif /*STM8S208 or STM8S207 or STM8AF52Ax or STM8AF62Ax */

#ifdef STM8S903
/**
  * @brief Timer6 Update/Overflow/Trigger Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(TIM6_UPD_OVF_TRG_IRQHandler, 23)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
 }
#else /*STM8S208, STM8S207, STM8S105 or STM8S103 or STM8AF52Ax or STM8AF62Ax or STM8AF626x */
/**
  * @brief Timer4 Update/Overflow Interrupt routine.
  * @param  None
  * @retval None
  */

 __IO uint32_t uwTick = 0;
extern __IO uint8_t Int_Type;


#define TDL GPIO_WriteLow(GPIOC, GPIO_PIN_7)
#define TDH GPIO_WriteHigh(GPIOC, GPIO_PIN_7)
#define RBIT (GPIO_ReadInputPin(GPIOC, GPIO_PIN_6)==GPIO_PIN_6)?1:0;


#define buff_len 128

uint8_t txd[buff_len];
uint8_t rxd[buff_len];

uint32_t txd_index=0;
uint32_t rxd_index=0;

#define sampling_timing_opt_us 1

uint8_t half_period_ctl = 0;
 INTERRUPT_HANDLER(TIM4_UPD_OVF_IRQHandler, 23)
 {
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
extern uint8_t bytebit;
extern uint8_t bytebit_s;
extern uint8_t tx_status;
extern uint8_t rx_status;


   if(Int_Type == 0)
   { 
     uwTick++;   
   }
   else
   {

 		  switch(rx_status)
		  {
		  case 0:{
                    	      bytebit = RBIT;
			      if(bytebit==0)
			      {
                                if(half_period_ctl==0) half_period_ctl = 1;
                                else 
                                {
                                  half_period_ctl = 0;
                                  rx_status = 1;
                                }
			    	  
			      }
				  break;
			     }
		  case 1:{//start bit
			      rx_status ++;
			      rxd[rxd_index] = 0;
			      break;
		         }
		  case 2:{//bit 1
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 4:{//bit 2
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 6:{//bit 3
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 8:{//bit 4
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 10:{//bit 5
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 12:{//bit 6
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 14:{//bit 7
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 16:{//bit 8
			     //PY_Delay_us_t(sampling_timing_opt_us);
	             bytebit = RBIT;
	             (bytebit==0)?(rxd[rxd_index]>>=1):(rxd[rxd_index]=(rxd[rxd_index]>>1)|0x80);
	             rx_status ++;
	             break;
		         }
		  case 18:{//stop bit
			     //PY_Delay_us_t(sampling_timing_opt_us);
			     bytebit = RBIT;
	             if(bytebit==1)
	             {
	            	 bytebit_s = rxd[rxd_index];
	                 tx_status = 1; //Sending enable for loop-back test

	            	 if(rxd_index==(buff_len-1)) rxd_index=0;
	            	 else rxd_index++;
	             }
	             rx_status = 0;
			     break;
		         }
		  default: {
			     rx_status++;
			     break;
		         }
		  }

		  switch(tx_status)
		  {
		  case 0:{
			      TDH;
			      break;
		          }
		  case 2:{//start bit
	              TDL;
	              tx_status ++;
			      break;
		         }
		  case 4:{//bit 1
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 6:{//bit 2
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 8:{//bit 3
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 10:{//bit 4
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 12:{//bit 5
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 14:{//bit 6
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 16:{//bit 7
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 18:{//bit 8
	             if((bytebit_s&0x01)==0) TDL;
	             else TDH;
	             bytebit_s >>= 1;
	             tx_status ++;
			      break;
		         }
		  case 20:{//stop bit
			      TDH;
			      tx_status ++;
			      break;
		         }
		  case 22:{//stop
			      tx_status = 0;
			      break;
		         }
		  default: {
			     tx_status++;
			     break;
		         }
		  } 
     
     
     
   }

	TIM4_ClearITPendingBit(TIM4_IT_UPDATE);
 }
#endif /*STM8S903*/

/**
  * @brief Eeprom EEC Interrupt routine.
  * @param  None
  * @retval None
  */
INTERRUPT_HANDLER(EEPROM_EEC_IRQHandler, 24)
{
  /* In order to detect unexpected events during development,
     it is recommended to set a breakpoint on the following instruction.
  */
}

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

main.c代码:

/**
  ******************************************************************************
  * @file    Project/main.c 
  * @author  MCD Application Team
  * @version V2.1.0
  * @date    18-November-2011
  * @brief   Main program body
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 


/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "stm8s_clk.h"

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/

/* Private defines -----------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
void Init_GPIO(void)
{
	    GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_FAST);//LED        

        
        GPIO_Init(GPIOC, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT);     //UART_RX
        GPIO_Init(GPIOC, GPIO_PIN_7, GPIO_MODE_OUT_PP_HIGH_FAST);//UART_TX
        GPIO_WriteHigh(GPIOC, GPIO_PIN_7);
	
}
void Init_Timer4(void)  //1ms period counter
{
	TIM4_TimeBaseInit(TIM4_PRESCALER_128, 125);
    /* Clear TIM4 update flag */
    TIM4_ClearFlag(TIM4_FLAG_UPDATE);
	
	/* Enable update interrupt */
	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
	TIM4_Cmd(ENABLE);
}


void Init_Timer4_plus(void) //for UART with BDS 9600
{
	TIM4_TimeBaseInit(TIM4_PRESCALER_16, 51);
    /* Clear TIM4 update flag */
    TIM4_ClearFlag(TIM4_FLAG_UPDATE);
	
	/* Enable update interrupt */
	TIM4_ITConfig(TIM4_IT_UPDATE, ENABLE);
	TIM4_Cmd(ENABLE);
}
/*Start of Pegasus Segment*/
extern  __IO uint32_t uwTick;

__IO float usDelayBase;
void PY_usDelayTest(void)
{
  __IO uint32_t firstms, secondms;
  __IO uint32_t counter = 0;

  firstms = uwTick+1;
  secondms = firstms+1;

  while(uwTick!=firstms) ;

  while(uwTick!=secondms) counter++;

  usDelayBase = ((float)counter)/1000;
}

void PY_Delay_us_t(uint32_t Delay)
{
  __IO uint32_t delayReg;
  __IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);

  delayReg = 0;
  while(delayReg!=usNum) delayReg++;
}

void PY_usDelayOptimize(void)
{
  __IO uint32_t firstms, secondms;
  __IO float coe = 1.0;

  firstms = uwTick;
  PY_Delay_us_t(1000000) ;
  secondms = uwTick;

  coe = ((float)1000)/(secondms-firstms);
  usDelayBase = coe*usDelayBase;
}



uint8_t bytebit=1;
uint8_t bytebit_s=1;
uint8_t tx_status = 0;
uint8_t rx_status = 0;


__IO uint8_t Int_Type = 0;

uint8_t td=0;

/*Enf of Pegasus Segment*/
void main(void)
{
  
        /* Config internal high speed clock as 16MHz main clock */ 
        //CLK_HSICmd(ENABLE);
        CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
        CLK_HSICmd(ENABLE);
        CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI, DISABLE, CLK_CURRENTCLOCKSTATE_DISABLE);
	    CLK_ClockSwitchCmd(ENABLE);
	    
   
   
        Init_GPIO();
        
	    Init_Timer4();
        Int_Type = 0;                        //1 ms counter mode
	    enableInterrupts();                  
        
        for(uint32_t j=0; j<0xffff; j++) ;  //for clock stability
        
        PY_usDelayTest();
        PY_usDelayOptimize();
        
        disableInterrupts() ;
        TIM4_Cmd(DISABLE);
  
        Int_Type = 1;                       //uart detection mode
        Init_Timer4_plus();        
        enableInterrupts();
  
  while (1)
  {
    
    PY_Delay_us_t(1000000);    
    GPIO_WriteReverse(GPIOB, GPIO_PIN_5);  
 
  }
  
}

#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(u8* file, u32 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

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/

代码下载后测试效果:
在这里插入图片描述

总结

实现最简化资源占用代码设计时,如果没有时序要求高的情况,可以用微秒延时函数模拟所有常规接口协议。如果有时序要求高的情况,可以用TIM配合实现所有常规接口协议,只用到TIM这一种官方库代码。如果MCU资源足够,用官方库代码可以减少一些模拟协议代码编程量,所以本篇内容的介绍,多用在资源少的场景。

例程下载

(IAR开发环境工程)STM8S103F3P6 UART TX ONLY模拟协议例程下载
(IAR开发环境工程)STM8S103F3P6 UART TX/RX 模拟协议及环回测试例程下载

–End–

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1323832.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android年份选择器(超简单-可直接复制使用)

效果图 思路 1、流程&#xff1a; 通过点击textview触发年份选择器dialog显示&#xff0c;选中年份后&#xff0c;更新到textview。 2、如何只显示年份&#xff1f; 隐藏月份和天数即可&#xff08;但仍需给一个初始化数据&#xff09;。 实现 1、直接新建一个工具类OnPickY…

实时天气预警信息API:全面提供各种天气灾害预警

前言 随着气候变化的不断加剧&#xff0c;天气灾害成为大家关注的焦点。人们对于天气信息的获取需求越来越大&#xff0c;特别是在天气灾害发生时&#xff0c;及时、准确的天气预警信息能够极大地帮助人们做好防范准备&#xff0c;减少灾害带来的损失。为了满足这一需求&#…

去掉乘法运算的加法移位神经网络架构

[CVPR 2020] AdderNet: Do We Really Need Multiplications in Deep Learning? 代码&#xff1a;https://github.com/huawei-noah/AdderNet/tree/master 核心贡献 用filter与input feature之间的L1-范数距离作为“卷积层”的输出为了提升模型性能&#xff0c;提出全精度梯度…

鸿蒙 - arkTs:快速开始

index.ets文件理解&#xff1a; 新建模拟器&#xff1a; 1. 找到并打开设备管理器 2. 点击新建模拟器 3. 选择硬件之后下一步 4. 选择系统镜像&#xff0c;没有安装的话需要先安装 5. 设置模拟设备名称并点击完成 6. 提示创建成功代表刚才创建的模拟设备可以进行使用了…

【实战】如何在Docker Image中轻松运行MySQL

定义 使用Docker运行MySQL有许多优势。它允许数据库程序和数据分离&#xff0c;增强了数据的安全性和可靠性。Docker Image的轻便性简化了MySQL的部署和迁移&#xff0c;而Docker的资源隔离功能确保了应用程序之间无冲突。结合中间件和容器化系统&#xff0c;Docker为MySQL提供…

CentOs7.x安装部署SeaTunnelWeb遇到的坑

CentOs7.x安装部署SeaTunnelWeb遇到的坑 文章目录 1. 环境2. SeaTunnel安装部署2.1下载安装包2.2 设置环境变量2.3 安装连接器插件2.4 拷贝jar包到lib下2.5 启动命令2.6 执行官方client提交任务demo 3. SeaTunnel-Web安装部署3.1 下载安装包3.2 初始化数据库脚本或修改配置appl…

Springboot数据校验与异常篇

一、异常处理 1.1Http状态码 HTTP状态码是指在HTTP通信过程中&#xff0c;服务器向客户端返回的响应状态。它通过3位数字构成&#xff0c;第一个数字定义了响应的类别&#xff0c;后两位数字没有具体分类作用。以下是常见的HTTP状态码及其含义&#xff1a; - 1xx&#xff08;信…

基于ssm同学录网站论文

同学录网站 摘要 本文介绍了同学录网站的开发全过程。通过分析企业对于同学录网站的需求&#xff0c;创建了一个计算机管理同学录网站的方案。文章介绍了同学录网站的系统分析部分&#xff0c;包括可行性分析等&#xff0c;系统设计部分主要介绍了系统功能设计和数据库设计。…

【学习笔记】部署yolov8到安卓手机

一、环境配置和源码安装 首先你需要配置好pytorch环境&#xff0c;本文不再详细阐述&#xff0c;若未配置好环境&#xff0c;可以参考我的另一篇博客&#xff1a;https://blog.csdn.net/liujiahao123987/article/details/128743017 yolov8的安装可参考&#xff1a;https://blog…

旅游景区项目信息化建设运营方案:PPT47页,附下载

关键词&#xff1a;智慧景区解决方案&#xff0c;智慧景区建设&#xff0c;智慧景区开发与管理&#xff0c;智慧景区建设的意义&#xff0c;智慧景区管理 一、旅游景区项目信息化建设背景 1、旅游业发展迅速&#xff1a;随着旅游业的不断发展&#xff0c;游客对旅游体验的需求…

php跨域检测类允许部分域名访问

参考gpt PHP跨域检测类是一种封装了跨域检测逻辑的PHP类。它可以用于在PHP应用程序中检测和处理跨域请求&#xff0c;以确保安全和正常的跨域通信。 一个典型的PHP跨域检测类通常会包含以下功能&#xff1a; 跨域请求检测&#xff1a;检查请求的来源域名是否在允许的域名列表…

Bugku- misc-神奇宝贝-WP

下载压缩包发现解压错误。010打开发现文件尾部是zip压缩包的文件结尾(504B)&#xff0c;将文件头改成zip的文件头&#xff0c;得出一张图片和一个压缩包 来经过结合题目和百度&#xff0c;终于搜到了&#xff0c;这么一个东西 对照得到whereisflag&#xff0c;拿去解压得到美…

Linux学习(1)——初识Linux

目录 一、Linux的哲学思想 1.1 基础知识 1.2 根目录下的文件夹 二、Shell 1、Shell的定义 2、Shell的作用 三、Linux命令行 1、Linux通用命令行使用格式 四、Linux命令的分类 1、内部命令和外部命令的理解 2、内部命令和外部命令的区别 3、命令的执行过程 五、编辑…

高通切换到Emergency Download:adb reboot edl

刷机 开机下adb reboot edl 切到QDloader 9008 点下载。 The command “adb reboot edl” is used to reboot an Android device into EDL (Emergency Download) mode using the Android Debug Bridge (ADB) tool. EDL mode is primarily used for low-level firmware flashing…

光模块市场分析与发展趋势预测

光模块是光通信领域的重要组成部分&#xff0c;随着数字经济&#xff0c;大数据&#xff0c;云计算&#xff0c;人工智能等行业的兴起&#xff0c;光模块市场经历了快速发展&#xff0c;逐渐在数据中心、无线回传、电信传输等应用场景中得到广泛应用。本文将基于当前光模块全球…

vm 位置修正

##为了让那个图片旋转时也能被识别到字母 &#xff1a;创建精度匹配位置修正 ##为了其他特征相似的图片能识别到其他的字母 &#xff1a;创建BLOB分析 不然到时后换张图&#xff0c;还是会定位在前一个标识点

NCV8460ADR2G在汽车和工业应用中高压侧驱动如何破?

NCV8460ADR2G是一款完全保护的高压侧驱动器&#xff0c;可用于开关各种负载&#xff0c;如灯泡、电磁阀和其他致动器。该器件可以通过有源电流限制和高温关断针对过载情况进行内部保护。 诊断状态输出引脚提供了高温以及开关状态开路负载情况的数字故障指示。 特性&#xff1a;…

数据库故障Waiting for table metadata lock

场景&#xff1a;早上来发现一个程序&#xff0c;链接mysql数据库有点问题&#xff0c;随后排查&#xff0c;因为容器在k8s里面。所以尝试重启了pod没有效果 一、重启pod: 这里是几种在Kubernetes中重启Pod的方法: 删除Pod,利用Deployment重建 kubectl delete pod mypodDepl…

IP地址定位如何助力反欺诈?

随着互联网的快速发展&#xff0c;网络欺诈行为也日益猖獗&#xff0c;给广大用户和企业带来了巨大的经济损失。为了应对这一挑战&#xff0c;IP地址定位技术逐渐成为反欺诈领域的重要手段。本文将介绍IP地址定位如何助力反欺诈&#xff0c;并探讨其技术与实践的结合。 一、IP地…

重磅荣誉 | 竹云荣登《2023胡润全球猎豹企业榜》

12月19日&#xff0c;“2023胡润全球猎豹企业大会”暨《2023胡润全球猎豹企业榜》发布活动在广州成功举办。大会邀请上百位嘉宾&#xff0c;其中包含猎豹企业创始人/CEO、猎豹企业投资机构代表、政府领导及知名专家学者等一同交流分享&#xff0c;聚焦猎豹企业和创业企业相关产…