<LOGO> ElectroMac
Chapter 2 - PIC information

Introduction to PICs and the Macintosh
PICs are RISC based processors with their own 35 single word instruction set. The code is written on the computer in a text editor and then downloaded to the PIC's EPROM memory using a programmer device. Microchip does not support the development of PIC programs on the Macintosh but a number of enthusiasts have developed a very good set of software and hardware allowing other to develop code for PICs.

PIC machine code is based around the working register, there are commands which load data in and out of the working and into other storage registers. Other commands perform operations on the working.

I used a shareware program called 'MacPIC' by Kevin Coble to write the code on the PIC. With MacPIC you can edit source code, assemble, simulate, program the EPROM and disassemble code. MacPIC connects via serial to my PIC programmer. Because MicroChip does not support the Macintosh platform I had to build my own PIC programmer. I followed a circuit design by Francis Deck with a few small modifications by myself.

EPROM program description
The PIC program starts off by initialising port directions (In or Out), and setting up various options for the rest of the program. All the outputs are turned off to make sure that the interface is in a standard state upon powerup. The status LED is flashed on for a short time to indicate the program is running correctly. The program then enters the main event loop.

At the start of the main event loop, the registers which contain the previously processed command (if any) are cleared and the ElectroMac prompt 'EM>' is sent out on the serial port. It then waits for a command to arrive on the serial port. However if 'interrupts' are enabled and the input port is different form the previously stored value, then a serial message is sent to the computer along with the input port value. When a character is detected on the incoming serial line then it is received and stored in the command1 register. The command is then looked up in a table to see how many 'operand' characters are still to come. This is done to make sure that a <RETURN> character (ASCII value 13) is not interpreted as the command terminator rather than a piece of data for the command. The following character from serial is stored in the command2, command3 and command4 registers. It enters a loop until a return character is received.

Upon receiving a <RETURN> the command data received is processed. The first register command1 is looked up, if it is valid then the program jumps to the code for that command. If invalid then the error message 'ERR' is sent back to the computer. When a valid command has been processed then the status LED is flashed so the user knows that the interface is functioning and the program returns to the start of the main event loop. Note that the LED is turned on for a comparatively very long time, so that the next command is not missed the delay subroutine checks the serial line and exits if a serial character is detected.

Command set description
[A] - Get EPROM program version
    This lets the computer know which version the ElectroMac's EPROM is. This is information if stored in the program memory is the form of a look up table. A counter the length of the serial message is decremented and a character is looked up and returned over the serial.

[B] - Get Digital Input value
    ElectroMac loads the input port value (Port B) into the working and then transmits it over serial with 'D' proceeding it and a <RETURN> character appending it.

[C] - Get Digital Output value
    ElectroMac loads the output port value (Port D) into the working and then transmits it over serial with 'D' proceeding it and a <RETURN> character appending it. This could be used by a program which modifies the bits of an output and needs to know the current port value.

[D] - Set Digital Output value
    ElectroMac takes the second character received (command2) and sets output port value (Port D) to its ASCII code.

[E] - Enable Input change interrupt
    Sets a flag in the 'flags' register. Any time after this, when the input port changes then the interface will send a 'I' character, the input port value and a <RETURN> character. Note that after the input port changes the timer over flow flag is cleared and another interrupt signal will not be sent until the timer overflows again (256 clock cycles later). This provides some degree of de-bounceing so that if an input turns on and off several time when changing then the 'flicker' is ignored.

[F] - Disable Input change interrupt
    Clears a flag in the 'flags' register. Interrupts will not be sent over serial until the flag is set again.

[G] - Resets the interface
    Simply jumps to the initialisation (called by reset vector) routine. This is not a very clean method because a call was made, which will never be returned to. When the program runs again one level on the stack will be full and never cleared. It seems not to cause any problems but I may change this at a later date.

[H] - Get analogue input channel X
    X can be any value between 1 and 8, but on the current ElectroMac case only channels 1 and 2 are connected and the current EPROM version only supports these channels. The channel number is then used to fetch a config word from a lookup table. This is then sent to the Maxim A/D converter chip and the 10-bit response is sent back to the computer.

[I] - Get analogue output channel X
    Just send the 16-bit number for channel X (1 or 2) that was last sent to the D/A converter. This number is stored as two 8-bit registers on the PIC. This would only be used if the computer program forgot what value had been sent and wanted to find out what it is again.

[J] - Set analogue output channel X to 16-bit value YZ
    Stores the new value YZ in the two 8-bit registers for channel X (1 or 2). This new value is then sent to the D/A converter. The D/A converter then outputs a voltage between 0 and 2.5v proportional to the 16-bit value (0 to 65535).

©1998 Nicholas Humfrey <e-mail> <www>