C语言基于数组的简单FIFO

目录

 

 

.c文件

.h文件


 

.c文件

/**
******************************************************************************
  * Copyright (C),  
  * @file    	Bsp_adc.c	  
  * @brief   
  *
  * @version           @date	@author       @explain
  * V1.0   	       2020-07-01  LoveMyBaoBaoGao    创建该文件
  *	
******************************************************************************
  * @attention
  
******************************************************************************
**/

/* Includes ------------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/

Fifo_Sturct  test = FIFO_INIT;     //电压采样

/****************************************************************************                  
* @file    void Fifo_Reset(void)
* @brief    将Fifo数据清零
* @explain  
* @parameter    无
* @return   最大值位置                     
****************************************************************************/
void Fifo_Reset(void)
{
    test.Fifo_Write = 0;
    test.Fifo_Read = 0;
    test.Fifo_Write = 0;
}
/****************************************************************************                  
* @file    static void Fifo_Data_Write(u16 _write_data)
* @brief    将数据写入Fifo中
* @explain  
* @parameter    _write_data:需要写入的数据
* @return   最大值位置                     
****************************************************************************/
static void Fifo_Data_Write(u16 _write_data)
{
    test.Fifo_Buf[test.Fifo_Write] = _write_data;
    if(++test.Fifo_Write>=FIFO_SIZE)
    {
        test.Fifo_Write=0;
    }
    if(test.Fifo_Count<FIFO_SIZE)
    {
        test.Fifo_Count++;
    }
}
/****************************************************************************                  
* @file   static u8 Fifo_Data_Read(u16 *_pByte)
* @brief    取最大值的位置
* @explain  
* @parameter    _pByte:需要写入的指针
* @return   0:失败  1:成功                     
****************************************************************************/
static u8 Fifo_Data_Read(u16 *_pByte)
{
    u8 us_count = 0;
    
    us_count = test.Fifo_Count;
    if(us_count == 0)
    {
         return 0;       
    }
    else
    {
        *_pByte = test.Fifo_Buf[test.Fifo_Read];		/* 从串口接收FIFO取1个数据 */

    	if (++test.Fifo_Read >=FIFO_SIZE)
    	{
    		test.Fifo_Read = 0;
    	}
    	test.Fifo_Count--;

        return 1;
    }

}

