Vydělávej až 160.000 Kč měsíčně! Akreditované rekvalifikační kurzy s garancí práce od 0 Kč. Více informací.
Hledáme nové posily do ITnetwork týmu. Podívej se na volné pozice a přidej se do nejagilnější firmy na trhu - Více informací.

Diskuze: 8-bit + 8-bit + 8-bit => 24-bit a 24-bit => 8-bit + 8-bit + 8-bit

Aktivity
Avatar
Jack
Člen
Avatar
Jack:19.12.2017 18:37

Dobrý den Pánové.
Předem moc děkuji za pomoc.
Nejsem odborník, ale pouze zapálený člověk pro elektroniku a informatiku.
Mám 24-bitový AD převodník, který posílá data do mikrokontroleru PIC18F4550.
Mikrokontroler rozloží 24-bitovou hodnotu na tři 8-bitové hodnoty a odešle je přes USB do PC.
No a já se snažím o jednoduchý program, který umí složit a rozložit tyto hodnoty.

Po stisknutí tlačítka "Command1" jsou spojeny tři 8-bitové hodnoty na jednu 24-bitovou hodnotu:

Private Sub Command1_Click()
    Dim bit_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bit_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bit_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bit_24_01 As Long    ' definition - joining three 8-bit numbers
    bit_08_01 = Text1.Text   ' first 8 bits to TextBox1
    bit_16_09 = Text2.Text   ' second 8 bits to TextBox2
    bit_24_17 = Text3.Text   ' third 8 bits to TextBox3

    ' translate

    Text4.Text = bit_24_01   ' joining three 8-bit numbers to TextBox4
End Sub

Po stisknutí tlačítka "Command2" je rozdělena jedna 24-bitová hodnota na tři 8-bitové hodnoty:

Private Sub Command2_Click()
    Dim bix_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bix_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bix_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bix_24_01 As Long    ' definition - joining three 8-bit numbers
    bix_24_01 = Text4.Text   ' joined three 8-bit numbers to TextBox4

    ' translate

    Text5.Text = bix_08_01   ' first 8 bits to TextBox5
    Text6.Text = bix_16_09   ' second 8 bits to TextBox6
    Text7.Text = bix_24_17   ' third 8 bits to TextBox7
End Sub
 
Odpovědět
19.12.2017 18:37
Avatar
Michal Žůrek - misaz:19.12.2017 21:54

Dělá se to pomocí shiftovacíh operároru a ORu.

Příklad (dosaď si tam své názvy)

Dim b1 = 64     ' 0100 0000
Dim b2 = 15     ' 0000 1111
Dim b3 = 234    ' 1110 1010

Dim num = (b1 << 16) Or (b2 << 8) Or (b3)
Debugger.Break()

Funguje to tak, že se hodnoty bitově posunou o 8násobky a pak se operátoem OR proloží

tzn. b1 se posová o 16, takže

z
                              |||| ||||
0000 0000 0000 0000 0000 0000 0100 0000
se stane
          |||| ||||
0000 0000 0100 0000 0000 0000 0000 0000

b2 se posová o 8, takže

z
                              |||| ||||
0000 0000 0000 0000 0000 0000 0000 1111
se stane
                    |||| ||||
0000 0000 0000 0000 0000 1111 0000 0000

a b3 se ponechává

                              |||| ||||
0000 0000 0000 0000 0000 0000 1110 1010

No pak se to "spojí" pomocí operátoru OR.

          |||| ||||
0000 0000 0100 0000 0000 0000 0000 0000
OR
                    |||| ||||
0000 0000 0000 0000 0000 1111 0000 0000
OR
                              |||| ||||
0000 0000 0000 0000 0000 0000 1110 1010

se rovná

0000 0000 0100 0000 0000 1111 1110 1010

tedy 4198378.

Správně bys ještě měl uvést že datový typ je místo Integer (Int32) UInt32, protože se jedná o neznaménkové číslo. Tím že nenastavuješ nejvyšší byte ti to však bude fungovat i se znaménkovým.

Editováno 19.12.2017 21:57
 
Nahoru Odpovědět
19.12.2017 21:54
Avatar
krepsy3
Tvůrce
Avatar
Odpovídá na Michal Žůrek - misaz
krepsy3:19.12.2017 22:45

Ve VB se nedá "Or" napsat jako "|"?

Nahoru Odpovědět
19.12.2017 22:45
Programátor je stroj k převodu kávy na kód.
Avatar
Odpovídá na krepsy3
Michal Žůrek - misaz:20.12.2017 4:55

Myslím ze ne.

 
Nahoru Odpovědět
20.12.2017 4:55
Avatar
Jack
Člen
Avatar
Odpovídá na Michal Žůrek - misaz
Jack:20.12.2017 10:19

Dobrý den Pánové.
Děkuji moc za vaše rady.
Zapoměl jsem však napsat, že jde o "Visual Basic 6.0".
Asi není možné vložit soubor ZIP.rar což mi komplikuje podat jednoduchou otázku.
Alespoň tedy přidávám obrázek a upravený kód.

Po stisknutí tlačítka "Command1" jsou spojeny tři 8-bitové hodnoty na jednu 24-bitovou hodnotu:

Private Sub Command1_Click()
    Dim bit_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bit_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bit_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bit_24_01 As Integer ' definition - joining three 8-bit numbers
    bit_08_01 = Text1.Text   ' first 8 bits from TextBox1
    bit_16_09 = Text2.Text   ' second 8 bits from TextBox2
    bit_24_17 = Text3.Text   ' third 8 bits from TextBox3

    ' bit_24_01 = (bit_24_17 \ 16) Or (bit_16_09 \ 8) Or (bit_08_01)

    Text4.Text = bit_24_01   ' joining three 8-bit numbers to TextBox4
