常驻嘉宾
- 积分
- 4795
- 威望
- 2150
- 贡献
- 1733
- 兑换币
- 1442
- 注册时间
- 2013-8-7
- 在线时间
- 456 小时
|
6#
楼主 |
发表于 2016-7-26 15:47:48
|
只看该作者
本帖最后由 樱月樱诗 于 2016-7-26 16:00 编辑
有,开源吧。
/*******************************************************
本文件中,是操作称重传感器专用AD芯片 HX711 的程序。
********************************************************/
#include "HX711_AD.h"
#include "include.h"
#include "Nokia5110.h"
#include "common.h"
#include "delay.h"
#include "gpio_cfg.h"
#define N 12 //
unsigned long BUF[N];
unsigned long SUM=0;
int i=0;
#define SCK_1(x) PTC0_OUT=(x);
#define DOUT_1(x) PTC1_OUT=(x);
#define SCK_2(x) PTE0_OUT=(x);
#define DOUT_2(x) PTE1_OUT=(x);
void HX711_Init()
{
//-------------1-------------//
gpio_init (PORTC,0,GPO,HIGH);//SCK
gpio_init (PORTC,1,GPO,HIGH);//DOUT
//-------------2-------------//
gpio_init (PORTE,0,GPO,HIGH);//SCK
gpio_init (PORTE,1,GPO,HIGH);//DOUT
}
unsigned long HX711_GET1()
{
unsigned long int value=0;
unsigned int x16;
char i=0;
DOUT_1(1);
delayus(10);
SCK_1(0);
value=0 ;
gpio_init (PORTC,1,GPI,HIGH);//DOUT
while(gpio_get(PORTC,1));
for(i=0;i<24;i++)
{
SCK_1(1); delayus(17);
value=value<<1;
SCK_1(0); delayus(17);
gpio_init (PORTC,1,GPI,HIGH);
if(gpio_get(PORTC,1))
{
value++;
}
}
SCK_1(1); delayus(17);
// value=value>>8;
// value = value^0x800000;
SCK_1(0); delayus(17);
// x16 = value&0x0000ffff;
//printf("%d\n",x16);
return value;
}
unsigned long HX711_GET2()
{
unsigned long int value=0;
unsigned int x16;
char i=0;
DOUT_2(1);
delayus(10);
SCK_2(0);
value=0 ;
gpio_init (PORTE,1,GPI,HIGH);//DOUT
while(gpio_get(PORTE,1));
for(i=0;i<24;i++)
{
SCK_2(1); delayus(17);
value=value<<1;
SCK_2(0); delayus(17);
gpio_init (PORTE,1,GPI,HIGH);
if(gpio_get(PORTE,1))
{
value++;
}
}
SCK_2(1); delayus(17);
value=value>>8;
// value = value^0x800000;
SCK_2(0); delayus(17);
// x16 = value&0x0000ffff;
// printf("%d\n",x16);
// LCD_Write_Num(0, 1, x16,7);
return value;
}
void BUFInit(void) //初始化队列数组BUFFSUM,即往里面存入N个采样值求和
{
int j=0;
for(j=0;j<N;j++)
{
BUF[j] = HX711_GET1();
SUM+=BUF[j];
}
}
long double filter(void) //滑动均值滤波函数
{
unsigned long temp=0,max=0,min=0;
int j=0;
temp = HX711_GET1();
SUM = SUM+temp-BUF;
BUF=temp;
i++;
if(i == N)
i=0;
max=BUF[0];
min=BUF[0];
for(j=0;j<N;j++)
{
if(max<BUF[j]) max=BUF[j];
if(min>BUF[j]) min=BUF[j];
}
return (SUM-max-min)/(N-2);
}
//---------------------------------------------------------//
//AD采样处理——算术平均数字滤波
//--------------------------------------------------------//
unsigned long Sampling(void)
{
unsigned int Sam[12],tmpmax,tmpmin,sum=0,Average;
unsigned char i;
for(i=0;i<12;i++)
{
Sam= HX711_GET1(); //赋值处
if(i==0)
{
tmpmax=Sam[0];
tmpmin=Sam[0];
}
if(i>0)
{
if(Sam>tmpmax)tmpmax=Sam;
if(Sam<tmpmin)tmpmin=Sam;
}
}
for(i=0;i<12;i++)
{
if(!(Sam==tmpmax||Sam==tmpmin))
{
sum=sum+Sam;
}
}
Average=sum/10;
return(Average);
}
void Get_Maopi()
{
HX711_Buffer_1 = HX711_GET1();
Weight_Maopi = HX711_Buffer_1/1000;
}
void Get_Weight()
{
HX711_Buffer_1 = HX711_GET1();
HX711_Buffer_1 = HX711_Buffer_1/1000;
if(HX711_Buffer_1 >= Weight_Maopi+120)
{
Weight_Shiwu = HX711_Buffer_1;
Weight_Shiwu = Weight_Shiwu - Weight_Maopi;
Weight_Shiwu = (unsigned long int)((float)Weight_Shiwu/0.223);
}
else
{
Weight_Shiwu=0;
}
Weight=Weight_Shiwu;
LCD_Write_String(1, 2, "体重:") ;
LCD_Write_Num(36, 2, Weight_Shiwu%100000/10000,1); //wan
LCD_Write_Num(42, 2, Weight_Shiwu%100000%10000/1000,1); //qian
LCD_Write_Num(48, 2, Weight_Shiwu%100000%10000%1000/100,1); //bai
LCD_Show_Char(54,2, '.');
LCD_Write_Num(60, 2, Weight_Shiwu%100000%10000%1000%100/10, 1);
LCD_Write_Num(66, 2, Weight_Shiwu%10, 1);
// LCD_Write_Num(60, 2, Weight_Shiwu%1000, 3); //小数点后
LCD_Write_String(72, 3, "Kg") ;
}
注意传感器电压是5V,主函数开始while(1)之前调用 HX711_Init(); Get_Maopi();
|
|