?? lcd.c
字號(hào):
//20080707
//TO RED
//T1 UART0
//T2 系統(tǒng)時(shí)基
//T3 AD
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f410.h> // SFR declarations
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <intrins.h>
//-----------------------------------------------------------------------------
// Global CONSTANTS
//-----------------------------------------------------------------------------
#define SYSCLK 24576000 // SYSCLK frequency in Hz
#define BAUDRATE 19200
#define T2_Freq 1000
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F30x
//-----------------------------------------------------------------------------
// Timer2 counter
sfr16 TMR3RLOAD = 0x92;
sbit CS = P0^0; // LCD ChipSelect
sbit SID = P0^1; // Serial Data
sbit SCLK = P0^2; //Serial Clock
sbit RST = P0^3; //Reset
unsigned char GB,k;
unsigned char code AC_TABLE[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87, //第一行漢字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97, //第二行漢字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f, //第三行漢字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f, //第四行漢字位置
};
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void SYSCLK_Init(void);
void PORT_Init(void);
void Timer2_Init (int counts);
void delay1MS(unsigned char n);
void delay10US(unsigned char x);
void LCDInit();
void SendData(unsigned char Data);
void SPIWR(unsigned char Wdata,unsigned char RS);
void SendByteLCD(unsigned char WLCDData);
void Put(unsigned char *p);
void SendCMD(unsigned char CMD);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void) {
// disable watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
SYSCLK_Init(); // Initialize system clock to
// 24.576MHz
PORT_Init();
Timer2_Init (SYSCLK / 12 /T2_Freq); // Init Timer2 to generate
EA = 1;
LCDInit();
GB=0x80;
while (1)
{
if(GB<=0x84)
{
SendCMD(0x01);//清除顯示
SendCMD(GB);
SendData(0xb0);
SendData(0xae);
delay1MS(7);
SendCMD(GB+1);
SendData(0xca);
SendData(0xc7);
delay1MS(7);
SendCMD(GB+2);
SendData(0xca);
SendData(0xb2);
delay1MS(7);
SendCMD(GB+3);
SendData(0xc3);
SendData(0xb4);
SendCMD(GB+4);
GB++;
for(k=0;k<=80;k++)delay1MS(50000);
}
}
}
void SYSCLK_Init (void)
{
int i; // delay counter
P1MDIN = 0xFC; // P0.2 P0.3為模擬輸入
OSCXCN = 0x67; // start external oscillator with
// 24.576MHz crystal
for (i=0; i < 3000; i++) ; // XTLVLD blanking interval (>1ms)
// while (!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settle
CLKSEL = 0x01; // select external oscillator as SYSCLK
OSCICN = 0x00;
// source and enable missing clock
// detector
}
//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
//
// Configure the Crossbar and GPIO ports.
// P0.6 - LED (push-pull)
//
void PORT_Init (void)
{
//----------------------------------------------------------------
// CROSSBAR REGISTER CONFIGURATION
//
// NOTE: The crossbar register should be configured before any
// of the digital peripherals are enabled. The pinout of the
// device is dependent on the crossbar configuration so caution
// must be exercised when modifying the contents of the XBR0,
// XBR1 registers. For detailed information on
// Crossbar Decoder Configuration, refer to Application Note
// AN001, "Configuring the Port I/O Crossbar Decoder".
//----------------------------------------------------------------
// Configure the XBRn Registers
XBR0 = 0x01; // Crossbar Register 1
XBR1 = 0x40; // Crossbar Register 2
// Select Pin I/0
// NOTE: Some peripheral I/O pins can function as either inputs or
// outputs, depending on the configuration of the peripheral. By default,
// the configuration utility will configure these I/O pins as push-pull
// outputs.
// Port configuration (1 = Push Pull Output)
P0MDOUT = 0x0F; // Output configuration for P0
P1MDOUT = 0x00; // Output configuration for P1
P2MDOUT = 0x00; // Output configuration for P2
P0MDIN = 0xFF; // Input configuration for P0
P1MDIN = 0xF8; // Input configuration for P1
P2MDIN = 0xFF; // Input configuration for P1
P0SKIP = 0x00; // Port 0 Crossbar Skip Register
P1SKIP = 0x00; // Port 1 Crossbar Skip Register
P2SKIP = 0x00; // Port 1 Crossbar Skip Register
}
//-----------------------------------------------------------------------------
// Timer2_Init
//-----------------------------------------------------------------------------
//
// Configure Timer2 to 16-bit auto-reload and generate an interrupt at
// interval specified by <counts> using SYSCLK/48 as its time base.
//
void Timer2_Init (int counts)
{
TMR2CN = 0x00; // Stop Timer2; Clear TF2;
// use SYSCLK/12 as timebase
TMR2RL = -counts; // Init reload values
TMR2 = 0xffff; // set to reload immediately
ET2 = 1; // enable Timer2 interrupts
TR2 = 1; // start Timer2
}
//-----------------------------------------------------------------------------
// Interrupt Service Routines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Timer2_ISR
//-----------------------------------------------------------------------------
// 20ms
//
void Timer2_ISR (void) interrupt 5
{
static unsigned int T2times=0;
TF2H = 0; // clear Timer2 interrupt flag
T2times++;
//-----------------------------------
switch(T2times)
{
case 500:
T2times=0;
break;
}
}
void delay1MS(unsigned char x) //1ms
{
uchar k,j;
x=x<<1;
for(k=0;k<x;k++)
for(j=0;j<255;j++)_nop_();
}
void delay10US(unsigned char x)
{
unsigned char k;
x=x*10;
for(k=0;k<x;k++);
}
//=============================
void SendByteLCD(unsigned char WLCDData)
{
unsigned char i;
CS=1;
for(i=0;i<8;i++)
{
if((WLCDData<<i)&0x80)SID=1;
else SID=0;
SCLK=0;
SCLK=1;
}
}
void SPIWR(unsigned char Wdata,unsigned char CS)
{
SendByteLCD(0xf8+(CS<<1));
SendByteLCD(Wdata&0xf0);
SendByteLCD((Wdata<<4)&0xf0);
}
void SendCMD(unsigned char CMD)
{
CS=1;
SPIWR(CMD,0);
//delay10US(100);//8155來模擬串行通信,所以,加上8155的延時(shí),
delay1MS(7);
}
void SendData(unsigned char Data)
{
SPIWR(Data,1);
}
void LCDInit()
{
RST=0;
delay1MS(100);
RST=1;
SendCMD(0x01);//清除顯示
SendCMD(0x30);//功能設(shè)置,一次送8位數(shù)據(jù),基本指令集
//delay1MS(8);
SendCMD(0x0C);//0000,1100 整體顯示,游標(biāo)off,游標(biāo)位置off
// delay1MS(8);
SendCMD(0x01);//0000,0001 清DDRAM
// delay1MS(8);
SendCMD(0x02);//0000,0010 DDRAM地址歸位
// delay1MS(8);
SendCMD(0x80);//1000,0000 設(shè)定DDRAM 7位地址000,0000到地址計(jì)數(shù)器AC//
// delay1MS(8);
SendCMD(0x04);//點(diǎn)設(shè)定,顯示字符/光標(biāo)從左到右移位,DDRAM地址加 一//
//delay1MS(8);
//SendCMD(0x1f);//顯示設(shè)定,開顯示,顯示光標(biāo),當(dāng)前顯示位反白閃動(dòng)
delay1MS(8);
}
void Put(unsigned char *p)
{
unsigned char xx,yy;
for(yy=0;yy<32;yy++)
for(xx=0;xx<8;xx++)
{
SendCMD(0x80+yy); //y 按位
SendCMD(0x80+xx);//x 按16位
SendData(*p);
p++;
SendData(*p);
p++;
}
for(yy=0;yy<32;yy++)
for(xx=8;xx<16;xx++)
{
SendCMD(0x80+yy); //y 按位
SendCMD(0x80+xx);//x 按16位
SendData(*p);
p++;
SendData(*p);
p++;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -