// File DS8007-1.C
#include <REG5000.H> // special function register declarations
// // for the DS5000/5002
#include <stdio.h> // prototype declarations for I/O functions
// Main C function. Program execution starts here.
void main (void) {
// Set up the serial port for 38400 baud at 14.7MHz.
TMOD=0x21; // Timer 1: 8-bit auto-reload from TH1, Timer 0: 16-bit
TH1 = 0xFE; // Set timer reload value for 38,400 baud
PCON|=0x80; // Turn on baud rate doubler SMOD_0
TCON=0x50; // Enable timers 0 and 1
SCON=0x50; // 10-bit async, enabled
TI=1; // Set TI to send first character
TR1 = 1; // Start timer 1
// Start main program
printf ("\nHello DS8007 World!\n\n"); // Output message
while (1) ; // End program by looping here.
}
右键单击工程工作空间窗口中的Target 1,将该文件加到工程列表中,然后单击"Manage Components"选项。在Project Components标签下,单击"Add Files"按钮,在文件名区输入文件名(DS8007-1.c)。单击"Add"按钮,然后单击"Close"按钮。单击"OK"按钮,关闭Components窗口。您会看到该文件已经被加入到Source Group 1中。
右键单击工程工作空间中的Target 1,选择"Options for Target 'Target 1",配置工程选项。单击Target标签,在Xtal框中输入14.7。如下面的图3所示,Memory Model选择"Small: variables in DATA",Code ROM Size选择"Large: 64K program",Operating system选择"None"。该窗口中的所有其他选项不进行设置,为默认状态。
在Output标签中,选中"Create Executable" (如果还没有选它),确定选中了"Create HEX File"框。从下拉选择框中选择HEX Format of HEX-80,如图4所示。该窗口中的所有其他选项都应保持默认状态。单击"OK",关闭'Target 1'窗口的Options for Target。
单击"File",然后是"Save All",保存工程文件。
单击"Project"标题栏,选择"Rebuild All Target Files",建立可执行文件。屏幕底部的构建窗口应显示0 Error(s), 0 Warning(s)。如果没有这一显示,则应该找到错误,进行必要的改正。重复该步骤,直到不再报告错误为止。
为演示DS8007评估套件非常有用的一项功能,我们现在建立一个应用程序,在2行20字符的液晶显示屏(LCD)上显示一条消息。但是在开始前,必须设置LCD的对比度,使其在程序执行时能够正确地显示消息。首先,对电路板加电,LCD应不亮。找到电路板上的可变电阻R7 (参见上面的图2),使用小螺丝刀对电阻进行调整,直到一个5 x 7点阵出现在显示屏的字符位置。慢慢调整R7,直到点阵刚刚消失。这设置LCD的对比度,使我们能够看到字符,而看不到间隔。
// file DS8007-2.c
//
#include <REG5000.H> // special function register declarations
// // for the DS5000/5002 #include <string.h>
#include "LCD_Funct.h"
int tmp = 0;
uint8_t LCD_Str[42];
void main(void)
{
// Initialize LCD Module, and display 2-line message.
LCD_Init();
strcpy(LCD_Str, "DS8007 Dual"); // Create first line of LCD text
tmp = LCD_Curpos(1,5); // Position cursor line 1 char 5
if (tmp == 0) LCD_WRStr(LCD_Str); // Write string to LCD
strcpy(LCD_Str, "Smart Card Interface"); // Create 2nd line of LCD text
tmp = LCD_Curpos(2,1); // Position cursor on line 2, char 1
if (tmp == 0) LCD_WRStr(LCD_Str); // Write string to LCD
tmp = LCD_Curpos(1,20); // Return cursor to line 1 char 20
while (1); // Loop here to end program
}
void LCD_Init()
{
LCD_Delay(); // Delay to ensure that LCD power-up is complete
LCD_WRCmd(FnctSet); // Write FnctSet instruction
LCD_WRCmd(DispCnt); // Write Display Control instruction
LCD_WRCmd(DispClear); // Write Display Clear instruction
LCD_WRCmd(EntryMode); // Write Entry Mode instruction
}
// Write a string to the 2 x 20 LCD module
void LCD_WRStr(uint8_t LCD_Str[])
{
int i = 0;
while ((LCD_Str[i] != '\0') && (i < 20))
{
LCD_WRChr(LCD_Str[i]) ; // Write first 20 characters
i++;
}
if (LCD_Str[i] != '\0')
LCD_WRCmd(Line2Ad); // Set CGRAM address to first char 2nd line
while ((LCD_Str[i] != '\0') && (i < 40))
{
LCD_WRChr(LCD_Str[i]) ;
i++;
}
}
// Write a single character to the 2 x 20 LCD module
void LCD_WRChr(uint8_t LCD_Chr)
{
LCD_Busy(); // Wait until the LCD is not busy
LCD_RS = 1; // Set RS high for character write
LCD_Dat = LCD_Chr; // Output character
LCD_EN = 1; // Set enable high
LCD_EN = 0; // Clear enable
LCD_RS = 0;
LCD_Dat = 0xFF; // Re-establish Port Pins as inputs
}
// Write a command to the 2 x 20 LCD module
void LCD_WRCmd(uint8_t LCD_Cmd)
{
LCD_EN = 0; // Make sure that the LCD enable is low
LCD_RS = 0; // Set LCD register select and R/W lines
LCD_RW = 0;
LCD_Busy(); // Make sure that the display is not busy
LCD_Dat = LCD_Cmd; // Output instruction
LCD_EN = 1; // Set enable high
LCD_EN = 0; // Clear enable
LCD_Dat = 0xFF; // Re-establish Port Pins as inputs
}
// Set the cursor position to a specific location
int LCD_Curpos(uint8_t VPos, uint8_t HPos)
{
int rtn;
uint8_t Addr, Cmd;
// Check input range 1..2 line, 1..20 characters per line
if (((VPos == 0) || (VPos > 2)) || ((HPos == 0) || (HPos > 20)))
rtn = -1;
else
{
if(VPos == 2) Addr = (0x40 + (HPos - 1));
else Addr = HPos - 1;
rtn = 0;
Cmd = Addr | 0x80;
LCD_WRCmd(Cmd);
}
return(rtn);
}
// Test the LCD Module to see if it is Busy (loop until
// not busy)
void LCD_Busy()
{
uint8_t LCD_Stat = LCD_Dat; // Get byte containing status
while (LCD_Stat & 0x80){ // Wait for busy flag to clear
LCD_RW = 1; // LCD RW needs to be high
LCD_EN = 1; // Strobe enable signal
LCD_Stat = LCD_Dat; // Read staus
LCD_EN = 0;
LCD_RW = 0;
}
}
// Time delay for 2 x 20 LCD module (approximately 50ms)
void LCD_Delay()
{
uint8_t i, j ;
for (i=0;i!=100;i++)
{
for (j=0;j!=153;j++);
}
}
µVision是Keil, An ARM Company的注册商标。
Windows是Microsoft Corp.的注册商标。