Newbie: Read /Write Parallel port (Pure C)

C and PASCAL (or any other high-level languages) in here please

Moderators: exxos, simonsunnyboy, Mug UK, Zorro 2, Moderator Team

FreekBos
Atarian
Atarian
Posts: 9
Joined: Sat Dec 31, 2016 12:44 pm

Newbie: Read /Write Parallel port (Pure C)

Postby FreekBos » Sun Jan 08, 2017 9:18 am

Hi guys,

I'm having trouble accessing the parallel port on my Atari 50ST+
I have a LED (and resistor) connected to pin 2 and 25.

Why isn't my LED lighting up when I write:

Code: Select all

WriteByte((char)127)


iodriver.c

Code: Select all

#include <stdio.h>
#include "iodriver.h"

const char *ptrPortSelect = (char *)0xFF8802L;
const char *ptrIO = (char *)0xFF8800L;

/*************************************\
* Reads a byte from the parallel port *
\*************************************/
char ReadByte()
{
   ptrPortSelect = (char)15;
   return (char)(ptrIO);
}

/*************************************\
* Writes the byte to the parallel port *
\*************************************/
void WriteByte(char data)
{
   ptrPortSelect = (char)15;
   printf("W Port   : %i\n", (char)ptrPortSelect);
   ptrIO = data;
   printf("W IO Data: %i\n", (char)ptrIO);
}

User avatar
mfro
Atari Super Hero
Atari Super Hero
Posts: 663
Joined: Thu Aug 02, 2012 10:33 am
Location: SW Germany

Re: Newbie: Read /Write Parallel port (Pure C)

Postby mfro » Sun Jan 08, 2017 9:45 am

You are mixing pointers and values. prtPortSelect, for example is (obviously) a pointer, i.e. pointing to an I/O address. If you want to write a value to that address, you need to dereference the pointer instead of assigning a new pointer:

Code: Select all

    *ptrPortSelect = (char) 15;


The same applies to ptrIO.

Your code will fail with a bus error if you didn't set the CPU to supervisor mode (accessing I/O addresses is a privileged operation). Furthermore, I/O addresses should be declared volatile (NOT const) as smart compilers will detect you are writing a value without ever reading from it and optimize the (apparently useless) code away.

User avatar
Arne
Captain Atari
Captain Atari
Posts: 397
Joined: Thu Nov 01, 2007 10:01 am

Re: Newbie: Read /Write Parallel port (Pure C)

Postby Arne » Sun Jan 08, 2017 9:48 am

Have you checked this: http://www.atari-forum.com/viewtopic.php?f=16&t=30821?
Hope you haven't atached a 20mA LED!
Image

FreekBos
Atarian
Atarian
Posts: 9
Joined: Sat Dec 31, 2016 12:44 pm

Re: Newbie: Read /Write Parallel port (Pure C)

Postby FreekBos » Sun Jan 08, 2017 11:16 am

Hi Arne,

A little backgroun dinformation.. I am a C# developer (since .Net 1.1) so I do know about development.
I am not experienced in electronics. So I am a bit worried about the 20mA LED you've been talking about.
Can you explain a little bit what you mean?

My assembler knowledge is also very limited.

Anyway thank you guys for the response!

FreekBos
Atarian
Atarian
Posts: 9
Joined: Sat Dec 31, 2016 12:44 pm

Re: Newbie: Read /Write Parallel port (Pure C)

Postby FreekBos » Sun Jan 08, 2017 11:45 am

Can somebody provide me with an example in c how to enter and leave the supervisor mode?

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4774
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: Newbie: Read /Write Parallel port (Pure C)

Postby simonsunnyboy » Sun Jan 08, 2017 12:26 pm

You have to use the XBIOS function Supexec() or GEMDOS Super(). Check http://toshyp.atari.org/en/ for more information.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
Arne
Captain Atari
Captain Atari
Posts: 397
Joined: Thu Nov 01, 2007 10:01 am

Re: Newbie: Read /Write Parallel port (Pure C)

Postby Arne » Sun Jan 08, 2017 12:38 pm

