Awesome FAQ / Tips / Help for PIC C18

I found this really awesome FAQ for using the C18 libraries & compiler for Microchip PIC devices.  Check it out!

http://www.xargs.com/pic/c-faq.html

Cheers!

RS-232 with a PIC18 and MAX232

Getting serial to work with a PIC is supposed to be fairly easy, especially with the Microchip C18 libraries.  For whatever reason, however, it rarely seems as easy as it should be.  Searching around for posts with the keywords ‘usart, eusart, uart, serial, rs232′ and PIC should give you some idea of just how bizzarely difficult this can be.

Part of the problem is Microchip’s documentation.  The C18 libraries were supposed to streamline a lot of programming for the PIC… and for the most part, it did; Microchip just hasn’t done a good job of making sure to completely document it all.

Anyway, below follows a pure-C18 example of an echo server, operating on a PIC18, with a MAX232 serial converter in between the PIC and my PC.  First the code, and then some discussion.

2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <p18cxxx.h>
#include <usart.h>
 
void main(void)
{
	char test = 't';
	TRISCbits.RC7 = 1;
	TRISCbits.RC6 = 0;
	OSCCON = 0b11100010;
	OSCTUNEbits.PLLEN = 0;
 
        while (!OSCCONbits.IOFS);
 
	baudUSART(BAUD_IDLE_CLK_LOW & 
         	  BAUD_AUTO_OFF & 
         	  BAUD_8_BIT_RATE & 
         	  BAUD_IDLE_RX_PIN_STATE_HIGH &
	          BAUD_IDLE_TX_PIN_STATE_HIGH);
 
	OpenUSART(USART_TX_INT_OFF &
          	  USART_RX_INT_OFF &
        	  USART_ASYNCH_MODE &
        	  USART_EIGHT_BIT &
        	  USART_CONT_RX &
        	  USART_ADDEN_OFF &
        	  USART_BRGH_HIGH, 207);
    while (1) {
        WriteUSART(test);
	while(!DataRdyUSART());
        test = ReadUSART();
        Nop();
    }
}

If you were to try and copy/paste this code, there is a chance that it would work, out-of-the-box.  If it doesn’t work, presuming your hardware & MAX232 circuits are correct, the most likely culprit is the OSCON configuration.

As with all forms of communication, analog or digital, timing is the key.  There are two important timing lines in the above code; first, when we configure the oscillator inside of the PIC:

2
OSCCON = 0b11100010;

And when we tell the USART module how fast we are operating:

2
.... USART_BRGH_HIGH, 207);

The first bit, the OSCCON register setting, is configuring my PIC’s clock to operate at 8 Mhz.  This may be different for you, depending on what model PIC you are using – make sure to check your datasheet! The second bit includes the number ’207′ – this is the number that the PIC uses to calculate how fast you expect it to operate on the RS232 line – i.e., your baud rate.

The datasheet for your PIC should have a formula to help you calculate that number, probably in a section titled something like ‘Baud Rate Generator’.  Figure out what baud rate you want to operate at, and use the appropriate number for it!

This is a very brief and not a very in-depth discussion of this topic, but I had trouble finding a decent example online to use as a starting place; I’m hoping this post serves that role for some other poor hacker fighting with the C18 docs =)

Cheers!

Thesis and Dissertation ETD Template / Example

While writing my thesis ETD for my M.S. at Virginia Tech, I discovered that the available ETD LaTeX template was sorely out-of-date.  As a result, I had to spend sometime ‘upgrading’ the LaTeX packages and template source as I went along to make it work well.  LaTeX has come a long way since 1997, after all.

Now that my defense is all said and done, I created an ETD template / example file for theses and dissertations for modern LaTeX distributions.  It contains all of the necessary ETD formatting guidelines, as well as many of the packages that a technical thesis / dissertation may require and some good default settings.

You can see the template here:

http://git.hokietux.net/?p=latex.git&a=blob&f=etd_template/etd_template.tex

And the Git source tree summary is here:

http://git.hokietux.net/?p=latex.git&a=summary

If you find this useful, or if you have any comments / suggestions, please let me know!

 

Hokie LaTeX Beamer Theme v1.0

One week ago, I posted the alpha release of a Hokie theme for the LaTeX Beamer class for fellow Hokies using Beamer for slide shows.  I got a few good comments / suggestions, and I made a few edits as I used the theme myself.  I’m pretty happy with where it is, and it remains highly configurable for anyone else that wants to tweak it.

 

Here are some of the changes made since the last post:

  • The background canvas is now a lighter color, and the navigation bar is now darker.
  • Bullets fade from maroon to orange depending on the indent level.
  • The source code now includes two logos, the VT logo and the Wireless@VT logo, for anyone that wants to use them.
  • Updated the example presentation file.
  • Added a README for people who want to tweak it.
  • Changed the color of the block example backgrounds.

Here are a few pictures that illustrate what it looks like:

Title Slide

Table of Contents

Basic Slide

Bullet Points

The example presentation in the beamer-example/ directory of the source tree has more slides, including a table, figure, and equation, but the above should be enough to give you an idea of the theme’s general appearance.

The source code is available here: http://git.hokietux.net/?p=latex.git&a=summary

Let me know if you have any comments / suggestions, and enjoy! =)

Hokie Beamer Theme – 1st Release

Okay, so in the process of creating the slides for my Master’s defense, I decided to make a Hokie theme for the Beamer LaTeX class.  Right now, there is only one available color scheme, which more or less mimics the VT web color palette guidelines.

I grabbed an example presentation from Jean-Etienne, and compiled it using the Hokie theme.  The four images below show what some of the basic slides look like:

The title slide of the default layout.

The outline slide of the default layout.

An example basic slide using the default layout.

An beamer example block in a slide.

Using the theme is pretty simple – grab the files from my Git repository, put them in your project directory, and then simply use the command:

usetheme{Hokie}

…in your *.tex file.  You can see all of this in the beamer-example directory, as well.

There are three available layouts that I have tested the theme with, and they all work well.  If you look inside of ‘beamerthemeHokie.sty’, you will see three theme lines, only one of which isn’t commented out.  Try each of them, and choose the one you like the most!  I’ll post pictures of the rest as soon as I have time.

Head here for the code!

If you have any bug reports, suggestions, or comments, please let me know!