modified serial for HX711 readout

Fork of Hexi_Serial_Example by Hexiwear

Committer:
eddienuel
Date:
Sun Oct 09 01:45:12 2016 +0000
Revision:
1:cad368af277b
Parent:
0:79c0c4cd1ab3
implemented auto calibration

Who changed what in which revision?

UserRevisionLine numberNew contents of line
GregC 0:79c0c4cd1ab3 1 #include "mbed.h"
GregC 0:79c0c4cd1ab3 2
eddienuel 1:cad368af277b 3
eddienuel 1:cad368af277b 4 float _scale;
GregC 0:79c0c4cd1ab3 5 DigitalOut myled(LED_BLUE);
eddienuel 1:cad368af277b 6
eddienuel 1:cad368af277b 7 DigitalIn HX_DOUT(PTC3);
eddienuel 1:cad368af277b 8 DigitalOut HX_PD_SCK(PTC5);
eddienuel 1:cad368af277b 9
GregC 0:79c0c4cd1ab3 10 Serial pc(USBTX, USBRX);
eddienuel 1:cad368af277b 11 long offset = 0;
eddienuel 1:cad368af277b 12 long ReadWeight(void);
eddienuel 1:cad368af277b 13 //double getGram();
eddienuel 1:cad368af277b 14 uint16_t getGram();
eddienuel 1:cad368af277b 15 long getAverageValue(int times);
GregC 0:79c0c4cd1ab3 16
GregC 0:79c0c4cd1ab3 17 int main()
GregC 0:79c0c4cd1ab3 18 {
eddienuel 1:cad368af277b 19 offset = getAverageValue(20);
GregC 0:79c0c4cd1ab3 20 int i = 0;
GregC 0:79c0c4cd1ab3 21 pc.printf("Hello World!\n");
GregC 0:79c0c4cd1ab3 22 while (true) {
GregC 0:79c0c4cd1ab3 23 wait(0.5f); // wait a small period of time
eddienuel 1:cad368af277b 24 pc.printf("%d \n", getGram()); // print the value of variable i
GregC 0:79c0c4cd1ab3 25 i++; // increment the variable
GregC 0:79c0c4cd1ab3 26 myled = !myled; // toggle a led
GregC 0:79c0c4cd1ab3 27 }
GregC 0:79c0c4cd1ab3 28 }
GregC 0:79c0c4cd1ab3 29
eddienuel 1:cad368af277b 30 long ReadWeight(void)
eddienuel 1:cad368af277b 31 {
eddienuel 1:cad368af277b 32 long Count;
eddienuel 1:cad368af277b 33 unsigned char i;
eddienuel 1:cad368af277b 34 HX_PD_SCK.write(0);
eddienuel 1:cad368af277b 35 Count = 0;
eddienuel 1:cad368af277b 36 while(HX_DOUT.read() == 1);
eddienuel 1:cad368af277b 37 for (i=0;i<24;i++)
eddienuel 1:cad368af277b 38 {
eddienuel 1:cad368af277b 39 HX_PD_SCK.write(1);
eddienuel 1:cad368af277b 40 Count=Count<<1;
eddienuel 1:cad368af277b 41 HX_PD_SCK.write(0);
eddienuel 1:cad368af277b 42 if(HX_DOUT.read() == 1) Count++;
eddienuel 1:cad368af277b 43 }
eddienuel 1:cad368af277b 44 HX_PD_SCK.write(1);
eddienuel 1:cad368af277b 45 Count=Count^0x800000;
eddienuel 1:cad368af277b 46 HX_PD_SCK.write(0);
eddienuel 1:cad368af277b 47 return(Count);
eddienuel 1:cad368af277b 48 }
eddienuel 1:cad368af277b 49
eddienuel 1:cad368af277b 50 //double getGram(){
eddienuel 1:cad368af277b 51 // return ((getAverageValue(20) - offset)*0.0000407059);//8409088
eddienuel 1:cad368af277b 52 // //return getAverageValue(20);// - offset)*0.00793457031);//8409088
eddienuel 1:cad368af277b 53 //
eddienuel 1:cad368af277b 54 //}
eddienuel 1:cad368af277b 55
eddienuel 1:cad368af277b 56 uint16_t getGram(){
eddienuel 1:cad368af277b 57 return ((getAverageValue(20) - offset))/1000;//*0.0000407059);//8409088
eddienuel 1:cad368af277b 58 //return getAverageValue(20);// - offset)*0.00793457031);//8409088
eddienuel 1:cad368af277b 59
eddienuel 1:cad368af277b 60 }
eddienuel 1:cad368af277b 61
eddienuel 1:cad368af277b 62 long getAverageValue(int times){
eddienuel 1:cad368af277b 63 long sum = 0;
eddienuel 1:cad368af277b 64 for (int i = 0; i < times; i++)
eddienuel 1:cad368af277b 65 {
eddienuel 1:cad368af277b 66 sum += ReadWeight();
eddienuel 1:cad368af277b 67 }
eddienuel 1:cad368af277b 68 return sum / times;
eddienuel 1:cad368af277b 69 }
eddienuel 1:cad368af277b 70