TM7711 or HX710B ?? How is your mandarin aka Chinese (simplified)

Started by Fanie, Today at 04:20 PM

Previous topic - Next topic

Fanie

I got some atmospheric pressure sensors using a TM7711 chip.
The same module also uses a HX710B chip.  Boards look identical.
The data sheets are in Chinese.

On this page I found this C-code
https://www.homemade-circuits.com/hx710b-air-pressure-sensor-datasheet-how-to-connect/

// Define pins for HX710B
const int dataPin = 2; // DOUT
const int clockPin = 3; // SCK

void setup() {
  pinMode(dataPin, INPUT);
  pinMode(clockPin, OUTPUT);
  Serial.begin(9600);
}

long readHX710B() {
  long result = 0;

  // Wait for the module to be ready
  while (digitalRead(dataPin) == HIGH);

  // Read 24-bit data
  for (int i = 0; i < 24; i++) {
    digitalWrite(clockPin, HIGH);
    result = (result << 1) | digitalRead(dataPin);
    digitalWrite(clockPin, LOW);
  }

  // Apply clock pulse to complete the conversion
  digitalWrite(clockPin, HIGH);
  delayMicroseconds(1);
  digitalWrite(clockPin, LOW);

  // Return the 24-bit result
  return result;
}

void loop() {
  long pressureValue = readHX710B();

  Serial.print("Pressure (raw value): ");
  Serial.println(pressureValue);

  // Add a delay
  delay(500);
}

The aricle suggest you can obtain an analog output from the module
QuoteUsing Analogue Pins
The HX710B Air Pressure Sensor Module can be also connected with the analog pin of the Arduino board by programming the following code.

Here is an example code to read the analog output from the HX710B module and print it to the serial monitor:

And offered this code -

// Assign the analog input pin
int pressurePin = A0;

void setup() {
  // Start the serial communication
  Serial.begin(9600);
}

void loop() {
  // Read the analog input value
  int pressureValue = analogRead(pressurePin);

  // Print the pressure value to the serial monitor
  Serial.print("Pressure: ");
  Serial.print(pressureValue);
  Serial.println(" Pa");

  // Add a delay to prevent rapid reading of the same value
  delay(500);
}

The Chinese TM7711 data sheet provided this code -
Reference procedure C Language: (For reference only) /* TM7711.h header file*/
参考程序
C 语言:(仅供参考)
/* TM7711.h 头文件*/
#ifndef _TM7711_H_
#define _TM7711_H_
#define CH1_10HZ 0x01
#define CH1_40HZ 0x02
#define CH2_TEMP 0x03
#define CH1_10HZ_CLK 25
#define CH1_40HZ_CLK 27
#define CH2_TEMP_CLK 26
unsigned long Read_TM7711(unsigned char next_select);
#endif
/* TM7711.c Program files*/
#include "TM7711.h"
#include "global.h" //定义端口  Define ports
#include "delay.h" //延时子程序  Time-delay subprogram
unsigned long Read_TM7711(unsigned char next_select)
{
 unsigned char i = 0;
 unsigned long data_temp = 0;
 for(i = 0;i < 24;i++)
 {
 SET_SCK_H(); //Defined in the global.h file, the SCK pin outputs a high level
 data_temp <<= 1;
delay_us(5); //Delay 5 microseconds, customize this function according to different MCUs
 if(READ_PORT & (1 << PIN_DOUT)) //Determine if DOUT is high
 data_temp |= 1;
 SET_SCK_L();//Defined in the global.h file, the SCK pin outputs a low level
 }
 switch(next_select) //Determine the next data update rate or switch channels
 {
 case CH1_10HZ:
 SET_SCK_H();
 delay_1us();
 SET_SCK_L();
 break;
 case CH1_40HZ:
 SET_SCK_H();
 delay_1us();
 SET_SCK_L();
 delay_1us();
 SET_SCK_H();
 delay_1us();
 SET_SCK_L();
 delay_1us();
 SET_SCK_H();
delay_1us();
 SET_SCK_L();
 break;
 case CH2_TEMP:
 SET_SCK_H();
 delay_1us();
 SET_SCK_L();
 delay_1us();
 SET_SCK_H();
 delay_1us();
 SET_SCK_L();
 break;
 default:
 break;
 }
 return(data_temp); //Returns data read out from TM7711
}

