Hello people at freepascal.org
At the attached picture you can see my latest project. IMG_1428
It includes a raspberry 4 in a enclosure with 2 channels. For that it is equipped with two USB hubs for 6 different AVR USB programmers.
It switched with 2 board containing a 4066
So 2 channels, left en right to my TFT screen in de front panel.
It boots from a old 12V laptop adapter.
This system is controlled by a terminal app and a own delphi 10 application.
The SDA and SCL are connected with two 10K PULL-UP resistors
The code from our i2C unit:
unit i2c;
interface
type
{ TI2CMaster }
TI2CMaster = object
retVal: byte;
procedure init(bitRate: byte);
// Low level functions
// Address should be left adjusted 7-bit value
function start(address: byte; readTransaction: boolean): boolean;
function readByte(out data: byte; ack: boolean): boolean;
function writeByte(data: byte): boolean;
function stop: boolean;
// Higher level implementation
function ReadByteFromReg(i2caddress, regAddress: byte; out data: byte): boolean; overload;
function ReadBytesFromReg(i2caddress, regAddress: byte; data: PByte; size: byte): boolean; overload;
function ReadBytes(i2caddress: byte; const data: PByte; size: byte): boolean; overload;
function WriteByteToReg(i2caddress, regAddress: byte; const data: byte): boolean; overload;
function WriteBytesToReg(i2caddress, regAddress: byte; data: PByte; size: byte
): boolean; overload;
function WriteBytes(address: byte; const data: PByte; size: byte): boolean;
// data pointer should be pointer to flash memory
function WriteBytesToReg_P(i2caddress, regAddress: byte; data: PByte; size: byte
): boolean;
function clearStalledBus: boolean;
end;
Code left channel:
program WVW_LEFT;
uses
i2c, delay;
procedure ModePortB(Pin: byte; Value: Boolean);
begin
if Value then
begin
DDRB := DDRB or (1 shl pin);
end
else
begin
DDRB := DDRB and not (1 shl pin);
end;
end;
procedure WritePortB(Pin: byte; Value: Boolean);
begin
if Value then
begin
PORTB := PORTB or (1 shl pin);
end
else
begin
PORTB := PORTB and not (1 shl pin);
end;
end;
const AddressI2C = $68;
var
i2c_master: TI2CMaster;
i, wb ,data: byte;
S:String;
C:Char;
begin
i:=0;
wb:=0;
data:=0;
S:=' ';
C:=' ';
ModePortB(1, TRUE); // Status LED to output
i2c_master.init(I2C_50kHz);
S := 'Hello World!';
repeat
inc(i);
if i = 12 then i:=1;
i2c_master.start(AddressI2C,I2C_Wri]"]>Blockedde);
wb := Ord(S[i]);
i2c_master.writeByte(wb);
i2c_master.stop;
WritePortB(1,True); // Status LED OFF
delay_ms(20);
WritePortB(1,False); // Status LED ON
delay_ms(20);
until 1=2;
end.
Code right channel:
program LCDtest;
uses lcd_config, lcd_hd44780, delay, i2c;
procedure ModePortB(Pin: byte; Value: Boolean);
begin
if Value then
begin
DDRB := DDRB or (1 shl pin);
end
else
begin
DDRB := DDRB and not (1 shl pin);
end;
end;
procedure WritePortB(Pin: byte; Value: Boolean);
begin
if Value then
begin
PORTB := PORTB or (1 shl pin);
end
else
begin
PORTB := PORTB and not (1 shl pin);
end;
end;
function IntToStr(const i: Integer): string;
begin
Str(i, Result);
end;
function StrToInt(const s: string): Integer;
var
code: Integer;
begin
Val(s, Result, code);
end;
const AddressI2C = $68;
var
i2c_master: TI2CMaster;
i, Data: Byte;
C:Char;
S:String;
begin
i:=0;
C:=' ';
S:=' ';
Data := 0;
ModePortB(1, TRUE); // Status LED to output
lcd_init(lcd_displayOnCursorOnCursorBlink);
lcd_clrscr;
i2c_master.init(I2C_50kHz);
repeat
inc(i);
if i = 12 then begin i:=1; S:=''; lcd_clrscr; end;
i2c_master.start(AddressI2C,I2C_ReadMode);
i2c_master.ReadByte(Data,False);
i2c_master.stop;
S := S + Chr(Data);
lcd_gotoxy(1, 1); // Display
lcd_puts(S); // Send string to Display
WritePortB(1,True); // Status LED OFF
delay_ms(20);
WritePortB(1,False); // Status LED ON
delay_ms(20);
until 1=2;
end.
Now the question is weither somebode can help me out debugging this to pieces of pascal code. So it displays text on my lcd_hd44780 display. From the left MCU to the right MCU
Are you all crazy enough? Greets, PascalByThree, Wotuer van Wegen
PS: Al my MCU's on the breadboard are ATMEGA328P running at 8MHZ