« [AVR]ATmega644P版ArduinoであるところのSanguinoのキットが到着したので作ってみた | Main | [iPhone][AVR]iPhone/iPod touch用Dock Connector型温度センサを作ってみた »

2008.10.26

[iPhone][AVR]続・iPhoneにAVRマイコン(Atmel ATtiny45)をつなぎ温度計測してみた

[iPhone][AVR]iPhoneにAVRマイコン(Atmel ATtiny45)をつなぎ温度計測してみたの続き。

AVR ATtiny45内蔵温度センサを使ってみたものの、どうにもわけ分からん結果しか出なかったので、温度センサLM60を外付けして再チャレンジ。

Iphoneavr06

例のアテにならない内蔵基準電圧については、動作環境での読み取り電圧をLM60からの出力をテスタで計測した電圧と大体合わせてやるという方法で適当に校正してやれば、それなりに動作しているようだ。SOTパッケージのLM60を買ってこよう。

Iphoneavr07_2

ソースは、内蔵ADC1を使い、LM60からの入力を読み取り、摂氏温度に変換するという処理内容。

BASCOM-AVRにはprintfの"%01.2f"みたいなものが無いみたいなので、文字列処理で小数点第1位まで表示。

'-----------------------------
'Compiler Options
'-----------------------------

$regfile = "attiny45.dat"
$crystal = 1000000

$hwstack = 24
$swstack = 16
$framesize = 32

'-----------------------------
'Variable Definitions
'-----------------------------

Dim I As Byte
Dim Sum As Word
Dim Intpart As Integer
Dim Fracpart As Single
Dim Fracpartstr As String * 11
Dim Res As Single

'-----------------------------
'Constant Definitions
'-----------------------------

'ADC1(Pin:7)
Const Channel = 1
'Reference Voltage
Const Refvcc = 1.1

'-----------------------------
'Peripheral Settings
'-----------------------------

'PB1(Pin:6): Serial TX
Open "COMB.1:9600,8,N,1" For Output As #1

'PB3(Pin:2): LED
Config Portb.3 = Output

Config Adc = Single , Prescaler = Auto , Reference = Internal_1.1
'Config Adc = Single , Prescaler = Auto , Reference = Avcc

'ADC Ref 1.1V / Use Temperature Sensor
'Admux = &B10001111

'ADC Ref VCC / Use ADC1
'Admux = &B00000001

Start Adc

'-----------------------------
'Main Loop
'-----------------------------

Do
   Sum = 0

   For I = 1 To 100
      Sum = Sum + Getadc(channel)
   Next I

   Rem (format "%01.2f" (* (/ (- (/ (* 123 4.98) 1024.0) 0.424) 6.25) 1000.0))

   'Average Up
   Sum = Sum / 100
   'Dependent on Reference Voltage
   Res = Sum * Refvcc
   Res = Res / 1024.0
   'Internal_1.1 Offset
   'Res = Res - 0.015
   Res = Res - 0.005
   'Debug Print
   'Print #1 , Res
   'DC Offset 424mV
   Res = Res - 0.424
   '6.25mV/C
   Res = Res / 6.25
   Res = Res * 1000.0

   'Formatting
   Intpart = Int(res)
   Fracpart = Frac(res)
   Fracpartstr = Str(fracpart)
   Fracpartstr = Mid(fracpartstr , 2 , 2)

   Print #1 , Intpart ; Fracpartstr

   'LED Flash
   Gosub Ledflash
Loop

Close #1

End

'-----------------------------
'Subroutine Definitions
'-----------------------------

Ledflash:
   Set Portb.3
   Waitms 200
   Reset Portb.3
   Waitms 500
   Return

« [AVR]ATmega644P版ArduinoであるところのSanguinoのキットが到着したので作ってみた | Main | [iPhone][AVR]iPhone/iPod touch用Dock Connector型温度センサを作ってみた »

blogparts

  • Last.fm
  • twitter

Recent Comments

無料ブログはココログ