Hi , do you know how much of the forum help me convert a code to b4j where am I wrong?
code to be reproduced
********************************************************************************************/
#include <stdio.h>
#include <wiringPi.h>
#include <wiringPiI2C.h>
#include <string.h>
int LCDAddr = 0x27;//IIc LCD address
int BLEN = 1;//1--open backlight.0--close backlight
int fd;//linux file descriptor
//writ a word(16 bits) to LCD
void write_word(int data){
int temp = data;
if ( BLEN == 1 )
temp |= 0x08;
else
temp &= 0xF7;
//printf("%d\n Word scrittura SOLO - ",temp);
wiringPiI2CWrite(fd, temp);
}
//send command to lcd
void send_command(int comm){
int buf;
// Send bit7-4 firstly
buf = comm & 0xF0;
// printf("%d\n buffeffere1 commando AND - ",buf);
buf |= 0x04; // RS = 0, RW = 0, EN = 1
// printf("%d\n buffeffere1-1 commando OR- ",buf);
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
// printf("%d\n buffeffere2 send commando AND -",buf);
write_word(buf);
// Send bit3-0 secondly
buf = (comm & 0x0F) << 4;
// printf("%d\n buffeffere3 send commando AND - ",buf);
buf |= 0x04; // RS = 0, RW = 0, EN = 1
// printf("%d\n buffeffere3-1 send commando OR- ",buf);
write_word(buf);
delay(2);
buf &= 0xFB;
// printf("%d\n buffeffere3 send commando AND -",buf); // Make EN = 0
write_word(buf);
}
//send data to lcd
void send_data(int data){
int buf;
//printf("%d\n primo numero data - ",data);
// Send bit7-4 firstly
buf = data & 0xF0;
// printf("%d\n buffeffere1 send data AND - ",buf);
buf |= 0x05; // RS = 1, RW = 0, EN = 1
// printf("%d\n buffeffere1-1 send data OR- ",buf);
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
//printf("%d\n buffeffere2 send data AND- " ,buf);
write_word(buf);
// Send bit3-0 secondly
buf = (data & 0x0F) << 4;
//printf("%d\n buffeffere3 send data AND- ",buf);
buf |= 0x05; // RS = 1, RW = 0, EN = 1
// printf("%d\n buffeffere3-1 send data OR- ",buf);
write_word(buf);
delay(2);
buf &= 0xFB; // Make EN = 0
//printf("%d\n buffeffere4 send data AND- ",buf);
write_word(buf);
}
//initialize the lcd
void init(){
send_command(0x33); // Must initialize to 8-line mode at first
delay(5);
send_command(0x32); // Then initialize to 4-line mode
delay(5);
send_command(0x28); // 2 Lines & 5*7 dots
delay(5);
send_command(0x0C); // Enable display without cursor
delay(5);
send_command(0x01); // Clear Screen
wiringPiI2CWrite(fd, 0x08);
}
//clear screen
void clear(){
send_command(0x01); //clear Screen
}
//Print the message on the lcd
void write(int x, int y, char data[]){
int addr, i;
int tmp;
if (x < 0) x = 0;
if (x > 15) x = 15;
if (y < 0) y = 0;
if (y > 1) y = 1;
//printf("X: %*d\n",2,x);
//printf("Y: %*d\n" 2,y);
printf ("WXXX: %*d \n", 5, x);
printf ("YYY: %*d \n", 5, y);
// Move cursor
addr = 0x80 + 0x40 * y + x;
// printf("%x\n e ADR ",addr);
send_command(addr);
tmp = strlen(data);
printf ("YYY: %*d \n", 10, tmp);
for (i = 0; i < tmp; i++){
//printf("%c\n e questo ",data[i]);
send_data(data[i]);
}
}
void print_info()
{
printf("\n");
printf("|***************************|\n");
printf("| IIC 1602 LCD test |\n");
printf("| --------------------------|\n");
printf("| | LCD | | Pi |\n");
printf("| --------------------------|\n");
printf("| | GND | connect to | GND |\n");
printf("| | VCC | connect to | 5V |\n");
printf("| | SDA | connect to | SDA.1|\n");
printf("| | SCL | connect to | SCL.1|\n");
printf("| --------------------------|\n");
printf("| OSOYOO|\n");
printf("|***************************|\n");
printf("Program is running...\n");
printf("Press Ctrl+C to end the program\n");
}
int main(){
//init I2C
fd = wiringPiI2CSetup(LCDAddr);
// printf("%x\n I2CSetup",fd);
init();
print_info();
write(0, 0, "AIUTO");
write(0, 1, "SECONDA");
delay(3000);
// clear();
// while(1){
// write(0,0,"This is Lesson13");
// write(0,1,"IIC LCD Test");
// delay(1000);
//}
return 0;
}
code to be reproduced
b4j:
Dim LCDAddr As Object = 0x27
Dim LCD_Backlight As Byte = 0x08
Dim BLEN As Int = 0
Dim fd As JavaObject
Dim fd1 As Int
Private Sub write_word(data As Int)
Dim temp As Int = data
If BLEN = 1 Then
' temp = Bit.And(data,0x08)
ADCDEVICE.wiringPiI2CWrite(fd, Bit.And(data,0x08))
Else
' temp = Bit.Or(data,0xF7)
ADCDEVICE.wiringPiI2CWrite(fd, Bit.Or(data,0xF7))
End If
'ADCDEVICE.wiringPiI2CWrite(fd, temp)
End Sub
'send control command to lcd
Private Sub send_command(comm As Int)
Dim buf As Int
' Send bit7-4 firstly
write_word(Bit.And(Bit.Or(comm, 0xF0), 0x04))
' buf = Bit.And(comm,0xF0)
' buf =Bit.Or(comm, 0x04)
' write_word(buf)
' Sleep(2)
buf = Bit.And(comm,0xFB)' Make EN = 0
write_word(buf)
' Send bit3-0 secondly
write_word(Bit.And(Bit.Or(comm, 0x0F), 0x04))
' buf = Bit.And(comm,0x0F)
' buf = Bit.Or(comm,0x04)
' write_word(buf)
' Sleep(2)
buf = Bit.And (comm,0xFB) ' Make EN = 0
write_word(buf)
' Sleep(2)
End Sub
Private Sub send_data(data As Int)
Dim buf As Int
' Send bit7-4 firstly
write_word(Bit.And(Bit.Or(data, 0xF0), 0x05))
' buf = Bit.And(data,0xF0)
' buf = Bit.Or(data,0x05) ' RS = 1, RW = 0, EN = 1
' write_word(buf)
' Sleep(2)
buf = Bit.And(data,0xFB) ' Make EN = 0
write_word(buf)
'Send bit3-0 secondly
'(data And &HF) buf = (data And &HF) buf Or &H5 ' RS = 1, RW = 0, EN = 1
write_word(Bit.And(Bit.Or(data, 0x0f), 0x05))
' buf = Bit.And(data,0x0f)
' buf = Bit.Or(data,0x05)
' write_word(buf)
' Sleep(2)
buf = Bit.And(data,0xFB) ' Make EN = 0
write_word(buf)
' Sleep(2)
End Sub
Private Sub Clear
send_command(0x01)
End Sub
'inizia lcd
Private Sub init()
send_command(0x33) ' Must initialize to 8-line mode at first
' Sleep(5)
send_command(0x32) ' Then initialize to 4-line mode
' Sleep(5)
send_command(0x28) ' 2 Lines & 5*7 dots
' Sleep(5)
send_command(0x0C) ' Enable display without cursor
' Sleep(5)
send_command(0X01) ' Clear Screen
ADCDEVICE.wiringPiI2CWrite(fd, LCD_Backlight)
Sleep(5)
End Sub
'Print the message on the lcd
Private Sub write( x As Int, y As Int, data As String)
Dim addr As Int
Dim i As Int
Dim tmp As Int
If x < 0 Then
x = 0
End If
If x > 15 Then
x = 15
End If
If y < 0 Then
y = 0
End If
If y > 1 Then
y = 1
End If
' Move cursor
addr = 0x80 + 0x40 * y + x
send_command(addr)
tmp = data.Length
For i = 0 To tmp - 1
send_data(i)
Next
End Sub
Public Sub lcdinizia
fd = Me
fd = ADCDEVICE.wiringPiI2CSetup(0x27)
If CInt(fd) <0 Then
Log(" ==>> GPIO SETUP FAILED")
Else
Log("Fatto")
End If
init
write(0, 0, "Hi man.Welcome ")
write(0, 1, "to osoyoo.com")
Sleep(3000)
Clear
Do While True
write(0,0,"This is Lesson13")
write(0,1,"IIC LCD Test")
Sleep(1000)
Loop
End Sub
Sub CInt(o As Object) As Int
Return Floor(o)
End Sub
Last edited: