51单片机-1602液晶-滚动显示

51单片机-1602液晶-滚动显示


1. 视频

哔哩哔哩视频地址:

https://www.bilibili.com/video/av95003326

51单片机-1602液晶-滚动显示

2. 文件

文件下载地址:

https://download.csdn.net/download/weixin_43130546/12236854

3. 代码

main.c


#include 

sbit DULA = P2^6;
sbit WELA = P2^7;

extern bit flag1s;

extern void ConfigTimer0(void);
extern void InitLcd1602(void);
extern void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str, unsigned char len);

void main(void)
{
	unsigned char code str1[] = {"ABCDEFG       "};
	unsigned char code str2[] = {"       abcdefg"};
	unsigned char pdata str1buff[16 + sizeof(str1) + 16];
	unsigned char pdata str2buff[16 + sizeof(str2) + 16];
	unsigned char i;
	unsigned char index = 0;

	ConfigTimer0();
	InitLcd1602();

	P0 = 0x00;
	DULA = 1;
	DULA = 0;
	P0 = 0xFF;
	WELA = 1;
	WELA = 0;

	for(i=0; i<16; i++)
	{
		str1buff[i] = ' ';
		str2buff[i] = ' ';
	}
	for(i=16; i<(16+sizeof(str1)-1); i++)
	{
		str1buff[i] = str1[i-16];
		str2buff[i] = str2[i-16];
	}
	for(i=(16+sizeof(str1)-1); i<sizeof(str1buff); i++)
	{
		str1buff[i] = ' ';
		str2buff[i] = ' ';
	}

	while(1)
	{
		if(flag1s)
		{
			flag1s = 0;
			//P1 = 0x0F;

			LcdShowStr(0,0,str1buff+index,16);
			LcdShowStr(0,1,str2buff+index,16);

			index++;
			if(index>=(16+sizeof(str1)-1))
			{
				index = 0;
			}
		}
	}
}

LCD1602.c


#include 

#define LCD1602_DB P0
sbit LCD1602_RS = P3^5;
sbit LCD1602_RW = P3^6;
sbit LCD1602_E = P3^4;

void LcdWaitReady(void)
{
	unsigned char sta;
	LCD1602_DB = 0xFF;
	LCD1602_RS = 0;
	LCD1602_RW = 1;
	do
	{
		LCD1602_E = 1;
		sta = LCD1602_DB;
		LCD1602_E = 0;
	}
	while(sta & 0x80);
}

void LcdWriteCmd(unsigned char cmd)
{
	LcdWaitReady();
	LCD1602_RS = 0;
	LCD1602_RW = 0;
	LCD1602_DB = cmd;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdWriteDat(unsigned char dat)
{
	LcdWaitReady();
	LCD1602_RS = 1;
	LCD1602_RW = 0;
	LCD1602_DB = dat;
	LCD1602_E = 1;
	LCD1602_E = 0;
}

void LcdSetCursor(unsigned char x, unsigned char y)
{
	unsigned char addr;
	if(0==y)
	{
		addr = 0x00 + x;
	}
	else
	{
		addr = 0x40 + x;
	}
	LcdWriteCmd(addr | 0x80);
}

void LcdShowStr(unsigned char x, unsigned char y, unsigned char *str, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(*str++);
	}
}

void LcdAreaClear(unsigned char x, unsigned char y, unsigned char len)
{
	LcdSetCursor(x,y);
	while(len--)
	{
		LcdWriteDat(' ');
	}
}

void LcdFullClear(void)
{
	LcdWriteCmd(0x01);
}

void InitLcd1602(void)
{
	LCD1602_E = 0;
	LcdWriteCmd(0x38);
	LcdWriteCmd(0x0F);
	LcdWriteCmd(0x06);
	LcdWriteCmd(0x01);
}

interrupt.c


#include 

const unsigned char TIMER0MS = 1;
const unsigned long BAUD = 9600;

unsigned char T0RH, T0RL;
bit flag1s = 0;
unsigned char RxdByte;

void ConfigTimer0(void)
{
	unsigned long tmp;
	tmp = 11059200 / 12;
	tmp = tmp * TIMER0MS / 1000;
	tmp = 65536 - tmp;
	tmp += 0;

	T0RH = (unsigned char)(tmp>>8);
	T0RL = (unsigned char)tmp;

	TH0 = T0RH;
	TL0 = T0RL;
	TMOD &= 0xF0;
	TMOD |= 0x01;
	EA = 1;
	ET0 = 1;
	TF0 = 0;
	TR0 = 1;
}

void ConfigUART(void)
{
	TH1 = 256 - (11059200/12/32) / BAUD;
	TL1 = TH1;
	TMOD &= 0x0F;
	TMOD |= 0x20;
	SCON = 0x50;
	EA = 1;
	ET1 = 0;
	ES = 1;
	TF1 = 0;
	TR1 = 1;
}

void InterruptTimer0(void) interrupt 1
{
	static unsigned int tmr1s = 0;

	TH0 = T0RH;
	TL0 = T0RL;

	tmr1s++;
	if(tmr1s>=1000)
	{
		tmr1s = 0;
		flag1s = 1;
		//P1 = 0x0F;
	}
}

void InterruptUART(void) interrupt 4
{
	if(RI)
	{
		RI = 0;
		RxdByte = SBUF;
		SBUF = RxdByte;
	}
	if(TI)
	{
		TI = 0;
	}
}

你可能感兴趣的:(51单片机-1602液晶-滚动显示)