Fanie

I was hoping an analog output would be the default of one of the pins but it is not.
If you require digital communication to make a pin analog, you can just as well use the digital value.

I cannot make heads or tail of the code.  I have translated the Chinese to English but it does not help much.
If anyone understand the different frequencies and channels ?  please explain to me.
A translation to basic will help.

Thank you.

top204

You can try the code listing below that I have just translated from C:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Untested code translation to read a TM7711 or HX710B device.
'
' Written by Les Johnson for use with the Positron8 BASIC compiler.
'
    Device = 18F26K22                                           ' Tell the compiler what device to compile for
    Declare Xtal = 64                                           ' Tell the compiler what frequency the device will be operating at (in MHz)
    Declare Auto_Heap_Arrays = On                               ' Tell the compiler to create arrays above standard variables, so assembler code is more efficient
    Declare Auto_Heap_Strings = On                              ' Tell the compiler to create Strings above standard variables, so assembler code is more efficient
    Declare Auto_Variable_Bank_Cross = On                       ' Tell the compiler to create any multi-byte variables in the same RAM bank. For more efficiency
    Declare Float_Display_Type = Fast                           ' Make the floating point diaplay faster and more accurate
'
' Setup USART1
'
    Declare Hserial_Baud = 9600                                 ' Set USART1 baud rate to 9600
    Declare HSerout1_Pin = PORTB.6                              ' Set the TX pin
'
' Define pins for the HX710B
'
$define HX_Data_Pin     PORTB.0                                 ' DOUT pin
$define HX_Clock_Pin    PORTB.1                                 ' SCK pin
$define HX_Pressure_Pin PORTA.0                                 ' The analogue input pin
'
' Create Global variables here
'
    Dim dPressureValue As SDWord                                ' Holds the results read from the TM or HX device

'---------------------------------------------------------------------------------------------
' The main program starts here.
' Read an HX device and transmit the results to a serial terminal.
'
Main:
    Setup()                                                     ' Setup the program and any peripherals

    Do                                                          ' Create a loop
        dPressureValue = ReadHX710B()                           ' Read the HX710B device digitally
        HRsout1Ln "Pressure (raw value): ", SDec dPressureValue ' Transmit the value to a serial terminal
       
        dPressureValue = ADC_Read(0)                            ' Read the analogue input value
        HRsout1Ln "Pressure : ", Dec dPressureValue, " Pa"      ' Transmit the value to a serial terminal
        DelayMs 500                                             ' Create a delay so we can see what is happening on the serial terminal
    Loop                                                        ' Loop forever

'---------------------------------------------------------------------------------------------
' Setup the program and any peripherals
' Input     : None
' Output    : None
' Notes     : None
'
Proc Setup()
    Osc_64MHz()                                                 ' Set the device to 64MHz operation using its internal oscillator

    PinInput HX_Data_Pin                                        ' Set the HX Data pin as an input
    PinOutput HX_Clock_Pin                                      ' Set the HX Clock pin as an output low
    ADC_Init()                                                  ' Setup the ADC
EndProc

'---------------------------------------------------------------------------------------------
' Read the HX710B device digitally
' Input     : None
' Output    : Returns the raw 24-bit value
' Notes     : None
'
Proc ReadHX710B(), SDWord
    Dim bBitIndex As Byte Access
    Result = 0

    While HX_Data_Pin == 1: Wend                                ' Wait for the module to be ready
'
' Read the 24-bit data
'
    For bBitIndex = 23 DownTo 0
        PinSet HX_Clock_Pin
        Result = Result << 1
        Result.0 = PinGet HX_Data_Pin
        PinClear HX_Clock_Pin
    Next
'
' Apply clock pulse to complete the conversion
'
    PinSet HX_Clock_Pin
    DelayUs 1
    PinClear HX_Clock_Pin
EndProc

'------------------------------------------------------------------------------------
' Setup the ADC on a PIC18F2xK22 device
' Input     : None
' Output    : None
' Notes     : Set for 10-bit operation
'           : Uses the Gnd and VDD as vrefs
'           : Uses the FRC oscillator
'           : Sets AN0 as an analogue pin
'
Proc ADC_Init()
    ADCON1 = $00
    ADCON2 = 0b10001111
    ADRESL = $00
    ADRESH = $00
    ADCON0 = $00
    PinInput PORTA.0                                            ' Set the analogue pin as an input (just in case)
    ANSELAbits_ANSA0 = 1                                        ' Setup AN0 as an analogue pin
