LoginSignup
0
0

PICAXE奮闘記06 ロードセルの加重をHX711

Last updated at Posted at 2023-08-31

今回はスケーリングした値を0.1g単位で表示します。
せっかくのカスタム重量計なので、荷重によって表示内容を変えてみました。
重さによってSサイズ、Mサイズ、Lサイズと切り替わります。

IMG_0199.JPG
IMG_0201.JPG

'**************************************
'     I2C LCD test Display test 
'	AE-AQM1602A(KIT)
'     *3.1V to 3.5V!! Not use AA battry*3=4.5V
'     (I2C LCD 16 x 2 lines)
'	 for PICAXE 08M2
'**************************************
'	Information of Connection
'		pin1 VCC
'		pin2 serial in
'		pin3 out HX711 SCK in
'		pin4 in HX711 DT OUT
'	----
'		pin8 GND
'		pin7 serial out
'		pin6 SCL(10kohm pull up by lcdkit)
'		pin5 SDA(10kohm pull up by lcdkit)
'**************************************


#PICAXE 08M2
#NO_DATA
#NO_DEBUG	'プログラム中のDEBAGコマンドを無効にする。デバッグする時はコメントアウトする。

#macro CaractorSet(Command,Byte0,Byte1,Byte2,Byte3,Byte4,Byte5,Byte6,Byte7)
	hi2cout ($00,Command)
	pause 50
	hi2cout ($40,Byte0,Byte1,Byte2,Byte3,Byte4,Byte5,Byte6,Byte7)
	pause 50
#endmacro
#macro BitRead()
	pulsout c.4,1	'4MHz 10us unit,8MHz 5us unit
    w0 = w0*2 '1ビット左シフト
	if pinc.3=1 then
		inc w0 '最下位ビットを追加
	end if
#endmacro
#macro BitSkip()
	pulsout c.4,1	'4MHz 10us unit,8MHz 5us unit
	PAUSEUS 1	'4MHz 10us unit,8MHz 5us unit
#endmacro
setfreq m8

low c.4
'I2C initialize
hi2csetup i2cmaster,$7C, i2cfast, i2cbyte 'AKIDUKI LCD
hi2cout ($00,$01)	'Clear display
pause 50
hi2cout ($00,$38)	'拡張コマンドOFF
pause 50		'Wait
hi2cout ($00,$0C)	'ディスプレイON カーソルOFF
pause 50		'Wait
hi2cout ($00,$06)	'エントリーモードセット
pause 300		'Wait
hi2cout ($00,$39)	'コントラスト設定を行うため拡張命令ON
pause 50		'Wait
symbol Contrast = 30	'0 - 32 - 63 $00 - $3f 液晶のコントラスト設定値
'定数同士の計算は予め計算されるため、プログラムサイズに影響無し。
symbol ContrastL1 = Contrast // 16
symbol ContrastL2 = ContrastL1 or $70
hi2cout ($00,ContrastL2)	'液晶コントラスト下位指定
pause 50
symbol ContrastH1 = Contrast / 16
symbol ContrastH2 = ContrastH1 or $54
hi2cout ($00,ContrastH2)	'液晶コントラスト上位指定
pause 50
hi2cout ($00,$6C)	'フォロワー制御設定
pause 50
hi2cout ($00,$38)	'拡張命令OFF
pause 50
hi2cout ($00,$01)	'Clear display
pause 50

'Command,Byte0,Byte1,Byte2,Byte3,Byte4,Byte5,Byte6,Byte7
'Command = Set CGRam Address $40,$48
CaractorSet($40,$00,$0A,$00,$04,$00,$11,$0E,$00)'キャラクタ0 スマイル
CaractorSet($48,$04,$0E,$0e,$0e,$1f,$00,$04,$00)'キャラクタ1 ベル

'Displaying Hello world
hi2cout ($00,$80)	'Set DDR Address
pause 50		'Wait
hi2cout ($40,"HX711 Read  ",$00,$01)

gosub LoadcellRead
w5 = w0'起動時の計測値をオフセットのため保存
'起動時は物が乗ってない状態でお願いします。

main:
	gosub LoadcellRead
	w0 = w0 - w5
	b8 ="+"
	if b1>=128 then
		w0 = 0-w0
		b8 ="-"
	end if
	'スケーリング 19257 を 10000 (1000.0)とするスケーリング。
	'
	'10000 = $2710
    '19257 = $4B39
	'$27100000 = $4B39 * X
	'X = $2710 0000 / $4B39 = $84F0
	'確認
	'$4B39 * $84F0 = $270F E970
	'$270F = 9999
	'1word(16bit) * 1word(16bit)=2word(32bit)
	'** 演算子は計算結果のhigh wordが帰ってくる。
	w0 = w0 ** $84F0	'high word return

	bintoascii w0,b6,b5,b4,b3,b2	' convert to ascii

	'最上位桁のゼロサプレス処理
ZeroSuppressStart:
	if b6 <> "0" then goto ZeroSuppressEnd
	 b6 = " "
	if b5 <> "0" then goto ZeroSuppressEnd
	 b5 = " "
	if b4 <> "0" then goto ZeroSuppressEnd
	 b4 = " "
ZeroSuppressEnd:
	hi2cout ($00,$c2)	'Set DDR Address
	hi2cout ($40,b8,b6,b5,b4,b3,".",b2," g")
'重さによって上段の表示を変える。
TopLineMessageStart:
	hi2cout ($00,$80)	'Set DDR Address
	if w0 < 500 then'50.0g以下の表示
		hi2cout ($40,"キカクガイ         ")
		goto TopLineMessageEnd
	end if
	if w0 < 1000 then'100.0g以下の表示
		hi2cout ($40,"  Sサイズ        ")
		goto TopLineMessageEnd
	end if
	if w0 < 2000 then'200.0g以下の表示
		hi2cout ($40,"     Mサイズ     ")
		goto TopLineMessageEnd
	end if
	'その他の表示
	hi2cout ($40,"         Lサイズ ")
TopLineMessageEnd:
	pause 200		'wait 0.2sec
	goto main
LoadcellRead:
	do
	  if pinC.3 = 0 then exit	; exit if pinC.3 is low
	loop
	let w0 = 0
	 '最上位ビットに正負の極性が残る範囲で
	 '24ビット幅のいいとこらへん16ビットを取得する。
	BitSkip
	BitSkip
	BitSkip
	
	BitRead
	BitRead
	BitRead
	BitRead
	
	BitRead
	BitRead
	BitRead
	BitRead
	
	BitRead
	BitRead
	BitRead
	BitRead
	
	BitRead
	BitRead
	BitRead
	BitRead

	BitSkip
	
	BitSkip
	BitSkip
	BitSkip
	BitSkip
	BitSkip	'25発パルスを入れた
	return
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0