Wie Word Variable zu Byte konvertieren
Messe Spannung mit meinem Atmega 8 am ADC Eingang, der gemessene Wert ist ein 10 Bit Wert, nun möchte ich den Wert im Seriellen EEprom speichern, der akzeptiert aber nur Byte Werte. Wie kann ich nun ein Word in zwei Byte umwandeln ?
Kann mir da einer helfen ??
Nase
Word in Byte konvertieren klappt nicht !! ??
Zitat:
Zitat von Bluesmash
Am einfachsten und ohne zu rechnen geht es mit einem overlay
Auszug aus der Bascom Hilfe:
OVERLAY
The OVERLAY option will not use any variable space. It will create a sort of phantom variable:
Dim x as Long at $60 'long uses 60,61,62 and 63 hex of SRAM
Dim b1 as Byte at $60 OVERLAY
Dim b2 as Byte at $61 OVERLAY
B1 and B2 are no real variables! They refer to a place in memory. In this case to &H60 and &H61. By assigning the phantom variable B1, you will write to memory location &H60 that is used by variable X.
So to define it better, OVERLAY does create a normal usable variable, but it will be stored at the specified memory location which could be already be occupied by another OVERLAY variable, or by a normal variable.
gruss bluesmash
So hab ich es versucht, klappte leider nicht.
Bei einer Eingangsspannung von 2,2 Volt werden mir 466 Bit angezeigt, soweit ja auch richtig.
Diese teile ich nun auf in B1 und B2 , dabei bekomme ich bei B1 den Wert 210 und bei B2 den Wert 1 angezeigt.
Wenn ich diese beiden allerdings unter B3 wieder addiere erhalte ich nicht wie Ursprünglich den Wert 466 sondern nur 211.
Es sei noch gesagt das Ich Laie bin im programmieren von Mikroprozessoren. Also wenn jemand hilft bitte etwas Rücksicht nehmen.
Hier einmal mein kleines Programm:
'------------------------------------------------------------------------------
' Display an folgenden Ports
' Enable = Port B.5
' RS = Port B.4
' DB7 = Port B.3
' DB6 = Port B.2
' DB5 = Port B.1
' DB4 = Port B.0
'--------------------------- Konfiguration-------------------------------------
$baud = 9600
$regfile "m8def.dat" 'Definiert den Atmega als Atmega 8
$crystal = 3686400 'Gibt die Quarzfrequenz an in Herz
Config Lcd = 16 * 2 'definiert das LCD Display in Art und Größe
'------------------------------ Analogeingang----------------------------------
Config Adc = Single , Prescaler = Auto , Reference = Avcc
Start Adc
Dim Analoginput1 As Long At $60
Dim B1 As Byte At $60 Overlay
Dim B2 As Byte At $61 Overlay
Dim B3 As Word
Ddrd = &B10100000
Portd = &B00001100
'------------------------------------------------------------------------------
Do
B3 = B1 + B2
Analoginput1 = Getadc(0)
Cls
Print Analoginput1
Print B1
Print B2
Print "-------------------------"
Print B3
Print "-------------------------"
Locate 1 , 1
Lcd Analoginput1
Locate 2 , 1
Lcd B1
Locate 2 , 7
Lcd B2
Wait 2
Loop