[mw_shl_code=c,true]=====================main.c===================
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"
#include "misc.h"
#include "lcm.h"
int main(void)
{
LCM_Init();
LCM_User_CN(0, 0, 0);
LCM_User_CN(1, 0, 1);
LCM_User_CN(2, 0, 0);
while (1)
;
}
=====================LCM.h====================
#ifndef _LCM_H_
#define _LCM_H_
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#define DB0 GPIO_Pin_3
#define DB1 GPIO_Pin_4
#define DB2 GPIO_Pin_5
#define DB3 GPIO_Pin_6
#define DB4 GPIO_Pin_7
#define DB5 GPIO_Pin_8
#define DB6 GPIO_Pin_9
#define DB7 GPIO_Pin_10
#define DATA_PORT GPIOA
#define CS1 GPIO_Pin_5
#define CS2 GPIO_Pin_6
#define DI GPIO_Pin_8
#define E GPIO_Pin_9
#define CONTROL_PORT GPIOB
#define RW GPIO_Pin_0
#define RST GPIO_Pin_1
#define READ_PORT GPIOA
#define DI_H GPIO_SetBits(CONTROL_PORT,DI)
#define DI_L GPIO_ResetBits(CONTROL_PORT,DI)
#define E_H GPIO_SetBits(CONTROL_PORT,E)
#define E_L GPIO_ResetBits(CONTROL_PORT,E)
#define CS1_H GPIO_SetBits(CONTROL_PORT,CS1)
#define CS1_L GPIO_ResetBits(CONTROL_PORT,CS1)
#define CS2_H GPIO_SetBits(CONTROL_PORT,CS2)
#define CS2_L GPIO_ResetBits(CONTROL_PORT,CS2)
#define RW_H GPIO_SetBits(READ_PORT,RW)
#define RW_L GPIO_ResetBits(READ_PORT,RW)
#define RST_H GPIO_SetBits(READ_PORT,RST)
#define RST_L GPIO_ResetBits(READ_PORT,RST)
#define WRITE_DATA(DDAT) GPIO_Write(DATA_PORT,DDAT)
#define READ_DATA GPIO_ReadInputData(DATA_PORT)
void LCM_Delay(unsigned int uiTime);
void LCM_Busy(void);
void LCM_Reset(void);
void LCM_Clear(unsigned char ucData);
void LCM_Write(unsigned char ucDAT_or_CMD, unsigned char ucLeft_or_Right,unsigned char ucData);
void LCM_Write_Screan(char x, char y, unsigned char ucData);
void LCM_Switch(unsigned char ucSwitch);
void LCM_Init(void);
void LCM_User_CN(unsigned char ucRow, unsigned char ucColumn,unsigned char ucChn);
#endif
======================LCM.c==============================
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_flash.h"
#include "misc.h"
#include "lcm.h"
unsigned char HZ[] =
{ 0x40, 0x40, 0x40, 0x40, 0x20, 0x42, 0x20, 0x42, 0x50, 0x4A, 0x48, 0x72, 0x44,
0x42, 0xC3, 0x7F, 0x44, 0x42, 0x48, 0x62, 0x50, 0x5A, 0x50, 0x42, 0x20,
0x42, 0x60, 0x40, 0x20, 0x40, 0x00, 0x00,/*"踢",0*/
0x00, 0x00, 0xFE, 0xFF, 0x22, 0x02, 0x5A, 0x04, 0x86, 0x43, 0x10, 0x48,
0x94, 0x24, 0x74, 0x22, 0x94, 0x15, 0x1F, 0x09, 0x34, 0x15, 0x54, 0x23,
0x94, 0x60, 0x94, 0xC0, 0x10, 0x40, 0x00, 0x00, /*"鍬",1*/
};
enum LCM_CMD_DAT
{
cmd, dat
} LCM_CMD,LCM_DAT;
enum LCM_LEFT_RIGHT
{
left, right
} LCM_LEFT,LCM_RIGHT;
enum LCM_OFF_ON
{
off, on
} LCM_OFF, LCM_ON;
void LCM_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = DB0 | DB1 | DB2 | DB3 | DB4 | DB5 | DB6 | DB7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(DATA_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = DI | E | CS1 | CS2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(CONTROL_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RW | RST;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(READ_PORT, &GPIO_InitStructure);
}
void LCM_Delay(unsigned int uiTime)
{
unsigned short int i, j;
for (i = 0; i < uiTime; i++)
for (j = 0; j < 10; j++)
;
}
void LCM_Reset(void)
{
RST_L;
LCM_Delay(500);
RST_H;
LCM_Delay(500);
}
void LCM_Busy(void)
{
DI_L; //di=0;
RW_H; //rw=1;
LCM_Delay(100);
}
void LCM_Write(unsigned char ucDAT_or_CMD, unsigned char ucLeft_or_Right,
unsigned char ucData)
{
u16 temp;
if (ucDAT_or_CMD == LCM_DAT) //dat
{
if (ucLeft_or_Right == LCM_LEFT) //left
{
CS1_H; //cs1=1;
CS2_L; //cs2=0;
}
else //right
{
CS1_L; //cs1=0;
CS2_H; //cs2=1;
}
LCM_Delay(10);
LCM_Busy();
DI_H; //di=1;
RW_L; //rw=0;
}
else //cmd
{
CS1_H; //cs1=1;
CS2_H; //cs2=1;
LCM_Delay(10);
LCM_Busy();
RW_L; //rw=0;
DI_L; //di=0;
}
LCM_Delay(10);
temp = ucData;
temp = temp << 8;
WRITE_DATA(temp);
//lcm=a;
LCM_Delay(10);
E_H; //e=1;
LCM_Delay(10);
E_L; //e=0;
LCM_Delay(10);
}
void LCM_Write_Screan(char x, char y, unsigned char ucData)
{
if (x >= 64)
{
x = x - 64;
x = x + 0x40;
y = y + 0xb8;
LCM_Write(LCM_CMD, 0, x);
LCM_Write(LCM_CMD, 0, y);
LCM_Write(LCM_DAT, LCM_RIGHT, ucData);
}
else
{
x = x + 0x40;
y = y + 0xb8;
LCM_Write(LCM_CMD, 0, x);
LCM_Write(LCM_CMD, 0, y);
LCM_Write(LCM_DAT, LCM_LEFT, ucData);
}
}
void LCM_Clear(unsigned char ucData)
{
unsigned char x, y;
for (y = 0; y < 8; y++)
{
for (x = 0; x < 128; x++)
{
LCM_Write_Screan(x, y, ucData);
}
}
}
void LCM_Switch(unsigned char ucSwitch)
{
ucSwitch = ucSwitch + 0x3e;
LCM_Write(LCM_CMD, 0, ucSwitch);
}
void LCM_Init(void)
{
LCM_CMD = cmd;
LCM_DAT = dat;
LCM_LEFT = left;
LCM_RIGHT = right;
LCM_OFF = off;
LCM_ON = on;
LCM_GPIO_Configuration();
LCM_Reset();
LCM_Switch(LCM_OFF);
LCM_Clear(0);
LCM_Switch(LCM_ON);
LCM_Write(LCM_CMD, 0, 0xc0);
}
void LCM_User_CN(unsigned char ucRow, unsigned char ucColumn,
unsigned char ucChn)
{
int i, dx;
for (i = 0; i < 16; i++)
{
dx = HZ[2 * i + ucChn * 32];
LCM_Write_Screan(ucRow * 16 + i, ucColumn, dx);
dx = HZ[(2 * i + 1) + ucChn * 32];
LCM_Write_Screan(ucRow * 16 + i, ucColumn + 1, dx);
}
}
[/mw_shl_code]