Pic16f628a Serial Programmer
I've been suprised by the dearth of examples for programming Microchip PIC® processors in, given there's been continuous effort in improving it's support since about 2001 or so. As such, I am placing the things I've managed to create or find, here. Mostly I've been experimenting with the PIC16f628a processor.
Programming specifications for the following devices: • PIC16F627A • PIC16F628A • PIC16F648A • PIC16LF627A. The Serial mode will allow the.
I like it because it has, among other things:. An internal oscillator that runs at 4MHz or 48KHz - no more crystal setting madness, and you still get to slow your programs way down when they get uppity! Set it to 9600 BAUD and talk to your computer. An extra crystal oscillator designed to run a standard 32Khz watch crystal. An architecture where every instruction except branches takes one cycle. Given a 4.0000MHz clock, a 'nop' instruction will take 1.0000 microseconds. It works reliably with my JDM-style programmer, even under Linux®.
I've been unable to find any consistent source for the programmer but it's available all over the place and looks exactly like. The JDM is an anyway - buy it or build your own, the PIC doesn't care.:) Of course you'll have a hard time finding all the fiddly zeners and transistors so personally I think you're better off buying the whole programmer - it's got a nice ZIF socket, and comes with useful Windows softare too. Note you need a real serial port, the crap they put in laptops doesn't get enough voltage and the counterfeits you get in USB don't have the right control pins. My workstation is as follows:.
JDM-style programmer specified above. A desktop computer running Gentoo Linux, with SDCC and picprog installed.
A nice digital logic trainer breadbord that the local university idiotically threw away. An old 2-channel 20MHz analog oscilloscope that the local university idiotically threw away.
A from the era when Radio Shack sold anything but appliances. A 200-watt pre-ATX computer power supply. Parallax serial LCD for debugging output. PS/2 keyboard with RS232 converter. board, which includes a built-in MAX232 chip and serial connector for easy connecting to computer. A much-tabbed and benoted printout. A.
Header Files. an include file I made to make it obvious what types are what size. A Uint16 is an unsigned 16-bit type, an Sint8 is a signed 8-bit type, etc. Most of my PIC® programs use this. A header defining values needed to declare EEPROM-stored constants.
Delay routines in a more convenient header. Serial routines in a more convenient header. macros for CRC16 calculation. It'll work most places, not just PIC. macros for binary coded decimal on PIC. Example Source. an examle source file modified from an example gleaned from.
It is extremely simple. It counts, and displays the output on ports A and B both. Look for successively larger squarewaves on B0., B7.
this is a quantum leap for me, a working pic example that uses interrupts! First time in 10 years I managed to get those to do something. The program sits there and does nothing while interrupts fire periodically and increment PORTA. Based on code by Mac Cody on this.
the PIC16f628a has an internal clock that can be set to one of two frequencies - 4MHZ, or 48KHz. This program is a modified version of 0002-interrupt.c that alternates between 4MHz and 48KHz every interrupt as well as twiddling PORTA.
Watch for the frequency changes on CLKOUT. This example, unlike those above, cannot be modified to work on the PIC16F84, since the PIC16F84 has no internal clock! Note that the clock speed bit doesn't do anything if the PIC is configured to use any other clock source than it's internal oscillator!. Configures the USART to asynchronous 9600 N81 and transmits the string 'Q! Thoroughly useless, but a decent example and actually works for me. I use the terminal program to read the serial port on a Windows PC. Configures the USART to asynchronous 9600 N81, then repeats whatever you type back at you.
Very close to 0004-uarttx.c but transmits AND recieves. A simple example using B0 as an interrupt pin. Also illustrates the sleep instruction. Adapted from an example in assembly. A simple example illustrating how the PIC's interrupt-on-B3.B7 feature works. It is modified from 0006. A simple example illustrating how the PIC16f628a's internal voltage reference works.
Depending on the input on A0, it sets the voltage reference, output on A2, to either minimum or maximum value. An example showing how to write to the internal EEPROM memory of a PIC16f628a. It will write a string into EEPROM memory, which you can read back with your PIC programmer to see whether it actually worked. An example showing how to read values from the internal EEPROM memory of a PIC16f628a.
Put whatever byte you want at address 0x00 in EEPROM using your PIC programmer, and this program will display that byte on PORTB. By request of a denizen of the, I've put up the source to this unipolar stepper tester. simple interfacing of a PIC16f628a to a PS/2 keyboard.
deluxe keyboard example, reads in decimal digits from a numeric keypad to produce ASCII or raw bytes on the serial port. A no-holds-barred PS/2 keyboard to ASCII RS232 converter with all the functionality I could think of. Alphanumeric, numpad, punctuation, function, arrow, pgdn/pgup/etc keys all function.
Caps lock and num lock work as you'd expect. Scroll lock sends XON and XOFF. Keyboard lights operate correctly. Printscreen, sysrq, and pause all toggle a hardware output.
It also uses another include file,. I've also included a for it. I've generally been of the opinion that copy/pasting code from a code-generator is an admission that a computer language stinks - it's not flexible enough to even handle it's own templates. I've resisted for years for this reason, even though it's a fine tool; a higher level language ought to be capable of it without it. After a few false starts I finally have an accurate, flexible, and self-contained C function + macro for generating small to medium time delays in PIC microprocessors.
On a 4MHz processor it can generate delays between 13 microseconds and 190 milliseconds. It is capable of producing the same delays on different clock speeds, when possible, by recompiling with a different KHZ value. I finally got around to interfacing with PS2 mice as well as keyboards. This example reads data from a PS2 mouse and displays, over its UART, button states and cumulative x, y, and (for scrollwheel mice) z values. An illustration of how the pic16f628a's watchdog timer operates.
The program flashes B0 once per reboot, that is, several times per second, unless the watchdog timer is cleared in time. An expansion on 0017-wdt.c, this program is capable of detecting how it was rebooted or woken and sending that to a serial LCD. A simple watch, using T0 to count 1 million pulses per second as a timing reference. The opposite of 0012-keyboard.c, acts AS a ps/2 device instead of talking with one. a basic example showing how timer1 works, and how to use it with a watch crystal. It functions as a frequency generator, outputting a variety of frequencies from 16Khz to 0.5hz on ports A and B.
A simple example using the timer1 crystal oscillator to wake the chip from sleep precisely once per second. How to get that 32Khz watch-crystal signal out of the PIC in the form of a squarewave on B0. A single-channel serial-controlled servo controller using timer0 interrupts and delay loops. A beefed-up version of 0024-servo.c supporting seven simultaneous channels.
Would be 8 if the PIC could use MCLR as an output!. An example of how to use the PIC16f628a's internal PWM controller. It produces a 300Hz squarewave of gradually varying duty cycle on B3. A LED hooked to this pin should appear to brighten and fade.
A less elaborate example of how to use timer2 by itself, with no PWM involved. An interval counter, made for monitoring thermostats and the like, derived from the 32-khz crystal examples. It can report how long a monitored device has been on, in seconds, every 4 minutes. An improved version of 0025-servo7.c that operates at 2400 baud to prevent overruns, expects a preamble byte of 0xaa for error control, and forwards any commands for servos 7 out its serial pin with 8 subtracted from the address. Theoretically you could chain 32 of these chips together, to run 224 analog RC servos off of one serial port.
and - source code and macros for calculating CRC-16 checksums. an example of how to store constants in EEPROM. Generates weird-looking ASM that when you try it ends up right because of 16-bit alignment considerations.
an improvement on the old 0011-ustepper.c using internal/external timers, fixing half-stepping, and adding a 3/4 phase switch for running hard-drive spindles. A modification of 0032-ustepper.c which starts slowly and accelerates to several thousand RPM.
Fixed to three phases. Intended for running hard drive motors. A single-axis stepper motor controller which displays position in mm on a standard 16x2 display. Much thanks to Gaurav Chaudhary for his code from for using the LCD in 4-bit mode. Compiling and Linking Instructions. Please note the recent change! If you have sdcc 3.0 or newer, nearly every useful PIC header has been odiously moved into the 'non-free' header trees, which are off by default, but fortunately can be included with one commandline option, -use-non-free.
Some file locations have changed as well. Note the highlighted changes from /pic/ to /pic14/, etc. Also note, these paths may be distribution-dependent and subject to change. If you run it and it says it 'can't find file', it probably can't find the file and you ought to figure out what the correct paths are to fix it. To turn a.c file into a.asm file: sdcc -S -V -mpic14 -p16f628a -D16f628a -use-non-free -DCONFIGWORD=INTOSCOSCNOCLKOUT &WDTOFF &LVPOFF &DATACPOFF &PWRTEON file.c To turn a.asm file into a.o file: gpasm -c file.asm To link multiple.o files into one.hex file: gplink -m -s /usr/share/gputils/lkr/16f628a.lkr -o output.hex /usr/share/sdcc/ non-free/lib/pic14/pic16f628a.lib /usr/share/sdcc/lib/ pic14/libsdcc.lib file1.o file2.o file3.o To burn a.hex file to your PIC: picprog -device=pic16f628a -erase -burn -pic-serial-port=/dev/ttyS0 -input-hexfile output.hex Other Notes.
As of SDCC 3.0 the header files for PICs have been reorganized. You now need to #include, not #include 'pic/pic16f628a.h'. You also need to give sdcc the -use-non-free commandline option to compile for PIC. Brown-out reset is only useful if you're runing at 5 volts.
You need to turn off brown-out reset if you want to run the PIC at lower voltage. They seem to run at anything from 1.5V on up to 5 without it. If you're playing with interrupts, keep in mind, arrays are not interrupt-safe! If an interrupt happens while you're using an array, the program could go into la-la land or read bizzare values.
You have to disable interrupts while you're using an array outside an interrupt handler. Comments Author Message Pharmg908 on 2018-01-21 18:24:11 Hello! Pharma690 on 2018-01-21 18:24:09 Hello! Viagra from india Pharme325 on 2018-01-21 18:24:05 Hello! url=from india/url Pharmc426 on 2018-01-21 18:24:01 Hello!viagra from india Pharmd128 on 2018-01-20 12:22:57 Hello!
Pharmb318 on 2018-01-20 12:22:53 Hello! Sildenafil generic india Pharmd873 on 2018-01-20 12:21:26 Hello! url=generic india/url Pharmd282 on 2018-01-20 12:21:18 Hello!sildenafil generic india Pharmd925 on 2018-01-19 06:19:33 Hello! Pharmk590 on 2018-01-19 06:18:27 Hello! Viagra india Pharmd985 on 2018-01-19 06:18:23 Hello! url=india/url Pharmd180 on 2018-01-19 06:18:20 Hello!viagra india Pharme762 on 2018-01-18 00:06:15 Hello! Pharma156 on 2018-01-18 00:06:12 Hello!
Viagra from india Pharmb300 on 2018-01-18 00:06:07 Hello! url=from india/url Pharma363 on 2018-01-18 00:06:04 Hello!viagra from india Pharmg740 on 2018-01-16 17:53:21 Hello! Pharmc76 on 2018-01-16 17:53:15 Hello! Viagra from india Pharmd853 on 2018-01-16 17:53:12 Hello! url=from india/url Pharma653 on 2018-01-16 17:53:04 Hello!viagra from india Pharme496 on 2018-01-15 11:45:03 Hello!
Pharme583 on 2018-01-15 11:43:26 Hello!viagra india Pharme225 on 2018-01-14 05:23:27 Hello! Pharmk272 on 2018-01-14 05:23:25 Hello! Viagra from india Pharmd284 on 2018-01-14 05:23:15 Hello! url=from india/url Pharmf743 on 2018-01-14 05:23:12 Hello!viagra from india Johnf753 on 2018-01-13 11:42:30 Farmville farms even include free gift that gfeddedadgbd JoE on 2017-03-07 10:59:51 Since there aren't so many SDCC examples on the net this is really useful, thank you. Johnd694 on 2016-12-23 11:49:07 Article Source a viral game app is not that much cdefkgfeefea Johnf841 on 2016-12-23 11:49:05 I'm really impressed with your writing skills as well as with the layout on your blog. Is this a paid theme or did you customize it yourself? Anyway keep up the excellent quality writing, its rare to see a great blog like this one these days.
Dddaddbdggd lnxslack on 2016-09-17 15:11:44 where i get the header file tsmasm.h Jorge on 2015-12-22 16:24:15 Thanks, I like what I have found here.useful code. Tyler Montbriand on 2015-09-10 13:-toestubber.c and tsmbcd6.h added. Admin on 2015-09-07 03:58:10 Wellcome Joypurhat Jim Daldry on 2014-11-13 18:47:50 I use MPLABX and XC8 to 'autogenerate' config #pragmas - quick but dirty Suwang Jang on 2014-10-26 05:17:34 Thank you for your wonderful works. Your examples are very useful. Tyler Montbriand on 2014-06-20 13:-hdd.c has been added, a program to run hard drive motors without the hard drive. Tyler Montbriand on 2014-06-20 11:-ustepper.c hsa been added, an improved version of the stepper tester.
Tyler Montbriand on 2014-06-16 14:20:41 Athena: You may use it for whatever purpose. Ronybc on 2014-03-09 07:13:01 Very good. Thanks a Ton.! Athena on 2014-01-04 00:42:00 Here is the github page of the project - Athena on 2014-01-04 00:40:42 May I put the code into an open source project? WertAsdmon on 2013-10-22 14:32:16 'Cool thanks! LilButcher on 2013-10-22 14:29:31 ' Knut on 2013-09-20 02:29:06 in 'other notes' there is a filename missing after the first #include.
Vomitusmaximum on 2012-10-30 06:23:44 I came here looking for the auto vomit circuit but found the PIC micro controller. A pathetic MCU compared to proper MCU Dave on 2012-09-24 06:29:08 check your html - your need to use entity refs for less than and greater than Tyler Montbriand on 2012-07-17 13:46:45 uniqdom: That's because you have to give it that file.
Download it from this page. Diego Herranz on 2011-10-24 06:13:04 Brown-Out Reset happens at 4.0V (typical) when enabled regardless of your VDD. That's why if you use BOR with a lower voltage the pic is held at reset. Uniqdom on 2011-10-15 17:51:49 i'm trying to set a uart in a 16f877a using sdcc but tsmtypes.h file can't be found. I have seen another header called usart.h in some projects.
• Find your television code In order to program your Sanyo universal remote, find the television's code. Look for this information in the manual that came with your remote. How to program a sanyo fxwk remote control. To do this, locate the make and model or your television set.
I have that one but it have some syntax errors. Maybe you can disclose to me the trick. Also want to say thank qwe on 2011-05-17 04: Tyler Montbriand on 2011-03-08 19:-eetest.c added to settle the EEPROM argument. It produces ASM that.looks.
broken but isn't: The useless extra EEPROM bytes are discarded due to 16-bit alignment issues. Tyler Montbriand on 2011-03-03 13:50:08 Added 0030-crc.c and tsmcrc16.h, code and macros for calculating CRC16 checksums. Tyler Montbriand on 2011-02-20 22:02:44 Added 0029-servo7.c, an improved 7-output servo controller Tyler Montbriand on 2011-01-27 21:51:13 Added 0028-uptime.c, a serial-output interval counter/monitor device.
Tyler Montbriand on 2011-01-27 21:50:27 Personally verified that 0014-ascii.c, which heavily uses EEPROM data, actually talks to keyboards given SDCC 3.0 and the new gplink. Tyler Montbriand on 2011-01-27 21:43:16 I stand corrected. EEPROM data variables now need code specifier. Fixed the fix of the fix.
Tyler Montbriand on 2011-01-15 19:18:12 EEPROM data variables now need data specifier in SDCC 3.x Updated 0014-ascii.c to reflect this! Tyler Montbriand on 2010-12-22 18:54:42 I have updated every source file to reflect changes in header file locations for sdcc 3.0 hesperaux on 2010-12-10 23:09:08 Thank you for making this site. I'm trying to get into C with my 16F628A right now. I have the same serial LCD from parallax.
Hoping to use your example code to get a feel for things. Tyler Montbriand on 2010-10-09 14:42:54 I'm not hiding PIC18 examples in my sleeve, I post what I have. If/when I start using them I'll post their examples too.
Tyler Montbriand on 2010-10-09 14:41:11.asm and.inc files sound like they need MPASM, not SDCC. Etnalubma on 2010-10-08 23:17:53 Good stuff, dude. This was very useful. I apreciate it. Saurabh on 2010-09-25 00:30:13 i have.asm and.inc files how can i get a.hex file out of it to program my PIC16F628A.THANKS zolli.italia on 2010-08-27 22:26:02 good examples, i have used sdcc (pic16f628a) for first time. Jagdish on 2010-08-24 22:47:44 Any link/resource how to start using SDCC on windows for PIC18 devices?
Drogba on 2010-08-24 22:42:30 Firstly thanks. I am looking for SDCC code examples on PIC18F(windows platform). Someone pls help as I am having difficulty in running SDCC properly Ramesh on 2010-08-24 06:49:17 Dai Tyler Montbriand on 2010-08-15 14:-timer2.c uploaded, a simple example using timer #2 Tyler Montbriand on 2010-08-11 10:-servo7.c fixed.
I'd uploaded the wrong file. Tyler Montbriand on 2010-08-11 10:05:24 Added 0025-servo7.c and 0026-pwm.c, also, tsmdelay.h updated again. Tyler Montbriand on 2010-08-10 12:26:33 A video of my servo controller in action has been linked.
Tyler Montbriand on 2010-08-09 22:43:12 Added 0023-32khz.c, a hack to get a 32Khz squarewave following the crystal out of a 16f628a. Tyler Montbriand on 2010-08-09 22:42:33 Added 0022-oscint.c, showing how to have the 32Khz crystal cause interrupts. Tyler Montbriand on 2010-08-09 21:18:50 Added 0024-servo.c, a single channel serial-controlled servo controller. Also, an update to tsmdelay.h to cache values.
Pic16f628a Serial
Tyler Montbriand on 2010-08-08 13:51:40 Added 0021-freq.c, a very basic timer1 + crystal example Tyler Montbriand on 2010-07-24 11:26:41 A few comments seem to be from the future. Server clock fixed. Tyler Montbriand on 2010-07-24 11:17:12 'static void main' fixed. Thank you, maxpage. Tyler Montbriand on 2010-07-24 11:16:07 Typo in linking dir fixed.
Thanks, maxpage. Tyler Montbriand on 2010-07-24 11:15:09 This is not a 'help' board. Pic16f628.h should come with SDCC.
Tyler Montbriand on 2010-07-24 11:14:15 Added 0020-keyhost.c. Fixed a bug in tsmdelay.h. Bipin on 2010-07-08 12:33:10 From where can I get pic16f628a.h file? Fabio.italia on 2010-06-24 08:24:02 very good site and all open source. Thank's a lot maxpage on 2010-03-21 09:36:18 Important: the last snapshoot version of sdcc doesn't allow to declare 'static main'. You should use simply 'main'!
Maxpage on 2010-03-21 09:33:36 Yoo've made mistake in gplink execution (instead of./gputils/lrk/./ it has to be./gputils/lkr/./) Jonas on 2010-03-16 07:50:38 Exactly what I was looking for. Chris on 2010-03-09 22:19:04 Super useful page - many thanks. Tzicatl on 2010-01-24 20:46:19 Great!! Thanks for sharing!!! Tyler Montbriand on 2009-11-25 11:25:39 Uploaded 0019-watch.c Tyler Montbriand on 2009-11-14 20:55:47 Uploaded 0017-wdt.c, 0018-bootmode.c, tsmserial.h Tyler Montbriand on 2009-11-13 14:55:23 Uploaded 0016-ps2mouse.c Tyler Montbriand on 2009-11-11 15:57:17 Uploaded 0015-delay.c.
Tyler Montbriand on 2009-11-09 02:23:52 Uploaded 0014-ascii.c, 0014-ascii.hex, and tsmee.h. It's a PS/2 to RS232 converter. Tyler Montbriand on 2009-11-09 02:23:03 Good idea, nt4, I'll note that in the file. Nt4 on 2009-10-31 22:02:00 Don't forget disable hardware flow control in terminal when playing with 0005-uarttr.c%) James Waples on 2009-10-06 03:30:22 Thanks very much for the SDCC code - it works for me now too:D aleale99 on 2009-09-14 11:29:57 Fantastico: very usefull THANKS Tyler Montbriand on 2009-04-18 13:11:17 Added a new example, PS/2 keyboard interfacing Tyler Montbriand on 2009-04-18 12:24:58 Useless comments deleted. If I had more examples they'd be here, stop asking.
Jim on 2008-03-30 16:48:29 Useful info, thanks! Tyler Montbriand on 2008-03-01 11:01:36 For some versions of SDCC, change 'static void main' to 'void main'. Tyler Montbriand on 2008-03-01 11:00:50 Added compiling and linking instructions. Tyler Montbriand on 2007-07-13 09:22:02 Documentation improved in 0004-uarttx.c and 0005-uarttr.c Tyler Montbriand on 2007-07-09 19:-eeprom.c updated similarly. Tyler Montbriand on 2007-07-09 19:-uarttx.c updated to support modern versions of SDCC. Tyler Montbriand on 2007-02-08 19:-ustepper.c added. Tyler Montbriand on 2006-12-31 00:-eepromrd.c added.
Tyler Montbriand on 2006-12-30 22:-eeprom.c added. Tyler Montbriand on 2006-12-30 20:-vref.c added. Also tweaked many other examples to disable their comparators.
Tyler Montbriand on 2006-12-29 14:-interrupt.c added. Documentation tweaked on 0002 and 0006. Tyler Montbriand on 2006-12-29 12:-interrupt.c modified again, fixing the backwards comment on PORTB pullups Tyler Montbriand on 2006-12-29 11:-interrupt.c modified today, paring away many pointless statements Tyler Montbriand on 2006-12-28 13:28:20 The comments page is now OPEN.
Posted on 14th October 2012 Tagged in, This is the second in a series of articles describing the construction of an interface board suitable for controlling 2 motors, 2 servos and providing a collection of binary inputs and outputs (here is the ). The board provides limited onboard intelligence (only enough to protect the motors and servos from invalid operations) - it is designed to be controlled from an external computer through an RS232 connection. Introduction My apologies for this post being so late - a combination of illness and personal commitments contributed to the delay. It turns out there is a lot of ground to cover for this topic so I have split the very unwieldy first draft of the post into three separate ones that will appear on the site over the week. Here is what we will cover in the different parts:. This post.
Covers the basics of serial communications, introduces the serial periphial on the PIC, implements code to send data via RS232 and walks through using the integrated simulator in MPLAB/X to test the operation of the software. Introduces interrupt handling on the PIC and modifies the code to handle interrupt driven serial input. Once again the code is tested on the simulator to verify its operation. Covers the electrical characteristics of RS232 and provides the circuitry for RS232 communication with generic devices.
This also covers the special case of communicating with the via RS232. I've also started including full MPLABX projects with the posts - you can find this one (older posts have been updated with the appropriate links as well).
So without further ado let's move on to serial programming on the PIC. Serial communciations and RS232 Two (or more) devices can exchange data with each other using either or communication. In the parallel model all bits are communicated at the same time over a number of connecting wires (think of an old style printer port with 25 pins - 8 of those pins provided the data while the rest provided handshaking and power connections). With serial communication data is exchanged over a single wire - one bit at a time. Often there is one wire per direction (so one wire to transmit from device A to device B and another to transmit from device B to device A). The speed at which data is sent on a serial link is measured in bits per second (bps, commonly referred to as the ). Note that there is not necessarily a direct correlation between the baud rate and the number of bytes that are transmitted per second.
Although it would seem that a 9600 baud would result in 1200 bytes per second (9600 / 8) there may be additional framing bits sent with each byte (start, stop and parity bits for example) as well as a small delay between each byte. The resulting byte rate will be slightly lower as a result of this. Serial communications can be further broken down into synchronous and methods.
In a synchronous connection there will be an extra signal that indicates when data is valid - a clock signal. This allows the sender to indicate when the data line contains valid information (and allows for variable rate transfers). With asynchronous communication there is no such signal - both sides of the communication channel must already know the expected transfer speed. The specification is a common asynchronous data transfer protocol.
Although the data itself is transmitted asynchronously the full specification allows for some synchronisation (indications that the receiver is prepared to accept data, the sender has data available to transmit and that a device is connected at the other end). The synchronisation features are optional for RS232 (they are usually indicated as ' Flow Control' options in communications programs).
Note that the Flow Control options do not change the speed (the baud rate) of the connection, they simply allow the partners to decide when a byte can be received For minimal bidirectional communication an RS232 connection requires a reciever channel and a transmission channel at each end. These are defined as Tx (the transmitter) and Rx (the receiver). For a one to one communication connection the Tx at one end needs to be connected to the Rx at the other end and vice versa. In this case we are going to use the PIC to provide an RS232 (asynchronous) connection with a transfer rate of 9600 baud. There will be no flow control, no parity checking and a single stop bit.
This is normally represented as 9600 N81 in most communications software. The PIC USART The 16F628A version of the PIC has an onboard or Universal Synchronous/Asynchronous Receiver/Transmitter) which does much of the heavy lifting for us in terms of serial communication. This particular USART can be used for protocols other than RS232 such as or but we are only going to examine the RS232 functionality in this post. There are three main registers that control the function of the USART - TXSTA (Transmit Status and Control Register), RCSTA (Receive Status and Control Register) and SPBRG (Serial Protocol Baud Rate Generator). The first two registers are comprised of flag bits that determine the overall operation of the USART, the last register ( SPBRG) contains a value that controls the bit rate of serial communications. Bit rate generation uses an internal clock/counter and is dependant on the clock rate applied to the chip.
We are using the internal 4MHz oscillator and all our calculations are based on that value. The for the 16F682 provides a handy set of lookup tables to determine the appropriate baud rate settings for a given system clock speed (these start at around page 75). Reading and writing data over the serial connection is achieved by writing data to TXREG (for data to be transmitted) and reading from RCREG (for data that has been received). To determine if data can be sent or received requires checking individual bits in the control registers.