MicroCAN Project Parallel Port to MCP2510 Interface
Author: Embedded Micro Software
Last Update: 11/08/08
Introduction The MicroCAN Parallel Port Interface Project uses the parallel port on the PC and the MicroChip MCP2510 Stand-Alone CAN Controller to implement a CAN (Controller Area Network) interface.
The MicroCAN Project details the hardware and develops the software resulting in a low-cost PC to CAN interface.

Table of Contents (click on heading to go there):

 

Hardware The parallel port interfaces through the DB25 male connector (J1). The 74HC541 (U3) provides a buffer between the PC parallel port and the MCP2510 (U1). Ceramic resonator Y1 provides the 8mhz clock for the MCP2510.

microcan.gif (23992 bytes)

The PC acts as a master sending commands and/or data to the MCP2510, or receiving data from the MCP2510 using a Serial Peripherial Interface (SPI). The MCP2510 acts only as a slave. 

 

The SPI requires the following 4 signals;

  • CS: Chip Select
  • SO: Serial Out
  • SI: Serial In
  • SCK: Serial Clock

The Chip Select signal is an active low signal and enables the SPI port on the MCP2510 to clock in the data on the Serial In line or clock out data on the Serial Out line. The Serial Clock line clocks the data in or out.

The active low hardware Reset signal cause the MCP2510  to enter a 'known' initialized state. +5 volts for the module is provided by the 7805 voltage regulator. Diode D1 protects for reverse voltage and the surge suppressor D2 protects for over-voltage. Input voltage ranges from 5.6 to 15 volts and is connected to connector J3.

The 82C250 CAN physical layer provides the connection to the CAN+ and CAN- lines. Resistor R3 provides a 120 ohm termination resistance and can be jumpered in or out with jumper X1.

Schematic

 

Software The software to control the MicroCAN is written using Borland's Delphi programming language and runs under either Windows 95/98. Delphi is a powerful development environment for Windows applications. The object Pascal is similiar enough to C allowing applications to be built quickly and easily. The software is contained in the MicroCAN.dpr file.

Low Level Routines: Outb and Inb

The lowest level functions are required to control the parallel port lines. The 2 functions Outb and Inb are written in assembly langauge and write or read directly to/from the PC ports.

function Outb(Y: Integer; Port: Integer ): Byte;
asm
   out dx,al { Output al to dx }
end;

function Inb( Port: Integer ): Byte;
asm
   mov edx,Port { Set port address }
   in al,dx { input to al }
end;

Outb requires 2 parameters; Port contains the parallel port address and Y contains the value to write to it. Inb only requires the Port parameter (parallel port address) and returns the value read from it. Outb does not return anything.

Setting the Parallel Port Address

The parallel port is address as 3 consecutive ports and typical addresses are $378, $3BC, or $278. Before accessing the parallel port, the 3 address variables dport (Data Port), sport (Status Port), and cport (Control Port) must be set. The following function sets these 3 global variables.

function qcSetPortAddress( port: Integer ): integer; stdcall;
begin
   dport := port;
   sport := port+1;
   cport := port+2;
   qcSetPortAddress := OK;
end;

One of the instruction in the initialization code of the application would be call the qcSetPortAddress function with the address of the parallel port. The MicroCAN Interface application contains 3 buttons under to set the parallel port to $378, $3bc, or $278.

The printer port assignment can be found be launching a DOS window and displaying the contents of memory location 0040:0008 using the debug program.

>debug
-d 0040:0008 L8
0040:0008            78 03 78 02 00 00 00 00

LPT1 is at 0378, LPT2 is at 0278 and LPT3 and LPT4 are not assigned.

Control Signal Accessing

After the port address has been assigned, the parallel port signals can be accessed. The following function shows an example of setting the RESET line high. Notice that the port is read first and the RESET value is or'd in to set it high. To set the RESET line low, the and operation with the complement of the RESET value is used.

procedure Tmcform.bResetHIClick(Sender: TObject);
begin
   inv := Inb( dport );
   Outb( inv or RESET, dport);
end;

procedure Tmcform.bResetLowClick(Sender: TObject);
begin
   inv := Inb( dport );
   Outb( inv and not RESET, dport);
end;

Bit test buttons are provided for all 4 control signal; RESET, CS, SI, and SCK.

SerialRead: Reading the MCP2510

The SerialRead function reads a register value from the MCP2510. The register number is passed to the function and the value in the register is returned. The ClockData function is used to send and receive the data.

Reading a register from the MCP2510 involves the following step;
  1. Setting the Chip Select (CS) line low
  2. Clocking out the 8-bit READ command
  3. Clocking out the 8-bit register number
  4. Clocking in the 8-bit register value
  5. Setting the Chip Select (CS) line high
function SerialRead( reg: Byte ): Integer;
begin
{** first make sure i/o is in correct state **}
Outb(RESET + sCS, dport);

{** select the 2510, cs low and reset hi **}
Outb(RESET, dport);

ClockData( $03 );
ClockData( reg );
SerialRead :=ClockData( 0 );

{** return with chip select and reset HI **}
Outb(RESET + sCS, dport);
end;

 

SerialWrite: Writing MCP2510 Registers

The SerialWrite function write a register value to the MCP2510. The register number and the register value are passed to the function and the function does not return any values. The ClockData function is used to send and receive the data.

Writing a register in the MCP2510 involves the following step;
  1. Setting the Chip Select (CS) line low
  2. Clocking out the 8-bit WRITE command (value 02)
  3. Clocking out the 8-bit register number
  4. Clocking out the 8-bit register value
  5. Setting the Chip Select (CS) line high
function SerialWrite( reg: Byte; dta: Byte ): Integer;
begin
{** first make sure i/o is in correct state **}
Outb(RESET + sCS, dport);

{** select the 2510, cs low and reset hi **}
Outb(RESET, dport);

ClockData( $02 );
ClockData( reg );
ClockData( dta );

{** return with chip select and reset HI **}
Outb(RESET + sCS, dport);
end;



MCP2510 Reset

The qcReset function toggles the reset line low and then high as well as sends the reset command via the SPI interface. The clockout feature of the MCP2510 is disabled.

function qcReset: integer; stdcall;
begin
   if dport = 0 then
      qcReset := NOPORTADDRESS
   else
      begin
         Outb(RESET + sCS, dport);
         Outb(sCs, dport);
         Outb(RESET + sCS, dport);

         {** send the reset command **}
         Outb(RESET, dport);
         ClockData( $c0 );
         Outb(RESET + sCS, dport);

         {** disable the clockout **}
         SerialWrite( CANCTRL, $e3 );
         qcReset := OK;
      end;
end;

Search for MCP2510

The qcSearchPort function looks for the MicroCAN on one of the following ports; $378, $3bc, or $278. The qcSearchPort button under the Function tab in the MicroCAN Interface application executes this function and returns the results in the listbox. qcSearchPort returns the port address if found or -1 if not.

 

 

 

This page has been hit  times.