Description
Hereβs another daft yet interesting PIC12F675 idea I came up with. When I wanted to show someone how logic gates worked I could only find a NAND gate which wasnβt very handy for demonstrating ANDβs ORβs, NORβs and ExORβs.Β I also wanted to have a play with the A/D converter on the 12F675 so I came up with the idea of a PIC that could function as a single 2-input logic element.Β Β
The logic function is determined by an analogue voltage applied to the GPIO4 pin when the device is first reset, it isnβt sampled again after this so logic canβt be changed on the fly.Β The 3 MSBs of the A/D conversion give eight distinct voltage levels that map to specific logic functions.Β Six functions have been implemented;Β these are 2 input AND, OR and EXOR gates and their negated equivalents.Β
Just to prove the concept, here is a βDβ type flip flop (PIC Flop:-) I built out of five 12F675 Logic Elements operating as NAND gates.Β The sixth device is configured to operate in square wave mode to provide a clock input to the flip-flop for testing.Β The small pushbutton provides the βdataβ input.
Square wave generator
With 8 analogue voltage levels and only six logic functions, this left two unused functions so these have been used to provide a square wave generator.Β There are two modes, fast and slow selected by the voltage on the function select pin.Β Once selected the program reads the data on inputs A and B and sets the frequency on the output pin according to the table below.
This is read each cycle so the frequency can be adjusted during operation.
Β B A FreqβΒ
0 0 1KHz, 2.5Hz
0 1 100Hz, 1Hz (1s)
1 0 50Hz, 0.5Hz (2s)
1 1 25Hz, 0.25Hz (4s)
Selecting the Function
When the device is first powered up it reads the logic level on GPIO5.Β If the input is low, the analogue voltage on GPIO4 is sensed and the logic function determined as described above.Β The function selected is then saved to EEPROM.
IfΒ the GPIO5 input is high when the device is powered up, the previously saved function is read from the EEPROM and used.Β In this case the analogue voltage on GPIO4 is ignored. This allows the function that the PIC will perform to be selected once and saved so it can be used repeatedly without having to set it each time the PIC is powered on.
Function value | Function | Voltage select |
0 | AND | GP4-VssΒ |
1 | OR | Vdd-10K-GP4-2.2K-Vss |
2 | XOR | Vdd-10K-GP4-4.7K-Vss |
3 | Square Wave fast | Vdd-10K-GP4-8.2-Vss |
4 | NAND | Vdd-6.8-GP4-10K-Vss |
5 | NOR | Vdd-4.7K-GP4-10K-Vss |
6 | XNOR | Vdd-2.2K-GP4-10K-Vss |
7 | Square Wave slow | Vdd-10K-GP4 |
Floating Inputs
The inputs on pins GPIO0, 1 and 5 have the weak internal pull-up feature enabled so no external pull-up resistors are required.
Selecting function during device programming
If your programmer supports writing to the EEPROM at program time, you can set up the function when the code is programmed into the PIC.Β This avoids the need to βuser programβ the function with an analogue voltage on first use. To do this you need to set the first three memory locations in the EEPROM as follows: (see Source Code)
Address 00 | Β function from 0x00 thru 0x07 (see table above) |
Address 01 | Β 0x81 β validation byte |
Address 02 | Β 0x69 β validation byte |
If the validation bytes are not correct, the EEPROM will be initialised and the function set to 0
Code and schematics
This code is written to work on a 12F675 device. The HEX file below is assembled for and has been tested with the 12F675. It should work on a 12F683 too but I havenβt tried it. This code wonβt work with a 12F629 because it doesnβt have the ADC feature.Β
-
Hex(V2) file ready to program (right-click Save As)
For more detail: Voltage programmable simple logic device using PIC12F675