MPS_Lab_Ex1-IDE_ANSI

MPS_Lab_Ex1-IDE_ANSI
MPS
IDE & ANSI Display Lab Exercise
Using the IDE and ANSI Display Commands
Student's name & ID (1): ___________________________________________________
Partner's name & ID (2): ___________________________________________________
Your Section number & TA's name __________________________________________
Notes:
You must work on this assignment with your partner.
Hand in a printer copy of your software listings for the team.
Hand in a neat copy of your circuit schematics for the team.
These will be returned to you so that they may be used for reference.
------------------------------- do not write below this line -----------------------------
POINTS
(1)
(2)
TA init.
Grade for performance verification (50% max.)
Grade for answers to TA's questions (20% max.)
Grade for documentation and appearance (30% max.)
TOTAL
Grader's signature: ___________________________________________
Date: ______________________________________________________
Using the IDE and Programming an ANSI Display
GOAL
By doing this lab assignment, you will learn:
1. Exercise the 8051 CPU registers and built-in hardware.
2. To use the VT100 Terminal Interface (with HyperTerminal or ProComm Plus) on the 8051.
3. To program an ANSI terminal display through a C program using the SDCC Compiler.
4. Perform basic I/O with the 8051.
PREPARATION
•
•
•
Review the 8051 hardware features.
Review the C language stdio utilities.
References: C8051F12x-13x.pdf 8051 C8051F12X Reference Manual, Ch. 1, 11, 18
C8051F12x-DK.pdf C8051F12X Development Kit User’s Guide
VT100/ANSI ESCAPE SEQUENCES and Hello.c sample program
IDE SETUP
1.
Connecting The Board
See the C8051F12X Development Kit User’s Guide for details on connecting the board and USB Debug
Adapter (http://www.ecse.rpi.edu/courses/CStudio/Silabs/C8051F12x-DK.pdf).
2. Using The IDE
Start the SiLabs IDE by clicking on Silicon Laboratories on the Start Menu and choosing Silicon
Laboratories IDE. Next, create a new project to which C files can be added. To create a new project,
click on the Project menu and select New. This will create an empty project. Add a new C file to the
project by clicking on the Project menu and selecting Add Files to Project. Give your new file a name,
such as the Hello.c included in this lab. The file will now be part of the project, and can be compiled.
Save the project by clicking on Project and selecting Save Project. WARNING: The project must be
saved in a location whose path contains no folders with space characters in its name. It is suggested that
you use C:\MPSfiles\filename.wsp.
To compile your project, first verify that all of the code is correct. Click on the Project menu and select
Assemble/Compile File. The window at the bottom of the IDE will alert you of any errors or warnings it
finds in your code. If there are errors, correct them first before moving on to the next step. If there are
warnings, you may or may not want to correct them, depending on the nature of the warning. Click on the
Project menu and select Build/Make Project. After successfully building your project, you will want to
upload it to the development board. Before doing this, you will need to configure the adapter used to
download the source code. To do this, click on the Options menu and select Connection Options. Once
the window opens, select the USB Debug Adapter and click OK. You are now ready to download the
code. To do this, click on the Debug menu, and select Connect. This will connect the IDE to the
development board through the USB Debug Adapter. Click on the Debug menu again, and select
Download Object File. Select the file you just compiled, and click Download. This will download the
code to the development board. Finally, to execute your code on the C8051F120, click on the Debug
menu, and select Go.
Page 2
ANSI PROGRAMMING TASKS FOR THE 8051
1. Introduction To The User Interface
Input from the terminal keyboard and output to the terminal display can be done using the getchar(a) and
putchar(a) functions. Write a simple C program to run on the 8051 that outputs "The keyboard
character is *." whenever you type a printable character, where * stands for that character. Since
you will be waiting for an indefinite number of characters to be typed, use <ESC> (or ^[ key
combination, where ^ = <Control>) to end the program. Display this information at the top of the screen
when the program starts.
2. VT100/ANSI Terminal Control Escape Sequences
ProComm Plus and HyperTerminal use VT100/ANSI terminal emulation by default. By sending special
codes to the terminal, it is possible to clear the screen, position the cursor, set terminal colors, and many
other operations. These codes are called escape sequences because the first character is the <ESC>
character, or $1B in ASCII (033 octal). A table is included at the end of this lab that contains some useful
escape codes.
Modify the C program of Part 1 to display yellow characters on a blue background. Center the program
termination information on line 2. Display the keyboard response text on line 6. Change the color of the
keyed in character to white (leaving the rest of the characters in yellow). Now for nonprinting characters,
have the program blink the output "The keyboard character $XX is 'not
printable'." and beep, where XX are the hexadecimal digits of the nonprintable character (include
the underline on the terminal). This line should appear at the line in the center of the screen (e.g. line 12 if
24 lines are being displayed in the terminal window). Also, although the top message may overwrite the
previous message each time a new key is hit, the ‘not printable’ message should be written on the line
beneath the previous line. When it writes on the last line of the screen, the terminal should scroll just the
lines on the lower half of the screen. See the figure below of a terminal screen shot. Note that scrolling
only occurs when you output a ‘\n’ while on the bottom line of the display. Moving the cursor to the
bottom line and writing will only overwrite any text already there. There are escape sequences that
simplify these operations, which you are expected to use as much as possible. Unfortunately, the PCs and
PC cards in the Sun workstations do not have external speakers so headphones are needed to hear the
beep and ProComm must be used instead of HyperTerminal. The version of HyperTerminal used on the
PCs does not seem to support the beep sound (^G = Ctrl-G or ASCII 'BEL').
NOTE: If the terminal doesn’t respond properly to escape sequences, it may no longer be in an ANSI
compatible mode. Make sure the leftmost bottom parameter button in the HyperTerminal window is set
to either VT100 or ANSI BBS and likewise for ProComm Plus.
Good programmer's tip: Design the program top-down. Then write the routines bottom-up. Write them
one at a time and thoroughly test each one before integrating them. This way you will have isolated any
errors to the routine that you are currently writing. Good programmers follow this method.
Page 3
Program termination
information line 2
Overwritten printable
character line 6
Scroll area for
nonprintable
characters
(blinking text)
3. Port Input/Output
Configure the 8051 to properly execute the following tasks:

Set Port 2 to output port bits (use a voltmeter or a TA provided 6-LED module with series current
limiting resistors to verify values). You will first need to set the corresponding bits in the port’s data
direction register (P2MDOUT) to a 1 for output and use the latch register (P2) to write the bits for
output. See the C8051F120 manual, pp. 250-251, for details.

Set Port 1 to input logic levels applied to port pins (use 0 V (ground) for a logic low and +5 V
through a 1 k resistor for a logic high). Again, you will first need to set the registers (P1MDIN &
P1MDOUT on pp. 249-250) bits for input and read from the latch register (P1 on p. 249).

Continuously read in a value on a bit on input Port 1 and output the value to the corresponding bit on
Port 2, which has been connected to an LED. After wiring the LEDs to the correct output pins and
applying a voltage to the input pins, the minimum steps necessary to read in a voltage on pin P1.0
(Port 1) are:
1. Enable the crossbar (XBR0, XBR1 & XBR2: C8051F120 manual, pp. 245-247).
2. Configure P1.n (n = 7 – 0) to open drain; P2.n (n = 7 – 0) to push-pull.
Page 4
3. Read the 8-bit numerical value from P1 into a local variable.
4. Write the value to P2.
If a potentiometer (use the pot modules assigned to lab groups by the TA) whose end terminals
are between ground and +5 V with the wiper connected to the A/D input is used to give various
voltage readings, you should be able to confirm that voltages near 0 V yields a numerical value of
0, and +5 V yields values yields 1. Explain why this makes sense. Determine if the input uses a
Schmitt trigger (hysteresis) on voltage levels.
NOTE: Make sure you provide a common ground connection between the +5V of your external TTL
circuit and the +3.3V of the C8051F120 board. Also be sure the +5V supply is NOT connected to the
+3.3V supply on the microcontroller. It isn’t critical, but probably a little safer, if the potentiometers on
the A/D Converter Input Module are connected to +3.3V and ground from the C8051F120 board rather
than +5V.
Page 5
VT100/ANSI ESCAPE SEQUENCES
Name
Escape Code
Hexadecimal
Description
Reset
Device
Enable Line
Wrap
Disable
Line Wrap
Cursor Home
<ESC>c
$1B $63
Resets all terminal settings to default.
<ESC>[7h
$1B $5B $37
$68
$1B $5B $37
$6C
$1B $5B $48
Cursor
Position
<ESC>[{ROW};
{COL}H
Enables wrapping text to the next line if text is longer than
the display area.
Disables wrapping text; text will be clipped if longer than
display area.
Moves the cursor to the home position (upper left hand
corner).
Sets the position of the cursor at ({ROW}, {COL}).
Cursor Up
<ESC>[{NUM}A
Cursor Down
<ESC>[{NUM}B
Cursor Left
<ESC>[{NUM}D
Cursor
Right
Save Cursor
Restore
Cursor
Erase End
of Line
Erase Start
of Line
Erase Line
<ESC>[{NUM}C
Erase Down
<ESC>[J
Erase Up
<ESC>[1J
Erase
Screen
Scroll All
Scroll
Section
Scroll Down
Scroll Up
Attribute
Mode set
Standard
Values for
Attribute
Mode Set
<ESC>[7l
<ESC>[H
<ESC>[s
<ESC>[u
$1B $5B
${ROW} $3B
${COL} $48
$1B $5B
${NUM} $41
$1B $5B
${NUM} $42
$1B $5B
${NUM} $44
$1B $5B
${NUM} $43
$1B $5B $73
$1B $5B $75
<ESC>[K
$1B $5B $4B
<ESC>[1K
$1B $5B $31
$4B
$1B $5B $32
$4B
$1B $5B $4A
<ESC>[2K
Moves the cursor up {NUM} rows;
{NUM} defaults to 1 if omitted.
Moves the cursor down {NUM} rows;
{NUM} defaults to 1 if omitted.
Moves the cursor left {NUM} columns;
{NUM} defaults to 1 if omitted.
Moves the cursor right {NUM} columns;
{NUM} defaults to 1 if omitted.
Saves the current cursor position.
Restores the previously stored cursor position.
Erases from the current cursor position to the end of the
current row.
Erases from the start of the current row to the current cursor
position.
Erases the entire current row.
Erases from the current row down to the bottom of the
screen.
Erases from the current row to the top of the screen.
$1B $5B $31
$4A
<ESC>[2J
$1B $5B $32
Erases the entire screen and moves the cursor to the home
$4A
position.
<ESC>[r
$1B $5B $72
Enables scrolling for the entire display.
<ESC>[{SRT};
$1B $5B
Enables scrolling only for rows {SRT} to {END}.
{END}r
${SRT} $3B
${END} $72
<ESC>D
$1B $44
Scrolls the display down one line.
<ESC>M
$1B $4D
Scrolls the display up one line.
<ESC>[{ATR1}; $1B $5B
Sets multiple display attribute settings; any number can be
...;{ATRn}m
${ATR1} $3B
set. ATRn may be any of the following values:
... $3B
${ATRn} $6D
Foreground Colors
Background Colors
0 Reset Attributes
30 Black
40 Black
1 Bright
31 Red
41 Red
2 Dim
32 Green
42 Green
4 Underscore
33 Yellow
43 Yellow
5 Blink
34 Blue
44 Blue
7 Reverse
35 Magenta
45 Magenta
8 Hidden
36 Cyan
46 Cyan
37 White
47 White
Do not include the ‘{‘ or ‘}’ characters in the print statement.
Ex.) <ESC>[{ROW 10};{COL 20}H would be “\033[10;20H”.
Page 6
Page 7





Page 8
Page 9
Page 10



Page 11
//------------------------------------------------------------------------------------------------// Hello.c
//------------------------------------------------------------------------------------------------//8051 Test program to demonstrate serial port I/O. This program writes a message on the console
//using the printf() function, and reads characters using the getchar() function. An ANSI escape
//sequence is used to clear the screen if a '2' is typed. A '1' repeats the message and the program
//responds to other input characters with an appropriate message.
//------------------------------------------------------------------------------------------------// Includes
//------------------------------------------------------------------------------------------------#include <c8051f120.h>
#include <stdio.h>
#include "putget.h"
//------------------------------------------------------------------------------------------------// Global Constants
//------------------------------------------------------------------------------------------------#define EXTCLK
22118400
// External oscillator frequency in Hz
#define SYSCLK
49760000
// Output of PLL derived from (EXTCLK * 9/4)
#define BAUDRATE
115200
// UART baud rate in bps
//------------------------------------------------------------------------------------------------// Function Prototypes
//------------------------------------------------------------------------------------------------void main(void);
void SYSCLK_INIT(void);
void PORT_INIT(void);
void UART0_INIT(void);
//------------------------------------------------------------------------------------------------// MAIN Routine
//------------------------------------------------------------------------------------------------void main(void)
{
char choice;
WDTCN = 0xDE;
WDTCN = 0xAD;
// Disable the watchdog timer
PORT_INIT();
SYSCLK_INIT();
UART0_INIT();
// Initialize the Crossbar and GPIO
// Initialize the oscillator
// Initialize UART0
SFRPAGE = UART0_PAGE;
// Direct output to UART0
printf("\033[2J");
// Erase the screen and move the cursor to the home position.
printf("Test of the printf() function.\n\n");
while(1)
{
printf("Hello World!\n\n\r");
printf("( greetings from Russell P. Kraft )\n\n\n\r");
printf("1=repeat, 2=clear, 0=quit.\n\n\r"); // Menu of choices
choice=getchar();
putchar(choice);
//
// select which option to run
if (choice == '0')
return;
else if(choice == '1')
printf("\n\nHere we go again.\n\n\r");
else if(choice == '2') // clear the screen with <ESC>[2J
printf("\033[2J");
else
{
// inform the user how bright he is
printf("%c is not a valid choice.\n\r",choice);
printf("A ");
putchar(choice);
printf(" is not a valid choice.\n\n\r");
}
}
}
//------------------------------------------------------------------------------------------------// SYSCLK_Init
//-------------------------------------------------------------------------------------------------
Page 12
//
// Initialize the system clock to use a 22.1184MHz crystal as its clock source
//
void SYSCLK_INIT(void)
{
int i;
char SFRPAGE_SAVE = SFRPAGE;
SFRPAGE = CONFIG_PAGE;
OSCXCN = 0x67;
for(i=0; i < 256; i++);
while(!(OSCXCN & 0x80));
CLKSEL = 0x01;
OSCICN = 0x00;
// Save Current SFR page
// Start external oscillator with 22.1184MHz crystal
// Wait for the oscillator to start up
SFRPAGE = CONFIG_PAGE;
PLL0CN = 0x04;
SFRPAGE = LEGACY_PAGE;
FLSCL = 0x10;
SFRPAGE = CONFIG_PAGE;
PLL0CN |= 0x01;
PLL0DIV = 0x04;
PLL0FLT = 0x01;
PLL0MUL = 0x09;
for(i=0; i < 256; i++);
PLL0CN |= 0x02;
while(!(PLL0CN & 0x10));
CLKSEL = 0x02;
SFRPAGE = SFRPAGE_SAVE;
// Restore SFR page
}
//------------------------------------------------------------------------------------------------// PORT_Init
//------------------------------------------------------------------------------------------------//
// Configure the Crossbar and GPIO ports
//
void PORT_INIT(void)
{
char SFRPAGE_SAVE = SFRPAGE;
// Save Current SFR page
SFRPAGE = CONFIG_PAGE;
XBR0
XBR1
XBR2
= 0x04;
= 0x00;
= 0x40;
P0MDOUT |= 0x01;
SFRPAGE = SFRPAGE_SAVE;
// Enable UART0
// Enable Crossbar and weak pull-up
// Set TX0 pin to push-pull
// Restore SFR page
}
//------------------------------------------------------------------------------------------------// UART)_Init
//------------------------------------------------------------------------------------------------//
// Configure the UART0 using Timer1, for <baudrate> and 8-N-1
//
void UART0_INIT(void)
{
char SFRPAGE_SAVE = SFRPAGE;
// Save Current SFR page
SFRPAGE = UART0_PAGE;
SCON0
= 0x50;
SSTA0
= 0x10;
SFRPAGE = TIMER01_PAGE;
TMOD
&= ~0xF0;
TMOD
|= 0x20;
TH1
= -(SYSCLK/BAUDRATE/16);
CKCON |= 0x10;
TL1
= TH1;
TR1
= 1;
SFRPAGE = UART0_PAGE;
TI0 = 1;
SFRPAGE = SFRPAGE_SAVE;
// Mode 1, 8-bit UART, enable RX
// SMOD0 = 1
// Timer1, Mode 2, 8-bit reload
// Set Timer1 reload value for baudrate -- Timer1 High Byte
// Timer1 uses SYSCLK as time base
// Start Timer1
// Indicate TX0 ready
// Restore SFR page
}
Page 13
//-----------------------------------------------------------------------------------// putget.h
//-----------------------------------------------------------------------------------// Title:
Microcontroller Development: putchar() & getchar() functions.
// Author:
Dan Burke
// Date Created:
03.25.2006
// Date Last Modified: 03.25.2006
//
// Description:
http://chaokhun.kmitl.ac.th/~kswichit/easy1/easy1_3.html
//
//
// Target:
C8051F120
// Tool Chain:
KEIL C51 & SDCC
//-----------------------------------------------------------------------------------// putchar()
//-----------------------------------------------------------------------------------void putchar(char c)
{
while(!TI0);
TI0=0;
SBUF0 = c;
}
//-----------------------------------------------------------------------------------// getchar()
//-----------------------------------------------------------------------------------char getchar(void)
{
char c;
while(!RI0);
RI0 =0;
c = SBUF0;
putchar(c);
// echo to terminal
return SBUF0;
}
Page 14
LED Display Board
A/D Converter Input Voltage Board
Page 15
Was this manual useful for you? yes no
Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Download PDF

advertisement