128 lines
5.2 KiB
VB.net
128 lines
5.2 KiB
VB.net
'Imports Iot.Device.Board
|
|
'Imports Iot.Device.Ft232H
|
|
'Imports Iot.Device.FtCommon
|
|
'Imports System.Device.I2c
|
|
'Imports System.IO
|
|
'Imports System.Text
|
|
'Imports System.Threading
|
|
|
|
'Module Module1
|
|
' Dim i2cDevice As I2cDevice
|
|
' Dim modeBit As Integer = 8
|
|
|
|
' Sub Main()
|
|
' ' Disabilita la stampa degli errori sulla console
|
|
|
|
' ' Ottieni i dispositivi connessi
|
|
' Dim devices = FtCommon.GetDevices()
|
|
' Console.WriteLine($"{devices.Count} available device(s)")
|
|
|
|
' ' Verifica se ci sono dispositivi
|
|
' If devices.Count = 0 Then
|
|
' Console.WriteLine("No device connected")
|
|
' Return
|
|
' End If
|
|
|
|
' ' Seleziona il primo dispositivo FT232H
|
|
' Dim ftDevice As FtDevice = devices(0)
|
|
' Dim ft232h As New Ft232HDevice(ftDevice)
|
|
|
|
' ' Crea il bus I2C
|
|
' Dim i2c = ft232h.CreateOrGetI2cBus(0)
|
|
' Dim foundAddresses = i2c.PerformBusScan()
|
|
|
|
' ' Stampa gli indirizzi trovati
|
|
' If foundAddresses.Count > 0 Then
|
|
' ' Se il dispositivo è trovato all'indirizzo 0x27 (ad esempio per un LCD I2C)
|
|
' Dim deviceAddress As Byte = &H27
|
|
' Console.WriteLine($"Dispositivo trovato all'indirizzo: {deviceAddress:X2}")
|
|
|
|
' ' Crea il dispositivo I2C
|
|
' i2cDevice = i2c.CreateDevice(deviceAddress)
|
|
|
|
' ' Inizializzazione del display LCD I2C
|
|
' Try
|
|
' modeBit = 4
|
|
' ' Inizializza l'LCD
|
|
' InitializeLCDxx()
|
|
|
|
' ' Imposta il cursore su una posizione
|
|
' 'lcdSetCursor(1, 0) ' Posizione (1, 0) nella prima riga
|
|
' 'lcdSetCursor(4)
|
|
' ' Scrivi un messaggio
|
|
' Dim message As String = "Controllo:"
|
|
' ' Scrive l'intero messaggio in un'unica operazione
|
|
' Dim messageBytes As Byte() = Encoding.ASCII.GetBytes(message)
|
|
' For i As Integer = 0 To messageBytes.Length - 1
|
|
' lcdWritexx(messageBytes(i), 1) ' Invia i caratteri come dati
|
|
' Next
|
|
' Catch ex As Exception
|
|
' Console.WriteLine($"Errore durante la scrittura o l'inizializzazione: {ex.Message}")
|
|
' End Try
|
|
' Else
|
|
' Console.WriteLine("Nessun dispositivo trovato")
|
|
' End If
|
|
' End Sub
|
|
|
|
' Sub InitializeLCDxx()
|
|
' If modeBit = 4 Then
|
|
' ' Modalità 4-bit
|
|
' lcdWritexx(&H33, 0) ' Inizializza a 4-bit
|
|
' lcdWritexx(&H32, 0) ' Conferma modalità 4-bit
|
|
' lcdWritexx(&H28, 0) ' 2 righe, font 5x8
|
|
' lcdWritexx(&HC, 0) ' Display ON, cursore OFF
|
|
' lcdWritexx(&H6, 0) ' Scrittura da sinistra a destra
|
|
' lcdWritexx(&H1, 0) ' Pulisce il display
|
|
' Threading.Thread.Sleep(5) ' Pausa per completare l'operazione di clear display
|
|
' ElseIf modeBit = 8 Then
|
|
' ' Inizializzazione display (modalità 8 bit)
|
|
' lcdWritexx(&H38, 0) ' Funzione: modalità 8-bit, 2 righe, font 5x8
|
|
' lcdWritexx(&HC, 0) ' Display On, cursore OFF
|
|
' lcdWritexx(&H6, 0) ' Scrittura da sinistra a destra
|
|
' lcdWritexx(&H1, 0) ' Clear display
|
|
' Threading.Thread.Sleep(5) ' Pausa per completare l'operazione di clear display
|
|
' lcdWritexx(&HE, 0) ' Cursore lampeggiante
|
|
' Threading.Thread.Sleep(5)
|
|
' End If
|
|
' End Sub
|
|
|
|
' Sub lcdWritexx(value As Byte, mode As Byte)
|
|
' If modeBit = 4 Then
|
|
' ' Calcola la parte alta e bassa del comando
|
|
' Dim high As Byte = (value And &HF0) Or &H8 Or (mode << 0)
|
|
' Dim low As Byte = ((value << 4) And &HF0) Or &H8 Or (mode << 0)
|
|
|
|
' ' Scrivi la parte alta (E alta, E bassa)
|
|
' i2cDevice.Write(New Byte() {high})
|
|
' i2cDevice.Write(New Byte() {high Or &H4}) ' Imposta E alta
|
|
' i2cDevice.Write(New Byte() {high}) ' E bassa
|
|
|
|
' ' Scrivi la parte bassa (E alta, E bassa)
|
|
' i2cDevice.Write(New Byte() {low})
|
|
' i2cDevice.Write(New Byte() {low Or &H4}) ' Imposta E alta
|
|
' i2cDevice.Write(New Byte() {low}) ' E bassa
|
|
' ElseIf modeBit = 8 Then
|
|
' ' Scrittura a 8 bit
|
|
' Dim byteToSend As Byte = (value And &HFF) Or &H8 Or (mode << 0)
|
|
|
|
' ' Scrivi il comando/dato (byte a 8 bit)
|
|
' i2cDevice.Write(New Byte() {byteToSend})
|
|
|
|
' ' Aggiungi una pausa dopo la scrittura (tempo di abilitazione E)
|
|
' i2cDevice.Write(New Byte() {byteToSend Or &H4}) ' Imposta E alta
|
|
' Threading.Thread.Sleep(1) ' Breve ritardo per stabilizzare
|
|
' i2cDevice.Write(New Byte() {byteToSend}) ' Imposta E bassa per completare la scrittura
|
|
|
|
' ' Attendere un breve periodo per garantire che il display elabori il comando
|
|
' Threading.Thread.Sleep(2)
|
|
' End If
|
|
' End Sub
|
|
|
|
' Sub lcdSetCursor(line As Integer, Optional pos As Integer = 0)
|
|
' Dim addresses As Byte() = New Byte() {&H80, &HC0, &H94, &HD4} ' Indirizzi per LCD 4 righe
|
|
' If line >= 1 And line <= 4 Then
|
|
' lcdWritexx(addresses(line - 1) + CByte(pos), 0) ' Aggiungi la posizione al comando di indirizzo
|
|
' End If
|
|
' End Sub
|
|
'End Module
|