TM320*240的MSP430驱动
TM320*240,控制器为RA8806.
1、有DDRAM1,DDRAM2,具有双层显示功能。
2、接口和操作类似LCD12864。
3、需要对控制器配置很多寄存器的值。基本操作方法是写寄存器地址,写寄存器内容,写显示数据,读显示数据,判忙。
4、写文字字符步骤:a.设置显示位置.通过设置寄存器0x60[seg],0x70[com]的值实现的。b.内存写入指令。c.写数据。
.c文件
//=================================版本信息==================================// //Ver:2011082301 //Author:XZQ //Note:显示方式实例:示波器。在DDRAM2显示背景信息,网格,坐标等;在DDRAM1显示波形、数据 //===========================================================================// #include "TM320X240_Driver.h" /******************************************* 函数名称:LCD_CmdWrite 功 能:选择寄存器并向寄存器写入数据 参 数:unsigned char cmdaddr,unsigned char wrdata cmdaddr要写入寄存器地址 wrdata要写入的内容 返回值 :无 ********************************************/ void LCD_CmdWrite(unsigned char cmdaddr,unsigned char wrdata)//8080 { CtrlDIR |=0xff; DataDIR |=0xff; CtrlOUT &=~lcd_cs1; //cs1=0; CtrlOUT |=lcd_rd; //rd=1; CtrlOUT |=lcd_rs; //rs=1; DataOUT =cmdaddr; delay_us(1); CtrlOUT &=~lcd_wr; //wr=0; delay_us(1); CtrlOUT |=lcd_wr; //wr=1; CtrlOUT |=lcd_cs1; //cs1=1; CtrlOUT &=~lcd_cs1; //cs1=0; CtrlOUT |=lcd_rd; //rd=1; CtrlOUT &=~lcd_rs; //rs=0; delay_us(1); DataOUT=wrdata; delay_us(1); CtrlOUT &=~lcd_wr; //wr=0; delay_us(1); CtrlOUT |=lcd_wr; //wr=1; CtrlOUT |=lcd_cs1; //cs1=1; delay_us(1); } /******************************************* 函数名称:LCD_DataWrite 功 能:向液晶中写显示数据 参 数:unsigned char wrdata 返回值 :无 ********************************************/ void LCD_DataWrite(unsigned char wrdata) { CtrlDIR |=0xff; DataDIR |=0xff; CtrlOUT &=~lcd_cs1; //cs1=0; CtrlOUT |=lcd_rd; //rd=1; CtrlOUT &=~lcd_rs; //rs=0; DataOUT = wrdata; delay_us(1); CtrlOUT &=~lcd_wr; //wr=0; delay_us(1); CtrlOUT |=lcd_wr; //wr=1; CtrlOUT |=lcd_cs1; //cs1=1; } /******************************************* 函数名称:LCD_RegWrite 功 能:选择寄存器 参 数:unsigned char reg。寄存器地址 返回值 :无 ********************************************/ void LCD_RegWrite(unsigned char reg) { CtrlDIR |=0xff; DataDIR |=0xff; CtrlOUT &=~lcd_cs1; //cs1=0; CtrlOUT |=lcd_rd; //rd=1; CtrlOUT |=lcd_rs; //rs=1; DataOUT=reg; delay_us(1); CtrlOUT &=~lcd_wr; //wr=0; delay_us(1); CtrlOUT |=lcd_wr; //wr=1; CtrlOUT |=lcd_cs1; //cs1=1; delay_us(1); } /******************************************* 函数名称:LCD_DataRead 功 能:读取显示内容数据 参 数:无 返回值 :unsigned char data状态值 ********************************************/ unsigned char LCD_DataRead(void) { unsigned char data; LCD_ChkBusy(); DataDIR|=0xFF; //设置为输出方向 DataOUT=0xff; //lcd_data = 0xff DataDIR&=~0xFF; //设置为输入方向 CtrlOUT|=lcd_wr; //lcd_wr = 1 bWRZ = 1; CtrlOUT&=~lcd_cs1; //lcd_cs1 =0,chINT enable bCSZ = 0; CtrlOUT&=~lcd_rs; //lcd_rs = 0//数据选择 bA0 = 0; CtrlOUT&=~lcd_rd; //lcd_rd = 0 bRDZ = 0; data = DataIN; CtrlOUT|=lcd_rd; //lcd_rd = 1 bRDZ = 1; CtrlOUT|=lcd_cs1; //lcd_cs1 =1,chINT disable bCSZ = 1; CtrlOUT|=lcd_rs; //lcd_rs = 1 DataDIR|=0xFF; //设置为输出方向 DataOUT=0xff; //lcd_data = 0xff return(data); } /******************************************* 函数名称:lcd_data_read 功 能:从DDRAM读取数据 参 数:无 返回值 :unsigned char data状态值 ********************************************/ unsigned char lcd_data_read(void) //从DDRAM读取数据 { unsigned char data; LCD_RegWrite(0XB1); data=LCD_DataRead(); return(data); } /******************************************* 函数名称:LCD_StatusRead 功 能:读取状态数据 参 数:无 返回值 :unsigned char data状态值 ********************************************/ unsigned char LCD_StatusRead(void) { unsigned char data; DataDIR|=0xFF; //设置为输出方向 DataOUT=0xff; //lcd_data = 0xff DataDIR&=~0xFF; //设置为输入方向 CtrlOUT|=lcd_wr; //lcd_wr = 1 bWRZ = 1; CtrlOUT&=~lcd_cs1; //lcd_cs1 =0,chINT enable bCSZ = 0; CtrlOUT|=lcd_rs; //lcd_rs = 1//命令选择 bA0 = 1; CtrlOUT&=~lcd_rd; //lcd_rd = 0 bRDZ = 0; data = DataIN; CtrlOUT|=lcd_rd; //lcd_rd = 1 bRDZ = 1; CtrlOUT|=lcd_cs1; //lcd_cs1 =1,chINT disable bCSZ = 1; CtrlOUT|=lcd_rs; //lcd_rs = 1 DataDIR|=0xFF; //设置为输出方向 DataOUT=0xff; //lcd_data = 0xff return(data); } /******************************************* 函数名称:LCD_ChkBusy 功 能:查忙 参 数:无 返回值 :无 ********************************************/ void LCD_ChkBusy(void) { uchar temp; do { temp = LCD_StatusRead(); }while((temp & 0xc0)); } /******************************************* 函数名称:LCD_Rst 功 能:TM320X240复位操作 参 数:无 返回值 :无 ********************************************/ void LCD_Rst() { CtrlDIR |=0xff; CtrlOUT &=~lcd_cs1; //cs1=0; CtrlOUT |=lcd_rd; //rd = 1; CtrlOUT &=~lcd_rst; //rst=0; delay_ms(300);//300ms CtrlOUT |=lcd_rst; //rst=1; delay_ms(100);//100ms } /******************************************* 函数名称:lcd_inital 功 能:TM320X240初始化操作 参 数:无 返回值 :无 ********************************************/ void lcd_inital() { LCD_CmdWrite(WLCR,0x00); //[00H] , Default --> 0x00 绘图模式,屏幕显示关闭 LCD_CmdWrite(MISC,0x08); //[01H] , Default --> 0x00 扫描顺序 seg 0-319 com 0-239 LCD_CmdWrite(ADSR,0x00); //[03H] , Default --> 0x00 关闭卷动功能 LCD_CmdWrite(INTR,0x00); //[0FH] , Default --> 0x10 开触摸中断.0x00 禁止各类型中断 LCD_CmdWrite(WCCR,0x00); //[10H] , Default --> 0x00 写入DDRAM时游标自动加1,关游标显示,关游标闪烁 LCD_CmdWrite(CHWI,0x00); //[11H] , Default --> 0x00 设置游标大小 LCD_CmdWrite(MAMR,0x33); //[12H] , Default --> 0x11 双图层显示,同时存取DDRAM1/DDRAM2,两图层关系为'OR' LCD_CmdWrite(AWRR,0x27); //[20H] , Default --> 0x27 工作窗口边界 LCD_CmdWrite(DWWR,0x27); //[21H] , Default --> 0x27 工作窗口边界 LCD_CmdWrite(AWBR,0xEF); //[30H] , Default --> 0xEF 工作窗口边界 LCD_CmdWrite(DWHR,0xEF); //[31H] , Default --> 0xEF 工作窗口边界 LCD_CmdWrite(AWLR,0x00); //[40H] , Default --> 0x00 工作窗口边界 LCD_CmdWrite(DWLR,0x00); //[41H] , Default --> 0x00 工作窗口边界 LCD_CmdWrite(AWTR,0x00); //[50H] , Default --> 0x00 工作窗口边界 LCD_CmdWrite(DWTR,0x00); //[51H] , Default --> 0x00 工作窗口边界 LCD_CmdWrite(CURX,0x00); //[60H] , Default --> 0x00 设置segment位置 范围[0-39] LCD_CmdWrite(BGSG,0x00); //[61H] , Default --> 0x00 设置卷动模式seg起点 LCD_CmdWrite(EDSG,0x00); //[62H] , Default --> 0x00 设置卷动模式seg起点 LCD_CmdWrite(CURY,0x00); //[70H] , Default --> 0x00 设置common位置 范围[0-239] LCD_CmdWrite(BGCM,0x00); //[71H] , Default --> 0x00 设置卷动模式com起点 LCD_CmdWrite(EDCM,0x00); //[72H] , Default --> 0x00 设置卷动模式com起点 LCD_CmdWrite(BTMR,0x00); //[80H] , Default --> 0x00 设置光标闪烁和卷动时间 LCD_CmdWrite(ITCR,0x27); //[90H] , Default --> 0x00 设置空闲时间 LCD_CmdWrite(KSCR1,0x00); //[A0H] , Default --> 0x00 设置键盘功能 LCD_CmdWrite(KSCR2,0x00); //[A1H] , Default --> 0x00 设置键盘功能 LCD_CmdWrite(KSDR0,0x00); //[A2H] , Default --> 0x00 设置键盘功能 LCD_CmdWrite(KSDR1,0x00); //[A3H] , Default --> 0x00 设置键盘功能 LCD_CmdWrite(KSDR2,0x00); //[A3H] , Default --> 0x00 设置键盘功能 LCD_CmdWrite(PCR,0x40); //[D0H] , Default --> 0x00 PWM控制 LCD_CmdWrite(PDCR,0x00); //[D1H] , Default --> 0x00 PWM控制 LCD_CmdWrite(PNTR,0x00); //[E0H] , Default --> 0x00 要写入DDRAM内的数据 LCD_CmdWrite(FNCR,0x00); //[F0H] , Default --> 0x00 ASCII表选择等文字显示控制 LCD_CmdWrite(FVHT,0x00); //[F1H] , Default --> 0x00 设置字符大小为原始大小 } /******************************************* 函数名称:DrawPicture 功 能:TM320X240显示图片 参 数:unsigned char const *m。图片数据首地址 返回值 :无 说 明:取模方式 横向取模、字节正序。数据写入DDRAM2 ********************************************/ void DrawPicture(unsigned char const *m) { unsigned int y=0,x=0,c=0; //LCD_CmdWrite(0xf0,0xa8);//ISO 8859模式,开启内存清除功能,将PNTR数据填满工作窗口 LCD_CmdWrite(FNCR,0xa8);//ISO 8859模式,开启内存清除功能,将PNTR数据填满工作窗口 LCD_CmdWrite(WLCR,0x04);//设置为文字模式 LCD_CmdWrite(CURX,0x00);//设置seg 地址为0 LCD_CmdWrite(CURY,0x00);//设置com 地址为0 LCD_CmdWrite(CHWI,0x00);//设置游标宽度为8,高度为1,间距为1 LCD_CmdWrite(WCCR,0x00); //[10H] , Default --> 0x00 写入DDRAM时游标自动加1,关游标显示,关游标闪烁 LCD_CmdWrite(MAMR,0x32);//双图层显示模式,存取DDRAM2 LCD_RegWrite(MWCR); //内存写入指令 for(y=0;y<240;y++) { for(x=0;x<40;x++) { LCD_DataWrite(m[c++]); LCD_CmdWrite(CHWI,0x00);//设置游标宽度为8,高度为1,间距为1 delay_us(10); LCD_CmdWrite(MAMR,0x32);//双图层显示,存取DDRAM2 delay_us(10); LCD_RegWrite(MWCR);//内存写入指令 delay_us(10); } } } /******************************************* 函数名称:Clear_DDRAM1 功 能:清除显示 参 数:无 返回值 :无 ********************************************/ void Clear_DDRAM1(void) { LCD_CmdWrite(MAMR,0x31); //存取DDRAM1,同时显示DDRAM1/DDRAM2 LCD_CmdWrite(0xE0,0x00);//要写入DDRAM的内容。当寄存器0XF0 BIT3=1时,此数据 //将填满窗口 LCD_CmdWrite(0xF0,0xA8);//开启内存清理功能,将0XE0数据填满窗口 delay_ms(50); } /******************************************* 函数名称:Clear_All 功 能: 参 数:无 返回值 :无 ********************************************/ void Clear_All() { LCD_CmdWrite(MAMR,0x33); //双图层显示模式,同时存取DDRAM1/DDRAM2 LCD_CmdWrite(0xE0,0x00); //要写入DDRAM的内容。当寄存器0XF0 BIT3=1时,此数据 //将填满窗口 LCD_CmdWrite(0xF0,0xA8);//开启内存清理功能,将0XE0数据填满窗口 delay_ms(30); } /******************************************* 函数名称:LCD_show_str 功 能:显示字符串 参 数:unsigned char *text。字符串首地址 返回值 :无 说 明:没有设置显示位置,故显示位置为调用此函数 前,AC值所在地址 说 明:在DDRAM1中显示 ********************************************/ void LCD_show_str(unsigned char *text) { LCD_CmdWrite(0x00,0x0c);//文字模式 LCD_CmdWrite(MAMR,0x31);//存取DDRAM1 while(*text != '\0') { LCD_RegWrite(0xb0); LCD_DataWrite(*text); ++text; delay_us(20); } } /******************************************* 函数名称:Disp_char 功 能:显示中文和字符 参 数:uchar x,uchar y.显示位置首地址 unsigned char *text。字符串首地址 返回值 :无 说 明:可以显示中文和ASCII字符,在DDRAM1中显示 显示行位置可以是单行步进,显示列位置可以是2列步进 ********************************************/ void Disp_char(uchar x,uchar y,unsigned char *text) { LCD_CmdWrite(0x60,x);//segment address LCD_CmdWrite(0x70,y);//common address LCD_show_str(text); } /******************************************* 函数名称:LCD_point 功 能:在坐标(x,y)打点 参 数:int x,int y 返回值 :无 说 明: -----0---x---319---------- |0 |y |239 在DDRAM1中显示 ********************************************/ void LCD_point(int x,int y) { uchar x_d,x_b,x_bb,r_d; x_d=x/8; x_b=x%8; switch(x_b) { case 0x00:x_bb=0x80;break; case 0x01:x_bb=0x40;break; case 0x02:x_bb=0x20;break; case 0x03:x_bb=0x10;break; case 0x04:x_bb=0x08;break; case 0x05:x_bb=0x04;break; case 0x06:x_bb=0x02;break; case 0x07:x_bb=0x01;break; } LCD_CmdWrite(0x00,0x04); LCD_CmdWrite(0x60,x_d); LCD_CmdWrite(0x70,y); r_d=lcd_data_read(); x_bb |=r_d; LCD_CmdWrite(0x60,x_d); LCD_CmdWrite(0x70,y); LCD_CmdWrite(MAMR,0x31);//存取DDRAM1 LCD_RegWrite(0xb0); LCD_DataWrite(x_bb); } /******************************************* 函数名称:Draw_Line 功 能:TM320X240初始化操作 参 数:int x0, int y0, int x1, int y1 起点(x0,y0),终点(x1,y1) 返回值 :无 说 明: -----0---x---319---------- |0 |y |239 ********************************************/ void Draw_Line(int x0, int y0, int x1, int y1) { unsigned int x,y; unsigned int dx; // = abs(x1 - x0); unsigned int dy; // = abs(y1 - y0); if(y0==y1) { if(x0<=x1) { x=x0; } else { x=x1; x1=x0; } while(x <= x1) { LCD_point(x,y0); x++; } return; } else if(y0>y1) { dy=y0-y1; } else { dy=y1-y0; } if(x0==x1) { if(y0<=y1) { y=y0; } else { y=y1; y1=y0; } while(y <= y1) { LCD_point(x0,y); y++; } return; } else if(x0 > x1) { dx=x0-x1; x = x1; x1 = x0; y = y1; y1 = y0; } else { dx=x1-x0; x = x0; y = y0; } if(dx == dy) { while(x <= x1) { x++; if(y>y1) { y--; } else { y++; } LCD_point(x,y); } } else { LCD_point(x, y); if(y < y1) { if(dx > dy) { int p = dy * 2 - dx; int twoDy = 2 * dy; int twoDyMinusDx = 2 * (dy - dx); while(x < x1) { x++; if(p < 0) { p += twoDy; } else { y++; p += twoDyMinusDx; } LCD_point(x, y); } } else { int p = dx * 2 - dy; int twoDx = 2 * dx; int twoDxMinusDy = 2 * (dx - dy); while(y < y1) { y++; if(p < 0) { p += twoDx; } else { x++; p+= twoDxMinusDy; } LCD_point(x, y); } } } else { if(dx > dy) { int p = dy * 2 - dx; int twoDy = 2 * dy; int twoDyMinusDx = 2 * (dy - dx); while(x < x1) { x++; if(p < 0) { p += twoDy; } else { y--; p += twoDyMinusDx; } LCD_point(x, y); } } else { int p = dx * 2 - dy; int twoDx = 2 * dx; int twoDxMinusDy = 2 * (dx - dy); while(y1 < y) { y--; if(p < 0) { p += twoDx; } else { x++; p+= twoDxMinusDy; } LCD_point(x, y); } } } } }
.h文件
//=================================版本信息==================================// //Ver:2011082301 //Author:XZQ //===========================================================================// #ifndef _TM320X240_DRIVER_H #define _TM320X240_DRIVER_H #include <msp430f5438.h> #include "MCU_Init.h" #include "RA8806Reg.h" /*********************TM320X240引脚接口*****************************/ //控制线 P7口 #define CtrlDIR P7DIR #define CtrlOUT P7OUT #define lcd_rs BIT2//RS-->L:存取缓存器,H:存取DDRAM #define lcd_wr BIT3//WR--> L有效 #define lcd_rd BIT4//RD--> L有效 #define lcd_cs1 BIT5//CS1-->L有效 #define lcd_rst BIT6//RST-->L有效 #define lcd_busy BIT7 //BUSY-->检测忙信号 //液晶数据引脚 DB0~DB7 #define DataDIR P3DIR #define DataOUT P3OUT #define DataIN P3IN #define DataSEL P3SEL //底层函数 extern void LCD_CmdWrite(unsigned char cmdaddr,unsigned char wrdata); extern void LCD_DataWrite(unsigned char wrdata); extern void LCD_RegWrite(unsigned char reg); extern unsigned char LCD_DataRead(void); extern unsigned char lcd_data_read(void); extern unsigned char LCD_StatusRead(void); extern void LCD_ChkBusy(void); //基本功能函数 extern void lcd_inital(void); extern void LCD_Rst(void); extern void Clear_DDRAM1(void); extern void Clear_All(void); extern void LCD_show_str(unsigned char *text);//字符显示 extern void Disp_char(uchar x,uchar y,unsigned char *text); extern void LCD_point(int x,int y);//图形显示 extern void DrawPicture(unsigned char const *m); //高级功能函数 extern void Draw_Line(int x0, int y0, int x1, int y1); #endif
RA8806控制器寄存器宏定义文件
//=================================版本信息==================================// //Ver:20110822 //Author:XZQ //===========================================================================// #define WLCR 0x00 #define MISC 0x01 #define ADSR 0x03 #define INTR 0x0F #define WCCR 0x10 #define CHWI 0x11 //new(have change) #define MAMR 0x12 #define AWRR 0x20 #define DWWR 0x21 #define AWBR 0x30 #define DWHR 0x31 //new(have change) #define AWLR 0x40 #define DWLR 0x41 #define AWTR 0x50 #define DWTR 0x51 #define CURX 0x60 //new(have change) #define BGSG 0x61 #define EDSG 0x62 //new #define CURY 0x70 //new(have change) #define BGCM 0x71 #define EDCM 0x72 #define BTMR 0x80 #define ITCR 0x90 //new(have change) #define KSCR1 0xA0 #define KSCR2 0xA1 #define KSDR0 0xA2 //new(have change) #define KSDR1 0xA3 #define KSDR2 0xA4 #define MWCR 0xB0 //new(have change) #define MRCR 0xB1 //new #define TPCR 0xC0 #define TPXR 0xC1 #define TPYR 0xC2 #define TPZR 0xC3 #define PCR 0xD0 //new(have change) #define PDCR 0xD1 //new #define PNTR 0xE0 #define FNCR 0xF0 #define FVHT 0xF1
/***万好好-科研论文培训,微信号:wanhaohao258。如转载本博客内容请注明,如果存在知识产权问题请主动联系。***/