📄 lcd_io.c
字号:
// A collection of I/O functions for WSi eval boards which include
// LED's, LCD, UART.
// Mark Rootz 3/30/98 */
// debugged for eval boards T. Wilkerson 7-9/98
//
// Modified by William Chin for uPSD3234 from ST Microelectronics
// on June 14, 2002
// collection of LCD functions only
//
// Revision History
//
// 7/22/02 (JAM) - Added LCD buffer for mirroring LCD data to PC app.
//-- Includes ----------------------------------------------------------------
#include "general.h"
#include "upsd.h" // SFRs
#include "upsd_xreg.h" // extended registers in uPSD
#include "lcd_io.h"
#include "timer_func.h"
#include "app_intr.h"
//-- Variables ---------------------------------------------------------------
extern DISPLAY LCD_reg;
static idata uchar Cursor_LCD;
// User design CG data
static uchar code cg_data[] = {
0x1F, //14%
0x11,
0x11,
0x11,
0x11,
0x11,
0x11,
0x1F,
0x1F, //28%
0x11,
0x11,
0x11,
0x11,
0x11,
0x1F,
0x1F,
0x1F, //43%
0x11,
0x11,
0x11,
0x11,
0x1F,
0x1F,
0x1F,
0x1F, //57%
0x11,
0x11,
0x11,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F, //71%
0x11,
0x11,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F, //86%
0x11,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F, //100%
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
0x1F,
-1
};
char LCD_buffer[LCD_BUFFER_SIZE + 1]; // LCD mirror buffer
static char LCD_index; // Cursor pos in LCD_buffer
//-- Prototypes----------------------------------------------------------------
static void SetUserCG(uchar *); // initialize user character pattern
static void BusyCheck(void); // wait until BF is cleared
static char htoa_lo(uchar); // converts low nibble of unsigned byte
static char htoa_hi(uchar); // converts hi nibble of unsigned byte
//-- Functions ----------------------------------------------------------------
void initLCD(void) // initialize LCD module per specs
{
int i;
delay_ms(15);
LCD_reg.LCD_CMD_WR = 0x30;
delay_ms(4);
LCD_reg.LCD_CMD_WR = 0x30;
delay_ms(1);
LCD_reg.LCD_CMD_WR = 0x30;
delay_ms(1);
LCD_reg.LCD_CMD_WR = 0x38; // 8 bits, 2 lines, 5 x 7 font
delay_ms(4); // delay 4 ms
BusyCheck();
LCD_reg.LCD_CMD_WR = 0x0C; //Display on, Cursor off, Non-Blink
BusyCheck();
LCD_reg.LCD_CMD_WR = 0x01; //Clear display
BusyCheck();
LCD_reg.LCD_CMD_WR = 0x02; //Cursor home
BusyCheck();
LCD_reg.LCD_CMD_WR = 0x06; //Cursor inc, no shift/cursor move
SetUserCG(&cg_data); //set user desfined character
Cursor_LCD = DD_ADDR; //Display from 1st row, 1st column
BusyCheck();
LCD_reg.LCD_CMD_WR = Cursor_LCD;
// Initialize mirror buffer
for (i = 0; i < LCD_BUFFER_SIZE; i++)
{
LCD_buffer[i] = ' ';
}
LCD_buffer[LCD_BUFFER_SIZE] = 0;
LCD_index = 0;
}
static void SetUserCG(uchar *data_ptr) // initialize user character pattern
{
BusyCheck();
LCD_reg.LCD_CMD_WR = CG_ADDR | (8*1); //from character code 1
while (*data_ptr != -1) {
BusyCheck();
LCD_reg.LCD_RAM_WR = *data_ptr++;
}
}
static void BusyCheck(void) // wait until BF is cleared
{
while (LCD_reg.LCD_CMD_RD & BF_BIT);
}
/*
void setXY_LCD (uchar row, uchar col) {
Cursor_LCD = (DD_ADDR | ((row & 0x01) << 6)) + (col & LCD_LINE_LENGTH);
BusyCheck();
LCD_reg.LCD_CMD_WR = Cursor_LCD;
}
void putch_LCD(uchar ch) {
BusyCheck();
LCD_reg.LCD_RAM_WR = ch;
}
*/
void printfLCD(uchar *chr_ptr, ...)
{
uchar *var_ptr=&chr_ptr+1;
uchar var;
while (*chr_ptr != NULL)
{
BusyCheck();
if (*chr_ptr == '\r')
{
// Return to position 0 at current line
chr_ptr++;
Cursor_LCD &= 0xC0;
LCD_reg.LCD_CMD_WR = Cursor_LCD;
LCD_index -= LCD_index % LCD_LINE_SIZE;
}
else if (*chr_ptr == '\n')
{
chr_ptr++;
Cursor_LCD ^= 0x40; //goto next line
Cursor_LCD &= 0xC0; //return to position 0
LCD_reg.LCD_CMD_WR = Cursor_LCD;
if (LCD_index < (LCD_BUFFER_SIZE - LCD_LINE_SIZE))
{
LCD_index += LCD_LINE_SIZE;
}
}
else if (*chr_ptr == '%')
{
chr_ptr++;
if (*chr_ptr == 'd')
{
// Display 1 digit decimal 0-9
chr_ptr++;
var = *var_ptr++;
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = (var & 0x0F)+'0';
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
}
else if (*chr_ptr == 'x')
{
// Display 1 byte hex 00-FF
chr_ptr++;
var = *var_ptr++;
//LCD_reg.LCD_RAM_WR = Bin2Hex(var>>4);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_hi(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
BusyCheck();
//LCD_reg.LCD_RAM_WR = Bin2Hex(var&0x0F);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_lo(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
}
else if (*chr_ptr == 'w')
{
// Display 1 word hex 0000-FFFF
chr_ptr++;
var = *var_ptr++;
//LCD_reg.LCD_RAM_WR = Bin2Hex(var>>4);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_hi(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
BusyCheck();
//LCD_reg.LCD_RAM_WR = Bin2Hex(var&0x0F);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_lo(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
BusyCheck();
var = *var_ptr++;
//LCD_reg.LCD_RAM_WR = Bin2Hex(var>>4);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_hi(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
BusyCheck();
//LCD_reg.LCD_RAM_WR = Bin2Hex(var&0x0F);
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = htoa_lo(var);
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
}
else
{
// Out character to LCD Display RAM
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = *chr_ptr++;
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
}
}
else
{
// Out character to LCD Display RAM
LCD_reg.LCD_RAM_WR = LCD_buffer[LCD_index++] = *chr_ptr++;
if (LCD_index >= LCD_BUFFER_SIZE)
{
LCD_index = 0;
}
}
}
}
// These are the LCD functions.
/*
void lcd_clear(void) // clear all characters from display
{
BusyCheck();
LCD_reg.LCD_CMD_WR = 0x01;
Cursor_LCD = DD_ADDR;
}
void lcd_string_display(row,col,string)
// send string to LCD
// row = 0 is top row
// row = 1 is bottom
// col = 0 to 15
// length = string length
// string points to char array
uchar row;
uchar col;
uchar *string;
{
char k;
k = 0;
lcd_cursor_set(row,col); // position cursor
while (k < LCD_LINE_LENGTH+1 && *string) // truncate string to 16
// or end of string
{
BusyCheck();
LCD_reg.LCD_RAM_WR = *string++; // send character
k++;
}
}
void lcd_char_display(row,col,ch) // send single char to LCD
// row = 0 is top row
// row = 1 is bottom
// col = 0 to 15
uchar row;
uchar col;
char ch;
{
lcd_cursor_set(row,col); // position cursor
BusyCheck();
LCD_reg.LCD_RAM_WR = ch; // send character
}
void lcd_cursor_set(row,col) // move cursor to desired position
uchar row;
uchar col;
{
BusyCheck();
switch(row)
{
case 0:
LCD_reg.LCD_CMD_WR = (0x80 + col);
break;
case 1:
LCD_reg.LCD_CMD_WR = (0xC0 + col);
break;
}
}
*/
// this is a collection of conversion routines used in conjunction with the LCD display
/*
static char Bin2Hex(char temp) {
if (temp <= 9) temp += '0'; else temp=(temp-10)+'A';
return (temp);
}
*/
static char htoa_lo(byte) // converts low nibble of unsigned byte
// (0-F hex) to ascii
uchar byte;
{
byte = byte & 0x0F; // keep lower nibble only
if (byte <= 0x09)
return(byte + 0x30);
else
return (byte + 0x37);
}
/*
static char lhtoa_lo(word) // converts 2nd nibble of unsigned int
// (0-F hex) to ascii
unsigned int word;
{
word = word >> 8;
word = word & 0x000F;
if (word <= 0x0009)
return((char)word + 0x30);
else
return ((char)word + 0x37);
}
*/
static char htoa_hi(byte) // converts hi nibble of unsigned byte
// (0-F hex) to ascii
uchar byte;
{
byte = byte & 0xF0; // keep upper nibble only
byte = byte >> 4;
if (byte <= 0x09)
return(byte + 0x30);
else
return (byte + 0x37);
}
/*
static char lhtoa_hi(word) // converts 1st nibble of unsigned int
// (0-F hex) to ascii
unsigned int word;
{
word = word >> 12;
word = word & 0x000F;
if (word <= 0x0009)
return((char)word + 0x30);
else
return ((char)word + 0x37);
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -