本帖最后由 mftang2016 于 2017-11-12 23:10 编辑
Jlx12864g_drive.h
[mw_shl_code=c,true]/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __JLX12864G_DRIVE_H
#define __JLX12864G_DRIVE_H
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief LCD Control Interface pins (shield D4)
*/
#define LCD_RST_PIN GPIO_PIN_8
#define LCD_RST_GPIO_PORT GPIOA
#define LCD_RST_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
#define LCD_RST_GPIO_CLK_DISABLE() __HAL_RCC_GPIOA_CLK_DISABLE()
#define LCD_SCL_PIN GPIO_PIN_8
#define LCD_SDA_PIN GPIO_PIN_9
#define LCD_SCL_SDA_GPIO_PORT GPIOB
#define LCD_SCL_SDA_GPIO_CLK_ENABLE() __HAL_RCC_GPIOB_CLK_ENABLE()
#define LCD_SCL_SDA_GPIO_CLK_DISABLE() __HAL_RCC_GPIOB_CLK_DISABLE()
#define LCD_SCL_LOW() HAL_GPIO_WritePin(LCD_SCL_SDA_GPIO_PORT, LCD_SCL_PIN, GPIO_PIN_RESET)
#define LCD_SCL_HIGH() HAL_GPIO_WritePin(LCD_SCL_SDA_GPIO_PORT, LCD_SCL_PIN, GPIO_PIN_SET)
#define LCD_SDA_LOW() HAL_GPIO_WritePin(LCD_SCL_SDA_GPIO_PORT, LCD_SDA_PIN, GPIO_PIN_RESET)
#define LCD_SDA_HIGH() HAL_GPIO_WritePin(LCD_SCL_SDA_GPIO_PORT, LCD_SDA_PIN, GPIO_PIN_SET)
/**
* @brief LCD Control Lines management
*/
#define LCD_RST_LOW() HAL_GPIO_WritePin(LCD_RST_GPIO_PORT, LCD_RST_PIN, GPIO_PIN_RESET)
#define LCD_RST_HIGH() HAL_GPIO_WritePin(LCD_RST_GPIO_PORT, LCD_RST_PIN, GPIO_PIN_SET)
/**
* @brief ZK Control Interface pins (shield D4)
*/
#define LCD_ZK_CS_PIN GPIO_PIN_8
#define LCD_ZK_CS_GPIO_PORT GPIOC
#define LCD_ZK_CS_GPIO_CLK_ENABLE() __HAL_RCC_GPIOC_CLK_ENABLE()
#define LCD_ZK_CS_GPIO_CLK_DISABLE() __HAL_RCC_GPIOC_CLK_DISABLE()
#define LCD_ZK_CS_LOW() HAL_GPIO_WritePin(LCD_ZK_CS_GPIO_PORT, LCD_ZK_CS_PIN, GPIO_PIN_RESET)
#define LCD_ZK_CS_HIGH() HAL_GPIO_WritePin(LCD_ZK_CS_GPIO_PORT, LCD_ZK_CS_PIN, GPIO_PIN_SET)
void jlx12864_Init( void );
void jlx12864_Transfer(int Byte);
void jlx12864_GetDataFromROM(uint8_t addrHigh,uint8_t addrMid,uint8_t addrLow,uint8_t *pBuff,uint8_t DataLen );
#ifdef __cplusplus
}
#endif
#endif /* __JLX12864G_DRIVE_H */
/************************ (C) COPYRIGHT mingfei tang *****END OF FILE****/[/mw_shl_code]
Jlx12864g_drive.c
[mw_shl_code=c,true]/**
******************************************************************************
* @file driver/bap/Components/JLX12864/jlx12864g_drive.c
* @author mingfei tang
* @brief Main program body
* @date 2017.11.01
******************************************************************************
* @attention
*
* <h2><center>© COPYRIGHT(c) 2017 by mingfei tang</center></h2>
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#include "stm32l0xx_nucleo.h"
#define JLX12864_SLAVE_ADDRESS 0x7E /* I2C salve address */
static uint8_t jlx12864_IO_WriteByte(uint8_t Data)
{
uint8_t tmp;
/* Send the byte */
SPIx_WriteReadData(&Data,&tmp,1);
return tmp;
}
static void jlx12864_IO_Init(void)
{
GPIO_InitTypeDef gpioinitstruct = {0};
/* LCD_RST_GPIO Periph clock enable */
LCD_RST_GPIO_CLK_ENABLE();
LCD_SCL_SDA_GPIO_CLK_ENABLE();
LCD_ZK_CS_GPIO_CLK_ENABLE();
/* Configure LCD_RST_PIN pin: LCD RST pin */
gpioinitstruct.Pin = LCD_RST_PIN;
gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP;
gpioinitstruct.Pull = GPIO_PULLUP;
gpioinitstruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(LCD_RST_GPIO_PORT, &gpioinitstruct);
gpioinitstruct.Pin = LCD_SDA_PIN|LCD_SCL_PIN;
HAL_GPIO_Init(LCD_SCL_SDA_GPIO_PORT, &gpioinitstruct);
/* Configure LCD_ZK_CS_PIN pin: LCD ZK_CS pin */
gpioinitstruct.Pin = LCD_ZK_CS_PIN;
HAL_GPIO_Init(LCD_ZK_CS_GPIO_PORT, &gpioinitstruct);
}
static void jlx12864delay(uint16_t i)
{
uint16_t j,k;
for(j = 0;j < i; j++)
for( k=0; k<990; k++);
}
static void jlx12864_Start( void )
{
LCD_SCL_HIGH();
LCD_SDA_HIGH();
LCD_SDA_LOW();
}
static void jlx12864_Stop( void )
{
LCD_SCL_HIGH();
LCD_SDA_LOW();
LCD_SDA_HIGH();
}
static void jlx12864_ClearScreen( void )
{
uint8_t i,j;
for( i = 0; i< 10; i++ )
{
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS ); /* SLAVE ADDRESS */
jlx12864_Transfer(0);
jlx12864_Transfer( 0x38);
jlx12864_Transfer( 0x40+i);
jlx12864_Transfer( 0xe0);
jlx12864_Stop();
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS );
jlx12864_Transfer( 0x40);
for( j = 0 ;j < 132; j++)
{
jlx12864_Transfer( 0);
}
jlx12864_Stop();
}
}
static void jlx12864_Printf5x7(uint16_t page,uint16_t column,uint8_t *dp)
{
uint32_t i;
uint8_t page_address;
uint8_t column_address_L,column_address_H;
page=page-1;
page_address = 0x40+page;
column=column+3;
column_address_L =0xe0+(column&0x0f);
column_address_H =0xf0+((column>>4)&0x0f);
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS ); /*select SLAVE ADDRESS*/
jlx12864_Transfer( 0x00);
jlx12864_Transfer( 0x38); /*set fucntion:MX=0,MY=0,PD=0,H1=0,H0=0,*/
jlx12864_Transfer( page_address ); /*set page address */
jlx12864_Transfer( column_address_L);
jlx12864_Transfer( column_address_H);
jlx12864_Stop();
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS );
jlx12864_Transfer( 0x40);
for( i = 0; i< 6; i++ )
{
jlx12864_Transfer( *dp );
dp++;
}
jlx12864_Transfer( 0x00);
jlx12864_Stop();
}
static void display_5x7_string(uint8_t x,uint8_t y,uint8_t *text)
{
uint8_t i= 0;
uint8_t buff[8];
uint32_t fontaddr;
uint8_t addrHigh,addrMid,addrLow ;
while((text
>0x00))
{
if((text>=0x20) &&(text<=0x7e))
{
fontaddr = ( text - 0x20);
fontaddr = (uint32_t)(fontaddr*8);
fontaddr = (uint32_t)(fontaddr+0x3bfc0);
addrHigh = (fontaddr&0xff0000)>>16;
addrMid = (fontaddr&0xff00)>>8;
addrLow = fontaddr&0xff;
jlx12864_GetDataFromROM(addrHigh,addrMid,addrLow,buff,8);
jlx12864_Printf5x7(y,x, buff );
i+=1;
x+=6;
}
else
i++;
}
}
static void jlx12864_Printf16x16or8(uint16_t page,uint16_t column,uint8_t *dp, uint8_t type )
{
uint32_t i,k;
uint8_t page_address;
uint8_t column_address_L,column_address_H;
page=page-1;
page_address = 0x40+page;
column=column+3;
column_address_L =0xe0+(column&0x0f);
column_address_H =0xf0+((column>>4)&0x0f);
for( k = 0; k < 2; k++ )
{
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS );
jlx12864_Transfer( 0x00);
jlx12864_Transfer( 0x38);
jlx12864_Transfer( page_address + k );
jlx12864_Transfer( column_address_L);
jlx12864_Transfer( column_address_H);
jlx12864_Stop();
jlx12864_Start();
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS );
jlx12864_Transfer( 0x40);
for( i = 0; i< type; i++ )
{
jlx12864_Transfer(*dp );
dp++;
}
jlx12864_Stop();
}
}
static void display_GB2312_string(uint8_t y,uint8_t x,uint8_t *text)
{
uint32_t fontaddr=0;
uint8_t i= 0;
uint8_t addrHigh,addrMid,addrLow ;
uint8_t fontbuf[32];
while((text>0x00))
{
if(((text>=0xb0) &&(text<=0xf7))&&(text[i+1]>=0xa1))
{
/*国标简体(GB2312)汉字在晶联讯字库 IC 中的地址由以下公式来计算:*/
/*Address = ((MSB - 0xB0) * 94 + (LSB - 0xA1)+ 846)*32+ BaseAdd;BaseAdd=0*/
/*由于担心 8 位单片机有乘法溢出问题,所以分三部取地址*/
fontaddr = (text- 0xb0)*94;
fontaddr += (text[i+1]-0xa1)+846;
fontaddr = (uint32_t)(fontaddr*32);
addrHigh = (fontaddr&0xff0000)>>16; /*地址的高 8 位,共 24 位*/
addrMid = (fontaddr&0xff00)>>8; /*地址的中 8 位,共 24 位*/
addrLow = fontaddr&0xff; /*地址的低 8 位,共 24 位*/
/*取 32 个字节的数据,存到"fontbuf[32]"*/
jlx12864_GetDataFromROM(addrHigh,addrMid,addrLow,fontbuf,32 );
/*显示汉字到 LCD 上,y 为页地址,x 为列地址,fontbuf[]为数据*/
jlx12864_Printf16x16or8(y,x,fontbuf, 16);
i+=2;
x+=16;
}
else if(((text>=0xa1) &&(text<=0xa3))&&(text[i+1]>=0xa1))
{
/*国标简体(GB2312)15x16 点的字符在晶联讯字库 IC 中的地址由以下公式来计算:*/
/*Address = ((MSB - 0xa1) * 94 + (LSB - 0xA1))*32+ BaseAdd;BaseAdd=0*/
/*由于担心 8 位单片机有乘法溢出问题,所以分三部取地址*/
fontaddr = (text- 0xa1)*94;
fontaddr += (text[i+1]-0xa1);
fontaddr = (uint32_t)(fontaddr*32);
addrHigh = (fontaddr&0xff0000)>>16; /*地址的高 8 位,共 24 位*/
addrMid = (fontaddr&0xff00)>>8; /*地址的中 8 位,共 24 位*/
addrLow = fontaddr&0xff; /*地址的低 8 位,共 24 位*/
/*取 32 个字节的数据,存到"fontbuf[32]"*/
jlx12864_GetDataFromROM(addrHigh,addrMid,addrLow,fontbuf,32 );
/*显示汉字到 LCD 上,y 为页地址,x 为列地址,fontbuf[]为数据*/
jlx12864_Printf16x16or8(y,x,fontbuf, 16);
i+=2;
x+=16;
}
else if((text>=0x20) &&(text<=0x7e))
{
fontaddr = (text- 0x20);
fontaddr = (uint32_t)(fontaddr*16);
fontaddr = (uint32_t)(fontaddr+0x3cf80);
addrHigh = (fontaddr&0xff0000)>>16;
addrMid = (fontaddr&0xff00)>>8;
addrLow = fontaddr&0xff;
jlx12864_GetDataFromROM(addrHigh,addrMid,addrLow,fontbuf,16 );
jlx12864_Printf16x16or8(y,x,fontbuf, 8);
i+=1;
x+=8;
}
else
i++;
}
}
void jlx12864_Init( void )
{
jlx12864_IO_Init();
LCD_RST_LOW();
jlx12864delay( 100 );
LCD_RST_HIGH();
jlx12864_Start(); //开始标志
jlx12864_Transfer( JLX12864_SLAVE_ADDRESS ); //选择 SLAVE ADDRESS
jlx12864_Transfer(0x00); //表示以下传输的字节是指令*/
jlx12864_Transfer(0x3b); //功能设置:MX=1,MY=1,PD=0,H1=1,H0=1,
jlx12864_Transfer(0x03); //软件复位*/
jlx12864_Transfer(0x38); //功能设置:MX=1,MY=1,PD=0,H1=0,H0=0,
jlx12864_Transfer(0x05); //设置 VLCD 的范围:0X05 表示高电压 0X04 表示低电压
jlx12864_Transfer(0x0c); //打开显示
jlx12864_Transfer(0x39); //功能设置:MX=1,MY=1,PD=0,H1=0,H0=1,
jlx12864_Transfer(0x08); //显示配置:DO=0,V=0,(Top/bottom row mode set data order)
jlx12864_Transfer(0x10); //BIAS 设置为:0x10:1/11,0x11:1/10,0x12:1/9
jlx12864_Transfer(0x8f); //对比度设置:最低是 0x80,最高是 0xff,数值越大对比度就越高
jlx12864_Stop(); //结束标志
jlx12864_ClearScreen();
display_GB2312_string(1,1, "adc Value: 12365");
display_5x7_string(1,3, "afafafg1234567");
}
void jlx12864_Transfer(int Byte)
{
int i;
for(i = 0;i < 8; i++)
{
LCD_SCL_LOW();
if( Byte&0x80 )
LCD_SDA_HIGH();
else
LCD_SDA_LOW();
LCD_SCL_HIGH();
LCD_SCL_LOW();
Byte=Byte<<1;
}
LCD_SDA_LOW();
LCD_SCL_HIGH();
LCD_SCL_LOW();
}
void jlx12864_GetDataFromROM( uint8_t addrHigh,uint8_t addrMid,uint8_t addrLow,
uint8_t *pBuff,uint8_t DataLen )
{
uint8_t i;
LCD_ZK_CS_LOW();
jlx12864_IO_WriteByte(0x03);
jlx12864_IO_WriteByte(addrHigh);
jlx12864_IO_WriteByte(addrMid);
jlx12864_IO_WriteByte(addrLow);
for(i = 0; i < DataLen; i++ )
{
*(pBuff+i) =jlx12864_IO_WriteByte( 0xff );
}
LCD_ZK_CS_HIGH();
}
/************************ (C) COPYRIGHT by mingfei tang *******END OF FILE****/
[/mw_shl_code]
一周热门 更多>