Japanino ArduinoISPで ATtiny13 で AD入力 (7) (9/4)
まずは、AD入力を。
こちらのサイト様で、回路を検討。
A/D 変換でボリューム(可変抵抗)値を読む
PB3をAD入力として50KΩの可変抵抗とプルアップ抵抗10KΩをつなぐ。
0~1023に出来るだけ近づけるにはプルアップ抵抗は変えられないとして、500KΩぐらいにすれば分子と分母が近くなって1に近づく。
実際は可変抵抗が無かったので1KΩの抵抗をつないで、1/(1+10)で1/11を掛けて5V*1/11=0.45V、1023*(0.45/5)=93と表示されるはず。
ソース:
'****************************************
' Copyright (c) 2016 k1segawa
' License : free
' Program : ADC Control
'****************************************
$regfile = "attiny13.dat"
$crystal = 9600000
$hwstack = 24
$swstack = 4
$framesize = 8
Config Scl = Portb. 1
Config Sda = Portb. 0
Config I2cdelay = 5
Config Clockdiv = 1
Config Adc = Single , Prescaler = Auto
Dim A As String * 10 , I As Integer , W As Byte
'--- Instruction Description Command ---
Const Ins_cleardsp = &H01
Const Ins_returnhm = &H02
Const Ins_setddradr = &H80
'--- I2C Init ---
I2cinit
'--- LCD Init ---
Waitms 40
I2cstart
I2cwbyte &H7C
I2cwbyte 0
I2cwbyte &H38
I2cwbyte &H39
I2cwbyte &H14
I2cwbyte &H70
I2cwbyte &H52 '56->52
I2cwbyte &H6C
Waitms 201
I2cwbyte &H38
I2cwbyte &H0C
Waitms 2
I2cstop
W = 1 : Gosub G_i2cwrite
Waitms 2
'--- Set DDRAM Address ---
W = Ins_setddradr Or &H40 : Gosub G_i2cwrite
'--- Anime : CGRAM Char No.0 Data Change ---
Start Adc
Do
W = Ins_setddradr Or &H00 : Gosub G_i2cwrite
Wait 1
I = Getadc(3)
A = Str(i)
A = A + " "
A = Left(a , 4)
A = "@AD=" + A
I2csend &H7C , A , 8
Loop
End
'--- Sub routine : Output Command ---
G_i2cwrite:
I2cstart
I2cwbyte &H7C
I2cwbyte &H80
I2cwbyte W
I2cstop
Return
' History
' [2016/09/04]
' PB3 ADC3 Input
不要なConst定義を削除し、A+のスペースは4つ。数値が1023で4桁なので桁が短い時に前の数字を消す処理を追加している。GetAD(3)はPB3ではなくADC3の意味。