FreekBos wrote:I am not experienced in electronics.

You REALLY SHOULD if you are attaching self-made hardware! Get the basics fast or better go back to C#!
FreekBos wrote:So I am a bit worried about the 20mA LED you've been talking about.

According to the datasheet the PSG can drive up to one TT load. This is somewhere i the 0.5mA to 2mA range. If your hardware draws more current from the PSG's output you will have a good chance to brick it pretty fast!
Image

User avatar
simonsunnyboy
Moderator
Moderator
Posts: 4774
Joined: Wed Oct 23, 2002 4:36 pm
Location: Friedrichshafen, Germany
Contact:

Re: Newbie: Read /Write Parallel port (Pure C)

Postby simonsunnyboy » Sun Jan 08, 2017 1:15 pm

Not speaking for Atari but a friend of mine told me, he seperated his DOS PC LPT from his circuits by optocouplers. That was in the 90s.
Simon Sunnyboy/Paradize - http://paradize.atari.org/ - STOT: http://www.npoi.de/stot/

Stay cool, stay Atari!

1x2600jr, 1x1040STFm, 1x1040STE 4MB+TOS2.06+SatanDisk, 1xF030 14MB+FPU+NetUS-Bee

Jabber: simonsunnyboy@atari-jabber.org

User avatar
mfro
Atari Super Hero
Atari Super Hero
Posts: 663
Joined: Thu Aug 02, 2012 10:33 am
Location: SW Germany

Re: Newbie: Read /Write Parallel port (Pure C)

Postby mfro » Sun Jan 08, 2017 1:21 pm


FreekBos
Atarian
Atarian
Posts: 9
Joined: Sat Dec 31, 2016 12:44 pm

Re: Newbie: Read /Write Parallel port (Pure C)

Postby FreekBos » Sun Jan 08, 2017 4:14 pm

You guys are really helpful! I do appreciate it a lot!
It is strange to feel like a newbie since I have programming experience for so long.
Started back in the 90's with gw-basic on the olivetti pc. I now am a full-time software developer for profession for over 15 years.
Always wanted to fiddle around with hardware, and I thought I'd give it a shot.
I am willing to learn! I hope you guys can guide me through this.

So here is where I stand:
I now have 1 (green) led connected to pin 2 (Anode side).
The cathode is going through a 1KOhm resistor to pin 25.

Am I safe to run my code? (No offense taken when I brick it!)

Oh and SimonSunnyBoy, the optocoupler idea is a good one. I'll definitely go for it after I get my super-duper-fancy-smancy LED up.

User avatar
Smonson
Atari User
Atari User
Posts: 43
Joined: Sat Feb 20, 2016 9:45 am
Location: Canberra
Contact:

Re: Newbie: Read /Write Parallel port (Pure C)

Postby Smonson » Sat Jul 01, 2017 2:41 pm

Not sure if you're still looking at this, but...

You don't need to resort to optocouplers, you can use an NPN signal transistor such as a 2n2222 or a BC547. These are very cheap and simple to use compared to optocouplers. They usually have a gain of at least 100 which means you can switch on your 20mA LED with a current of 0.2mA from the parallel port, which seems safe according to what Arne posted.

The way to use the transistor is to connect the three pins (emitter, base, and collector) like so
  1. the emitter to ground
  2. the base to a 10K ohm resistor which then goes to the parallel port pin that controls the LED
  3. the collector to the cathode (-) of the LED

The LED (+) should go to your 1K ohm resistor, and then after that, to a +5v power source that comes from the Atari somewhere (I don't know if you can get 5v from the parallel port).

Explanation of the transistor: when the parallel port pin goes HIGH (5v), a small current passes through the transistor from base to emitter (via the 10K ohm resistor to keep it at a safe level). The presence of this current within the transistor also allows a much larger current to flow from the collector to the emitter - which in this case will be all the current that the LED needs because of the 100x gain factor.

Due to your choice of a 1K ohm resistor, the LED might be too dim to see properly in full sun.


Social Media

     

Return to “C / PASCAL etc.”

Who is online

Users browsing this forum: No registered users and 2 guests