You are on page 1of 6

12F675 tutorial 3 : Adding a PIC Serial port.

[? ] Subscribe
To This Site

Become a subscriber (Free)

Search

Join 29,000 other subscribers to receive subscriber sale discounts and


other free resources.
Name:

Your First Name

E-Mail:

Your Email

Custom Search

Sign up for MicroZine:


''The'' Microcontroller
Newsletter
Enter your first
Name and primary
email address in the
form below:

Home
PIC C Course
Project Ideas
PIC 18F Training
PIC Projects
TIPS & Techniques
PIC Introduction
PIC Programming
Programmer Types
PIC Programmer
Schematic Tool
PIC Tutorials
Digital Downloads
Store
My Secret
Oscilloscopes
About Me
Contact Me
Customer Support
Terms of Use
Search This Site
Articles
Forum
MicroBlog
Books
Resource Links
Site Map
Privacy Policy

And receive absolutely


FREE a full project for:

Don't worry -- your e-mail address is totally secure. I


promise to use it only to send you MicroZine.
Back

12F675 Tutorial Index

Next

12F675 Tutorial 3 : PIC Serial Port


Here you can find out how to
add a PIC Serial Port
Circuit...
Sponsored Links...

"Measuring
Analogue
Voltages
Without An ADC"
(Using only onepin).
Instant Download:
You Can
Get It Right Now
Warning: This
project could be
Removed
at any time.
It will NOT be
available
indefinitely SO
To avoid
disappointment
get it:

Now

Name:

First Name

EMail:

Primary Email

FREE Access Here

Don't worry -- your email address is totally


secure. I promise to use
it only to send you
MicroZine

...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).

Remember this is a project


with full description
andfully debugged C
Source code - and it's not
available from the main
website.

to Soft USARTSearching
Introduction.for serial port projects?
toSolderless breadboard.
You can only get it
to Circuit Diagram.
through this newsletter.
Regstrate
en
Facebook
to Software
nete gratis a la mayor comunidad online del mundo y disfruta! To get exclusive access
Enter your first name Name
Jump to I/O Usage summary.
Facebook.com
and primary email address
Jump to Setting OSCCAL.
Now in the form
Clickabove.:
here
Jump to Recovering lost calibration.
But wait !
The 12F675 does not have a built in USART that you can use for a PIC
There's more...
serial port so you have to use a software USART which you can download
from this page.
You'll receive more
free and exclusive
Jump
Jump
Jump
Jump

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

12F675 tutorial 3 : Adding a PIC Serial port.

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:

reports as well as
site information and
site product
updates.
Scroll up to the form above
and sign up NOW. Don't
forget it's FREE and if you
don't like it you can
unsubscribe at any time.
Click Here Now to use
the form above to get your
Valuable information
absolutely free.

Sidebar
I/O Port usage summary
PORTregister : TRISIO - The port direction register.
Youcan reset bits (low) to define port pins as outputs.
You can set bits (high) to defineport pins as inputs.
PORT register : GPIO - The input/output register.
Youwrite to this register to set output pins high or low.
Youread from this register to read the current value input pins.
Allof these actions were used in the previous two tutorial pages and the all
work using bit values:
TRISIO = 0x01; sets bit
TRISIO = 0x04; sets bit
GPIO = 0x01; sets bit 0
GPIO= 0x04; sets bit 2

0 of TRISIO all
2 of TRISIO all
ofGPIO all the
ofGPIO all the

the rest zero.


the rest zero.
rest zero.
rest zero.

Note: In other PIC devices ports are labeled alphabeticallyand 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.

PIC Serial Port : Software TX part


The code for the TX part of the USART is simplified in that it generates 10
bits of serial data with no parity bit.

Social Bookmarking

Click & Add:


Blink
Del.icio.us
Digg
Furl
Google
Simpy
Spurl
Technorati

Y! MyWeb
Readers comments
"Iwanted to thank
you so so so much
for all the information
you have provided in
your site it's
SUPERB and FANTASTIC. "

- Ranish Pottath

The basic PIC Serial port configuration for the TX software is:
Bits per second (BAUD) 2400
Number of bits
8
Parity
None
Stop bits
1
Flow control
None

