PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Spannung über einen Analogeingang einlesen



Dane
24.07.2005, 12:46
Hallo,

ich habe gerade ein bisschen mit meinem ATMega32 rumgespielt. Und zwar habe ich an den PA0 ein 25k-Poti als einstellbaren Spannungsteiler angeschlossen.
Der Wert wird eingelesen und dann auf das angeschlossene LCD ausgegeben, also zwischen 0 (=0V) und 1023 (=5V). Das funktioniert auch soweit.
Nur wollte ich jetzt das Programm erweitern, dass der Wert umgerechnet wird, sodass anstatt 1023 5,00V angezeigt wird.
Irgendwie klappt aber das Umwandeln der Variablen nicht,
kann mir da jemand von Euch weiterhelfen, bin noch ein Noob...



'--------------------------------------------------------------------
' ADC_INT.BAS
' demonstration of GETADC() function in combintion with the idle mode
' for a better noise immunity
' Getadc() will also work for other AVR chips that have an ADC converter
'--------------------------------------------------------------------
$regfile = "m32def.dat" 'ATMega32
$crystal = 12000000 'Quarz 12 MHz
Config Lcdpin = Pin , Db4 = Portc.4 , Db5 = Portc.5 , Db6 = Portc.6 , Db7 = Portc.7 , E = Portc.3 , Rs = Portc.2
Config Lcd = 20 * 4 ' LCD mit 4 Zeilen und 20 Zeichen pro Zeile

'configure single mode and auto prescaler setting
'The single mode must be used with the GETADC() function

'The prescaler divides the internal clock by 2,4,8,16,32,64 or 128
'Because the ADC needs a clock from 50-200 KHz
'The AUTO feature, will select the highest clockrate possible
Config Adc = Single , Prescaler = Auto
'Now give power to the chip
On Adc Adc_isr Nosave
Enable Adc
Enable Interrupts


Dim W As Word , V As Double , Spannung As Double , Channel As Byte

Channel = 0
'now read A/D value from channel 0

Cls

Do
Channel = 0
'idle will put the micro into sleep.
'an interrupt will wake the micro.
Start Adc
Idle
Stop Adc
Locate 1 , 1 'springe in 1.Zeile 1.Spalte
Cls 'Clear Screen vom LCD
Lcd "Channel " ; Channel ; " value " ; W 'Ausgabe aufs LCD
V = W 'Variablenwert von W in V übertragen
Spannung = V / 204.6 'umrechnen, damit 1023 5V entspricht
Lowerline 'springe in 2. Zeile
Lcd "Spannung: " ; Spannung ; "V" 'Ausgabe aufs LCD
Waitms 500
Loop
End

Adc_isr:
push r24
in r24,sreg
push r24
push r25
W = Getadc(channel)
pop r25
pop r24
!out sreg,r24
pop r24
Return


MfG, Dane

Dane
24.07.2005, 12:48
Ich hatte vergessen zu erwähnen, dass in der Zeile

Spannung = V / 204.6

eine Fehlermeldung kommt "Invalid datatype"

Dane

24.07.2005, 13:26
Damit geht's
Dim Spannung As Single

Dane
24.07.2005, 13:51
Hm, das hat aber auch nichts geändert.
Double ist ja praktisch nur eine "größere" Single

Dane

linux_80
24.07.2005, 14:13
Evtl. kommt der fehler von einer Zeile weiter oben, da steht V = W
und W ist ein Word ?!

LuK-AS
24.07.2005, 14:38
Probier mal das hier, bei mir funzt das absolut genau.

Dim Volt As Single
Dim Cap As Single
Dim Caps As String * 3
Dim Volts As String * 4

Do
C2 = Getadc(2)
Volt = C2
Volt = Volt / 73.75
Volts = Str(volt)
Volts = Left(volts , 4)
Locate 1 , 11
Lcd Volts ; " V"
Cap = Volt - 6.7
Cap = Cap / 6.3
Cap = Cap * 100
Caps = Str(cap)
Caps = Left(caps , 2)
Locate 2 , 13
Lcd Caps ; " %"
Loop

Viel Spass damit
Gruss Clemens