Hallo Forum,
Ich wollte jetzt mal anfangen in C zu programmieren, da Basic ja nicht so "top" ist und andere µCs ja auch fasst nur in C programmiert werden.
Jetzt habe ich die Lib vom LCD zu meinem Pro-Bot128 nur in Basic.
Hier der Basic-Code:
Code:
'-------------------------------------------------------------------------------
'I2C zu LCD über Porterweiterung Philips PCF8574T
'Ausgabe von Text als Char und Zahlen wie Word und Float
'Uref ist Versorgung 5Volt, andere Möglichkeiten siehe Hilfe
'Hier sehen Sie, welche PCF 8574 Ports dem LCD zugeordnet sind:
'Port 0 LCD DB4
'Port 1 LCD DB5
'Port 2 LCD DB6
'Port 3 LCD DB7
'Port 4 LCD R/W
'Port 5 LCD RS
'Port 6 LCD EN
'-------------------------------------------------------------------------------
Dim Daten As Byte 'Datenbyte für I²C-Bus- Routinen
Dim LCD_Param As Byte 'Datenbyte für LCD-Routinen
Dim LCD_Buffer As Byte 'zweites Byte für LCD- Routinen
Dim Text_Out_LCD As Char
'---[ LCD INIT ]----------------------------------------------------------------
Sub I2C_LCD_INIT()
I2C_Init(I2C_100kHz) '100kHz
'alle Ports 0
Daten = 0
I2C_SEND()
'8-Bit-Modus aktivieren
I2C_LCD_WRITE_COMMAND(&H38)
'mit 8-Bit-Modus in 4-Bit-Modus wechseln
Daten = &H02
I2C_SEND()
Daten = Daten + &H40
I2C_SEND()
AbsDelay(5)
Daten = Daten - &H40
I2C_SEND()
AbsDelay(5)
'ab jetzt 4-Bit-Modus / Cursor aus
I2C_LCD_WRITE_COMMAND(&H28)
End Sub
'---[ Cursor Blinken ein ]------------------------------------------------------
Sub BLINK_ON()
I2C_LCD_WRITE_COMMAND(&H0F)
End Sub
'---[ Cursor Blinken aus ]------------------------------------------------------
Sub BLINK_OFF()
I2C_LCD_WRITE_COMMAND(&H0C)
End Sub
'---[ Cursor sichtbar ohne blinken ]--------------------------------------------
Sub CURSOR_NO_FLASH()
I2C_LCD_WRITE_COMMAND(&H0E)
End Sub
'---[ Display löschen ]---------------------------------------------------------
Sub I2C_LCD_CLR()
I2C_LCD_WRITE_COMMAND(&H02)
I2C_LCD_WRITE_COMMAND(&H01)
End Sub
'---[ Zeilenwechsel ]-----------------------------------------------------------
Sub I2C_LCD_LOCATE(Zeile As Byte, Pos As Byte)
Dim Position As Byte
If Zeile = 1 Then
Position = (Pos - 1)
I2C_LCD_WRITE_COMMAND(Position Or &H80)
End If
If Zeile = 2 Then
Position = (Pos - 1)
I2C_LCD_WRITE_COMMAND(Position Or &HC0)
End If
If Zeile = 3 Then
Position = (Pos - 1)
I2C_LCD_WRITE_COMMAND(Position Or &H90)
End If
If Zeile = 4 Then
Position = (Pos - 1)
I2C_LCD_WRITE_COMMAND(Position Or &HD0)
End If
End Sub
'---[ LCD Steuerkommando ]------------------------------------------------------
Sub I2C_LCD_WRITE_COMMAND(com As Byte)
LCD_Buffer = &H0
LCD_Param = com
WRITE_TO_LCD()
End Sub
'---[ LCD_Write_Float_Ext ]-----------------------------------------------------
Sub I2C_LCD_WRITE_FLOAT(Var As Single, Len As Word)
Dim Str_Float(17) As Char
Dim x As Word
x = 0
Str_WriteFloat(Var,Len,Str_Float,0)
Do While True
Text_Out_LCD = Str_Float(x)
x = x + 1
If Text_Out_LCD = 0 Then
Exit
End If
I2C_LCD_WRITE(Text_Out_LCD)
End While
End Sub
'---[ LCD_Write_Word_Ext ]------------------------------------------------------
Sub I2C_LCD_WRITE_WORD(Var As Word, Len As Word)
Dim Str_Word(21) As Char
Dim x As Word
x = 0
Str_WriteWord(Var,10,Str_Word,0,Len)
Do While True
Text_Out_LCD = Str_Word(x)
x = x + 1
If Text_Out_LCD = 0 Then
Exit
End If
I2C_LCD_WRITE(Text_Out_LCD)
End While
End Sub
'---[ LCD_Write_Text ]----------------------------------------------------------
Sub I2C_LCD_WRITE_TEXT(ByRef text As Char)
Dim x As Word
x = 0
Do While True
Text_Out_LCD = text(x)
x = x + 1
If Text_Out_LCD = 0 Then
Exit
End If
I2C_LCD_WRITE(Text_Out_LCD)
End While
End Sub
'---[ LCD_Write_Ext ]-----------------------------------------------------------
Sub I2C_LCD_WRITE(Text_Print As Char)
LCD_Buffer = &H20
LCD_Param = Text_Print
WRITE_TO_LCD()
End Sub
'---[ Ausgabe über I2C ]--------------------------------------------------------
Sub WRITE_TO_LCD()
'Hi-Nibble
Daten = LCD_Buffer Or (LCD_Param >> 4)
I2C_SEND()
Daten = Daten + &H40
I2C_SEND()
AbsDelay(1)
Daten = Daten - &H40
I2C_SEND()
AbsDelay(1)
'Low-Nibble
Daten = LCD_Buffer Or (LCD_Param And &H0F)
I2C_SEND()
Daten = Daten + &H40
I2C_SEND()
AbsDelay(1)
Daten = Daten - &H40
I2C_SEND()
AbsDelay(1)
End Sub
'---[ I2C Routine ]-------------------------------------------------------------
Sub I2C_SEND()
I2C_Start()
I2C_Write(I2C_LCD_ADDR)
I2C_Write(Daten)
I2C_Stop()
End Sub
So... jetzt habe ich das gröbste schon in C überstetzt aber hier sind immer noch Fehler drinn(meldet die IDE)
Hier mein überstetzter Code in C:#
Code:
/*-------------------------------------------------------------------------------
'I2C zu LCD über Porterweiterung Philips PCF8574T
'Ausgabe von Text als Char und Zahlen wie Word und Float
'Uref ist Versorgung 5Volt, andere Möglichkeiten siehe Hilfe
'Hier sehen Sie, welche PCF 8574 Ports dem LCD zugeordnet sind:
'Port 0 LCD DB4
'Port 1 LCD DB5
'Port 2 LCD DB6
'Port 3 LCD DB7
'Port 4 LCD R/W
'Port 5 LCD RS
'Port 6 LCD EN
------------------------------------------------------------------------------*/
byte Daten; //Datenbyte für I²C-Bus- Routinen
byte LCD_Param; //Datenbyte für LCD-Routinen
byte LCD_Buffer; //zweites Byte für LCD- Routinen
byte Text_Out_LCD;
//---[ LCD INIT ]----------------------------------------------------------------
void I2C_LCD_INIT(void)
{
I2C_Init(I2C_100kHz); //100kHz
//alle Ports 0
Daten = 0;
I2C_SEND();
//8-Bit-Modus aktivieren
I2C_LCD_WRITE_COMMAND &H38;
//mit 8-Bit-Modus in 4-Bit-Modus wechseln
Daten = H02;
I2C_SEND();
Daten = Daten + H40;
I2C_SEND();
AbsDelay(5);
Daten = Daten - H40;
I2C_SEND();
AbsDelay(5);
//ab jetzt 4-Bit-Modus / Cursor aus
I2C_LCD_WRITE_COMMAND &H28;
}
//---[ Cursor Blinken ein ]------------------------------------------------------
void BLINK_ON(void)
{
I2C_LCD_WRITE_COMMAND &H0F;
}
//---[ Cursor Blinken aus ]------------------------------------------------------
void BLINK_OFF(void)
{
I2C_LCD_WRITE_COMMAND &H0C;
}
//---[ Cursor sichtbar ohne blinken ]--------------------------------------------
void CURSOR_NO_FLASH(void)
{
I2C_LCD_WRITE_COMMAND &H0E;
}
//---[ Display löschen ]---------------------------------------------------------
void I2C_LCD_CLR(void)
{
I2C_LCD_WRITE_COMMAND &H02;
I2C_LCD_WRITE_COMMAND &H01;
}
//---[ Zeilenwechsel ]-----------------------------------------------------------
void I2C_LCD_LOCATE(byte Zeile,byte Pos)
{
byte Position;
if(Zeile == 1){
Position = (Pos - 1);
I2C_LCD_WRITE_COMMAND(Position &H80);
}
if(Zeile == 2){
Position = (Pos - 1);
I2C_LCD_WRITE_COMMAND(Position &HC0);
}
if(Zeile == 3){
Position = (Pos - 1);
I2C_LCD_WRITE_COMMAND(Position &H90);
}
if(Zeile == 4){
Position = (Pos - 1);
I2C_LCD_WRITE_COMMAND(Position &HD0);
}
}
//---[ LCD Steuerkommando ]------------------------------------------------------
void I2C_LCD_WRITE_COMMAND(byte com)
{
LCD_Buffer = H0;
LCD_Param = com;
WRITE_TO_LCD();
}
//---[ LCD_Write_Float_Ext ]-----------------------------------------------------
void I2C_LCD_WRITE_FLOAT(Var As Single, Len As Word)
{
char Str_Float(17);
word x;
x = 0;
Str_WriteFloat(Var,Len,Str_Float,0);
while(true){
Text_Out_LCD = Str_Float(x);
x = x + 1;
if(Text_Out_LCD == 0){
Exit;
}
I2C_LCD_WRITE(Text_Out_LCD);
}
}
//---[ LCD_Write_Word_Ext ]------------------------------------------------------
void I2C_LCD_WRITE_WORD(word Var,word Len)
{
char Str_Word(21);
word x;
x = 0;
Str_WriteWord(Var,10,Str_Word,0,Len);
while(true){
Text_Out_LCD = Str_Word(x);
x = x + 1;
if(Text_Out_LCD == 0){
Exit;
}
I2C_LCD_WRITE(Text_Out_LCD)
}
}
//---[ LCD_Write_Text ]----------------------------------------------------------
void I2C_LCD_WRITE_TEXT(char ByRef text)
{
word x;
x = 0;
while(true){
Text_Out_LCD = text(x);
x = x + 1;
if(Text_Out_LCD == 0){
Exit;
}
I2C_LCD_WRITE(Text_Out_LCD)
}
}
//---[ LCD_Write_Ext ]-----------------------------------------------------------
void I2C_LCD_WRITE(char Text_Print)
{
LCD_Buffer = &H20;
LCD_Param = Text_Print;
WRITE_TO_LCD();
}
//---[ Ausgabe über I2C ]--------------------------------------------------------
void WRITE_TO_LCD(void)
{
//Hi-Nibble
Daten = LCD_Buffer Or (LCD_Param >> 4);
I2C_SEND();
Daten = Daten + &H40;
I2C_SEND();
AbsDelay(1);
Daten = Daten - &H40;
I2C_SEND();
AbsDelay(1);
//Low-Nibble
Daten = LCD_Buffer Or (LCD_Param And &H0F);
I2C_SEND();
Daten = Daten + &H40;
I2C_SEND();
AbsDelay(1);
Daten = Daten - &H40;
I2C_SEND();
AbsDelay(1);
}
//---[ I2C Routine ]-------------------------------------------------------------
void I2C_SEND(void)
{
I2C_Start();
I2C_Write(I2C_LCD_ADDR);
I2C_Write(Daten);
I2C_Stop();
}
Wäre nett wenn einer mal drüber schaun konnte da ich mit meinem bisherigen wissen am Ende bin.
Mfg
bnitram
Lesezeichen