论坛大神
  
- 积分
- 3323
- 金钱
- 3323
- 注册时间
- 2013-7-19
- 在线时间
- 195 小时
|
发表于 2016-1-7 09:22:45
|
显示全部楼层
本帖最后由 ricefat 于 2016-1-7 09:26 编辑
[mw_shl_code=c,true]/*****************************************************/
/* File name : LCD.c */
/* Description : Code for COG12864 LCD, display IC:ST7565S */
/* Platform : MDK for STM32F103RBT6 */
/* Author : Michael Zhang - 章其波 Modified by Rice */
/* Email : sudazqb@163.com */
/* MSN : zhangqibo_1985@hotmail.com */
/* Date : 2007-10-21 2009-12-01 */
/* NOT FOR COMMERCIAL USE, ALL RIGHT RESERVED! */
/*****************************************************/
/* Change Log: */
/* 20071122: fix a bug for some 3310lcd will not work with display function */
/* 20071021: original version */
/*****************************************************/
#include <stm32f10x_lib.h>
#include "lcd.h"
#include "lcd_app.h"
#include "lcd_ascii.h"
#include "delay.h"
/*SCE(片选E),RST(RST),D/C(A0),DIN(SI),SCLK(SCK)*/
/******************** IO utilities ***************************/
#define LCD_RS_H GPIOE->ODR |= 1<<2;
#define LCD_RS_L GPIOE->ODR &= ~(1<<2); //A0 PE2
#define LCD_E_H GPIOE->ODR |= 1<<0;
#define LCD_E_L GPIOE->ODR &= ~(1<<0); //SCE PE0
#define LCD_RST_H GPIOE->ODR |= 1<<1;
#define LCD_RST_L GPIOE->ODR &= ~(1<<1); //RST PE1
#define LCD_SCK_H GPIOE->ODR |= 1<<4;
#define LCD_SCK_L GPIOE->ODR &= ~(1<<4); //SCK PE4
#define LCD_MOSI_H GPIOE->ODR |= 1<<3;
#define LCD_MOSI_L GPIOE->ODR &= ~(1<<3); //SI PE3
/*#define PORT_INI { \
LCD_RS_H; \
LCD_E_H; \
LCD_RST_H; \
LCD_SCK_H; \
LCD_MOSI_H; \
LCD_RS_DDR |= 1<<LCD_RS_BIT; \
LCD_E_DDR |= 1<<LCD_E_BIT; \
LCD_RST_DDR |= 1<<LCD_RST_BIT; \
LCD_SCK_DDR |= 1<<LCD_SCK_BIT; \
LCD_MOSI_DDR |= 1<<LCD_MOSI_BIT; \
}*/
/*********** Display buffer in SRAM ************/
unsigned char disBuf[1024]; //12864有128*64个点,每8个占用1字节的数据,故一共有1024个字节
#if HW_SPI
/*********************** Hardware SPI Speed Set **************/
void lcdSpiSpeedSet(void)
{
SPCR = (1<<SPE) | (1<<MSTR);
#if (F_CPU>8000000)
SPSR &= ~(1<<SPI2X); /* 1/4 F_CPU */
#else
SPSR |= 1<<SPI2X; /* 1/2 F_CPU */
#endif
}
/************************ Hardware SPI Speed set End *********/
#endif
/************************ SPI write a byte *********************/
void lcdWriteByte(unsigned char val)
{
//LCD_E_L;
#if HW_SPI /* Use Hardware SPI */
SPDR = val;
while(!(SPSR & (1<<SPIF))); /* Wait transfer end */
#else /* Use common GPIO to simulate the timing*/
unsigned char i = 8;
while(i--)
{
LCD_SCK_L;
if(val&0X80)
{
LCD_MOSI_H; /* MSB first */
}
else
{
LCD_MOSI_L;
}
LCD_SCK_H;
val <<= 1; /* Data shift */
}
#endif
//LCD_E_H;
}
/***************************************************************/
/************************ SPI write a command *********************/
void lcdWriteCmd(unsigned char val)
{
unsigned char i = 8;
//LCD_E_L;
LCD_RS_L; //A0
#if HW_SPI /* Use Hardware SPI */
SPDR = val;
while(!(SPSR & (1<<SPIF))); /* Wait transfer end */
#else /* Use common GPIO to simulate the timing*/
while(i--)
{
LCD_SCK_L;
if(val&0X80)
{
LCD_MOSI_H; /* MSB first */
}
else
{
LCD_MOSI_L;
}
LCD_SCK_H;
val <<= 1; /* Data shift */
}
#endif
LCD_RS_H;
//LCD_E_H;
}
/***************************************************************/
/******** Clear the display buffer, external buffer in mico's SRAM ***************/
void lcdClrDisBuf(void)
{
unsigned int n = 1024;
unsigned char * p = disBuf;
while(n--)*p++ = 0X00;
}
/** Update the display, actually transfer the data of the SRAM to LCD through SPI ****/
void lcdUpdateDisplay(void)
{
//unsigned int n = 504;
unsigned char * p = disBuf;
unsigned int i,j;
#if HW_SPI /* If hardware SPI is used, adjust the SPI speed*/
lcdSpiSpeedSet(); /* Other SPI device may share the SPI & they may change the speed */
#endif
LCD_E_L; /* Chip select */
/***************************************************************************/
/*** It seems that some 3310lcd do not support the following code ****/
//lcdWriteCmd(0x0c);
//lcdWriteCmd(0x40 | 0); /* Set row to 0 */
//lcdWriteCmd(0x80 | 0); /* Set column to 0*/
/* To make sure the data always start at codinate(0,0) */
//while(n--)
// lcdWriteByte(*p++);
/***************************************************************************/
/* so we use this, set the address at each row */
for(i=0;i<8;i++)
{
lcdWriteCmd(0xB0 | i); /* Set row to 0 */
lcdWriteCmd(0X10); /* Set column to 0*/
lcdWriteCmd(0X04); //设置column时,如果ADC设置为反向(131~0,而不是127~0),那么每行应该从第5个(0~4)开始,否则第一个字符显示不全。
for(j=0;j<128;j++)
{
lcdWriteByte(*p++);
}
}
LCD_E_H; /* chip deselect */
}
/***************** LCD initialization *****************************/
void lcdInit(void)
{
RCC->APB2ENR|=1<<6;//PORTE时钟使能
GPIOE->CRL&=0XFFF00000; //复位GPIOE端口状态
GPIOE->CRL|=0X00033333; //PE4:0推挽输出
GPIOE->ODR|=0X1F; // PE4:0输出高
//PORT_INI; /* IO port initialization*/
#if HW_SPI /* If hardware SPI is used, adjust the SPI speed */
//lcdSpiSpeedSet();
SPCR = (1<<SPE) | (1<<MSTR)|(1<<SPR0)|(1<<SPR1);
SPSR &= ~(1<<SPI2X); /* 1/4 F_CPU */
#endif
lcdClrDisBuf(); /* clear display buffer: not the lcd internal buffer */
delay_ms(15); /*MAX F_CPU should below than 16MHz*/
delay_ms(15);
LCD_RST_L; /* Reset LCD */
delay_ms(2);
delay_ms(2);
LCD_RST_H;
delay_ms(1); /* Short Delay */
LCD_E_L; /* Enable LCD: CS = 0 */
lcdWriteCmd(0xA3); /* LCD Bias selection(1/65 Duty,1/7Bias) */
lcdWriteCmd(0xA1); /* ADC selection(SEG0->SEG128) */
lcdWriteCmd(0xC0); /* SHL selection(COM64->COM0) */
lcdWriteCmd(0x26); /* Regulator Resistor Selection*/
delay_ms(15);
lcdWriteCmd(0x81); /* Electronic Volume 该命令代表开始设置对比度,下一句为设置值*/
lcdWriteCmd(0x2A); /* Reference Register selection Vo=(1+Rb/Ra)(1+a)*2.1=10*/
lcdWriteCmd(0x2C); /* Power Control(Vc=1;Vr=0;Vf=0) */
delay_ms(15);
lcdWriteCmd(0x2C|0x2A);
delay_ms(15);
lcdWriteCmd(0x2C|0x2A|0x29);
delay_ms(15);
lcdWriteCmd(0xF8);
lcdWriteCmd(0x01);
lcdWriteCmd(0xA7);
delay_ms(15);
lcdWriteCmd(0xAF); //开显示
lcdUpdateDisplay(); /* Update display */
LCD_E_H; /* Disable LCD: CS =1 */
}
/*********** Set a pixel bit with value val *************************/
void OnePixel(unsigned char x,unsigned char y,unsigned char val)
{
unsigned char *p = &disBuf[ (unsigned int)y/8*128 + x ];//找到对应字节
if(val)
{
*p |= (1<<(y%8)); //修改对应位
}
else
{
*p &= ~(1<<(y%8));
}
}
/********** Read the pixel value withe the given codination ************/
unsigned char ReadPixel(unsigned char x,unsigned char y)
{
unsigned char *p = &disBuf[ (unsigned int)y/8*128 + x ];//找到对应字节
if(*p & (1<<(y%8)))return 1;
else return 0;
}
/* demo program*/
/*int main()
{
unsigned char i;
lcdInit();
OnePixel(0,0,1);
OnePixel(127,0,1);
OnePixel(0,63,1);
OnePixel(127,63,1);
OnePixel(63,31,1);
OnePixel(64,32,1);
for(i=0;i<128;i++)
{
OnePixel(i,60,1);
}
LCD_print12(10,15,"ABCDE\nFIPC\nOnePixel(127,63,1);for(i=0;i<128;i++)");
lcdUpdateDisplay();
while(1);[/mw_shl_code]
最开始是章大神写的代码,我针对7565做了修改 |
|