[?] Subscribe To This Site
12F675 Tutorial 3 : PIC Serial Port
Here you can find out how to
add a PIC Serial Port
...to the 12F675 microcontroller tutorial circuit (you can use it on any other PIC device even a 16F84 as it uses a software implementation of the transmit part of a USART).
Jump to Soft USART Introduction.
Jump to Solderless breadboard.
Jump to Circuit Diagram.
Jump to Software
Jump to I/O Usage summary.
Jump to Setting OSCCAL.
Jump to Recovering lost calibration.
The 12F675 does not have a built in USART that you can use for a PIC serial port so you have to use a software USART which you can download from this page.
Adding a pic serial port connection to the circuit gives you scope for much more interesting projects as you can collect data from the ADC (inputs) or comparator or external infrared receiver module etc. and transmit it to a PC.
This tutorial covers creating the software Transmitter (TX) part of the USART as this is the most useful part of a USART and you don't really need the receiver unless you want to control the microcontroller via a serial terminal such as Hyperterminal.
Note: The previous two tutorial pages indirectly cover the most important job in using microcontrollers i.e. input and output. Here's a summary of I/O port usage:
: TRISIO - The port direction register.
You can reset bits (low) to define port pins as outputs.
You can set bits (high) to define port pins as inputs.
PORT register : GPIO - The input/output register.
You write to this register to set output pins high or low.
You read from this register to read the current value input pins.
All of these actions were used in the previous two tutorial pages and the all work using bit values:
TRISIO = 0x01; sets bit 0 of TRISIO all the rest zero.
TRISIO = 0x04; sets bit 2 of TRISIO all the rest zero.
GPIO = 0x01; sets bit 0 of GPIO all the rest zero.
GPIO = 0x04; sets bit 2 of GPIO all the rest zero.
Note: In other PIC devices ports are labeled alphabetically and they do not use the GPIO name instead they use the text 'PORT'. So the first port is labeled PORTA the next PORTB etc. Corresponding port direction registers are TRISA and TRISB. You can use them in exactly the same way as GPIO and TRISIO.
The basic PIC Serial port configuration for the TX software is:
Note: To review how the RS232 pic serial port works click here.
Since each bit takes 1/2400 seconds the total time to transmit a digit is 4.16ms.
Note: The bit timing won't be exact as the PIC chip runs at 4MHz but it will be good enough for bench top use.
CircuitThe circuit uses the standard MAX232 level translator chip but you could use an SP202ECP which has identical pin out (and is cheaper!) and lets you use 100nF capacitors instead of electrolytic ones.
Connect the ground and transmit output to a serial port connector as shown and to a serial cable from it to the PC and it's ready to go.
For setting up hyperterminal click here.
Source code files :
To get the file software project files and c source code click here.
PIC Serial Port OperationThe code flashes the LED 3 times then repeats a message to the serial port.
The following code is part of the pic serial port transmitter:
character to transmit is shifted left one bit into the data variable
which acts a serial shift register.
This register changes the oscillation frequency when you write to it
Only the upper six bits are used as the calibration bits so values must be different by 4 to change to the next OSCCAL setting e.g. values 32,33,34 and 35 would give the same frequency setting.
Microchip calibrates the 12F675 microcontroller and places a calibration value at address 0x3FF. This is a RETLW instruction that returns a value when CALLed this is the value to use in the OSCCAL register.
Note: You have to use the calibration value it is not set automatically.
The code the data sheet suggests you use to set the calibration value into the OSCCAL register is :
All this does is call the location that contains the RETLW value which then returns the calibration value. This assembler code is packaged into the get_set_osc_cal() routine and does the OSCCAL setting for you.
Use a frequency counter to measure the output (you can find a project on this site here). Change the OSCCAL value until you find the one closest to 1MHz.
To get the value back into the 12F675 use ICPROG and put the hex code RETLW instruction at address 0x3FF. The first byte of the instruction is 0x34 and the second is the value returned so for a calibration value of 0x20 you would put:
0x3420 at address 0x3FF
Re-program the chip.
Note: See the tip for storing the calibration value so you will never forget it or loose it again.
Jump from 12F675 Tutorial 3 : PIC Serial Port to
Best-Microcontroller-Projects Home Page