AVR单片机_实验6:3310LCD液晶显示

曾巧文 发布于:2012-5-15 22:53 分类:AVR单片机 标签: 实验 单片机 AVR

1  实验目的

通过实验,掌握3310LCD 的用法。

2  实验内容

(1)      完成液晶的初始化及功能测试;

(2)      实现英文字符的液晶显示;

3  实验预习要求

仔细阅读ATmega16单片机的数据手册中SPI接口章节;仔细越多PCD8544数据手册,了解3310液晶的命令和显示RAM的内部结构;

4        实验步骤

1、启动ICCAVR,新建工程文件“LCD.PRJ”,新建LCD3310.c文件,并将LCD3310.c文件添加到Adkey工程中,并设置project->option->target 下的device configuration 选择ATMega16

2、硬件电路如图1所示,利用Application Builder产生SPI的初始化代码,步骤如下:

1)首先选择CPUM16。时钟选择7.3728M

2)初始化GPIOPA4PA5PA6PB4PB5PB7工作在输出模式。

3)初始化SPI接口,SPI工作在主模式、时钟选4分频,如图3所示:

AVR单片机_实验6:3310LCD液晶显示AVR单片机_实验6:3310LCD液晶显示

1 3310LCD接口电路                          2 SPI接口配置界面

3参照实例代码完成液晶模块的初始化,字符、数字显示功能。

3、在LCD3310.c文件下添加相应代码,实现在3310液晶模块上显示字符、数字和汉字。选择【Project->Rebuild All】编译工程,通过PROGISP程序下载程序到单片机,观察实验现象。

 AVR单片机_实验6:3310LCD液晶显示

1 实验目的

通过实验,掌握如何在3310液晶的使用方法,掌握英文字符以及字符串的显示;

2 实验内容

1)完成3310LCD液晶的初始化;

2)在液晶上显示数字、英文字母及英文字符串

3、实验问题


1、写出SPI总线的初始化函数(包括SPI总线相关IO口的初始化),并通过SPI口完成对3310LCD的初始化,清屏、填充功能;

 

#include "3310LCD.h"
#include <iom16v.h>

#define LCD_RST (1 << 5)
#define LCD_CE  (1 << 4)
#define LCD_DC  (1 << 6)

/*-----------------------------------------------------------------------
LCD_init          : 3310LCD初始化

-----------------------------------------------------------------------*/
void LCD_init(void)
  {
    PORTA &= ~LCD_RST;          // 产生一个让LCD复位的低电平脉冲
    delay10us();
    PORTA |= LCD_RST;
	
    PORTA |= LCD_CE;            // 关闭LCD
    delay10us();
    PORTA &= ~LCD_CE ;			// 使能LCD
    delay10us();

    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);	// 设定显示模式,正常显示
        
    PORTA |= LCD_CE;          // 关闭LCD PORTA=1
  }
  
  
/*-----------------------------------------------------------------------
LCD_clear          : 3310LCD清屏

-----------------------------------------------------------------------*/  
void LCD_clear()
  {
  int i;
    LCD_set_XY(0x00, 0x00);
	for (i = 0; i < 504;i++)
	{
	    LCD_write_byte(0x00, 1);
	}
  }
/*-----------------------------------------------------------------------
LCD_full          : 3310LCD填充

-----------------------------------------------------------------------*/  
void LCD_full()
  {
    int i;
    LCD_set_XY(0, 0);
	for (i = 0; i < 504;i++)
	{
	    LCD_write_byte(0xff, 1);
	}
  }
  
void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x70;
 PORTB = 0x00;
 DDRB  = 0xB0;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//SPI initialize
// clock rate: 1000000hz
void spi_init(void)
{
 SPCR = 0x50; //setup SPId
 SPSR = 0x00; //setup SPI
}

2、分析函数LCD_write_char()显示原理。以LCD_write_char()为基础设计英文字符串显示函数LCD_write_english_string(unsigned char X,unsigned char Y,char *s),其中X为在液晶中的X轴坐标,Y为在Y轴坐标,s为字符串指针,实现从(X,Y)位置显示字符串s的内容,注:暂时不考虑换行的情况。

 

/*-----------------------------------------------------------------------
LCD_set_XY        : 设置LCD坐标函数

输入参数:X       :0-83
          Y       :0-5

-----------------------------------------------------------------------*/
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_byte    : 使用SPI接口写数据到LCD

输入参数:data    :写入的数据;
          command :写数据/命令选择;

-----------------------------------------------------------------------*/
void LCD_write_byte(unsigned char data, unsigned char command)
  {
    PORTA &= ~LCD_CE ;		        // 使能LCD 低电平使能
    if (command == 0)
      PORTA &= ~LCD_DC ;	        // 传送命令
    else
      PORTA |= LCD_DC ;		        // 传送数据

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

    while ((SPSR & 0x80) == 0);         // 等待数据传送完毕
	
    PORTA |= LCD_CE ;			// 关闭LCD
  }

/*-----------------------------------------------------------------------
LCD_write_char         : 填充一个字符

-----------------------------------------------------------------------*/  
  
void LCD_write_char(unsigned char data)
  {
    unsigned char line;
	data -= 32;
	for(line = 0; line < 6; line++)
	{
	  LCD_write_byte(font6x8[data][line],1);
	}      
  }
/*-----------------------------------------------------------------------
LCD_write_english_string         : 填充多个字符 显示英文

-----------------------------------------------------------------------*/  
  
void LCD_write_english_string(unsigned char X, unsigned char Y, char *s)
  {
    LCD_set_XY(X, Y);
	while(*s != 0)
	{
	  LCD_write_char(*s);
	  s++;
	}
	  
  }

3 运用LCD_write_english_string()函数实现3310液晶上的英文字符的显示,效果如下:第一行空白,第二行显示:“3310LCD test!”,第四行显示:“1 3310LCD Init”,第五行显示:“2 3310 write”,第六行显示:“3 3310 display”分析该函数的显示原理;

 //main.c

 

 //ICC-AVR application builder : 2012-5-12 19:51:03
// Target : M16
// Crystal: 4.0000Mhz

#include <iom16v.h>
#include <macros.h>

#include "3310LCD.h"

