Panteltje's io_pic page

Panteltje's io_pic page

~

Here you find the listing for a PIC 16F690 to add the following IO via RS232 to for example a PC: io.asm
The idea is that the PC sends a command, and the PIC replies, or takes action.
This will provide you with:
8 analog input channels with 10 bit resolution.
2 digital input channels.
5 digital output channels.
1 PWM output channel (0 to 100).
1 time display, hours:minutes.

There are no external components needed for the PIC, except the RS232 interface, that can be a MAX232,
connected to pin 10 for TX and pin 12 for RX, and any attenuators and input protection you want.
The PIC uses the internal oscillator.

The PIC communicates with the PC at 19200 Baud, 8 bits no parity, and accepts the following commands:
unnENTER sets hour
mnnENTER sets minute
GnnnENTER sets clock calibration, timer1 reload low byte, use 175 for nominal, is saved in EEPROM
g displays clock calibration (timer1 reload low byte)
At first power up (directly after programming) the EEPROM is set with the default value for G(175).
h help message tells you to read README, please refer to the asm source code.

analog in, returns steps of the ADC (0 to 1023) on the specified pin:
a0ENTER AN0 pin 19
a1ENTER AN1 pin 18
a2ENTER AN2 pin 17
a3ENTER AN3 pin 3
a5ENTER AN5 pin 15
a7ENTER AN7 pin 7
a8ENTER AN8 pin 8
a9ENTER AN9 pin 9

analog in, returns mV on the specified pin:
v0ENTER AN0 pin 19
v1ENTER AN1 pin 18
v2ENTER AN2 pin 17
v3ENTER AN3 pin 3
v5ENTER AN5 pin 15
v7ENTER AN7 pin 7
v8ENTER AN8 pin 8
v9ENTER AN9 pin 9


digital in, returns 1 for specified pin is high, and 0 for specified pin is low.:
i0ENTER RA3 pin 4
i1ENTER RA5 pin 2


digital out, 's' sets specified output pin to Vdd, 'c' clear specified output pin to 0V:
s0ENTER RB4 pin 13
c0ENTER RB4 pin 13

s1ENTER RB6 pin 11
c1ENTER RB6 pin 11

s2ENTER RC0 pin 16
c2ENTER RC0 pin 16

s3ENTER RC2 pin 14
c3ENTER RC2 pin 14

s4ENTER RC4 pin 6
c4ENTER RC4 pin 6


PWM out, sets PWM pulse duty cycle on pin 5 from 0 to 100% (0 is all low, 101 is all high):
p100ENTER CCP1 pin 5


The above commands make it easy to interface the PIC to a program on the PC, for example
sending (typing) a7 followed by ENTER gets as response: 189CRLF , meaning 189 of 1024 ADC steps.
Sending a7ENTER is interpreted the same as sending the characters a7CR by the PIC, where CR is ASCII code 13.
If you type v7ENTER then the response is 1319mV .
The ASCII code 13 decimal is used to test for ENTER.
All this allows for easy parsing.
No characters are echoed, so c1ENTER will set pin 11 to 0V, and the PIC will be silent.
PWM range is from 0 to 101.

Please set the defines at the beginning of the source file for your supply voltage, it is also used as reference for the ADC.
CODE IS FOR GPASM, may need changes to work with MPLAB.
In case you cannot assemble it, here is the hex file (for a 5.06 V supply voltage): io.hex


Here is a small Linux program C code that can communicate with the PIC: ptlrc-0.2.tgz
It is about the simplest possible terminal program, and it can be used in a script to send commands to the PIC, and returns the response for processing.
Example of scripted mode:
You can send and receive single commands at the time from a script, using the -i flag.
For example read the voltage on pin 7, and store the result in file my_result:
echo v7 | ptlrc -i > my_result

cat my_result
2930mV




Click here for some other programs I make available under the GPL.

Click here to send mail