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.
|Square Wave fast
|Square Wave slow
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)
|function from 0x00 thru 0x07 (see table above)
|0x81 – validation byte
|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.
For more detail: Voltage programmable simple logic device using PIC12F675