Panteltje's io_pic page
Panteltje's io_pic page
Here you find the listing for a PIC 16F690 and PIC 18F14K22 to add analog and digital I/O via RS232 to for example a PC:
Click here to download io_pic-0.4.2.tgz
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 clock hours:minutes.
build in thermostat.
2 programmable timers to set 2 different temperatures in a day.
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:
annENTER displays AD channel nn count.
cnENTER clears digital output n.
GnnnENTER sets clock calibration, timer1 reload low byte, default 175, is saved in EEPROM.
g displays clock calibration.
HnnENTER sets hour.
h help, this help.
inENTER reads digital input n.
L locks the PIC, the PIC will no longer process commands, or reply to commands, WU_PIC will enable it again.
MnnENTER sets minute.
QnnnENTER sets PWM output value on pin 5.
q displays pwm setting.
rnnENTER displays AD channel nn converted to temperature in Celcius for a connected LM135 sensor.
snENTER sets digital output n.
t prints time.
vnnENTER displays AD channel nn converted to voltage.
WU_PIC this string unlocks the PIC, after that string the PIC will send the ID and program version, and reply to commands.
XnnnENTER sets temperature setpoint, saved in EEPROM. when X is 255 the thermostat is not operating, else
if the temperature at analog input A2 (pin 17) < temperature_setpoint, digital output C0 (pin 16) is set to logical '0',
else digital output C0 (pin 16) is set to logical '1',
negative setpoints are not supported.
x displays temperature setpoint.
Y program timer 1 command, set hour, minute, and an associated new temperature, saved in EEPROM, when hour > 23 or minute > 59 the timer is not operating.
y print timers.
Z program timer 2 command, set hour, minute, and an associated new temperature, saved in EEPROM, when hour > 23 or minute > 59 the timer is not operating.
I/O mapping to pins:
a0ENTER AN0 pin 19
a1ENTER AN1 pin 18
a2ENTER AN2 pin 17 used as temperature sensor for thermostat mode
a3ENTER AN3 pin 3
a5ENTER AN5 pin 15
a7ENTER AN7 pin 7
a8ENTER AN8 pin 8
a9ENTER AN9 pin 9
i0ENTER RA3 pin 4
i1ENTER RA5 pin 2
S0ENTER RB4 pin 13
C0ENTER RB4 pin 13
s1ENTER B6 pin 11
c1ENTER B6 pin 11
s2ENTER C0 pin 16 used as digital output in thermostat mode, 0 for temp lower then setpoint, else 1
c2ENTER C0 pin 16
s3ENTER C2 pin 14
c3ENTER C2 pin 14
s4ENTER C4 pin 6
c4ENTER C4 pin 6
Q100ENTER 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.
Here is a small Linux program C code that can communicate with the PIC: ptlrc-0.5.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
Here is a simple paralllel port programmer for the PIC 18F14K22:
Some pictures, this uses the PIC 18F14K22 with internal reference multiplied by 4, the cable is 25 1 to 1.
To calibrate analog in, put some voltage on an analog input, see how many steps the ADC gives, then do (1024 / steps) * voltage to find the reference, and enter that as reference in the asm file, then re-assemble.
This is because of tolerances from PIC to PIC in the internal reference.
io_pic plus breakout box:
I/O pic in its own box:
I cheated, the box was too big, so I used an extra connector:
Click here for some other programs I make available under the GPL.