void Fifo_test(void)
{
    u16 new_adc_value = 0;

    Fifo_Data_Write(0xA1);//写入数据
    Fifo_Data_Write(0xA2);//写入数据
    Fifo_Data_Write(0xA3);//写入数据
    Fifo_Data_Write(0xA4);//写入数据
    Fifo_Data_Write(0xA5);//写入数据

    
    while (Fifo_Data_Read(&(new_adc_value));//读取数据,直到没有数据读出时
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

 

 

.h文件

/**
******************************************************************************
  * Copyright (C),  simcotech
  * @file    	Bsp_adc.h	  
  * @brief   
  *
  * @version           @date	@author       @explain
  * V1.0   	       2020-07-01  LoveMyBaoBaoGao    创建该文件
  *	
******************************************************************************
  * @attention
  
******************************************************************************
**/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _FIFO_H
#define _FIFO_H

/* Includes ------------------------------------------------------------------*/
/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
#define FIFO_SIZE 30

/* Exported types ------------------------------------------------------------*/
typedef struct
{	
    u16 Fifo_Buf[FIFO_SIZE];//数据buf
    u8 Fifo_Write;//写入fifo
    u8 Fifo_Count;//fifo计数
    u8 Fifo_Read;//fifo读取
}Fifo_Sturct;

    /* Exported constants --------------------------------------------------------*/
#define FIFO_INIT \
{\
{0,0,0},\
0,\
0,\
0,\
0,\
} 

/* Exported macro ------------------------------------------------------------*/
extern Fifo_Sturct Test;


#endif /* _FIFO_H */
/******************* (C) COPYRIGHT Simcotech *****END OF FILE****/

 

经过实验验证,发现上面的代码有一些缺陷。当将fifo写入函数放在串口中断中进行数据写入时,如果此时主循环中正在读取fifo中的数据时,Fifo_Count变量有可能会被同时访问,造成该fifo读取数据出错,为了防止该问题发生,现将fifo改为如下代码,实测不再会出现问题。

/**
******************************************************************************
  * Copyright (C),  
  * @file    	Bsp_adc.c	  
  * @brief   
  *
  * @version           @date	@author       @explain
  * V1.0   	       2020-07-01  LoveMyBaoBaoGao    创建该文件
  *	
******************************************************************************
  * @attention
  
******************************************************************************
**/

/* Includes ------------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/

Fifo_Sturct  test = FIFO_INIT;     //电压采样

/****************************************************************************                  
* @file    void Fifo_Reset(void)
* @brief    将Fifo数据清零
* @explain  
* @parameter    无
* @return   最大值位置                     
****************************************************************************/
void Fifo_Reset(void)
{
    test.Fifo_Write = 0;
    test.Fifo_Read = 0;
}
/****************************************************************************                  
* @file    static void Fifo_Data_Write(u16 _write_data)
* @brief    将数据写入Fifo中
* @explain  
* @parameter    _write_data:需要写入的数据
* @return   最大值位置                     
****************************************************************************/
static void Fifo_Data_Write(u16 _write_data)
{
    test.Fifo_Buf[test.Fifo_Write] = _write_data;
    if(++test.Fifo_Write>=FIFO_SIZE)
    {
        test.Fifo_Write=0;
    }
}
/****************************************************************************                  
* @file   static u8 Fifo_Data_Read(u16 *_pByte)
* @brief    取最大值的位置
* @explain  
* @parameter    _pByte:需要写入的指针
* @return   0:失败  1:成功                     
****************************************************************************/
static u8 Fifo_Data_Read(u16 *_pByte)
{
    u8 us_count = 0;
    
    us_count = test.Fifo_Count;
    if(test.Fifo_Read ==  test.Fifo_Write)
    {
         return 0;       
    }
    else
    {
        *_pByte = test.Fifo_Buf[test.Fifo_Read];		/* 从串口接收FIFO取1个数据 */

    	if (++test.Fifo_Read >=FIFO_SIZE)
    	{
    		test.Fifo_Read = 0;
    	}
        return 1;
    }

}

void Fifo_test(void)
{
    u16 new_adc_value = 0;

    Fifo_Data_Write(0xA1);//写入数据
    Fifo_Data_Write(0xA2);//写入数据
    Fifo_Data_Write(0xA3);//写入数据
    Fifo_Data_Write(0xA4);//写入数据
    Fifo_Data_Write(0xA5);//写入数据

    
    while (Fifo_Data_Read(&(new_adc_value));//读取数据,直到没有数据读出时
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

/**
******************************************************************************
  * Copyright (C),  simcotech
  * @file    	Bsp_adc.h	  
  * @brief   
  *
  * @version           @date	@author       @explain
  * V1.0   	       2020-07-01  LoveMyBaoBaoGao    创建该文件
  *	
******************************************************************************
  * @attention
  
******************************************************************************
**/
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef _FIFO_H
#define _FIFO_H

/* Includes ------------------------------------------------------------------*/
#include "Bsp_HeadFile.h"
#include "stm32f0xx.h"
#include "main.h"
/* Exported types ------------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
#define FIFO_SIZE 30

/* Exported types ------------------------------------------------------------*/
typedef struct
{	
    u16 Fifo_Buf[FIFO_SIZE];//数据buf
    u8 Fifo_Write;//写入fifo
    u8 Fifo_Read;//fifo读取
}Fifo_Sturct;

    /* Exported constants --------------------------------------------------------*/
#define FIFO_INIT \
{\
{0},\
0,\
0,\
} 

/* Exported macro ------------------------------------------------------------*/
extern Fifo_Sturct Test;


#endif /* _FIFO_H */
/******************* (C) COPYRIGHT Simcotech *****END OF FILE****/

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值