本帖最后由 haorenyigea 于 2014-6-30 14:13 编辑
以下是我写的 一直点不亮啊,用IO模拟SPI
#include "pic.h"
#include "htc.h"
__CONFIG(FOSC_INTOSC&CLKOUTEN_OFF&MCLRE_ON&WDTE_OFF);//&PWRTE_ON&BOREN_ON
#define _XTAL_FREQ 4000000L //内部4MHz
#define uint unsigned int
#define uchar unsigned char
#define SCLK RD4
#define SDIN RD3
#define LCD_DC RD2
#define LCD_CE RD1
#define LCD_RST RD0
#define LCD_BL RD5
void System_Init(void);
void LCD_init(void);
void LCD_clear(void);
void LCD_move_chinese_string(uchar X, uchar Y, uchar T);
void LCD_write_english_string(uchar X,uchar Y,const char *s);
void LCD_write_chinese_string(uchar X, uchar Y,
uchar ch_with,uchar num,
uchar line,uchar row);
void chinese_string(uchar X, uchar Y, uchar T);
void LCD_write_char(uchar c);
void LCD_draw_bmp_pixel(uchar X,uchar Y,uchar *map,
uchar Pix_x,uchar Pix_y);
void LCD_write_byte(uchar dat, uchar dc);
void LCD_set_XY(uchar X, uchar Y);
/*void delay_1us(void); */
//*******************************************************************//
// 系统初始化
//*******************************************************************//
void System_Init(void)
{
OSCCON = 0b01101000; //内部4MHz
OSCTUNE= 0b00000000; //出厂校准
TRISA = 0b00000000; //RA0-RA3输出、RA5输入
ANSELA = 0b00000000; //RA5 模拟输入
TRISB = 0b00001111; //RB0-RB3触摸按键
ANSELB = 0b00000000; //4个触摸按键
WPUB = 0b00000000; //无上拉
IOCBP = 0b00000000; //关电平变化中断
IOCBN = 0b00000000;
TRISD = 0b00000000; //PORTD输出
ANSELD = 0b00000000; //数字 I/O
TRISC = 0b10011000; //串口 I2C
TRISE = 0b00000000; //PORTE输出
ANSELE = 0b00000000; //数字 I/O
WPUE = 0b00000000; //无上拉
}
//*******************************************************************//
// 主函数
//*******************************************************************//
void main(void)
{
LCD_init(); //初始化液晶
LCD_clear();
while(1)
{
LCD_write_english_string(0,0," Welcome to ");
LCD_write_english_string(0,1," Amy studio ");
LCD_write_english_string(0,2,"amy-studio.com");
LCD_write_english_string(0,3," Nokia5110 LCD");
LCD_write_chinese_string(12,4,12,4,0,5);
}
}
//*******************************************************************//
// nokia_5110函数
//*******************************************************************//
// 6 x 8 font
// 1 pixel space at left and bottom
// index = ASCII - 32
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 }, // #
};
unsigned char write_chinese[][24]={
//测
{0x89,0xF2,0x00,0xFF,0x01,0xF9,0xFF,0x00,0xFC,0x00,0xFF,0x00,0x00,0x07,0x04,0x04,0x02,0x01,0x02,0x04,0x01,0x04,0x07,0x00},
//试
{0x11,0xF6,0x00,0x04,0x24,0xE4,0x24,0x24,0xFF,0x05,0x06,0x00,0x00,0x07,0x02,0x01,0x02,0x03,0x01,0x01,0x01,0x02,0x07,0x00},
//程
{0x8A,0x6A,0xFF,0x49,0x20,0xAF,0xA9,0xE9,0xA9,0xAF,0x20,0x00,0x01,0x00,0x07,0x00,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00},
//序
{0x00,0xFE,0x42,0x4A,0x4A,0x5B,0xEA,0x5A,0x4A,0xC2,0x42,0x00,0x06,0x01,0x00,0x00,0x04,0x04,0x07,0x00,0x00,0x00,0x00,0x00},
//电
{0x00,0xFC,0x94,0x94,0x94,0xFF,0x94,0x94,0x94,0xFE,0x04,0x00,0x00,0x01,0x00,0x00,0x00,0x03,0x04,0x04,0x04,0x04,0x06,0x00},
//子
{0x20,0x21,0x21,0x21,0x21,0xF9,0x29,0x25,0x23,0x31,0x20,0x00,0x00,0x00,0x00,0x04,0x04,0x07,0x00,0x00,0x00,0x00,0x00,0x00},
//制
{0x18,0xD6,0x54,0xFF,0x54,0x56,0xD4,0x00,0xFC,0x00,0xFF,0x00,0x00,0x03,0x00,0x07,0x00,0x02,0x03,0x00,0x04,0x04,0x07,0x00},
//作
{0x10,0x08,0xFC,0x13,0x08,0x04,0xFF,0x24,0x24,0x24,0x04,0x00,0x00,0x00,0x07,0x00,0x00,0x00,0x07,0x01,0x01,0x01,0x01,0x00},
//杂
{0x40,0x62,0x52,0xCA,0x47,0xE2,0x42,0xDE,0x50,0x50,0x58,0x00,0x04,0x02,0x01,0x04,0x04,0x07,0x00,0x00,0x01,0x02,0x04,0x00},
//志
{0x04,0xA4,0x24,0xA4,0x24,0xBF,0x24,0x24,0x24,0xA6,0x04,0x00,0x02,0x01,0x00,0x07,0x04,0x04,0x05,0x04,0x06,0x00,0x03,0x00},
//社
{0x84,0x44,0xF5,0x2E,0x40,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,0x00,0x00,0x07,0x00,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00},
};
/*-----------------------------------------------------------------------
LCD_init : 3310LCD初始化
编写日期 :2004-8-10
最后修改日期 :2004-8-10
-----------------------------------------------------------------------*/
/*void delay_1us(void) //1us延时函数
{
unsigned int i;
for(i=0;i<1000;i++);
}
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();
}
*/
void LCD_init(void)
{
// 产生一个让LCD复位的低电平脉冲
LCD_RST = 0;
__delay_us(1);
LCD_RST = 1;
// 关闭LCD
LCD_CE = 0;
__delay_us(1);
// 使能LCD
LCD_CE = 1;
__delay_us(1);
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); // 设定显示模式,正常显示
// 关闭LCD
LCD_CE = 0;
}
/*-----------------------------------------------------------------------
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_english_String : 英文字符串显示函数
输入参数:*s :英文字符串指针;
X、Y : 显示字符串的位置,x 0-83 ,y 0-5
编写日期 :2004-8-10
最后修改日期 :2004-8-10
-----------------------------------------------------------------------*/
void LCD_write_english_string(unsigned char X,unsigned char Y,const char *s)
{
LCD_set_XY(X,Y);
while (*s)
{
LCD_write_char(*s);
s++;
}
}
/*-----------------------------------------------------------------------
LCD_write_chinese_string: 在LCD上显示汉字
输入参数:X、Y :显示汉字的起始X、Y坐标;
ch_with :汉字点阵的宽度
num :显示汉字的个数;
line :汉字点阵数组中的起始行数
row :汉字显示的行间距
编写日期 :2004-8-11
最后修改日期 :2004-8-12
测试:
LCD_write_chi(0,0,12,7,0,0);
LCD_write_chi(0,2,12,7,0,0);
LCD_write_chi(0,4,12,7,0,0);
-----------------------------------------------------------------------*/
void LCD_write_chinese_string(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(write_chinese[line+i][n],1);
}
i++;
LCD_set_XY((X+(ch_with+row)*i),Y);
}
}
/*-----------------------------------------------------------------------
LCD_draw_map : 位图绘制函数
输入参数:X、Y :位图绘制的起始X、Y坐标;
*map :位图点阵数据;
Pix_x :位图像素(长)
Pix_y :位图像素(宽)
编写日期 :2004-8-13
最后修改日期 :2004-8-13
-----------------------------------------------------------------------*/
void LCD_draw_bmp_pixel(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
-----------------------------------------------------------------------*/
void LCD_write_byte(unsigned char dat, unsigned char command)
{
unsigned char i;
//PORTB &= ~LCD_CE ; // 使能LCD
LCD_CE = 0;
if (command == 0)
// PORTB &= ~LCD_DC ; // 传送命令
LCD_DC = 0;
else
// PORTB |= LCD_DC ; // 传送数据
LCD_DC = 1;
for(i=0;i<8;i++)
{
if(dat&0x80)
SDIN = 1;
else
SDIN = 0;
SCLK = 0;
dat = dat << 1;
SCLK = 1;
}
// SPDR = data; // 传送数据到SPI寄存器
//while ((SPSR & 0x80) == 0); // 等待数据传送完毕
//PORTB |= LCD_CE ; // 关闭LCD
LCD_CE = 1;
}
|
|