In my previous post, I talked about two single board computers, a CP-JR51-USB and a CP-JR51-ADU842. Both are based on the Atmel ADuC842 processor which is an Intel 8051 single chip computer. What we would like to do in this programming example is to setup the computers so that they can support the basic printf statement in C and have the characters appear on the serial port. If we can properly do this, we can write more complex programs and write diagnostics to a tty terminal on our development computer. If we can get read operations to work through the serial port we can also get a command interpreter on the target computer and upload bulk results as desired.
The code is relatively simple for this test. To write character variables to the serial port we must first program the UART for the proper baud rate, call the putchar function and output a character, the look for the character on a terminal connected to the serial port.
First we must setup a serial connection and download a tty terminal. You could use the default modem communication program that comes with Windows but it does not record transactions and is very limited in how it works. I downloaded putty from putty.org. This is an industry standard tty program that works on Windows and allows you to connect to a serial port at different buad rates.
The second step is to develop a program to write characters and strings to the serial port. To do this we need to first program the UART for the proper baud rate. On the adu842 board, we were able to get a 9600 baud stream going using the value 0xf9 for register TH1. For the jr51usb board, we were only able to get 4800 bauds working by writing 0xf3 to this register. The code looks like:
#include <stdio.h>
#include <reg52.h>
#ifdef MONITOR51
char code reserve [3] _at_ 0×23;
#endif
void main (void) {
char buf[] = “Test String”;
char *p = buf;
int i;
#ifndef MONITOR51
SCON = 0×50
TMOD |= 0×20
TH1 = 0xf3; // 0xf9 for ad842 @ 9600 and 0xf3 for jr51usb board @4800
TR1 = 1;
TI = 1;
#endif
while (1) {
for (i=0×20; i<0×7f; i++) {
P1 ^= 0×01;
putchar(i);
}
printf(” \n String %s is at address %p – %d\n”, buf, p, i);
}
}
The code should cycle through the printable characters from 0×20 to 0×7f and follow it by a line reading “String Test String is at address i:0022 – 127″. When we connect to the serial line with putty we should see the characters coming across the terminal.
When you start putty, it comes up in a setup configuration. You will need to select Serial as the connection type and enter the Serial line and baud rate to make the connection. When you click on Open you should see the output to the screen.
Tags: ecg, ekg, jr51-adu842, jr51usb, single board computer