Jump to content
 

What every embedded programmer should know about ...

  • entries
    30
  • comments
    15
  • views
    2,597

Contributors to this blog

Getting Started in Embedded Programming Pt 5

Sign in to follow this  
N9WXU

102 views

It has been a busy few weeks but finally I can sit down and write another installment to this series on embedded programming.

Today's project will be another peripheral and a visualization tool for our development machines.  We are going to learn about the UART.  Then we are going to write enough code to attach the UART to a basic C printing library (printf) and finally we are going to use a program called processing to visualize data sent over the UART.  This should be a fun adventure so let us get started.

UART (USART?)

The word UART is an acronym that stands for Universal Asynchronous Receiver Transmitter.  Some UART's have an additional feature so they are Universal Synchronous Asynchronous Receiver Transmitter or USART.  We are not going to bother with the synchronous part so let us focus on the asynchronous part.  The purpose of the UART is to send and receive data between our micro controller and some other device.  Most frequently the UART is used to provide some form of user interface or simply debugging information live on a console.  The UART signaling has special features that allow it to send data at an agreed upon rate (the baud rate) and an agreed upon data format (most typically 8 data bits, no parity and 1 stop bit).  I am not going to go into detail on the UART signaling but you can easily learn about it by looking in chapter 36 of the PIC16F18446 datasheet.  Specifically look at figure 36-3 if you are interested.  UARTs are a device that transfer bytes of data (8-bits in a byte) one bit at a time across a single wire (one wire for transmit and one for receive) and a matching UART puts the bits back into a byte.  Each bit is present on the wire for a brief but adjustable amount of time.  Sending the data slowly is suitable for long noisy wires while sending the data fast is good for impatient engineers.  A common baud rate for user interfaces is 9600 baud which is sends 1 letter every millisecond (0.001 seconds).  Many years ago, before the internet, I had a 300 baud modem for communicating with other computers.  When I read my e-mail the letters would arrive slightly slower than I could read.  Later, after the internet was invented and we used modems to dial into the internet, I had a 56,600 baud modem so I could view web-pages and pictures more quickly.  Today I use UARTS to send text data to my laptop and with the help of the processing program we can make a nice data viewer.  Enough history... let us figure out the UART.

Configuring the UART

The UART is a peripheral with many options so we need to configure the UART for transmitting and receiving our data correctly.  To setup the UART we shall turn to page 571 of our PIC16F18446 data sheet.  The section labeled 36.1.1.7 describes the settings required to transmit asynchronous data.

image.png

And now for some code.

Here is the outline of our program.

void init_uart(void)
{
    
}

int getch(void)
{
    
}

void putch(char c)
{
    
}

void main(void)
{
    init_uart();
    while(1)
    {
        putch(getch()+1);
    }
}

This is a common pattern for designing a program.  I start by writing my "main" function simply using simple functions to do the tricky bits.  That way I can think about the problem and decide how each function needs to work.  What sort of parameters each function will require and what sort of values each function will return.  This program will be very simple.  After initializing the UART to do its work, the program will read values from the UART, add 1 to each value and send it back out the UART.  Since every letter in a computer is a simple value it will return the next letter in each series that I type.  If I type 'A' it will return 'B' and so on.  I like this test program for UART testing because it will demonstrate the UART is working and not simply shorting the receive pin to the transmit pin.  Now that we have an idea for each function.  Let us write them one a time.

init_uart()

This function will simply write all the needed values into the special function registers to activate the UART.  To do that, we shall follow the checklist in the data sheet.  We need to configure both the transmit and the receive function.

Step 1 is to configure the baud rate.  We shall use the BRG16 because that mode has the simplest baud rate formula.

image.png

Of course, it is even easier when you use the supplied tables.  I always figure that if you are not cheating, you are not trying so lets just find the 32MHz column in the SYNC=0, BRGH=0, BRG16 = 1 table.  I like 115.2k baud because that makes the characters send much faster.  So the value we need for the SPBRG is 16.

Step 2 is to configure the pins.  We shall configure TX and RX.  To do that, we need the schematics.  The key portion of the schematics is this bit.

image.pngimage.png

The confusing part is, the details of the TX and RX pin.  If I had a $ for every time I got these backwards, I would have retired already.  Fortunately the team at Microchip who drew these schematics were very careful to label the CDC TX connecting to the UART RX and the CDC RX connecting to the UART TX.  They also labeled the UART TX as RB4 and UART RX as RB6.  This looks pretty simple.  Now we need to steer these signals to the correct pins via the PPS peripheral.  

NOTE: The PPS stands for Peripheral Pin Select.  This feature allows the peripherals to be steered to almost any I/O pin on the device.  This makes laying out your printed circuit boards MUCH easier as you can move the signals around to make all the connections straight through.  You can also steer signals to more than one output pin enabling debugging by steering signals to your LED's or a test point.

After steering the functions to the correct pins, it is time to clear the ANSEL for RB6 (making the pin digital) and clear TRIS for RB4 (making the pin an output).

The rest of the initialization steps involve putting the UART in the correct mode.  Let us see the code.

void init_uart(void)
{
    // STEP 1 - Set the Baud Rate
    BAUD1CONbits.BRG16 = 1;
    TX1STAbits.BRGH = 0;
    SPBRG = 16;
    
    // STEP 2 - Configure the PPS
    // PPS unlock sequence
    // this should be in assembly because the hardware counts the instruction cycles
    // and will not unlock unless it is exactly right.  The C language cannot promise to
    // make the instruction cycles exactly what is below.
    asm("banksel PPSLOCK");
    asm("movlw 0x55");
    asm("movwf PPSLOCK");
    asm("movlw 0xAA");
    asm("movwf PPSLOCK");
    asm("bcf PPSLOCK,0");    
    
    RX1PPSbits.PORT = 1;
    RX1PPSbits.PIN  = 6;
    RB4PPS = 0x0F;
    // Step 2.5 - Configure the pin direction and digital mode
    ANSELBbits.ANSB6 = 0;
    TRISBbits.TRISB4 = 0;
    
    // Step 3
    TX1STAbits.SYNC = 0;
    RC1STAbits.SPEN = 1;
    // Step 4
    TX1STAbits.TX9  = 0;
    // Step 5 - No needed
    // Step 1 from RX section (36.1.2.1)
    RC1STAbits.CREN = 1;
    // Step 6
    TX1STAbits.TXEN = 1;
    // Step 7 - 9 are not needed because we are not using interrupts
    // Step 10 is in the putchar step.
}

To send a character we simply need to wait for room in the transmitter and then add another character.

void putch(char c)
{
    while(!TX1IF); // sit here until there is room to send a byte
    TX1REG = c;
}

And to receive a character we can just wait until a character is present and retrieve it.

int getch(void)
{
    while(!RC1IF); // sit here until there is a byte in the receiver
    return RC1REG;
}

And that completes our program.

Here is the test run.

image.png

Every time I typed a letter or number, the following character is echoed.

Typing a few characters is interesting and all but I did promise graphing.  We shall make a simple program that will stream a series of numbers one number per row.  Then we will introduce processing to convert this stream into a graph.

The easiest way to produce a stream of numbers is to use printf.  This function will produce a formatted line of text and insert the value of variables where you want them.

The syntax is as follows:

value = 5;
printf("This is a message with a value %d\n",value);

To get access to printf you must do two things.

1) You must include the correct header file.  So add the following line near the top of your program.  (by the xc.h line)

#include <stdio.h>

2) You must provide a function named putch that will output a character.  We just did that.

So here is our printing program.

#include <stdio.h>
#include <math.h>

/// Lots of stuff we already did

void main(void)
{    
    double theta = 0;
    init_uart();
    while(1)
    {
        printf("%f\r\n",sin(theta));
        theta += 0.01;
        if(theta >= 2 * 3.1416)
            theta = 0;
    }
}

Pictures or it didn't happen.

image.png

And now for processing.

Go to http://www.processing.org and download a suitable version for your PC.

Attached is a processing sketch that will graph whatever appears in the serial port from the PIC16.

image.png

And here is a picture of it working.

I hope you found this exercise interesting.  As always post your questions & comments below.  Quite a lot was covered in this session and your questions will guide our future work.

Good Luck

 

exercise_5a.zip exercise_5.zip graph_data.pde

  • Like 1
Sign in to follow this  


2 Comments


Recommended Comments

Awesome!

What do I have to do to get that to do printf("Hello, world") ?

Share this comment


Link to comment

That is covered!!!

As explained in the article and demonstrated in the 5'th listing, printf is setup by the putch function.

Of course I did not do something as mundane as Hello, world!, but I did produce floating point values for the graph.

 

Share this comment


Link to comment
Guest
Add a comment...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...