Example I2C driver in Turbo Pascal for SC407/SC144

293 views
Skip to first unread message

MogensB

unread,
Apr 1, 2021, 6:55:03 AM4/1/21
to retro-comp
I thought I wanted to share my very basic I2C driver for the SC407 I2C digital I/O board.
It is a very simple and rude Turbo Pascal implementation, so most likely it is not very accurate and timing and error handling is on the loose side :)

My goal was to simply test the SC407/SC144 board hardware is working (and it is).

The program compiles with Borland Turbo Pascal 3 for CP/M. I have only tested the program on my SC140 main board with SC144/SC407 I2C boards. Note there is a delay statement in the output loop; the only purpose is to slow down the loop so you can actually see something is happening - you can remove it.

So feel free to use the program for whatever you can, no warranties however!

Regards,
Mogens

I2C.PAS

MogensB

unread,
Apr 1, 2021, 8:29:23 AM4/1/21
to retro-comp
FWIW, I captured the I2C bus with a very cheap USB logic analyzer and it is not too bad, I guess, but fairly slow... about 9 KHz which is way below I2C 100 Khz "Standard" mode.
But, as far as I can tell, there is no real "minimum speed" in I2C?

I2C-sample.JPG

MogensB

unread,
Apr 1, 2021, 1:02:35 PM4/1/21
to retro-comp

MogensB

unread,
Apr 2, 2021, 6:50:57 AM4/2/21
to retro-comp
Now also tested on SC126, and confirmed working. I made some slight performance improvements so speed is now approx. 15 KHz. Use the github link above for the newer version.

Colin Little

unread,
Apr 4, 2021, 4:16:13 AM4/4/21
to retro-comp
Thank you for this , it works perfectly on my SC126 - Amazing difference in line entries between Pascal and assembler version !!!!  I was going to try and do a version for BBC Basic but a bit beyond my skill level.

Kurt Pieper

unread,
Apr 4, 2021, 7:35:35 AM4/4/21
to retro-comp
Hi,
I also like I2C with Turbo Pascal. That's retro.
I am always looking forward to classic programming with Aztec C, Bascom mit ksam or asm (M80 or z80asm).

Your program also works with the SC137 Card.
With me:
DEVICE_RD = $4F
DEVICE_WR = $4E
I2C_PORT = $20

Are you planning to program the modules SC404 and SC406?

Look for a reference book from the years 1983-88

greeting
Kurt

MogensB

unread,
Apr 4, 2021, 3:02:10 PM4/4/21
to retro-comp
I am happy to hear the I2C code works on SC137 too!
Currently, I only have the SC407, so no plans for SC406 or SC404 for now.
However, I have used the I2C driver to connect an LCD 1602 display succesfully.
My Pascal code needs a little cleaning, and then I will post this code too, if it can be useful for anyone.
Struggled a little to understand the way the 4-bit interface is mapped to I2C... but it is working now.
And a joy to work with Turbo Pascal 3, it is really a fast and efficient tool.

LCD1602.jpg

Regards,
Mogens

MogensB

unread,
Apr 4, 2021, 3:53:28 PM4/4/21
to retro-comp
The LCD1602 I2C example is now on github: https://github.com/mobra66/SC144-LCD1602 
No warranties :) just an example... I separated the I2C code and the LCD code into two include-files, to make it a little easier to reuse the code in another Pascal main program.
Turbo Pascal 3 did not really have a way to isolate "private" definitions, or a module/package concept, so please visit the include files to check the global constants and variables used.

Regards,
Mogens

Kurt Pieper

unread,
Apr 4, 2021, 6:44:55 PM4/4/21
to retro-comp
Hello,

well done.

Your LCD program does not work with my 24A Karlab CPU (20Mhz).
If I change the CPU board with a CPU (7.3728 Mhz), the program only works if I call up a working LCD (written in C) beforehand.
After that the program works.
Funny?

Kurt

Kurt Pieper

unread,
Apr 4, 2021, 8:08:35 PM4/4/21
to retro-comp
I do not understand?

I deactivated the init routine.

Program works?
begin <
   writeln ('Writing hello world message to LCD1602 via I2C'); <
