|
樓主 |
發表於 2013-12-19 22:22:18
|
顯示全部樓層
LEGION大:
線路週六傳給你,感謝!! 我主要是練習使用 74HC595 推動 LCM !!
#include<reg52.h>
#include <intrins.h> //use nop
//#include <delay.h>
#define DataPort P2
//sbit Oser = P1^0;
//bit SER;
unsigned char DISP;
unsigned char code Seg [10]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09};
//-----------------------------------------
sbit SER = P1^2;// 74hc595 control
sbit Latch = P1^1;
sbit Clk = P1^0;
char displayword[16];
void Send595(unsigned char Dat) ;//output data to D/A converter through the serial mode
//------------------------------------------------
/*------------------------------------------------
delay us
------------------------------------------------*/
void DelayUs2x(unsigned char t)
{
while(--t);
}
/*------------------------------------------------
delay ms
------------------------------------------------*/
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
//*******************************************
void Send595(unsigned char Dat) //output data to D/A converter through the serial mode
{
unsigned char i;
for (i=0;i<8;i++)
{
Clk=0;
SER=Dat & 0x80;
Dat<<=1;
Clk=1;
}
Latch=0;
Latch=1;
}
//-------------------------- 595 send to LCM 命令-------------------
void LCD_Write_Com(char com) // 4bit mode {
{
unsigned char D596=0;
//Send595(0);//RS=0
D596= D596|0x08;// EN=1 rs=0
Send595(D596);//送到 74HC595
D596=com & 0xf8;//指令上4BIT放入D595中
Send595(D596);//增加rs單獨動作
D596=D596 & 0xf7;//EN=0
Send595(D596);//輸出
com<<=4;
Send595(0x80);// EN=1 rs=0
D596=com|0x08;//EN=1
Send595(D596);
D596=D596 & 0xf7;//EN=0
Send595(D596);// 輸出
}
void LCD_Write_Data(char Data) // 4bit mode
{
unsigned char D595=0;
Send595(0x02);//RS=1
Send595(0x0a);////EN=1送到 74HC595
D595=Data & 0xfa;//資料上4BIT放入D595中 RS=1
Send595(D595);//增加rs單獨動作
D595=D595 & 0xf7;//EN=0
Send595(D595);//輸出
Data<<=4;
Send595(0x0a);// rs=1 en=1
D595=Data|0x0a;//EN=1+data
Send595(D595);
D595=D595 & 0xf7;//EN=0
Send595(D595);// 輸出
}
/*------------------------------------------------
2M?�??
------------------------------------------------*/
void LCD_Clear(void)
{
LCD_Write_Com(0x01);
DelayMs(5);
}
/*------------------------------------------------
?à¤J|r2?|ê¨???
------------------------------------------------*/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
while (*s)
{
LCD_Write_Data( *s);
s ++;
}
}
/*------------------------------------------------
?à¤J|r2?¨???
------------------------------------------------*/
void LCD_Write_Char(unsigned char x,unsigned char y,unsigned char Data)
{
if (y == 0)
{
LCD_Write_Com(0x80 + x);
}
else
{
LCD_Write_Com(0xC0 + x);
}
LCD_Write_Data( Data);
}
/*------------------------------------------------
initial LCM
------------------------------------------------*/
void LCD_Init(void)
{
LCD_Write_Com(0x28); /*4bit mode*/
DelayMs(5);
LCD_Write_Com(0x28);
DelayMs(5);
LCD_Write_Com(0x28);
DelayMs(5);
LCD_Write_Com(0x28);
LCD_Write_Com(0x08); /*/*显示关闭*/*/
LCD_Write_Com(0x01); /*显示清屏*/
LCD_Write_Com(0x06); /*显示光标移动设置*/
DelayMs(5);
LCD_Write_Com(0x0C); /*显示开及光标设置*/
}
void main(void)
{
DelayMs(100);
LCD_Init();
LCD_Clear();
//DISP=0x0f;
while(1)
{
displayword[0]='a';
displayword[1]='n';
displayword[2]='t';
displayword[3]= 'l';
displayword[4]= 'u';
displayword[5]= 'M';
displayword[6]='s';
displayword[7]='2';
displayword[8]= '3';
displayword[9]= ' ';
displayword[10]='g';
displayword[11]= 'o';
displayword[12]= 'o';
displayword[13]='d';
displayword[14]='9';
displayword[15]='8';
LCD_Write_String(0,0, displayword);
DelayMs(250);
LCD_Write_String(0,1, "A");
DelayMs(250);
DelayMs(250);
LCD_Write_String(0,0, "1");
DelayMs(250);
LCD_Clear();
}
}
|
|