PIC 12F675 Microcontroller Tutorial
PIC 12F675 Microcontroller Tutorial
PIC 12F675 Microcontroller Tutorial
A tutorial on the 12F675 PIC microcontroller which shows you how to program and use it with a series
of projects starting out with a simple LED flasher and progressing on to more advanced projects.
To use the tutorial files you need to have a PIC programmer with an ICSP output connector
and the components shown in each tutorial.
You don't have to install the compiler as hex file is contained in the downloadable zip file.
If you do want to re-compile the source code the compiler is free for the small amounts of
code used here as they all generate hex output files that are below the 2k limit.
Jump to Solderless breadboard.
Jump to Circuit diagram.
Jump to Software.
PIC 12F675 Tutorial Index
Features
Programming
ICSP Connection
Power Supply
Oscillator Calibration
Tip for storing the calibration value
Oscillator Modes
Tutorial 1 : Flash LED (Simple port output)
Tutorial 2 : Key reading and debounce (Simple port input)
Tutorial 3 : PIC Serial Transmit (Soft Serial Transmit)
Tutorial 4 : LM35 Temperature sensor to serial port(ADC)
Tutorial 5 : LM35 EEPROM Temperature data logger(EEPROM)
Tutorial 6 : Servo Motor driver using Timer 0 interrupt(Timer 0)
Tutorial 7 : Servo controller T0 & T1 interrupts (Soft Serial Rx, T1)
Before you start have a look at the following for background info:
12F675
The 12F675 microcontroller is packaged in an 8 pin chip and even though it is tiny it is
packed with peripherals. It even has a 10bit ADC built in (this is the same ADC that you can
find on the 16F877A and 16F88 used elsewhere on this site). So learning about this
Pinout
Note: you can compare this chip (using bubble diagrams) to some others used on this site
by clicking here.
x
All you will need is a wall power supply block with dc output (greater than 8V and no more
than 35V) or a 9V battery to plug into CN1.
Note: It is best to use the 5V power supply circuit as it not only correctly regulates the dc
voltage but it protects your PIC chip. The input voltage can go up to 35V without damaging
the 7805.
You would not want to use that high voltage for very long if using reasonable current as the
7805 would have to get rid of the excess power as heat. Say you used 100mA dropping
35V to 5V gives P=VxI = 30 *0.1 = 3W - a huge power output - the 7805 would get very
hot and go into thermal shutdown!
This is a tip I have seen on the web for storing your calibration value on the device itself - it's so good
I thought I would include it here.
All you do is think of the pins of the 8 pin device as a binary number and mark those pins with the
value you read out using the programmer (in read mode)
All you need is the last hex number as the 1st is always 34.
So lets say you read your device and get 348C. Just use the 8C part.
Oscillator modes
As with the 16F88 the 12F675 microcontroller has eight oscillator modes but unlike the
16F88 the internal oscillator is fixed at 4Mhz.
You can use an external oscillator either a resistor capacitor pair, an external clock signal or
a crystal (or resonator). You can even operate the crystal to 20Mhz if you need extra
performance.
Note: Only use the external modes if absolutely necessary as you loose the use of pins
(loosing 2 out of 6 I/O pins is a lot to loose).
Circuit diagram
The following diagram shows the above Plugblock circuit in schematic form. It is exactly the
same circuit but lets you view the circuit in an easier way and shows the layout of the circuit
from the point of view of the circuit block functions rather than how you have to place the
components (using the Plugblock).
Read 12F765
Using ICPROG (you can find a description of how to use it here) set the device to
12F675 and hit the read button. Remember to note down the contents of address 0x3FF.
Software
The next thing to do is to flash the LED to prove that the system you have is working as
reading back data is not very interesting.
Source code files :
To get the file software project files and c source code click here.
You can use the hex file directly to program the 12F675 then it will flash the led on and off
or you can re-compile the files using the free compiler from Mikroelectronika. You can find a
very brief compiler tutorial here.
Some of the C source code is :
//////////////////////////////////////////////////////////////////////
void init_ports(void) {
TRISIO = 0; // set as output
}
//////////////////////////////////////////////////////////////////////
// Start here
void main() {
init_ports();
while(1) { // infinite loop
GPIO = (1<<4);
delay_ms(200);
GPIO = 0;
delay_ms(200);
First of all the init_ports() routine sets up the direction of pins in the GPIO port - in common
with all the other PIC Micros you can change the port direction at any time using a TRIS
keyword (which is just another register location). Setting a bit in the TRISIO register to
zero sets the pin direction to an output. Here all bits are zero so all GPIO bits are set as
outputs.
As you can see main() is a very simple easily readable program the only slightly non
obvious part is the (1<<4) statement.
This just takes the value 1 and bit shifts it left four times so the number 4 is the same as bit
position 4. Bits in a byte are labeled 7 to 0 from left to right and (1<<0)=1, (1<<1)=2,
(1<<2)=4, (1<<3)=8 etc. so this gives an easy way of setting an individual bit in a byte
that is also easy to read. If you wanted to set bit 5 you could write GPIO = 32; (or 0x20 in
hex) but GPIO = (1<<5) is much easier to read.
Note: The C programming course has more on PORT control techniques.
Try changing the delay time (in both delay_ms statements) to a smaller or larger value and
re-compile and re-flash the chip to see the effect.
- See more at: http://www.best-microcontroller-projects.com/12F675.html#12F675_Programming