PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Wie Word Variable zu Byte konvertieren



nase
26.06.2008, 14:40
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

PicNick
26.06.2008, 15:02
Am Einfachsten:


DIM Vbyte1 AS BYTE
DIM Vbyte2 AS BYTE
DIM Vword AS WORD AT Vbyte1 OVERLAY

Vword = Getadc(x)
schreiben eeram = Vbyte1
schreiben eeram = Vbyte2

Bluesmash
26.06.2008, 15:06
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

nase
26.06.2008, 15:16
Danke erst einmal, werd es einmal ausprobieren !

Vitis
26.06.2008, 21:45
oder gleich den ADC auf nur 8-Bit einstellen und eben nur
n Byte lesen :o)

Ausserdem kannste auch 10 Bit ins EEPROM schreiben,
nämlich 8 und 2 Bit jeweils als Byte

linux_80
27.06.2008, 21:44
Hallo,

wenn wir schon Bascom haben ist es wohl das einfachste wenn man eine Variable direkt ins EEprom legt:
DIM varname as Eram Word
Dann braucht man dieser nur den Wert zu übergeben, nur direkt arbeiten damit kann man aber nicht, also vom GetADC gleich ins EEprom (obwohl probiert hab ich das jetzt auch schon länger nicht mehr) !

PicNick
28.06.2008, 13:16
..as Eram Word ..
Er hat einen seriellen EEPROM. da wird ihm das nix nützen.

Getadc() direkt in ERAM Variablen geht tatsächlich nicht. Da macht sich schon der Kompiler ins Halstuch

linux_80
28.06.2008, 16:23
Aja, das seriell hab ich dort überlesen.

Mit dem ERAM, der will immer vom SRAM ins ERAM kopieren (oder andersrum), deshalb gehts vom Register nicht direkt ins ERAM :-k

nase
29.06.2008, 18:42
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

PicNick
29.06.2008, 18:46
..B3 = B1 + B2

Beim Zeus, das heisst natürlich (auf bascomisch)

B3 = B2 * 256
B3 = B3 + B1

Vitis
30.06.2008, 01:54
shift variable, right, 2

nase
30.06.2008, 20:48
..B3 = B1 + B2

Beim Zeus, das heisst natürlich (auf bascomisch)

B3 = B2 * 256
B3 = B3 + B1

Ja hab es so versucht und es klappt:
B2 = B2 * 256
B3 = B2 + B1

Damit haben mir alle sehr geholfen, Besten Dank für die schnelle und Kompetente Hilfe

Gruß nase