⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stc3310lcdtest.c

📁 两个C51测试3310的源程序其中一个移植朋友网站上的AVR例程。芯片用STC89LE58RD+
💻 C
字号:
//3310LCDTest.c 
#include <reg52.h>

#include <intrins.h>        /* use _nop_() function */

sbit sclk=P2^0;//时钟 
sbit sdin=P2^1; //数据 
sbit dc=P2^2;//1写数据,0写指令 
sbit sce=P2^3;//片选 
sbit res=P2^4;//复位,0复位 

#define uchar unsigned char 
#define uint unsigned int 
#include "ASC16X24.H"
//#include <iom48v.h>
//#include <macros.h>
//#include "lcd.h"
#include "bmp.h"
//#include "6x8.h"
//#include "chinese.h"





/*************************************/
//lcd.h头文件 
//Mega48/8port B pinout for LCD.

//#define LCD_DC0x01//PB0

//#define LCD_CE0x04//PB2

//#define SPI_MOSI0x08//PB3

//#define LCD_RST0x10//PB4

//#define SPI_CLK0x20//PB5

void LCD_init(void);

void LCD_clear(void);

void LCD_write_String(unsigned char X,unsigned char Y,char *s);

//void LCD_write_chi(unsigned char X, unsigned char Y,unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row);

void LCD_move_chi(unsigned char X, unsigned char Y, unsigned char T);

void LCD_write_char(unsigned char c);

void LCD_draw_map(unsigned char X,unsigned char Y,unsigned char *map,

unsigned char Pix_x,unsigned char Pix_y);

//void LCD_write_byte(unsigned char data, unsigned char dc);
void LCD_write_byte(uchar dd,bit a);

void delay_1us(void);

void delay_nus(unsigned int n);

void delay_1ms(void);

void delay_nms(unsigned int n);





/*******************************************/

void main(void)
{
//OSCCAL=0x99;// 8M系统内部时钟校准

//设置MCU的I/O口

//DDRB |= LCD_RST | LCD_DC | LCD_CE | SPI_MOSI | SPI_CLK| 0X02;

//SPSR |= BIT(SPI2X);// 设置SPI时钟倍速

//SPCR |= BIT(SPE)|BIT(MSTR);// 使能SPI接口,主机模式,4M时钟

LCD_init(); //初始化LCD模块 

LCD_write_String(1,0,"www.qlmcu.com");
LCD_write_String(0,1,"www.talkie8.com");
LCD_write_String(1,5,"www.527dz.com"); //显示英文字符串”www.527dz.com“

LCD_draw_map(0,2,AVR_bmp,40,24); //显示“AVR”位图 

LCD_draw_map(44,3,china_bmp,36,15); //显示”实验室“位图 

//PORTB |= 0x02; //LED亮

while(1)
{
//LCD_move_chi(0, 0, 25); //汉字移动显示 
//LCD_draw_map(44,3,china_bmp,36,15);
LCD_write_String(1,0,"www.qlmcu.com");
LCD_write_String(0,1,"www.talkie8.com");
}
}

//LCD3310驱动程序:文件名3310LCD.c

//#include <iom48v.h>

//#include <macros.h>

//#include "lcd.h"



/*---------------------------------------

LCD_init: 3310LCD初始化

编写日期:2004-8-10 

最后修改日期:2004-8-10 

----------------------------*/

void LCD_init(void)
{

/*PORTB &= ~LCD_RST;// 产生一个让LCD复位的低电平脉冲

delay_1us();

PORTB |= LCD_RST;

PORTB &= ~LCD_CE ;// 关闭LCD

delay_1us();

PORTB |= LCD_CE;// 使能LCD
*/
  res=0;res=0;res=0; 
  res=1;
  sce=1; 
  sdin=1;
  sclk=1;
delay_1us();

LCD_write_byte(0x21, 0);// 使用扩展命令设置LCD模式

LCD_write_byte(0xc8, 0);// 设置偏置电压

LCD_write_byte(0x06, 0);// 温度校正

LCD_write_byte(0x13, 0);// 1:48

LCD_write_byte(0x20, 0);// 使用基本命令

LCD_clear();// 清屏

LCD_write_byte(0x0c, 0);// 设定显示模式,正常显示

//PORTB &= ~LCD_CE ;// 关闭LCD

}

/*-----------------------------------------------------------------------

LCD_clear: LCD清屏函数

编写日期:2004-8-10 

最后修改日期:2004-8-10 

-----------------------------------------------------------------------*/

void LCD_clear(void)
{

unsigned int i;

LCD_write_byte(0x0c, 0);

LCD_write_byte(0x80, 0);

for (i=0; i<504; i++)

LCD_write_byte(0, 1);

}

/*-----------------------------------------------------------------------

LCD_set_XY: 设置LCD坐标函数

输入参数:X:0-83

Y:0-5

编写日期:2004-8-10 

最后修改日期:2004-8-10 

-----------------------------------------------------------------------*/

void LCD_set_XY(unsigned char X, unsigned char Y)
{

LCD_write_byte(0x40 | Y, 0);// column

LCD_write_byte(0x80 | X, 0);// row

}

/*-----------------------------------------------------------------------

LCD_write_char: 显示英文字符

输入参数:c:显示的字符;

编写日期:2004-8-10 

最后修改日期:2004-8-10 

-----------------------------------------------------------------------*/

void LCD_write_char(unsigned char c)
{

unsigned char line;

c -= 32;

for (line=0; line<6; line++)

LCD_write_byte(font6x8[c][line], 1);

}

/*-----------------------------------------------------------------------

LCD_write_char: 英文字符串显示函数

输入参数:*s:英文字符串指针;

X、Y: 显示字符串的位置

-----------------------------------------------------------------------*/

void LCD_write_String(unsigned char X,unsigned char Y,char *s)
{

LCD_set_XY(X,Y);

while (*s) 

{

LCD_write_char(*s);

s++;

}

}

/*-----------------------------------------------------------------------

LCD_write_chi: 在LCD上显示汉字

输入参数:X、Y:显示汉字的起始X、Y坐标;

ch_with :汉字点阵的宽度

num:显示汉字的个数;

line:汉字点阵数组中的起始行数

row:汉字显示的行间距

编写日期:2004-8-11 

最后修改日期:2004-8-12 

-----------------------------------------------------------------------*/
/*
void LCD_write_chi(unsigned char X, unsigned char Y,unsigned char ch_with,unsigned char num,unsigned char line,unsigned char row)
{
unsigned char i,n;

LCD_set_XY(X,Y);//设置初始位置

for (i=0;i<num;)

{

for (n=0; n<ch_with*2; n++)//写一个汉字

{ 

if (n==ch_with)//写汉字的下半部分

{

if (i==0) LCD_set_XY(X,Y+1);

else

LCD_set_XY((X+(ch_with+row)*i),Y+1);

}

LCD_write_byte(china_char[line+i][n],1);

}

i++;

LCD_set_XY((X+(ch_with+row)*i),Y);

}

}*/

/*-----------------------------------------------------------------------

LCD_write_chi: 汉字移动

输入参数:X、Y:显示汉字的起始X、Y坐标;

T:移动速度;

编写日期:2004-8-13 

最后修改日期:2004-8-13 

-----------------------------------------------------------------------*/
/*
void LCD_move_chi(unsigned char X,unsigned char Y,unsigned char T)
{
unsigned char i,n,j=0;
unsigned char buffer_h[84]={0};
unsigned char buffer_l[84]={0};
for (i=0; i<156; i++)
{
buffer_h[83] = china_char[i/12][j];
buffer_l[83] = china_char[i/12][j+12];
j++;
if (j==12) j=0;
for (n=0; n<83; n++)
{ 
buffer_h[n]=buffer_h[n+1];
buffer_l[n]=buffer_l[n+1];
} 
LCD_set_XY(X,Y);
for (n=0; n<83; n++)
{ 
LCD_write_byte(buffer_h[n],1);
} 
LCD_set_XY(X,Y+1); 
for (n=0; n<83; n++)
{
LCD_write_byte(buffer_l[n],1);
} 
delay_nms(T);
}
}  
/*-----------------------------------------------------------------------

LCD_draw_map: 位图绘制函数

输入参数:X、Y:位图绘制的起始X、Y坐标;

*map:位图点阵数据;

Pix_x:位图像素(长)

Pix_y:位图像素(宽)

编写日期:2004-8-13

最后修改日期:2004-8-13 

-----------------------------------------------------------------------*/

void LCD_draw_map(unsigned char X,unsigned char Y,unsigned char *map,

unsigned char Pix_x,unsigned char Pix_y)
{

unsigned int i,n;

unsigned char row;

if (Pix_y%8==0) row=Pix_y/8;//计算位图所占行数

else

row=Pix_y/8+1;

for (n=0;n<row;n++)

{

LCD_set_XY(X,Y);

for(i=0; i<Pix_x; i++)

{

LCD_write_byte(map[i+n*Pix_x], 1);

}

Y++;//换行

}

}

/*-----------------------------------------------------------------------

LCD_write_byte: 使用SPI接口写数据到LCD

输入参数:data:写入的数据;

command :写数据/命令选择;

编写日期:2004-8-10 

最后修改日期:2004-8-13 

-----------------------------------------------------------------------*/

//---------------------超快,但多了135字-----------------------------
unsigned char mask_table[8]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

void LCD_write_byte(uchar dd,bit a)//写一字节
{
char i; 
   sce=0;
   dc=a;
   for(i=0;i<8;i++)
    {  
     if(dd&mask_table[i])
     sdin=1;
     else
	 sdin=0;
     sclk=0;     
     sclk=1;    
     }
   dc=1;
  sce=1;
sdin=1;
}

/*void LCD_write_byte(unsigned char dd, unsigned char command)

{

PORTB &= ~LCD_CE ;// 使能LCD

if (command == 0)

PORTB &= ~LCD_DC ;// 传送命令

else

PORTB |= LCD_DC ;// 传送数据

SPDR = data;// 传送数据到SPI寄存器

while ((SPSR & 0x80) == 0);// 等待数据传送完毕

PORTB |= LCD_CE ;// 关闭LCD
8/
}

延时程序:文件名delay.c 
/*-----------------------------------------------------------------------

延时函数

系统时钟:8M

-----------------------------------------------------------------------*/

void delay_1us(void)//1us延时函数
{

//asm("nop");
_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 + -