100% found this document useful (1 vote)
335 views

HX711

This document contains code for interfacing with an HX711 load cell amplifier and reading weight measurements from the load cell. It includes functions to initialize the HX711, set the gain and offset, read weight measurements, and display the readings on an LCD screen connected via I2C. The functions average multiple readings to reduce noise in the measurements.

Uploaded by

framqz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
335 views

HX711

This document contains code for interfacing with an HX711 load cell amplifier and reading weight measurements from the load cell. It includes functions to initialize the HX711, set the gain and offset, read weight measurements, and display the readings on an LCD screen connected via I2C. The functions average multiple readings to reduce noise in the measurements.

Uploaded by

framqz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 9

#if defined(__PCM__)

#include <16f877.h>
#fuses XT, NOWDT, NOPROTECT, NOLVP, PUT, BROWNOUT
#use delay(clock = 4000000) // Fosc = 4MHz
#define use_portb_lcd TRUE // Puerto B para el manejo de la LCD
#include <lcd420.c> // Libreria de manejo de la LCD
#BIT ADDO = 0X08.0 // 0x08 = Puerto D, posicion en el banco de memoria
#BIT ADSK = 0X08.1

unsigned int32 ReadCount(void){


unsigned int32 Count;
unsigned char i;
// Condicin inicial
set_tris_d(0xfc); // 1111 1100
ADDO=1;
ADSK=0;
set_tris_d(0xfd); // 1111 1101
Count=0;
while(ADDO);
// Empieza el proceso de conversin
for (i=0;i<24;i++){
ADSK=1;
Count=Count<<1;
ADSK=0;
if(ADDO) Count++;
}
ADSK=1;
Count=Count^0x800000;
ADSK=0;
return(Count);
}
void main()
{
unsigned int32 power=1000;
set_tris_c(0xFE);
set_tris_d(0XFE);
do
{
power = ReadCount();
lcd_init(); // Se imicializa la lcd
lcd_gotoxy(5,1); // Se ubica en la posicin X = 5 Y = 1
printf(lcd_putc,"fuerza="); // Se escibe en la LCD MICROCHIP
lcd_gotoxy(5,2);
printf(lcd_putc,"%Lu",power);
//delay_ms(1000);
//output_high(PIN_C0); // Led de Testeo si retorna de la conver
sin.
//delay_ms(1000);
//output_low(PIN_C0);
}
while(true); // Bucle infinito
}

#include <main.h>
#include <stdio.h>
#ZERO_RAM
#define HX711_DO PIN_B15
#define HX711_CLK PIN_B14
int32 measurement(void);
void main()
{
int1 test=0;
unsigned int32 Count,BUFFER[30],offseeet=0,mied=0;
unsigned int8 i=0;

while(TRUE){
while(i<10){
Count=measurement();
BUFFER[i]=Count;
mied+=BUFFER[i];
i++;
}
i=0;
mied/=11;
if(test==0){
offseeet=mied;
test=1;
}
//Count=measurement();
mied-=offseeet;
//printf("data=%lu \n\r",Count);
printf("mied=%lu \n\r",mied);
//delay_ms(1);
//printf("\f");
}
}
//********************************************
int32 measurement(void){
unsigned int32 Count;
unsigned int8 i,A_1,A_2,A_3;

output_bit( HX711_DO, 1);


output_bit( HX711_CLK, 0);
Count=0;
while(input(HX711_DO));
for (i=0;i<24;i++){// gain 128
output_bit( HX711_CLK, 1);
Count=Count<<1;
output_bit( HX711_CLK, 0);
if(input(HX711_DO)) Count++;
}
output_bit( HX711_CLK, 1);
Count=Count^0x800000;
output_bit( HX711_CLK, 0);
//************************
A_1=MAKE8(Count, 0);
A_2=MAKE8(Count, 1);
A_3=MAKE8(Count, 2);
A_2=(A_2 & 0b11111000);
Count= MAKE16(A_3, A_2);
return(Count);
}

/*http://www.ebay.com/itm/New-Weighing-Sensor-AD-Module-Dual-channel-24-bit-A-D-
Conversion-HX711-Shieding-/400567204760?pt=LH_DefaultDomain_0&hash=item5d43aa7b9
8
http://www.ebay.com/itm/Electronic-Scale-Sensor-HX711-ADC-Bridge-Weighing-Anti-J
amming-For-Arduino-AVR-/171217937819?pt=LH_DefaultDomain_0&hash=item27dd62659b
*/

