BPM180_STM32F103系列驱动代码,加入了移动平均值滤波算法
barometer.c文件中的代码
#include "barometer.h"
#include "usart.h"
#include "delay.h"
#define BMP085_SlaveAddress 0xee //¶¨ÒåÆ÷¼þÔÚIIC×ÜÏßÖеĴӵØÖ·
#define OSS 0 // Oversampling Setting (note: code is not set up to use other OSS values)
#define uchar unsigned char
#define uint unsigned int
void I2c_GPIO_Init()
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE );
GPIO_InitStructure.GPIO_Pin = I2C_Pin_SCL | I2C_Pin_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(ANO_GPIO_I2C, &GPIO_InitStructure);
}
//-----------------------------------
short ac1;
short ac2;
short ac3;
unsigned short ac4;
unsigned short ac5;
unsigned short ac6;
short b1;
short b2;
short mb;
short mc;
short md;
/*******************************/
//void Delay5us()
//{
// u16 i=0;
// u8 time=5;
// while(time--)
// {
// i=10;
// while(i--)
// ;
// }
//}
//void Delay_ms(float time)
//{
// u16 i=0;
// time=time*1000;
// while(time--)
// {
// i=10;
// while(i--)
// ;
// }
//}
void Delay5us()
{
delay_us(5);
}
void Delay_ms(float time)
{
delay_ms(time);
}
void BMP085_Start()
{
SDA_H;
SCL_H;
Delay5us();
SDA_L ;
Delay5us();
SCL_L;
}
///**************************************
//????
//**************************************/
void BMP085_Stop()
{
SDA_L; //?????
SCL_H; //?????
Delay5us(); //??
SDA_H; //?????
Delay5us(); //??
}
///**************************************
//??????
//????:ack (0:ACK 1:NAK)
//**************************************/
void I2c_Soft_Ask()
{
SDA_L;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
void I2c_Soft_NoAsk()
{
SDA_H;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
void BMP085_SendACK(u8 ack)
{
if(ack==0)
{
I2c_Soft_Ask();
}
else
{
I2c_Soft_NoAsk();
}
}
///**************************************
//??????
//**************************************/
u8 BMP085_RecvACK()
{
u8 ErrTime = 0;
SCL_H;
Delay5us();
while(SDA_read)
{
ErrTime++;
if(ErrTime>50)
{
BMP085_Stop();
return 1;
}
}
SCL_L;
Delay5us();
return 0;
}
/**************************************
?IIC??????????
**************************************/
void BMP085_SendByte(u8 SendByte)
{
u8 i=8;
while(i--)
{
SCL_L;
Delay5us();
if(SendByte&0x80)
{
SDA_H;
}
else
{
SDA_L;
}
SendByte<<=1;
Delay5us();
SCL_H;
Delay5us();
SCL_L;
Delay5us();
}
BMP085_RecvACK();
}
/**************************************
?IIC??????????
**************************************/
unsigned char BMP085_RecvByte()
{
u8 i;
u8 dat = 0;
SDA_H; //??????,??????,
for (i=0; i<8; i++) //8????
{
dat <<= 1;
SCL_H; //?????
Delay5us(); //??
if(SDA_read)
{
dat|=0x01;
}
SCL_L; //?????
Delay5us(); //??
}
return dat;
}
void Single_Write(uchar SlaveAddress,uchar REG_Address,uchar REG_data)
{
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BMP085_SendByte(REG_Address); //ÄÚ²¿¼Ä´æÆ÷µØÖ·
BMP085_SendByte(REG_data); //ÄÚ²¿¼Ä´æÆ÷Êý¾Ý
BMP085_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
}
uchar Single_Read(uchar REG_Address)
{ uchar REG_data;
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BMP085_SendByte(REG_Address); //·¢ËÍ´æ´¢µ¥ÔªµØÖ·
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress+1); //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ
REG_data=BMP085_RecvByte(); //¶Á³ö¼Ä´æÆ÷Êý¾Ý
BMP085_SendACK(1);
BMP085_Stop(); //Í£Ö¹ÐźÅ
return REG_data;
}
short Multiple_read(uchar ST_Address)
{
uchar msb, lsb;
short _data;
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BMP085_SendByte(ST_Address); //·¢ËÍ´æ´¢µ¥ÔªµØÖ·
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress+1); //·¢ËÍÉ豸µØÖ·+¶ÁÐźÅ
msb = BMP085_RecvByte(); //BUF[0]´æ´¢
BMP085_SendACK(0); //»ØÓ¦ACK
lsb = BMP085_RecvByte();
BMP085_SendACK(1); //×îºóÒ»¸öÊý¾ÝÐèÒª»ØNOACK
BMP085_Stop(); //Í£Ö¹ÐźÅ
Delay_ms(5);
_data = msb << 8;
_data |= lsb;
return _data;
}
//********************************************************************
long bmp085ReadTemp(void)
{
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x2E); // write register data for temp
BMP085_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
Delay_ms(4.5); // max time is 4.5ms
return (long) Multiple_read(0xF6);
}
//*************************************************************
long bmp085ReadPressure(void)
{
long pressure = 0;
BMP085_Start(); //ÆðʼÐźÅ
BMP085_SendByte(BMP085_SlaveAddress); //·¢ËÍÉ豸µØÖ·+дÐźÅ
BMP085_SendByte(0xF4); // write register address
BMP085_SendByte(0x34); // write register data for pressure
BMP085_Stop(); //·¢ËÍÍ£Ö¹ÐźÅ
Delay_ms(25.5); // max time is 4.5ms
pressure = Multiple_read(0xF6);
pressure &= 0x0000FFFF;
return pressure;
//return (long) bmp085ReadShort(0xF6);
}
//³õʼ»¯BMP085£¬¸ù¾ÝÐèÒªÇë²Î¿¼pdf½øÐÐÐÞ¸Ä**************
void Init_BMP085()
{
ac1 = Multiple_read(0xAA);
ac2 = Multiple_read(0xAC);
ac3 = Multiple_read(0xAE);
ac4 = Multiple_read(0xB0);
ac5 = Multiple_read(0xB2);
ac6 = Multiple_read(0xB4);
b1 = Multiple_read(0xB6);
b2 = Multiple_read(0xB8);
mb = Multiple_read(0xBA);
mc = Multiple_read(0xBC);
md = Multiple_read(0xBE);
printf("AC1:%d\r\n",ac1);
printf("AC2:%d\r\n",ac2);
printf("AC3:%d\r\n",ac3);
printf("AC4:%d\r\n",ac4);
printf("AC5:%d\r\n",ac5);
printf("AC6:%d\r\n",ac6);
printf("b1:%d\r\n",b1);
printf("b2:%d\r\n",b2);
printf("mb:%d\r\n",mb);
printf("mc:%d\r\n",mc);
printf("md:%d\r\n",md);
}
void barometerInit()
{
I2c_GPIO_Init();
Init_BMP085();
}
float arry[20];
float mathArry(float * Arrydata,u8 num);
u8 i=0;
float data_filter(long measure_pressure)
{
u8 j=0,k;
float dataFlaterResult;
while(i<=19)
{
arry[i]=measure_pressure;
i++;
printf("i%d\r\n",i);
}
//printf("i%d\r\n",i);
for (;j<=18;j++)
{
arry[j]=arry[j+1];
// printf("j%d\r\n",j);
}
//printf("j%d\r\n",j);
arry[j]=measure_pressure;
j=0;
dataFlaterResult=mathArry(arry,20);
return dataFlaterResult;
}
float mathArry(float * Arrydata,u8 num)
{
u8 i=0;
float caculate=0.0;
for (i=0;i<num;i++)
{
caculate=caculate+ *(Arrydata+i)/num;
}
return caculate;
}
long temperature;
long pressure;
float filterPressure;
void bmp085ConvertMeasure()
{
long ut;
long up;
long x1, x2, b5, b6, x3, b3, p;
unsigned long b4, b7;
ut = bmp085ReadTemp();
up = bmp085ReadPressure();
//printf("ut:%d\r\n",ut);
// printf("up:%d\r\n",up);
x1 = ((long)ut - ac6) * ac5 >> 15;
x2 = ((long) mc << 11) / (x1 + md);
b5 = x1 + x2;
temperature = (b5 + 8) >> 4;
b6 = b5 - 4000;
x1 = (b2 * (b6 * b6 >> 12)) >> 11;
x2 = ac2 * b6 >> 11;
x3 = x1 + x2;
b3 = (((long)ac1 * 4 + x3) + 2)/4;
x1 = ac3 * b6 >> 13;
x2 = (b1 * (b6 * b6 >> 12)) >> 16;
x3 = ((x1 + x2) + 2) >> 2;
b4 = (ac4 * (unsigned long) (x3 + 32768)) >> 15;
b7 = ((unsigned long) up - b3) * (50000 >> OSS);
if( b7 < 0x80000000)
p = (b7 * 2) / b4 ;
else
p = (b7 / b4) * 2;
x1 = (p >> 8) * (p >> 8);
x1 = (x1 * 3038) >> 16;
x2 = (-7357 * p) >> 16;
pressure = p + ((x1 + x2 + 3791) >> 4);
filterPressure=data_filter(pressure);
//printf("**********************************************pressure:%d\r\n",pressure);
printf("%f\r\n",filterPressure);
}
barometer.h文件中的代码
#ifndef __BAROMETER_H
#define __BAROMETER_H
#include "sys.h"
#define SCL_H GPIO_SetBits(ANO_GPIO_I2C, GPIO_Pin_6) /* SCL = 1 */
#define SCL_L GPIO_ResetBits(ANO_GPIO_I2C, GPIO_Pin_6) /* SCL = 0 */
#define SDA_H GPIO_SetBits(ANO_GPIO_I2C, GPIO_Pin_7) /* SDA = 1 */
#define SDA_L GPIO_ResetBits(ANO_GPIO_I2C, GPIO_Pin_7) /* SDA = 0 */
#define SDA_read GPIO_ReadInputDataBit(ANO_GPIO_I2C, GPIO_Pin_7) /* ?SDA???? */
/***************I2C GPIO??******************/
#define ANO_GPIO_I2C GPIOB
#define I2C_Pin_SCL GPIO_Pin_6
#define I2C_Pin_SDA GPIO_Pin_7
#define ANO_RCC_I2C RCC_APB2Periph_GPIOB
/*********************************************/
void barometerInit(void);
void bmp085ConvertMeasure(void);
#endif
main函数中的代码
barometerInit();
while(1)
{
bmp085ConvertMeasure();
delay_ms(50);
}
BPM180_STM32F103系列驱动代码,加入了移动平均值滤波算法相关推荐
- burg算法的matlab代码实现_导向滤波算法及其matlab代码实现
导向滤波同样是一种平滑滤波算法,其与最小二乘滤波和双边滤波相比,同样是一种具有边缘保持的功能的图形滤波算法,可以用于处理图形噪点较多的图像,而且此种滤波算法与最小二乘滤波和双边滤波相比,有其独特的特点 ...
- 滑动窗口滤波算法C代码实现
滑动窗口滤波算法 滑动窗口算法实现 平均值滤波与滑动窗口滤波对比 运用在实际中的一般写法 滑动窗口滤波实现 头文件实现 具体函数实现 测试程序 滑动窗口算法实现 在数字滤波器中,滑动窗口滤波法和平均值 ...
- 图像处理:双边滤波算法
今天主要是回顾一下双边滤波,我曾经在这篇--图像处理:推导五种滤波算法中推导过它,其中包含了我自己写的草稿图. 目录 双边滤波算法原理 (1)空间域核 (2)值域核 理解双边滤波 空域权重编辑和值域 ...
- 博途PLC 中位值滤波算法(FC功能块)
在图像处理算法中,中位值滤波的原理:如果一个信号是平缓变化的,那么某一点的输出值可以用这点的某个大小的邻域内的所有值的统计中值来代替(这个是图像处理里对中值的描述),我们现在研究的是时域信号sign( ...
- 树莓派IO口驱动代码的编写、微机总线地址、物理地址、虚拟地址、BCM2835芯片手册
地址总线: 百度百科解释: 地址总线 (Address Bus:又称:位址总线) 属于一种电脑总线 (一部份),是由CPU 或有DMA 能力的单元,用来沟通这些单元想要存取(读取/写入)电脑内存元件/ ...
- SSE图像算法优化系列二十二:优化龚元浩博士的曲率滤波算法,达到约1000 MPixels/Sec的单次迭代速度...
2015年龚博士的曲率滤波算法刚出来的时候,在图像处理界也曾引起不小的轰动,特别是其所说的算法的简洁性,以及算法的效果.执行效率等方面较其他算法均有一定的优势,我在该算法刚出来时也曾经有关注,不过 ...
- 晶联讯JLX12864G-086-PC-3S LCD显示屏 C51驱动代码
晶联讯JLX12864G-086-PC-3S LCD显示屏 C51驱动代码 //7.1.2 例程:以下为串行方式显示汉字及 ASCII 字符的例程: //液晶屏型号:JLX12864G-086-PC- ...
- 【鸿蒙OS开发入门】18 - HDF驱动子系统:加速度计传感器 Driver层驱动代码分析
[鸿蒙OS开发入门]18 - HDF驱动子系统:加速度计传感器 Driver层代码分析 一.如何添加速度计传感器驱动代码(代码.编译.配置) 1.驱动代码实现 2.驱动编译配置 2.1 linux 编 ...
- 痞子衡嵌入式:恩智浦SDK驱动代码风格、模板、检查工具
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家讲的是恩智浦 SDK 驱动的代码风格. 上周痞子衡受领导指示,给 SE 同事做了一个关于 SDK 代码风格的分享.随着组内新人的增多,这样的培训 ...
最新文章
- 14,matlab中如何查看程序中所调用的函数
- 设计模式之 - 简单工厂模式
- 小车主板哪家强?PS2手柄4路电机输出..火魔童主板了解一下?
- WinRAR 注册方法
- 2018成都初二计算机会考时间,2018年四川学业水平考试时间及科目
- 【视频】vue组件之props属性
- 使用identity+jwt保护你的webapi(一)——identity基础配置
- HDU 5950——Recursive sequence
- 常用数据结构--线性结构
- node ajax validator,使用validator.js对字符串数据进行验证
- 直播电商要处理好五个关系
- Redis入门到高可用(十一)—— 慢查询
- 智联招聘基于Apache Pulsar打造企业级事件中心
- vjc机器人灰度怎么编程_求用vc++编程实现显示灰度直方图的详细步骤,越详细越好...
- 全面解读设备状态监测
- 即将一统天下的OCP NIC 3.0及其未来
- 高数 | 导数极限定理、分段点求导能不能用公式?导数和导数的极限?
- 华罗庚 计算机,华罗庚有关计算机的故事
- 【雅思大作文考官范文】——第三篇:essay 'skeleton'(散文“骨架”)
- 高通平台读取USB ID