<
   lcd_backlightVal: = LCD_Bl; <
<
{<
   lcdInitialize; <
} <
<
   write ('LCD ready, press ENTER to display text'); readln; <
   lcdWriteAt (1, 2, 'I2C to LCD1602'); <
   lcdWriteAt (2, 1, 'Hello Z80 world!'); <

MogensB

unread,
Apr 5, 2021, 3:46:29 AM4/5/21
to retro-comp
Hi Kurt,
Your tests confirm that the problem is with the lcdInitialize procedure. Perhaps another program initialized the LCD? Have you tried testing after turning off the power so it is the first program running after power restart? I think there is also a hardware reset going on in the LCD as far as I can tell from the data sheets. But I thought the hardware reset would leave the LCD in 8-bit mode, which is not working with I2C.... 
I will have to test it here, both with and without the initialization procedure :)
Thinking about the delays, if you want to test, you could multiply by 5 all the delay values in the I2CLIB.PAS file. That should make it “safe” up to 20 MHz CPU even if Turbo Pascal is set to the default 4 MHz CPU.

Best Regards,
Mogens

MogensB

unread,
Apr 5, 2021, 5:23:18 AM4/5/21
to retro-comp
I made two bugfixes to the LCD1602 example code (https://github.com/mobra66/SC144-LCD1602):

1) The lcdInitialization procedure did not toggle the Enable pulse
2) The Enable pulse triggers on *falling* edge, which I did wrong in the sendInstructionCode procedure

For testing, I have isolated the delay in a waitMillis procedure in LCDLIB.PAS - only one place to add a multiplication factor if the delay is too short.

Best Regards,
Mogens

Kurt Pieper

unread,
Apr 5, 2021, 12:00:23 PM4/5/21
to retro-comp
Hello,

I have put together a rc201 series and can report a success.
Z80, RomWBW3.0, SIO SC137.

A new version Turbo 3.1 loaded onto the CF.
Tinst performed and set to 7Mhz.
All I2C addr. customized.

You photo it's going.

I suspect Turbo has a bug?

Will transfer the version that works here to the fast Z80. Let's take a look.

greeting

Kurt

LCD.jpg
Bild1.jpg
Bild3.jpg
Bild2.jpg

Kurt Pieper

unread,
Apr 5, 2021, 12:40:57 PM4/5/21
to retro-comp
So now I've tested all of my Z80 / Z180 boards with Pascal.

It only works with the z80 (7.2 ... Mhz), RomWBW, SC137.

It doesn't work with the SC126 either.
TINST carried out several times. Anyway, no success.

OK. Now I'm out and go back to C and z80asm.

I wish you every success. I stay as an observer!

Kurt

Kurt Pieper

unread,
Apr 5, 2021, 12:45:40 PM4/5/21
to retro-comp
Oh one more thing.

Regardless of whether it is 16x2 or 20x4, no result.

20x4 can be adapted to 20 characters with 2 parts.
4 lines option is still missing!

Kurt

MogensB

unread,
Apr 5, 2021, 12:47:51 PM4/5/21
to retro-comp
Thank you Kurt for the testing efforts! I am using Turbo Pascal version 3.02A. Did you get the latest version of the source code from Github with my bugfixes? I have tested on my SC126 as well and it works for me.... but something must be different in the setups. I don’t know if different LCD products can cause some issues. I will try to get a few other LCDs to test with too, to make it more generic and hopefully with less bugs :)

Best regards,
Mogens

Kurt Pieper

unread,
Apr 5, 2021, 7:19:43 PM4/5/21
to retro-comp
So, with the changes and reinstallation of Turbo 3.01a, it also works with the sc22c (CPU 20Mhz).

I set the turbo to 20 Mhz.

Good work.

greeting
Kurt

MogensB

unread,
Apr 6, 2021, 3:34:22 AM4/6/21
to retro-comp
I am happy to hear it is working now. I guess the bug fixes made a difference!
My next steps will be to try and build support for 20x4 displays as well.

Best regards,
Mogens

Reply all
Reply to author
Forward
0 new messages