//-----------------------------------------------------------------------------
// Title: i2c_Flex_LCD
// Description: Driver for common LCD with 1/2/3 or 4 row modules using PCF857
4T interface board with I2C protocol.
// Date: Nov-2013
// Ver.Rev.: 1.0
// Author: Hugo Silva ([email protected]) #Based on the routines of
20X4_LCD_I2C_DRIVER.h from Pumrin S.
//-----------------------------------------------------------------------------
//
// lcd_init() Must be called before any other function.
//
// lcd_putc(c) Will display c on the next position of the LCD.
//
// \f Clear LCD dispay
// \1 Set write position on LCD Line 1
// \2 Set write position on LCD Line 2
// \3 Set write position on LCD Line 3
// \4 Set write position on LCD Line 4
//
// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1)
//
//-----------------------------------------------------------------------------
// LCD pins D0-D3 are not used.
//-----------------------------------------------------------------------------
//
// Commment : Control of a compatible LCD HITACHI from a bus I2C with
// an EXPANDER of I/O with connection I2C. The tests of these
// routines have been programmed using the IC PCF8574T of Phillips.
// I used 4 bits mode programming. The 8 bits mode programming
// is possible if you use 2 x PCF8574T.
//
// As defined in the following structure the pin connection is as follows:
//
// PCF8574P LCD
// ======== ======
// P0 RS
// P1 RW
// P2 Enable
// P3 Led Backlight
// P4 D4
// P5 D5
// P6 D6
// P7 D7
//
// The SCL and SDA pins should be pull-up resistor as shown below:
//
// +5v
// |
// <
// > 4.7K
// <
//To PIC | To i2c slave
//pin xx ------------------ SDA pin
//(SDA)
// +5v
// |
// <
// > 4.7K
// <
//To PIC | To i2c slave
//pin xx ------------------ SCL pin
//(SCL)
//
//To PIC To i2c slave
//Vss pin ----------------- Vss or ground pin
// |
// -----
// --- Ground
// -
//
// THIS DOCUMENT IS PROVIDED TO THE USER "AS IS"
//-----------------------------------------------------------------------------
#define LCD_ADDR 0x7E //I2C slave address for LCD module for PCF8574A
#define ON 1
#define OFF 0
#define RS 0b00000001 //P0 - PCF8574T Pin connected to RS
#define RW 0b00000010 //P1 - PCF8574T Pin connected to RW
#define EN 0b00000100 //P2 - PCF8574T Pin connected to EN
#define BACKLIGHT_LED 0b00001000 //P3 - PCF8574T Pin connected to BACKLIGHT LE
D
#define lcd_line_one 0x80 // LCD RAM address for line 1
#define lcd_line_two 0xC0 // LCD RAM address for line 2
#define lcd_line_three 0x94 // LCD RAM address for line 3
#define lcd_line_four 0xD4 // LCD RAM address for line 4
byte address;
int1 lcd_backlight=ON;
void i2c_send_nibble(unsigned char data)
{
i2c_start();
delay_us(20);
i2c_write(LCD_ADDR); //the slave addresse
delay_us(20);
i2c_write(data);
delay_us(20);
i2c_stop();
delay_us(20);
}
void lcd_send_byte(unsigned char data)
{
if (lcd_backlight) data=data|EN|BACKLIGHT_LED; else data=data|EN; //set
pin EN
i2c_send_nibble(data);
data=data-4; //toggle EN back to 0
i2c_send_nibble(data);
}
void lcd_clear()
{
lcd_send_byte(0x00);
lcd_send_byte(0x10);
delay_ms(2);
}
void lcd_init()
{
delay_ms(200); //LCD power up delay
//Request works on the command by set the RS = 0 R/W = 0 write
lcd_send_byte(0x00);
lcd_send_byte(0x10);
lcd_send_byte(0x00);
lcd_send_byte(0x00);
lcd_send_byte(0x10);
//First state in 8 bit mode
lcd_send_byte(0x30);
lcd_send_byte(0x30);
//Then set to 4-bit mode
lcd_send_byte(0x30);
lcd_send_byte(0x20);
//mode 4 bits, 2 lines, characters 5 x 7 (28 h)
lcd_send_byte(0x20);
lcd_send_byte(0x80);
//no need cursor on (0Ch)
lcd_send_byte(0x00);
lcd_send_byte(0xC0);
//the cursor moves to the left (06 h)
lcd_send_byte(0x00);
lcd_send_byte(0x60);
//clears the display
lcd_clear();
}
void lcd_gotoxy( byte x, byte y)
{
static char data;
switch(y)
{
case 1: address= lcd_line_one; break;
case 2: address= lcd_line_two; break;
case 3: address= lcd_line_three; break;
case 4: address= lcd_line_four; break;
default: address= lcd_line_one; break;
}
address+=x-1;
data=address&0xF0;
lcd_send_byte(data);
data=address&0x0F;
data=data<<4;
lcd_send_byte(data);
}
//Display the character on LCD screen.
void LCD_PUTC(char in_data)
{
char data;
switch(in_data)
{
case '\f': lcd_clear() ; break;
case '\1': lcd_gotoxy(1,1); break;
case '\2': lcd_gotoxy(1,2); break;
case '\3': lcd_gotoxy(1,3); break;
case '\4': lcd_gotoxy(1,4); break;
default:
data=in_data&0xF0;
data=data|RS; //set RS pin to 1
lcd_send_byte(data);
data=in_data&0x0F;
data=data<<4;
data=data|RS; //set RS pin to 1
lcd_send_byte(data);
break;
}
}