End Sub

Po stisknutí tlačítka "Command2" je rozdělena jedna 24-bitová hodnota na tři 8-bitové hodnoty:

Private Sub Command2_Click()
    Dim bix_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bix_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bix_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bix_24_01 As Long    ' definition - joining three 8-bit numbers
    bix_24_01 = Text4.Text   ' joined three 8-bit numbers to TextBox4

    ' bix_08_01 = bix_24_01 (7-0)
    ' bix_16_09 = bix_24_01 (15-8)
    ' bix_24_17 = bix_24_01 (23-16)

    Text5.Text = bix_08_01   ' first 8 bits to TextBox5
    Text6.Text = bix_16_09   ' second 8 bits to TextBox6
    Text7.Text = bix_24_17   ' third 8 bits to TextBox7
End Sub
 
Nahoru Odpovědět
20.12.2017 10:19
Avatar
plelovsky
Člen
Avatar
Odpovídá na Jack
plelovsky:20.12.2017 13:02
Private Sub Command1_Click()
    Dim bit_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bit_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bit_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bit_24_01 As Integer ' definition - joining three 8-bit numbers
    bit_08_01 = Text1.Text   ' first 8 bits from TextBox1
    bit_16_09 = Text2.Text   ' second 8 bits from TextBox2
    bit_24_17 = Text3.Text   ' third 8 bits from TextBox3

    bit_24_01 = bit_24_17 * 65536 + bit_16_09 * 256 + bit_08_01

    Text4.Text = bit_24_01   ' joining three 8-bit numbers to TextBox4
End Sub

Private Sub Command2_Click()
    Dim bix_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bix_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bix_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bix_24_01 As Integer    ' definition - joining three 8-bit numbers
    bix_24_01 = Text4.Text   ' joined three 8-bit numbers to TextBox4

    bix_24_17 = bix_24_01 \ 65536
    bix_24_01 = bix_24_01 Mod 65536

    bix_16_09 = bix_24_01 \ 256
    bix_24_01 = bix_24_01 Mod 256

    bix_08_01 = bix_24_01

    Text5.Text = bix_08_01   ' first 8 bits to TextBox5
    Text6.Text = bix_16_09   ' second 8 bits to TextBox6
    Text7.Text = bix_24_17   ' third 8 bits to TextBox7
End Sub
 
Nahoru Odpovědět
20.12.2017 13:02
Avatar
Jack
Člen
Avatar
Jack:20.12.2017 13:51

Pane "plelovsky" - moc Vám děkuji. Problém je vyřešen.
Z důvodu přetečení jsem Integer nahradil Long a definoval jsem čísla "číslo" na CLng(číslo):

Po stisknutí tlačítka "Command1" jsou spojeny tři 8-bitové hodnoty na jednu 24-bitovou hodnotu:

Private Sub Command1_Click()
    Dim bit_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bit_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bit_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bit_24_01 As Long    ' definition - joining three 8-bit numbers
    bit_08_01 = Text1.Text   ' first 8 bits from TextBox1
    bit_16_09 = Text2.Text   ' second 8 bits from TextBox2
    bit_24_17 = Text3.Text   ' third 8 bits from TextBox3

    bit_24_01 = bit_24_17 * CLng(65536) + bit_16_09 * CLng(256) + bit_08_01

    Text4.Text = bit_24_01   ' joining three 8-bit numbers to TextBox4
End Sub

Po stisknutí tlačítka "Command2" je rozdělena jedna 24-bitová hodnota na tři 8-bitové hodnoty:

Private Sub Command2_Click()
    Dim bix_08_01 As Byte    ' definition - first 8 bits (7-0)
    Dim bix_16_09 As Byte    ' definition - second 8 bits (15-8)
    Dim bix_24_17 As Byte    ' definition - third 8 bits (23-16)
    Dim bix_24_01 As Long    ' definition - joining three 8-bit numbers
    bix_24_01 = Text4.Text   ' joined three 8-bit numbers to TextBox4

    bix_24_17 = bix_24_01 \ CLng(65536)
    bix_24_01 = bix_24_01 Mod CLng(65536)

    bix_16_09 = bix_24_01 \ CLng(256)
    bix_24_01 = bix_24_01 Mod CLng(256)

    bix_08_01 = bix_24_01

    Text5.Text = bix_08_01   ' first 8 bits to TextBox5
    Text6.Text = bix_16_09   ' second 8 bits to TextBox6
    Text7.Text = bix_24_17   ' third 8 bits to TextBox7
End Sub
 
Nahoru Odpovědět
20.12.2017 13:51
Avatar
plelovsky
Člen
Avatar
Odpovídá na Jack
plelovsky:20.12.2017 14:11

Pravda, ve VB 6 odpovídá datový typ Integer typu Short (Int16) v .NET, takže je potřeba použít Long, což odpovídá typu Integer (Int32) v .NET.

 
Nahoru Odpovědět
20.12.2017 14:11
Děláme co je v našich silách, aby byly zdejší diskuze co nejkvalitnější. Proto do nich také mohou přispívat pouze registrovaní členové. Pro zapojení do diskuze se přihlas. Pokud ještě nemáš účet, zaregistruj se, je to zdarma.

Zobrazeno 8 zpráv z 8.