?? 7920.c
字號:
//本程序是7920的LM3037的液晶驅動程序
//漢字2x9
//ATMEGA16, 內部晶振8M
//使用串行傳輸
//本程序測試通過
//綠葉子整理 -- 2005.08.30
#include <iom16v.h>
#include <macros.h>
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_DATA 0x0ff //portA out
#define LCD_CONTROL_PORT PORTB
#define LCD_CONTROL_DDR DDRB
#define RS 0 //RS(CS) 可直接接VCC -- 替代
#define RW 1 //RW(SID)
#define E 2 //E(sclk)
#define PSB 3 //可以直接接地 -- 替代
#define _RES 4 //可以去掉
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void delay_1us(void);
void delay_1ms(void);
void delay_nus (unsigned int n);
void delay_nms (unsigned int n);
#define SETBIT(x,y) (x|=(1<<y)) //set bit y in byte x
#define CLRBIT(x,y) (x&=(~(1<<y))) //clear bit y in byte x
#define CHKBIT(x,y) (x&(1<<y)) //check bit y in byte x
#define uchar unsigned char // 0~255
#define uint unsigned int // 0~65535
void CheckState();
void initLCDM(void);
void Send(unsigned char senddata);
void SdCmd(unsigned char scmd);
void SdData(unsigned char DData);
void WriteTextScreen(unsigned char *TxtData);
void WriteTextScreen2(unsigned char *pstr);
void WriteGraphicScreen(unsigned char *GDData);
void DispSetCursor(unsigned char LineNum, unsigned char ColumnNum);
unsigned char TextLogo[]={"液晶顯示模塊(LCDM)"};
void initLCDM(void)
{
delay_nms(100);
//端口初始化
//LCD_DATA_DDR=0xFF;
LCD_CONTROL_DDR=0xFF;
CLRBIT(LCD_CONTROL_PORT,E);
CLRBIT(LCD_CONTROL_PORT,RW);
CLRBIT(LCD_CONTROL_PORT,RS);
CLRBIT(LCD_CONTROL_PORT,PSB);
//LCD_DATA_PORT|=0x0;
//delay_nms(1500);
LCD_DATA_PORT|=0xFF;
//SETBIT(LCD_CONTROL_PORT,_RES);
CLRBIT(LCD_CONTROL_PORT,_RES);
delay_nms(1);
SETBIT(LCD_CONTROL_PORT,_RES);
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x0C); // display on
SdCmd(0x06); // cursor right shift
SdCmd(0x01); // cursor right shift
}
/*-----------------------------------------------------
狀態檢查函數,判斷是否處于忙狀態
-------------------------------------------------------*/
void CheckState()
{
unsigned char dat;
CLRBIT(LCD_CONTROL_PORT,RS); //RS=0
SETBIT(LCD_CONTROL_PORT,RW); //RW=1
LCD_DATA_DDR=0x00; // portA as input
do
{
SETBIT(LCD_CONTROL_PORT,E);
NOP();
CLRBIT(LCD_CONTROL_PORT,E);
}
while (LCD_DATA_PIN&0x80);
}
void Send(unsigned char senddata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((senddata)&0x80)
{
//D_OUT=1 ;
SETBIT(LCD_CONTROL_PORT,RW);
}
else
{
//D_OUT=0;
CLRBIT(LCD_CONTROL_PORT,RW);
}
//SCK=1;
SETBIT(LCD_CONTROL_PORT,E);
NOP();
//SCK=0;
CLRBIT(LCD_CONTROL_PORT,E);
senddata<<=1;
}
}
void SdCmd(unsigned char scmd) //send command
{
//ST7920CS=1;
SETBIT(LCD_CONTROL_PORT,RS);
Send(0xf8);
Send(scmd&0xf0);
Send(scmd<<4);
//ST7920CS=0;
SETBIT(LCD_CONTROL_PORT,RS);
delay_nus(20);
}
void SdData(unsigned char DData)
{
//ST7920CS=1;
SETBIT(LCD_CONTROL_PORT,RS);
Send(0xfa);
Send(DData&0xf0);
Send(DData<<4);
//ST7920CS=0;
SETBIT(LCD_CONTROL_PORT,RS);
delay_nus(20);
}
void main(void)
{
CLI(); // disable interrupts
initLCDM();
while(1)
{
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x01); // clr text screen
delay_nms(100);
WriteTextScreen2(TextLogo);
delay_nms(1500);
WriteTextScreen2("世界你好12345678901234567890");
delay_nms(1500);
}
}
void DispSetCursor(unsigned char LineNum, unsigned char ColumnNum)
{
unsigned char i=0x00;
switch(LineNum&0x0f) //確定行號
{
case 0x00:
i=0x80;
break;
case 0x01:
i=0x90;
break;
case 0x02:
i=0x88;
break;
case 0x03:
i=0x98;
break;
default :
break;
}
i = (ColumnNum&0x0f)|i; //確定列號
SdCmd(i);
}
void WriteTextScreen2(unsigned char *pstr)
{
unsigned char i;
unsigned char j;
SdCmd(0x34); // 8bit I/F, basic command
SdCmd(0x30); // 8bit I/F, basic command, graphic off
for(i=0;i<36;i++) //清空屏幕
{
if (i%18==0) //判斷是否換行
{
DispSetCursor(i/18,0); //如換行, 則光標移動到行首
}
SdData(' '); //
}
j=0;
//while (*pstr)
while (*pstr && j<36)
{
if (j%18==0) //判斷是否換行
{
DispSetCursor(j/18,0); //如換行, 則光標移動到行首
}
//避免最后一格寫半個漢字, 把漢字寫到下一行
if (((j+1)%18==0) && *pstr>127 && *(pstr-1)<128)
{
SdData(' '); //
j++;
}
else
{
SdData(*pstr++);
j++;
}
//SdData(*pstr++);
//j++;
}
}
/*-----------------------------------------------------------------------
延時函數
系統時鐘:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延時函數
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延時函數
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延時函數
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -