Serial Communications
#1

[attachment=12624]
Serial Communications
Serial communications with the C8051F020 microcontroller is a simple process. All you have
to do is figure out how to configure the chip to transport data to the serial port and then it’s
a matter of just using printf or scanf statements in your C compiler and using the serial port as
standard output or standard input. Pretty slick! The included Keil compiler will also allow you
to use the C++ language and std::cin and std::cout which will work the same way.
First lets get to know the ed-board a bit better (figure ).
On the above diagram block J5 is the RS232 connector for UART0. Of importance to us
is jumpers J6 and J9. The serial port is not already connected and ready for communications and
these two jumpers will allow you to double info from pins 0.0 and 0.1 (read as port 0 pin 0 & 1
respectively) to the RS232 connector. Setting J6 to short will set the UART 0 Tx to the RS232
connector and setting J9 to short will allow Rx on the serial port. If these jumpers are not
shorted your communications will only appear on pins 0.0 and 0.1. While on the topic, there are
some inconstancies I have found with the ed-board. Amongst them and of importance to you at
this point is the silk screening labeling pins and ports. The board has many ports and pins
labeled beginning with pin 1. This is only a standard used to show where the beginning pin for a
port is, in your use however the ports begin with pin 0 and go up through pin 9. Additionally, in
creating headers for your ports or just connecting wires the port pins are numbered from the
lower left and then increment up then back to the bottom in the next row then up again. So, pin 1
is above pin 0 and pin 2 is to the right of pin 0 etc. This is if you are looking at the board as in
figure 1.
Now that you are familiar with the port you will be using lets consider the code required
to enable communications. This will set up transmit only and if you get it to work you will
receive a message from the microcontroller on your computer screen. Here is a copy of the
overall program. Remember when you are writing code for the 8051 to use all the tools at your
disposal to make your life as easy as possible. I put this piece together in Microsoft’s visual C++
compiler using some of my own code, some generated by the Cygnal IDE port setup wizard. So
you can use whatever tool is easiest for you to understand, (even a text file) to create your
programs then compile and program with IDE. There is a lot out there to help you, do not
narrow your vision to the IDE package that came with the developer kit. Just be certain you
understand what is going on in each register when you add a piece of code or your debugging
will be impossible. Know what you are adding before you add it!
//serial communications

#include <c8051f020.h>
#include <stdio.h>

//Constants
#define SYSCLK 22118400 // SYSCLK frequency in Hz
#define BAUDRATE 9600 // Static buad rate, remember to set
// telnet session to 9600, 8, n, 1, hardware
//set up sbits for led and reset switch
sbit LED = P1^6; // set bit for onboard LED: on when running
sbit SW1 = P3^7; // press button to ground for reset

//Function Prototypes
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);

// Main
void main (void) {

WDTCN = 0xde; // disable watchdog timer
WDTCN = 0xad;

//init routine
SYSCLK_Init (); // clock setup
PORT_Init (); // crossbar and GPIO
UART0_Init (); // UART0 for serial output
EA = 0 //diable global interrupts. We do not need them for

// this program.
printf("You have enabled serial Tx!");


}//end main


//Initialization routines

// SYSCLK_Init
void SYSCLK_Init (void)
{
int i; // delay counter

OSCXCN = 0x67; // start external oscillator with
// 22.1184MHz crystal

for (i=0; i < 256; i++) ; // delay

while (!(OSCXCN & 0x80)) ; // Let oscilator settle

OSCICN = 0x88; // select external oscillator as SYSCLK
// source and enable missing clock
// detector
}

// Configure the Crossbar and GPIO ports
void PORT_Init (void)
{
XBR0 = 0x04; // Enable UART0
XBR1 = 0x00;
XBR2 = 0x40; // Enable crossbar and weak pull-ups
P0MDOUT |= 0x01; // enable TX0 as a push-pull output
P1MDOUT |= 0x40; // enable P1.6 (LED) as push-pull output
}

// UART0_Init
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1.
void UART0_Init (void)
{
SCON0 = 0x50; // SCON0: mode 1, 8-bit UART, enable RX
TMOD = 0x20; // TMOD: timer 1, mode 2, 8-bit reload
TH1 = -(SYSCLK/BAUDRATE/16); // set Timer1 reload value for baudrate
TR1 = 1; // start Timer1
CKCON |= 0x10; // Timer1 uses SYSCLK as time base
PCON |= 0x80; // SMOD0 = 1
TI0 = 1; // Indicate TX0 ready
}
Now lets take a closer look at this program function by function beginning with the
includes.
#include <c8051f020.h>
#include <stdio.h>
The first line is one you had better get used to. This is the include for the header file used with
the C8051f020 microcontroller. The statement #include <c8051f020.h> assumes that this
header file is in the default location for the compiler. Of course you can include files which are
anywhere on your system and in the next part, “the Resist-O-Meter” we will discuss this.
Regardless, without this header file you will be getting nowhere fast. The second line will be
familiar to anyone who has used a C compiler. The statement #include <stdio.h>, allows us to
include the standard input-output library for C. Of course if you are using C++ these statements
will be #include <cstdio> and #include <cc8051f020>.
Next the constants used in this program.
//Constants
#define SYSCLK 22118400
#define BAUDRATE 9600
As you can probably figure out from the provided commenting these values are set for
ease of reuse. SYSCLK is set to the frequency of the on board ed-board external oscillator and
BAUDRATE is set to 9600 for communications with a telnet session. These values can be easily
changed in the constants section and by doing so will change them throughout the code without
having to search.
Now the sbit declarations.
//set up sbits for led and reset switch
sbit LED = P1^6;
sbit SW1 = P3^7;
These declarations allow you to give a logical name to a pin on the board. It is like
declaring a constant with #define but instead it gives you a name you can use for any I/O pin on
the board. In this way you can write code that says LED and this will put a value on pin 1.6
thereby turning the on-board LED on or ~LED or !LED (not LED) which will change the value
on pin 1.6 from high to low or low to high. The line sbit SW1 = P3^7, sets the logical name
SW1 to pin 3.7 on the ed-board which is the reset switch. Of course you could just as easily use
any pin and wire it to an external switch and ground.
Function prototypes.
void SYSCLK_Init (void);
void PORT_Init (void);
void UART0_Init (void);
Reply

Important Note..!

If you are not satisfied with above reply ,..Please

ASK HERE

So that we will collect data for you and will made reply to the request....OR try below "QUICK REPLY" box to add a reply to this page
Popular Searches: oscilator with a 7414, stdio h,

[-]
Quick Reply
Message
Type your reply to this message here.

Image Verification
Please enter the text contained within the image into the text box below it. This process is used to prevent automated spam bots.
Image Verification
(case insensitive)

Possibly Related Threads...
Thread Author Replies Views Last Post
  Trends in satellite communications and the role of optical free-space communications seminar class 2 15,083 23-12-2018, 02:07 PM
Last Post:
  Space Laser Communications: Systems, Technologies, and Applications seminar class 3 3,177 21-12-2012, 11:48 AM
Last Post: seminar details
  Image Processing & Communications Project ideas list computer science crazy 1 1,783 20-02-2012, 10:39 PM
Last Post: jayeshrane
  MATLAB in Digital Signal Processing and Communications seminar class 1 2,297 16-01-2012, 11:25 AM
Last Post: seminar addict
  Principles of Communications Bluetooth FM Transmitter smart paper boy 0 1,231 29-08-2011, 10:59 AM
Last Post: smart paper boy
  Global System for Mobile communications [GSM] smart paper boy 0 917 16-08-2011, 04:33 PM
Last Post: smart paper boy
  Conceptual design of 8 channel fiber optics serial data link for digital signal smart paper boy 0 751 10-08-2011, 02:09 PM
Last Post: smart paper boy
  A Scalable Robust Authentication Protocol For Secure Vehicular Communications smart paper boy 0 817 28-07-2011, 03:36 PM
Last Post: smart paper boy
  FAST FPGA-BASED PIPELINED DIGIT-SERIAL/PARALLEL MULTIPLIERS smart paper boy 0 1,095 21-07-2011, 12:32 PM
Last Post: smart paper boy
  WIRELESS COMMUNICATIONS AND MOBILE TECHNOLOGY smart paper boy 0 817 19-07-2011, 02:49 PM
Last Post: smart paper boy

Forum Jump: