一、前期准备
单片机:STM32F103ZET6
开发环境:MDK5.14
库函数:标准库V3.5
二、实验效果
三、驱动原理
1002521871,验证:呵呵。
四、驱动代码
5110.h
#ifndef __NOKIA_5110_H__
#define __NOKIA_5110_H__
#include "stm32f10x.h"
#include "gpio.h"
#include "delay.h"
#include "nokiafont.h"
#define Nokia_CLK PEout(0)
#define Nokia_DIN PEout(1)
#define Nokia_DC PEout(2)
#define Nokia_CE PEout(3)
#define Nokia_RST PEout(4)
extern void Nokia5110Configuration(void);
extern void Nokia_Clear(void);
extern void Nokia_Write_Chinese_String(uint8_t X, uint8_t Y, uint8_t ch_with, uint8_t num, uint8_t line, uint8_t row);
extern void Nokia_Write_English_String(uint8_t X, uint8_t Y, char *s);
#endif
5110.c
#include "nokia5110.h"
void Nokia_Write_Byte(uint8_t dat, uint8_t cmd)
{
uint8_t i;
Nokia_CE = 0; // 使能LCD
if (cmd == 0)
{
Nokia_DC = 0; // 传送命令
}
else
{
Nokia_DC = 1; // 传送数据
}
for(i = 0; i < 8; i ++)
{
if(dat & 0x80)
{
Nokia_DIN = 1;
}
else
{
Nokia_DIN = 0;
}
Nokia_CLK = 0;
dat = dat << 1;
Nokia_CLK = 1;
}
Nokia_CE = 1;
}
void Nokia_Clear(void)
{
uint16_t i;
Nokia_Write_Byte(0x0c, 0);
Nokia_Write_Byte(0x80, 0);
for (i = 0; i < 504; i ++)
{
Nokia_Write_Byte(0, 1);
}
}
void Nokia_Set_Position(uint8_t X, uint8_t Y)
{
Nokia_Write_Byte(0x40 | Y, 0); // column
Nokia_Write_Byte(0x80 | X, 0); // row
}
void Nokia_Write_Char(uint8_t c)
{
uint8_t line;
c -= 32;
for (line = 0; line < 6; line ++)
{
Nokia_Write_Byte(font6x8[c][line], 1);
}
}
void Nokia_Write_English_String(uint8_t X, uint8_t Y, char *s)
{
Nokia_Set_Position(X, Y);
while (*s)
{
Nokia_Write_Char(*s);
s ++;
}
}
/*-----------------------------------------------------------------------
Nokia_Write_Chinese_String: 在LCD上显示汉字
输入参数:X、Y :显示汉字的起始X、Y坐标;
ch_with :汉字点阵的宽度
num :显示汉字的个数;
line :汉字点阵数组中的起始行数
row :汉字显示的行间距
-----------------------------------------------------------------------*/
void Nokia_Write_Chinese_String(uint8_t X, uint8_t Y, uint8_t ch_with, uint8_t num, uint8_t line, uint8_t row)
{
uint8_t i, n;
Nokia_Set_Position(X, Y); //设置初始位置
for (i = 0; i < num;)
{
for (n = 0; n < ch_with * 2; n ++) //写一个汉字
{
if (n == ch_with) //写汉字的下半部分
{
if (i == 0)
{
Nokia_Set_Position(X, Y + 1);
}
else
{
Nokia_Set_Position((X + (ch_with + row) * i), Y + 1);
}
}
Nokia_Write_Byte(write_chinese[line + i][n], 1);
}
i ++;
Nokia_Set_Position((X + (ch_with + row) * i), Y);
}
}
/*-----------------------------------------------------------------------
Nokia_Draw_Bmp_Pixel : 位图绘制函数
输入参数:X、Y :位图绘制的起始X、Y坐标;
*map :位图点阵数据;
Pix_x :位图像素(长)
Pix_y :位图像素(宽)
-----------------------------------------------------------------------*/
void Nokia_Draw_Bmp_Pixel(uint8_t X,uint8_t Y,uint8_t *map,
uint8_t Pix_x, uint8_t Pix_y)
{
uint16_t i, n;
uint8_t row;
if (Pix_y % 8 == 0)
{
row = Pix_y / 8; //计算位图所占行数
}
else
{
row = Pix_y / 8 + 1;
}
for (n = 0; n < row; n ++)
{
Nokia_Set_Position(X, Y);
for(i = 0; i < Pix_x; i ++)
{
Nokia_Write_Byte(map[i + n * Pix_x], 1);
}
Y ++; //换行
}
}
void Nokia5110Configuration(void)
{
GPIO_InitTypeDef GPIO;
//Enable APB2 Bus
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
//Register IO
GPIO.GPIO_Pin = 0x1F;
GPIO.GPIO_Speed = GPIO_Speed_50MHz;
GPIO.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOE, &GPIO);
Nokia_RST = 0;
DelayUs(1);
Nokia_RST = 1;
Nokia_CE = 0; // 关闭LCD
DelayUs(1);
Nokia_CE = 1; // 使能LCD
DelayUs(1);
Nokia_Write_Byte(0x21, 0); // 使用扩展命令设置LCD模式
Nokia_Write_Byte(0xc8, 0); // 设置偏置电压
Nokia_Write_Byte(0x06, 0); // 温度校正
Nokia_Write_Byte(0x13, 0); // 1:48
Nokia_Write_Byte(0x20, 0); // 使用基本命令
Nokia_Clear(); // 清屏
Nokia_Write_Byte(0x0c, 0); // 设定显示模式,正常显示
Nokia_CE = 0; // 关闭LCD
}
|