EndProc

'------------------------------------------------------------------------------------
' Read an ADC channel
' Input     : pChannel holds the channel to read
' Output    : Returns the 10-bit ADC value
' Notes     : For a PIC18F2xK22 device
'
Proc ADC_Read(pChannel As Byte), Word
    Dim ADC_wResult As ADRESL.Word                              ' Create a 16-bit SFR from ADRESL\H

    pChannel = pChannel << 2                                    ' Move the channel its ito their correct position
    ADCON0 = pChannel | %00000001                               ' Select the ADC channel and keep the ADC enabled
    ADCON0bits_GO_DONE = 1                                      ' Start the conversion
    Repeat: Until ADCON0bits_GO_DONE = 0                        ' Wait for the conversion to finish
    Result = ADC_wResult                                        ' Return the ADC result
EndProc

'---------------------------------------------------------------------------------------------
' Set the PIC18F2xK22 device to 64MHz operation using its internal oscillator
' Input     : None
' Output    : None
' Notes     : None
'
Proc Osc_64MHz()
    OSCCON  = $70
    OSCCON2 = $04
    OSCTUNE = $40
    DelayMS 100
EndProc

'---------------------------------------------------------------------------------------------
' Setup the fuses to use the internal oscillator on a PIC18F2xK22 device.
' OSC pins RA6 and RA7 are general purpose I/O.
'
Config_Start
    FOSC     = INTIO67                                          ' Internal oscillator
    PRICLKEN = Off                                              ' Primary clock disabled
    MCLRE    = EXTMCLR                                          ' MCLR pin enabled
    WDTEN    = Off                                              ' Watchdog Timer disabled
    PLLCFG   = Off                                              ' Oscillator used directly
    FCMEN    = Off                                              ' Fail-Safe Clock Monitor disabled
    IESO     = Off                                              ' Oscillator Switchover mode disabled
    PWRTEN   = On                                               ' Power up timer enabled
    BOREN    = Off                                              ' Brown-out Reset disabled
    BORV     = 190                                              ' VBOR set to 1.9 V nominal
    WDTPS    = 128                                              ' Watchdog Timer Postscale is 1:128
    HFOFST   = Off                                              ' The Access clock is not held Off until the HF-INTOSC is stable
    PBADEN   = Off                                              ' PORTB<4:0> pins are configured as digital on reset
    CCP2MX   = PORTC1                                           ' CCP2 input/output is multiplexed with RC1
    CCP3MX   = PORTB5                                           ' P3A/CCP3 input/output is multiplexed with RB5
    T3CMX    = PORTC0                                           ' T3CKI is on RC0
    P2BMX    = PORTB5                                           ' P2B is on RB5
    STVREN   = On                                               ' Stack full/underflow will cause a reset
    LVP      = Off                                              ' Single-Supply ICSP disabled
    Debug    = Off                                              ' Background debugger disabled. RB6 and RB7 configured as general purpose I/O pins
    XINST    = Off                                              ' Extra mnemonics disabled
    Cp0      = Off                                              ' Block 0 (000800-001FFF) not code protected
    CP1      = Off                                              ' Block 1 (002000-003FFF) not code protected
    CPB      = Off                                              ' Boot block (000000-0007FF) not code protected
    CPD      = Off                                              ' Data EEPROM not code protected
    WRT0     = Off                                              ' Block 0 (000800-001FFF) not write protected
    WRT1     = Off                                              ' Block 1 (002000-003FFF) not write protected
    WRTB     = Off                                              ' Boot block (000000-0007FF) not write protected
    WRTC     = On                                               ' Configuration registers (300000-3000FF) write protected
    WRTD     = Off                                              ' Data EEPROM not write protected
    EBTR0    = Off                                              ' Block 0 (000800-001FFF) not protected from table reads executed in other blocks
    EBTR1    = Off                                              ' Block 1 (002000-003FFF) not protected from table reads executed in other blocks
    EBTRB    = Off                                              ' Boot block (000000-0007FFh) not protected from table reads executed in other blocks
Config_End

It is untested, but looks OK, and compiles OK.

Regards
Les