本帖最后由 lilihua0721 于 2011-12-31 18:42 编辑
二、工程文件代码展示
(1) main.c
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include "st7920.h"
#include "Type.h"
#include "delay.h"
INT32U TheSysClock; //定义全局的系统时钟变量
/**************************************************************************************
函数名: int main(void)
功 能: 测试LCD12864单色液晶
说 明: 显示一张图片,分辨率为128*64
输 入:无
输 出:无
其 他:初始化可分为以下步骤:
1:防止JTAG死锁
2:配置系统时钟,保证板子能正常工作起来
3:使能GPIO端口外设,也就是开GPIO功能。
4:设置LCD管脚为输出管脚。
5:初始化LCD,保证LCD正常工作
**************************************************************************************/
int main(void)
{
/**********JTAG防止死锁*************/
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOC); //使能外设GIOPC
GPIOPinTypeGPIOInput(GPIO_PORTC_BASE, GPIO_PIN_4); //设置USER按键所在管脚为输入
if (GPIOPinRead(GPIO_PORTC_BASE, GPIO_PIN_4) == 0x00) //若复位时按下USER按键,则进入
{
for (;;); //死循环,以等待JTAG连接
}
SysCtlPeripheralDisable(SYSCTL_PERIPH_GPIOC); //禁止USER按键所在的GPIO端口
/***********配置系统时钟 *********/
SysCtlClockSet(SYSCTL_SYSDIV_1 | //不分频
SYSCTL_USE_OSC | //系统时钟设置
SYSCTL_OSC_MAIN | //采用主振荡器
SYSCTL_XTAL_6MHZ); //外接6MHz晶振
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE,GPIO_PIN_2 |GPIO_PIN_3|GPIO_PIN_5);
TheSysClock = SysCtlClockGet(); //获取当前的系统时钟频率
open_lcd(); //初始化12864液晶
start_**(); //显示显示图片,分辨率为128*64
/* 向液晶缓冲区写入数据 */
write_char_buf(0, 0, "TI LM3S811开发板");
write_char_buf(1, 0, "TI Stellaris M3 ");
write_char_buf(2, 0, " 感谢21IC ");
write_char_buf(3, 0, " 感谢TI ");
//进入循环处理用户代码
while(1)
{
delay10ms(10);
lcd();
}
}
(2) st7920.c文件
#include <string.h>
#include "st7920.h"
#include "Type.h"
#include "delay.h"
INT16U buf_lcd_char[4][8]; // LCD文字显示缓冲区
/**************************************************************************************
名称:void start_**(void)
说明: 开机画面显示
输入:image_**[] 开机画面信息
输出:无
**************************************************************************************/
INT8U const **_image[1024] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x86,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xEC,0xF0,0x3F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0xCD,0xF0,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x8C,0xF0,0x06,0x3F,0x73,0x18,0x3C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x0F,0x08,0x70,0x06,0x31,0x32,0x1C,0x64,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x1F,0x18,0x78,0x06,0x30,0x3E,0x1C,0x60,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x07,0xFF,0x18,0xF8,0x06,0x3E,0x1C,0x26,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x03,0xFF,0x9B,0xFC,0x06,0x32,0x1C,0x26,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x01,0xFF,0x9B,0xFC,0x06,0x30,0x16,0x7E,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x93,0xFC,0x06,0x30,0x26,0x43,0x46,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFF,0x93,0xFC,0x07,0x3F,0x63,0xC3,0x7C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x7F,0x81,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x61,0x81,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x01,0x81,0x80,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFF,0x00,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xFE,0x00,0x06,0x61,0x3E,0xFE,0xF8,0xC2,0x60,0xCF,0x98,0x5F,0xCF,0x00,
0x00,0x00,0x7C,0x00,0x06,0x71,0x22,0xB2,0xCC,0xC2,0x71,0xCC,0x1C,0x57,0x59,0x00,
0x00,0x00,0x7C,0x00,0x06,0x79,0x60,0x10,0xC6,0xC2,0x71,0xC8,0x1E,0x42,0x10,0x00,
0x00,0x00,0x3C,0x00,0x06,0x6D,0x3C,0x30,0xCC,0xC2,0x52,0xCF,0x97,0x42,0x1E,0x00,
0x00,0x00,0x1E,0x00,0x06,0x67,0x1E,0x30,0xF8,0xC2,0x5A,0xCF,0x93,0xC2,0x0F,0x80,
0x00,0x00,0x00,0x00,0x06,0x67,0x03,0x10,0xCC,0xC2,0x4C,0xC8,0x11,0xC2,0x01,0x80,
0x00,0x00,0x00,0x00,0x06,0x63,0x43,0x30,0xCC,0xC6,0x4C,0xCC,0x10,0xC2,0x11,0x80,
0x00,0x00,0x00,0x00,0x07,0x61,0x3E,0x30,0xC6,0x7C,0x48,0xCF,0xD8,0x47,0x1F,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
void start_**(void)
{
//INT8U *ptr_**;
INT8U i, j;
INT16U addr;
INT8U tmp_data[32];
/* 显示信息 */
//ptr_** = (INT8U *)**_image;
for (i = 0; i < 32; i++)
{
/* 组织数据 */
addr = i * 16;
for (j = 0; j < 16; j++)
{
tmp_data[j] = **_image[addr + j];
tmp_data[j+16] = **_image[512+addr+j];
}
addr = i;
addr <<= 8;
write_st7920(ST7920_RAM_GDRAM, addr, tmp_data, 32);
// __RESET_WATCHDOG();
}
/* 开机画面,等待3s */
delay10ms(300);
}
/**************************************************************************************
名称:void open_st7920(void)
说明: 液晶初始化函数
输入:无
输出:无
**************************************************************************************/
void open_st7920(void)
{
/* Set function RE = 0 */
send_cmd_st7920(0x30);
/* Set Entry point */
send_cmd_st7920(0x06);
/* Turn on display */
send_cmd_st7920(0x0C);
/* Clear screen */
send_cmd_st7920(0x01);
/* Set function RE = 1 */
send_cmd_st7920(0x34);
/* Set SR = 0 */
send_cmd_st7920(0x20);
}
/**************************************************************************************
名称:void send_cmd_st7920(INT8U cmd)
说明: 发送一个命令至st7920
输入:无
输出:无
**************************************************************************************/
void send_cmd_st7920(INT8U cmd)
{
INT8U sbyte = 0xF8;
INT8U i = 0;
ST7920_CLK_0();
ST7920_CS_1();
/* 输入前导字节 */
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if (sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
ST7920_CLK_1();
sbyte <<= 1;
}
/* 输入命令码高4位 */
sbyte = cmd & 0xF0;
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if (sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
ST7920_CLK_1();
sbyte <<= 1;
}
/* 输入命令码低4位 */
sbyte = cmd << 4;
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if(sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
ST7920_CLK_1();
sbyte <<= 1;
}
ST7920_CS_0();
/* 等待命令执行 */
delay1ms(1);
if (cmd == 0x01)
{
delay10ms(1);
}
}
/**************************************************************************************
名称:void send_data_st7920(INT8U ptr_data, INT8U len)
说明: 写数据至液晶
输入:ptr_data:数据起始地址
len:数据长度
输出:无
**************************************************************************************/
void send_data_st7920(INT8U *ptr_data, INT8U len)
{
INT8U sbyte = 0xFA;
INT8U i = 0;
INT8U j = 0;
ST7920_CS_1();
/* 输入前导字节 */
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if (sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
sbyte <<= 1;
ST7920_CLK_1();
}
/* 输入数据 */
for (j = 0; j < len; j++)
{
/* 输入数据字节高4位 */
sbyte = ptr_data[j] & 0xF0;
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if (sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
sbyte <<= 1;
ST7920_CLK_1();
}
/* 输入数据字节低4位 */
sbyte = ptr_data[j] << 4;
for (i = 0; i < 8; i++)
{
ST7920_CLK_0();
if (sbyte & 0x80)
{
ST7920_DI_1();
}
else
{
ST7920_DI_0();
}
sbyte <<= 1;
ST7920_CLK_1();
}
}
/* 延时等待数据写入完成 */
delay1ms(1);
ST7920_CS_0();
}
/**************************************************************************************
名称:void init_lcd(void)
说明: 初始化LCD显示
输入:无
输出:无
**************************************************************************************/
void open_lcd(void)
{
INT8U i, j;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 8; j++)
{
buf_lcd_char[j] = 0xA1A0;
}
}
open_st7920();
}
/**************************************************************************************
名称:void write_char_buf(INT8U row, INT8U col, INT8U *pc)
说明: 装载文字缓冲区
输入:无
输出:无
**************************************************************************************/
void write_char_buf(INT8U row, INT8U col, INT8U *pc)
{
INT8U r = row & 0x03u;
INT8U c = col & 0x07u;
INT8U i;
INT8U k;
INT8U len;
if (pc == NULL)
return;
len = strlen(pc);
for (i=0,k=c; 2*i<len;i++)
{
/* #if 0
buf_lcd_char[r][k] = (pc[2*i]<<8);
buf_lcd_char[r][k++] += pc[2*i+1];
if(len%2==1&&i==(len/2-1))
{
buf_lcd_char[r][k] = (pc[2*i+2]<<8);
}
#endif */
buf_lcd_char[r][k] = pc[2*i];
buf_lcd_char[r][k++] += (pc[2*i+1]<<8);
if(len%2==1&&i==(len/2-1))
{
buf_lcd_char[r][k] = pc[2*i+2];
}
}
}
/**************************************************************************************
名称:void lcd(void)
说明: LCD显示模块接口程序
输入:buf_lcd_char[] LCD文字显示缓冲区
输出:无
**************************************************************************************/
void lcd(void)
{
INT8U static ddram_row = 0; /* 刷新行号,每次刷新一行 */
INT8U *ptr_data;
INT8U lcd_row = 0;
INT8U lcd_col = 0;
/* ASCII字符码转 GB字符码 */
for (lcd_row = 0; lcd_row < 4; lcd_row++)
{
for (lcd_col = 0; lcd_col < 8; lcd_col++)
{
if ( buf_lcd_char[lcd_row][lcd_col] <= 125
&& buf_lcd_char[lcd_row][lcd_col] >= 32 )
{
buf_lcd_char[lcd_row][lcd_col] += 0xA380;
}
}
}
/* 选择行 */
switch (ddram_row)
{
case 0: /* LCD缓冲区 0行对应DDRAM 0行 */
lcd_row = 0;
break;
case 1: /* LCD缓冲区 1行对应DDRAM 2行 */
lcd_row = 2;
break;
case 2: /* LCD缓冲区 2行对应DDRAM 1行 */
lcd_row = 1;
break;
case 3: /* LCD缓冲区 3行对应DDRAM 3行 */
lcd_row = 3;
break;
default:
break;
}
ptr_data = (INT8U *)buf_lcd_char[lcd_row];
write_st7920(ST7920_RAM_DDRAM, ddram_row * 8, ptr_data, 16);
ddram_row++;
if (ddram_row >= 4)
{
ddram_row = 0;
}
} |