HX711_set_scale(1375);
HX711_set_gain(128);
HX711_tare(20);
for(;;)
{
loadvalue=HX711_get_units(10);// takes 10 measurement and average
lcd_gotoxy(1,1);
printf(lcd_putc,"Value");
lcd_gotoxy(1,2);
printf(lcd_putc," ");
lcd_gotoxy(1,2);
printf(lcd_putc,"%4.1f gr",loadvalue);
delay_ms(200);
/*
HX711_power_down();
delay_ms(1000);
HX711_power_up();
*/
}

void HX711_init(unsigned int8 gain);


void HX711_set_gain(unsigned int8 gain);
int32 HX711_read();
int32 HX711_read_average(int8 times);
unsigned int32 HX711_get_value(int8 times);
//unsigned int16 HX711_get_units(int8 times);
float HX711_get_units(int8 times);
void HX711_tare(int8 times);
void HX711_set_scale(float scales);
void HX711_set_offset(unsigned int32 offsets);
void HX711_power_down(void);
void HX711_power_up(void);
unsigned int8 GAIN; // amplification factor
int32 OFFSET; // used for tare weight
float SCALE; // used to return weight in grams, kg, ounces, whatever

// IOpin.PDSCK is pinF4 of pic controller


// IOpin.PDOUT is pinF0 of pic controller
void HX711_init(unsigned int8 gain)
{
HX711_set_gain(gain);
}
void HX711_set_gain(unsigned int8 gains)
{
switch (gains) {
case 128: // channel A, gain factor 128
GAIN = 1;
break;
case 64: // channel A, gain factor 64
GAIN = 3;
break;
case 32: // channel B, gain factor 32
GAIN = 2;
break;
}
IOpin.PDSCK=0;
HX711_read();
}
unsigned int32 HX711_read()
{
int8 data[3];
unsigned int8 i,j;
unsigned int32 value=0;
set_tris_f(0x48);
IOpin.PDOUT=1;
IOpin.PDSCK=0;
set_tris_f(0x49);
while(IOpin.PDOUT);
for(j=0;j<3;j++)
{
for(i=0;i<8;i++)
{
IOpin.PDSCK=1;
delay_us(5);
IOpin.PDSCK=0;
if(IOpin.PDOUT) bit_set(data[2-j],7-i);
else bit_clear(data[2-j],7-i);
}
}
for (i=0;i<GAIN;i++)
{
IOpin.PDSCK=1;
delay_us(5);
IOpin.PDSCK=0;
}
data[2]^= 0x80;
data[0]=data[0]&0xF0;
//data[1]=data[1]&0xf8;
value=((unsigned int32)data[2]<<16)|((unsigned int32)data[1]<< 8)|((unsigned int
32)data[0]);
return(value);
}
//------------------------------------------------------------------------------
unsigned int32 HX711_read_average(int8 times)
{
unsigned int32 sum=0;
int8 i;
for(i=0;i<times;i++)
{
sum+=HX711_read();
}
return(sum/times);
}
//------------------------------------------------------------------------------
unsigned int32 HX711_get_value(int8 times)
{
unsigned int32 diff=0;
diff=HX711_read_average(times)-OFFSET;
return(diff);
}
//------------------------------------------------------------------------------
float HX711_get_units(int8 times)
{
return((float)(HX711_get_value(times)/SCALE));
}
/*
unsigned int16 HX711_get_units(int8 times)
{
return((unsigned int32)(HX711_get_value(times)/SCALE));
}
*/
//------------------------------------------------------------------------------
void HX711_tare(int8 times)
{
unsigned int32 sum;
sum=HX711_read_average(times);
HX711_set_offset(sum);
}
void HX711_set_scale(float scales)
{
SCALE=scales;
}
void HX711_set_offset(unsigned int32 offsets)
{
OFFSET=offsets;
}
void HX711_power_down(void)
{
IOpin.PDSCK=0;
IOpin.PDSCK=1;
}
void HX711_power_up(void)
{
IOpin.PDSCK=0;
}

You might also like