void port_init(void)
{
 PORTA = 0x00;
 DDRA  = 0x70;
 PORTB = 0x00;
 DDRB  = 0xB0;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//SPI initialize
// clock rate: 1000000hz
void spi_init(void)
{
 SPCR = 0x50; //setup SPId
 SPSR = 0x00; //setup SPI
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();
 spi_init();
 LCD_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

int main()
  {
    init_devices();
	while(1)
	{
	  LCD_write_english_string(1, 0," hello world");
	  LCD_write_english_string(1, 1,"3310LCD test!");
	  LCD_write_english_string(0, 2," choovin.com");
	  LCD_write_english_string(1, 3,"1 3310LCD Int");
	  LCD_write_english_string(1, 4,"2 3310 write");
	  LCD_write_english_string(0, 5,"3 3310 display");
	}  
	
	return 0;  
  }
 
 
 //3310LCD.c
 
 #include "3310LCD.h"
#include <iom16v.h>

#define LCD_RST (1 << 5)
#define LCD_CE  (1 << 4)
#define LCD_DC  (1 << 6)

/*-----------------------------------------------------------------------
LCD_init          : 3310LCD初始化

-----------------------------------------------------------------------*/
void LCD_init(void)
  {
    PORTA &= ~LCD_RST;          // 产生一个让LCD复位的低电平脉冲
    delay10us();
    PORTA |= LCD_RST;
	
    PORTA |= LCD_CE;            // 关闭LCD
    delay10us();
    PORTA &= ~LCD_CE ;			// 使能LCD
    delay10us();

    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);	// 设定显示模式,正常显示
        
    PORTA |= LCD_CE;          // 关闭LCD PORTA=1
  }
  
  
/*-----------------------------------------------------------------------
LCD_clear          : 3310LCD清屏

-----------------------------------------------------------------------*/  
void LCD_clear()
  {
  int i;
    LCD_set_XY(0x00, 0x00);
	for (i = 0; i < 504;i++)
	{
	    LCD_write_byte(0x00, 1);
	}
  }
/*-----------------------------------------------------------------------
LCD_full          : 3310LCD填充

-----------------------------------------------------------------------*/  
void LCD_full()
  {
    int i;
    LCD_set_XY(0, 0);
	for (i = 0; i < 504;i++)
	{
	    LCD_write_byte(0xff, 1);
	}
  }
  
/*-----------------------------------------------------------------------
LCD_set_XY        : 设置LCD坐标函数

输入参数:X       :0-83
          Y       :0-5

-----------------------------------------------------------------------*/
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_byte    : 使用SPI接口写数据到LCD

输入参数:data    :写入的数据;
          command :写数据/命令选择;

-----------------------------------------------------------------------*/
void LCD_write_byte(unsigned char data, unsigned char command)
  {
    PORTA &= ~LCD_CE ;		        // 使能LCD 低电平使能
    if (command == 0)
      PORTA &= ~LCD_DC ;	        // 传送命令
    else
      PORTA |= LCD_DC ;		        // 传送数据

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

    while ((SPSR & 0x80) == 0);         // 等待数据传送完毕
	
    PORTA |= LCD_CE ;			// 关闭LCD
  }

/*-----------------------------------------------------------------------
LCD_write_char         : 填充一个字符

-----------------------------------------------------------------------*/  
  
void LCD_write_char(unsigned char data)
  {
    unsigned char line;
	data -= 32;
	for(line = 0; line < 6; line++)
	{
	  LCD_write_byte(font6x8[data][line],1);
	}      
  }
/*-----------------------------------------------------------------------
LCD_write_english_string         : 填充多个字符 显示英文

-----------------------------------------------------------------------*/  
  
void LCD_write_english_string(unsigned char X, unsigned char Y, char *s)
  {
    LCD_set_XY(X, Y);
	while(*s != 0)
	{
	  LCD_write_char(*s);
	  s++;
	}
	  
  }


/*-----------------------------------------------------------------------
delay10us()         : 延时函数

-----------------------------------------------------------------------*/  
  
void delay10us()
  {
    unsigned int i;
	for(i = 0; i < 3000; i++) {}   
  }  

 //english6x8Pixel.c
const unsigned char font6x8[][6] =
{
    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   // sp
    { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // !
    { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // "
    { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // #
    { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $
    { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // %
    { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // &
    { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   // '
    { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // (
    { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // )
    { 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 },   // *
    { 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 },   // +
    { 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 },   // ,
    { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // -
    { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // .
    { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // /
    { 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E },   // 0
    { 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 },   // 1
    { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2
    { 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 },   // 3
    { 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 },   // 4
    { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5
    { 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 },   // 6
    { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7
    { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8
    { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E },   // 9
    { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // :
    { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ;
    { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // <
    { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // =
    { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // >
    { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ?
    { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E },   // @
    { 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C },   // A
    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 },   // B
    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 },   // C
    { 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C },   // D
    { 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 },   // E
    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 },   // F
    { 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A },   // G
    { 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F },   // H
    { 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 },   // I
    { 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 },   // J
    { 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 },   // K
    { 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 },   // L
    { 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F },   // M
    { 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F },   // N
    { 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E },   // O
    { 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 },   // P
    { 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E },   // Q
    { 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 },   // R
    { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // S
    { 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 },   // T
    { 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F },   // U
    { 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F },   // V
    { 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F },   // W
    { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // X
    { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // Y
    { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // Z
    { 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 },   // [
    { 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 },   // 55
    { 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 },   // ]
    { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^
    { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _
    { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   // '
    { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a
    { 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 },   // b
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c
    { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F },   // d
    { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e
    { 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 },   // f
    { 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C },   // g
    { 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 },   // h
    { 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 },   // i
    { 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 },   // j
    { 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 },   // k
    { 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 },   // l
    { 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 },   // m
    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 },   // n
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o
    { 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 },   // p
    { 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC },   // q
    { 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 },   // r
    { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s
    { 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 },   // t
    { 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C },   // u
    { 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C },   // v
    { 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C },   // w
    { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x
    { 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C },   // y
    { 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 },   // z
    { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }    // horiz lines
};

//3310LCD.h

void LCD_init(void);
void LCD_clear(void);
void LCD_set_XY(unsigned char X, unsigned char Y);
void LCD_write_byte(unsigned char data, unsigned char command);
void LCD_write_char(unsigned char c);
void delay10us(void);
void LCD_full(void);
extern const unsigned char font6x8[][6];

版权所有:《曾巧文博客-关注互联网IT技术,记录生活点滴》 => 《AVR单片机_实验6:3310LCD液晶显示
本文地址://qiaowen.net/post-1228.html
除非注明,文章均为 《曾巧文博客-关注互联网IT技术,记录生活点滴》 原创,欢迎转载!转载请注明本文地址,谢谢。

有 7688 人浏览,获得评论 2 条

评论:

郑州婚纱摄影
2012-05-17 09:06
对单机片不太懂啊,学校时候同学做了个单机片小车很好玩啊
曾巧文
2012-05-17 13:16
@郑州婚纱摄影:嗯,每年的智能小车比赛,很多都有采用mega系列的单片机。

发表评论:

Powered by emlog 粤ICP备12040901号

>>本作品采用-知识共享署名-非商业-禁止演绎-协议-进行许可 |站点地图 | | | | 开放分类目录 |