"This site really is


the best and my favorite.
I find here many useful
projects and tips."
- Milan
bursach<at>gmail.com

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.

Circuit
The 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.

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

Learn PIC C Now

Wondering
how to
program your
next project
using C and
need a great
start?
"First Module
Free:" CLICK
HERE

12F675 tutorial 3 : Adding a PIC Serial port.

For setting up hyperterminal click here.

Solderless breadboard
Add the MAX232 Chip and capacitors.

"Awesome site,
very, very easy and nice
to navigate!"
- Matt
matt_tr<at>
wolf359.cjb.net

"I am a newbie to PIC


and I wanted to say
how great your
site has been for me."
- Dave
de_scott<at>bellsouth.net

LearnMicrocontrollers

"Interested in
Microcontrollers?
"
Sign up for The
Free 7 day
guide:
FREE GUIDE :
CLICK HERE

"Your site is a great


and perfect work.
congratulations."
- Suresh
integratredinfosys<at>
yahoo.com

Learn about the tool used for creating this diagram.

Circuit diagram
Again the pic serial port circuit is easier to see on a schematic.

"I couldn't find the correct


words to define
yourweb site.
Very useful, uncovered,
honest and clear.
Thanks so much for
your time and works.
Regards."
- Anon

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

12F675 tutorial 3 : Adding a PIC Serial port.

Learn about the tool used for creating this diagram.

Software

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

12F675 tutorial 3 : Adding a PIC Serial port.

Source code files :


To get the file software project files and c source code click here.
PIC Serial Port Operation
The 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:
void _Soft_USART_Write(unsigned short chr) {
unsigned short mask=1,i;
unsigned int data;

data = chr << 1;


data &= ~0x0001; // START bit (=0).

// 10 output bits
for (i=0;i<9;i++) { // lsb 1st 8 bits
if (mask & data) {
asm {CLRW}
SU_set; // output low for logic 1
} else {
SU_res;// output high for logic 0
asm {CLRW}
}
mask <<=1;
delay_us(395); // 2400 simulate to calibrate the loop.
}
SU_set; // stop bit
delay_us(417); // 2400
}
The character to transmit is shifted left one bit into the data variable which
acts a serial shift register.
Using 'data' in this way lets the routine evaluate in a constant time without
having to check an index to do a different action so for higher (more
critical) speeds it works better.
The stop bit (10th bit) is a delay with the output set high.
Note: You need to calibrate the loop by timing from SU_Set to the next
SU_set or SU_res if you change the baud rate.
Note: The advantage of using the hard coded timing is that the routine only
takes up about 90 words.

PIC Serial Port: OSCCAL


The project relies on the accuracy of the oscillator to accurately generate
the bit timings for the USART and for this you need to set the OSCCAL
value.
This register changes the oscillation frequency when you write to it
OSCCAL settings
Maximum frequency

0xFF

Middle frequency

0x80

Minimum frequency

0x00

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 thecalibration value it is not set automatically.

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

12F675 tutorial 3 : Adding a PIC Serial port.

The code the data sheet suggests you use to set the calibration value into
the OSCCAL register is :
bsf STATUS, RP0
call 0x3ff
movwf OSCCAL
bcf STATUS, RP0

;
;
;
;

Bank 1
Get the cal value
Calibrate
Bank 0

All this does is call the location that contains the RETLW value which then
returns the calibration value. This assembler code is packaged into
theget_set_osc_cal() routine and does the OSCCAL setting for you.

Recovering lost calibration


If you loose the calibration value then you'll need to re-calibrate - the best
way is to set the internal clock at an output pin (Fosc/4) using the chip
settings so you will be looking for 1MHz at the clock out pin.
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.
12F675 Tutorial Index

Back

Jump from 12F675 Tutorial 3 : PIC Serial Port to


Best-Microcontroller-Projects Home Page
Share this page:
What's This?

Enjoy this page? Please pay it forward. Here's how...

Search

Web

www.best-microcontroller-projects.com

<-- --> <-- -->

http://www.best-microcontroller-projects.com/pic-serial-port.html[23/06/2014 18:45:33]

Next

You might also like