跟大家分享字库芯片GT20L16S1Y的IO口模拟SPI和硬件SPI这两种C语言驱动程序。
本帖最后由 jianhong_wu 于 2016-5-1 14:32 编辑★坚鸿-深圳:
字库芯片GT20L16S1Y很常用,芯片内部的取模方式是:纵向取模,字节倒序。
现在我把它的IO口模拟SPI和硬件SPI这两种驱动程序都分享给大家参考,我是直接节选我项目中的片段,大家使用的时候只要做一些简单的删减移植
就能使用。建议大家使用的时候应该辅助GT20L16S1Y的数据手册来看比较方便理解,然后再取其精华。
(1)IO口模拟SPI的驱动程序:
.H头文件:
#ifndef _GT20L16S1Y_
#define _GT20L16S1Y_
#define SPI0_CS_ON() (GPIO_WriteHigh(GPIOB,GPIO_PIN_4)) //拉高
#define SPI0_CS_OFF() (GPIO_WriteLow(GPIOB,GPIO_PIN_4)) //拉低
#define SPI0_SCLK_ON() (GPIO_WriteHigh(GPIOB,GPIO_PIN_3)) //拉高
#define SPI0_SCLK_OFF() (GPIO_WriteLow(GPIOB,GPIO_PIN_3)) //拉低
#define SPI0_MOSI_ON() (GPIO_WriteHigh(GPIOB,GPIO_PIN_2)) //拉高
#define SPI0_MOSI_OFF() (GPIO_WriteLow(GPIOB,GPIO_PIN_2)) //拉低
void SPI0_clear(void);
unsigned char SPI0_readByte(void);
void SPI0_writeByte(unsigned char dat);
void WORDLIB_read_ASCII_8x16(unsigned int ASCIICode, unsigned char *buffer);
void WORDLIB_read_GB_16x16(unsigned int GBCode, unsigned char*buffer);
void diy_zi_ku(const unsigned char *pucConstZiKu,unsigned char *pucResultBuffer,unsigned char ucZiKuCnt);
extern const unsigned charZf8x16_zheng_san_jiao[];
extern const unsigned charZf8x16_dao_san_jiao[];
extern const unsigned charZf8x16_quan_xian[];
extern const unsigned charZf8x16_men_kuang_tu[];
extern const unsigned charZf8x16_shuang_jian_tou[];
extern const unsigned charZf8x16_zuo_san_jiao[];
extern const unsigned charZf8x16_you_san_jiao[];
extern const unsigned charZf8x16_you_biao[];
extern const unsigned charHz16x16_kong[];
extern const unsigned charHz16x16_full[];
#endif
.C源文件:
#include"stm8s.h"
#include "GT20L16S1Y.h"
#include "delay.h"
const unsigned charZf8x16_zheng_san_jiao[]=
{
/*--文字:正三角 --*/
0x00,0x20,0x60,0xE0,0xE0,0xE0,0x60,0x20,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,//68
};
const unsigned charZf8x16_dao_san_jiao[]=
{
/*--文字:倒三角 --*/
0x00,0x80,0xC0,0xE0,0xC0,0x80,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,//67
};
const unsigned charZf8x16_quan_xian[]=
{
/*--文字:全显--*/
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff, //71
};
const unsigned charZf8x16_men_kuang_tu[]=
{
/*--文字: 门框图--*/
0x00,0xFE,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,0x7F,0x40,0x40,0x40,0x40,0x7F,0x00, //73
};
const unsigned charZf8x16_shuang_jian_tou[]=
{
/*--文字: 双箭头--*/
0x40,0x60,0x70,0x78,0x70,0x60,0x40,0x00,0x02,0x06,0x0E,0x1E,0x0E,0x06,0x02,0x00, //74
};
const unsigned charZf8x16_zuo_san_jiao[]=
{
/*--文字: 左三角 --*/
0x00,0x00,0x80,0xC0,0xE0,0xF0,0xF8,0x00,0x00,0x01,0x03,0x07,0x0F,0x1F,0x3F,0x00, //77
};
const unsigned charZf8x16_you_san_jiao[]=
{
/*--文字: 右三角--*/
0x00,0xF8,0xF0,0xE0,0xC0,0x80,0x00,0x00,0x00,0x3F,0x1F,0x0F,0x07,0x03,0x01,0x00, //78
};
const unsigned charZf8x16_you_biao[]=
{
/*--文字: --*/
0x00,0x80,0x80,0x90,0xA0,0xC0,0x80,0x00,0x00,0x00,0x00,0x04,0x02,0x01,0x00,0x00,
};
const unsigned charHz16x16_kong[]=
{
/*--文字:空白无--*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //100
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
const unsigned charHz16x16_full[]=
{
/*--文字:全显--*/
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
};
/*******************************************************
*函数名称:SPI0_clear
*函数功能:清除SPI总线状态
*入口参数:无
*出口参数:无
******************************************************/
void SPI0_clear(void)
{
SPI0_CS_ON(); //拉高
SPI0_SCLK_ON();//拉高
SPI0_MOSI_ON();//拉高
}
/*******************************************************
*函数名称:SPI0_readByte
*函数功能:SPI0读取一个字节数据
*入口参数:无
*出口参数:unsigned char
******************************************************/
unsigned char SPI0_readByte(void)
{
unsigned chari;
unsigned chardat;
for(i = 0; i < 8; i++)
{
dat <<= 1;
SPI0_SCLK_OFF(); //下降沿,MISO字库芯片移出一个bit
if(GPIO_ReadInputPin(GPIOB,GPIO_PIN_1)!= 0)
{
dat |= 0x01;
}
else
{
dat |= 0x00;
}
delay(1);
SPI0_SCLK_ON();
delay(1);
}
return dat;
}
/*******************************************************
*函数名称:SPI0_writeByte
*函数功能:SPI0写出一个字节数据
*入口参数:unsigned char
*出口参数:无
******************************************************/
void SPI0_writeByte(unsigned char dat)
{
unsigned char i;
for(i = 0; i < 8; i++)
{
SPI0_SCLK_OFF();
if(dat & 0x80)
{
SPI0_MOSI_ON();
}
else
{
SPI0_MOSI_OFF();
}
dat <<= 1;
delay(1);
SPI0_SCLK_ON(); //上升沿,MOSI字库芯片移入一个bit
delay(1);
}
}
/*******************************************************
*函数名称:WORDLIB_read_ASCII_8x16
*函数功能:读取字库芯片中一个ASCII字符的8x16点阵编码
*入口参数:ASCIICode,ASCII码;buffer,点阵编码的缓冲区;
buffer必须大于等于16个字节。
*出口参数:无
******************************************************/
void WORDLIB_read_ASCII_8x16(unsigned int ASCIICode,unsigned char *buffer)
{
unsigned long BaseAddr = 0x3b7c0; //8x16点阵的起始地址,见手册《GT23L32S4W用户手册》
unsigned long WordAddr = 0; //ASCII字符点阵在芯片中的字节地址
unsigned long ASCIICodeTemp =0;
const unsigned int BUFSIZE = 16; //缓冲区的大小
unsigned chari;
ASCIICodeTemp=ASCIICode&0xff;
//计算地址,见手册《GT20L16S1Y用户手册》
if(ASCIICodeTemp==0x7E)
{
diy_zi_ku(Zf8x16_you_biao,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==1)
{
diy_zi_ku(Zf8x16_zheng_san_jiao,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==2)
{
diy_zi_ku(Zf8x16_dao_san_jiao,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==255)
{
diy_zi_ku(Zf8x16_quan_xian,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==254)
{
diy_zi_ku(Zf8x16_men_kuang_tu,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==0)
{
diy_zi_ku(Zf8x16_shuang_jian_tou,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==23)
{
diy_zi_ku(Zf8x16_zuo_san_jiao,buffer,BUFSIZE);
return;
}
else if(ASCIICodeTemp==22)
{
diy_zi_ku(Zf8x16_you_san_jiao,buffer,BUFSIZE);
return;
}
else if((ASCIICodeTemp >= 0x20) && (ASCIICodeTemp <= 0x7E))
{
WordAddr = (ASCIICodeTemp - 0x20) * BUFSIZE + BaseAddr;
}
else //空格0x20
{
ASCIICodeTemp=0x20;
WordAddr = (ASCIICodeTemp - 0x20) * BUFSIZE + BaseAddr;
}
SPI0_clear(); //清除SPI总线
delay(1);
//开始快速读取点阵数据:Read Data Bytes At Higher Speed
SPI0_CS_OFF();
delay(1);
SPI0_writeByte(0x0b);//发送命令字:0x0B
SPI0_writeByte((WordAddr >> 16) & 0xff); //从高到低,依次发送三个字节的地址
SPI0_writeByte((WordAddr >> 8) & 0xff);
SPI0_writeByte(WordAddr & 0xff);
SPI0_writeByte(0xff); //最后发送一个字节的假数据 Dummy Byte
for(i = 0; i < BUFSIZE; i++)
{
*(buffer + i) = SPI0_readByte(); //读取字库芯片发送过来的点阵编码
}
SPI0_CS_ON(); //CS置高,结束本次操作
delay(1);
SPI0_clear(); //清除SPI总线
delay(1);
}
/*******************************************************
*函数名称:WORDLIB_read_GB_16x16
*函数功能:读取字库芯片中一个国标汉字的16x16点阵编码
*入口参数:GBCode,汉字内码;buffer,点阵编码的缓冲区;
buffer必须大于等于32个字节。
*出口参数:无
******************************************************/
void WORDLIB_read_GB_16x16(unsigned int GBCode, unsigned char*buffer)
{
unsigned long BaseAddr = 0; //16x16点阵的起始地址,见手册《GT20L16S1Y用户手册》
unsigned long GBCode_MSB = 0; //汉字内码的高八位
unsigned long GBCode_LSB = 0; //汉字内码的低八位
unsigned long WordAddr = 0; //汉字或者ASCII字符点阵在芯片中的字节地址
const unsigned char BUFSIZE = 32; //缓冲区的大小
unsigned char i;
GBCode_MSB = (GBCode >> 8) & 0xFF; //汉字内码的高八位
GBCode_LSB = GBCode & 0xFF; //汉字内码的低八位
//计算地址,见手册《GT20L16S1Y用户手册》
if(GBCode==256||GBCode==0x2020)
{
diy_zi_ku(Hz16x16_kong,buffer,BUFSIZE);
return;
}
else if(GBCode_MSB == 0xA9&& GBCode_LSB >= 0xA1)
{
WordAddr = (282+(GBCode_LSB-0xA1))*32+ BaseAddr;
}
else if(GBCode_MSB >= 0xA1&&GBCode_MSB <= 0xA3&& GBCode_LSB >= 0xA1)
{
WordAddr = (GBCode_MSB-0xA1)*94+(GBCode_LSB-0xA1)*32+ BaseAddr;
}
else if(GBCode_MSB >= 0xB0&&GBCode_MSB <= 0xF7&& GBCode_LSB >= 0xA1)
{
WordAddr = ((GBCode_MSB-0xB0)*94+(GBCode_LSB-0xA1)+846)*32+ BaseAddr;
}
SPI0_clear(); //清除SPI总线
delay(1);
//开始快速读取点阵数据:Read Data Bytes At Higher Speed
SPI0_CS_OFF();
delay(1);
SPI0_writeByte(0x0B); //发送命令字:0x0B
SPI0_writeByte((WordAddr >> 16) & 0xff); //从高到低,依次发送三个字节的地址
SPI0_writeByte((WordAddr >> 8) & 0xff);
SPI0_writeByte(WordAddr & 0xff);
SPI0_writeByte(0xff); //最后发送一个字节的假数据 Dummy Byte
for(i = 0; i < BUFSIZE; i++)
{
*(buffer + i) = SPI0_readByte(); //读取字库芯片发送过来的点阵编码
}
SPI0_CS_ON(); //CS置高,结束本次操作
delay(1);
SPI0_clear(); //清除SPI总线
delay(1);
}
void diy_zi_ku(const unsigned char *pucConstZiKu,unsigned char *pucResultBuffer,unsigned char ucZiKuCnt)
{
unsigned char i;
for(i=0;i<ucZiKuCnt;i++)
{
pucResultBuffer=pucConstZiKu;
}
}
(2)硬件SPI的驱动程序:
.H头文件:
#ifndef _GT20L16S1Y_
#define _GT20L16S1Y_
#defineSPI0_CS_DIR_OUT(1)
#defineSPI0_CS_DIR_IN (0)
#defineSPI0_CS_PORT(2)
#defineSPI0_CS_PIN (23)
#defineSPI0_CS_MASK(1<<SPI0_CS_PIN)
#defineSPI0_CS_ON() (GPIO_OutputValue(SPI0_CS_PORT, SPI0_CS_MASK, 1))
#defineSPI0_CS_OFF() (GPIO_OutputValue(SPI0_CS_PORT, SPI0_CS_MASK, 0))
#define FIFOSIZE 8
/* SSP CR1 register */
#define SSPCR1_LBM (1 << 0)
#define SSPCR1_SSE (1 << 1)
#define SSPCR1_MS (1 << 2)
#define SSPCR1_SOD (1 << 3)
/* SSP Status register */
#define SSPSR_TFE (1 << 0)
#define SSPSR_TNF (1 << 1)
#define SSPSR_RNE (1 << 2)
#define SSPSR_RFF (1 << 3)
#define SSPSR_BSY (1 << 4)
void Gt12l16s1y_Initial(void);
void SPI0_clear(void);
unsigned char SPI0_readByte(void);
void SPI0_writeByte(unsigned char dat);
void WORDLIB_read_ASCII_8x16(unsigned int ASCIICode, unsigned char *buffer);
void WORDLIB_read_GB_16x16(unsigned int GBCode, unsigned char*buffer);
void diy_zi_ku(const unsigned char *pucConstZiKu,unsigned char *pucResultBuffer,unsigned char ucZiKuCnt);
#endif
.C源文件:
#include "GT20L16S1Y.h"
#include "Timer.h"
#include "lpc177x_8x_gpio.h"
#include "lpc177x_8x_ssp.h"
#include "Delay.h"
#include "Can.h"
#include "UserGlobal.h"
void Gt12l16s1y_Initial(void)
{
uint8_t i, Dummy=Dummy;
//uint8_t regVal;
/* Enable AHB clock to the SSP0. */
LPC_SC->PCONP |= (0x1<<21);
/* Further divider is needed on SSP clock. */
// regVal = LPC_SC->PCLKSEL;
// if ( regVal < 4 )
// {
// LPC_SC->PCLKSEL = 4;
// }
LPC_IOCON->P2_22 &= ~0x07;
LPC_IOCON->P2_22 |= 0x02; /* SSP CLK */
LPC_IOCON->P2_26 &= ~0x07;
LPC_IOCON->P2_26 |= 0x02; /* SSP MISO */
LPC_IOCON->P2_27 &= ~0x07;
LPC_IOCON->P2_27 |= 0x02; /* SSP MOSI */
LPC_IOCON->P2_23 &= ~0x07;
LPC_GPIO2->DIR |= (0x1<<23);
LPC_GPIO2->SET |= (0x1<<23);/* port2, bit 23(SSEL) is set to GPIO output and high */
/* Set DSS data to 8-bit, Frame format SPI, CPOL = 0, CPHA = 0, and SCR is 15 */
LPC_SSP0->CR0 = 0x0707;
/* SSPCPSR clock prescale register, master mode, minimum divisor is 0x02 */
LPC_SSP0->CPSR = 0x2;
for ( i = 0; i < FIFOSIZE; i++ )
{
Dummy = LPC_SSP0->DR; /* clear the RxFIFO */
}
/* Master mode */
LPC_SSP0->CR1 = SSPCR1_SSE;
}
void SPI0_clear(void) //清除SPI总线
{
SPI0_CS_ON();
}
/*******************************************************
*函数名称:SPI0_readByte
*函数功能:SPI0读取一个字节数据
*入口参数:无
*出口参数:unsigned char
******************************************************/
unsigned char SPI0_readByte(void)
{
unsigned chardat;
LPC_SSP0->DR = 0xFF;
/* Wait until the Busy bit is cleared */
while ( (LPC_SSP0->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );
dat = LPC_SSP0->DR;
return dat;
}
/*******************************************************
*函数名称:SPI0_writeByte
*函数功能:SPI0写出一个字节数据
*入口参数:unsigned char
*出口参数:无
******************************************************/
void SPI0_writeByte(unsigned char dat)
{
uint8_t Dummy = Dummy;
/* Move on only if NOT busy and TX FIFO not full. */
while ( (LPC_SSP0->SR & (SSPSR_TNF|SSPSR_BSY)) != SSPSR_TNF );
LPC_SSP0->DR = dat;
while ( (LPC_SSP0->SR & (SSPSR_BSY|SSPSR_RNE)) != SSPSR_RNE );
/* Whenever a byte is written, MISO FIFO counter increments, Clear FIFO
on MISO. Otherwise, when SSP0Receive() is called, previous data byte
is left in the FIFO. */
Dummy = LPC_SSP0->DR;
}
/*******************************************************
*函数名称:WORDLIB_read_ASCII_8x16
*函数功能:读取字库芯片中一个ASCII字符的8x16点阵编码
*入口参数:ASCIICode,ASCII码;buffer,点阵编码的缓冲区;
buffer必须大于等于16个字节。
*出口参数:无
******************************************************/
void WORDLIB_read_ASCII_8x16(unsigned int ASCIICode,unsigned char *buffer)
{
unsigned long BaseAddr = 0x3b7c0; //8x16点阵的起始地址,见手册《GT23L32S4W用户手册》
unsigned long WordAddr = 0; //ASCII字符点阵在芯片中的字节地址
unsigned long ASCIICodeTemp =0;
const unsigned int BUFSIZE = 16; //缓冲区的大小
unsigned chari;
ASCIICodeTemp=ASCIICode&0xff;
//计算地址,见手册《GT20L16S1Y用户手册》
if((ASCIICodeTemp >= 0x20) && (ASCIICodeTemp <= 0x7E))
{
WordAddr = (ASCIICodeTemp - 0x20) * BUFSIZE + BaseAddr;
}
else //空格0x20
{
ASCIICodeTemp=0x20;
WordAddr = (ASCIICodeTemp - 0x20) * BUFSIZE + BaseAddr;
}
SPI0_clear(); //清除SPI总线
u8Delay_42Ns(2);
//开始快速读取点阵数据:Read Data Bytes At Higher Speed
SPI0_CS_OFF();
u8Delay_42Ns(2);
SPI0_writeByte(0x0b);//发送命令字:0x0B
SPI0_writeByte((WordAddr >> 16) & 0xff); //从高到低,依次发送三个字节的地址
SPI0_writeByte((WordAddr >> 8) & 0xff);
SPI0_writeByte(WordAddr & 0xff);
SPI0_writeByte(0xff); //最后发送一个字节的假数据 Dummy Byte
for(i = 0; i < BUFSIZE; i++)
{
*(buffer + i) = SPI0_readByte(); //读取字库芯片发送过来的点阵编码
}
SPI0_CS_ON(); //CS置高,结束本次操作
u8Delay_42Ns(2);
SPI0_clear(); //清除SPI总线
u8Delay_42Ns(2);
}
/*******************************************************
*函数名称:WORDLIB_read_GB_16x16
*函数功能:读取字库芯片中一个国标汉字的16x16点阵编码
*入口参数:GBCode,汉字内码;buffer,点阵编码的缓冲区;
buffer必须大于等于32个字节。
*出口参数:无
******************************************************/
void WORDLIB_read_GB_16x16(unsigned int GBCode, unsigned char*buffer)
{
unsigned long BaseAddr = 0; //16x16点阵的起始地址,见手册《GT20L16S1Y用户手册》
unsigned long GBCode_MSB = 0; //汉字内码的高八位
unsigned long GBCode_LSB = 0; //汉字内码的低八位
unsigned long WordAddr = 0; //汉字或者ASCII字符点阵在芯片中的字节地址
const unsigned char BUFSIZE = 32; //缓冲区的大小
unsigned char i;
GBCode_MSB = (GBCode >> 8) & 0xFF; //汉字内码的高八位
GBCode_LSB = GBCode & 0xFF; //汉字内码的低八位
//计算地址,见手册《GT20L16S1Y用户手册》
if(GBCode_MSB == 0xA9&& GBCode_LSB >= 0xA1)
{
WordAddr = (282+(GBCode_LSB-0xA1))*32+ BaseAddr;
}
else if(GBCode_MSB >= 0xA1&&GBCode_MSB <= 0xA3&& GBCode_LSB >= 0xA1)
{
WordAddr = (GBCode_MSB-0xA1)*94+(GBCode_LSB-0xA1)*32+ BaseAddr;
}
else if(GBCode_MSB >= 0xB0&&GBCode_MSB <= 0xF7&& GBCode_LSB >= 0xA1)
{
WordAddr = ((GBCode_MSB-0xB0)*94+(GBCode_LSB-0xA1)+846)*32+ BaseAddr;
}
SPI0_clear(); //清除SPI总线
u8Delay_42Ns(2);
//开始快速读取点阵数据:Read Data Bytes At Higher Speed
SPI0_CS_OFF();
u8Delay_42Ns(2);
SPI0_writeByte(0x0B); //发送命令字:0x0B
SPI0_writeByte((WordAddr >> 16) & 0xff); //从高到低,依次发送三个字节的地址
SPI0_writeByte((WordAddr >> 8) & 0xff);
SPI0_writeByte(WordAddr & 0xff);
SPI0_writeByte(0xff); //最后发送一个字节的假数据 Dummy Byte
for(i = 0; i < BUFSIZE; i++)
{
*(buffer + i) = SPI0_readByte(); //读取字库芯片发送过来的点阵编码
}
SPI0_CS_ON(); //CS置高,结束本次操作
u8Delay_42Ns(2);
SPI0_clear(); //清除SPI总线
u8Delay_42Ns(2);
}
void diy_zi_ku(const unsigned char *pucConstZiKu,unsigned char *pucResultBuffer,unsigned char ucZiKuCnt)
{
unsigned char i;
for(i=0;i<ucZiKuCnt;i++)
{
pucResultBuffer=pucConstZiKu;
}
}
页:
[1]