STM32——人机交互界面(延迟法)

/***********************************************/
#include “stdio.h”
#include “sting.h”
#include “stm32f10x.h”
//RW=SID
//0 = 写入,1 = 读出
#define DAT_128 GPIO_Pin_9
#define SID_H SetBits(GPIOB,DAT_128)
#define SID_L ResetBits(GPIOB,DAT_128)
//CS = RS
//0 = 指令,1 = 数据
#define CS_128 GPIO_Pin_8
#define CS_H SetBits(GPIOB,CS_128)
#define CS_L ResetBits(GPIOB,CS_128)
//E=SCLK
//串行时钟,使能信号
#define SCLK_128 GPIO_Pin_10
#define SCLK_H SetBits(GPIOB,SCLK_128)
#define SCLK_L ResetBits(GPIOB,SCLK_128)

#define x1 0x80
#define x2 0x88
#define y 0x80
#define comm 0
#define dat 1
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStrue.GPIO_Pin=CS_128 | DAT_128 | SCLK_128;
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_50MHz; //设置最大输出速度
GPIO_Init(GPIOB,&GPIO_InitStrue); //GPIO端口初始化设置

}

void Delay_ms(u16 time)

{
u16 i = 0;
while(time–)
{
i=12000;
while(i–);
}
}

void SendByte (u8 bbyte)
{
u8 i,t;
for (i==0;i<8;i++)
{
if(bbyte & 0x80)//取出最高位
SID_H; // 读数据
else
SID_L; // 写数据
SCLK_H;//必须高电平,时钟上升沿有效
t = 0x10;//t的值根据系统时钟调整到合适的值
while(t–);
SCLK_L;//为下一次跳变做准备
bbyte<<=1;//取出下一位
}
}

void WriteChar(u8 start,u8 ddata)
{
u8 start_data,Hdata,Ldata;
if (start == 0)
start_data = 0xf8;//指令
else
start_data = 0xfa;//数据
Hdata = ddata&0xf0;//取出高四位
Ldata = (ddata<<4) & 0xf0;//取出低四位
Delay_ms(10);
SendByte(Hdata); //发送高四位
Delay_ms(10);
SendByte(Ldata);// 发送低四位
Delay_ms(10);
}

void Lcd_Init(void)
{
Delay_ms(50);
CS_H;//必须高电平才能连接液晶模块
WriteChar(0,0x30);//基本指令集
WriteChar(0,0x0c);
//0 0 0 0 0 0 D C B
//D = 1 ,显示开
//C = 1 ,光标开
//B = 1 ,闪烁开
WriteChar(0,0x01);//清屏
WriteChar(0,0x02);
WriteChar(0,0x80);
Delay_ms(50);
}

void Clr_Ser(void)
{
WriteChar(0,0x01);
Delay_ms(50);
}

void LCD_Set_XY(u8 X,u8 Y)
{
u8 address;
// 80H 81H 82H … 87H
// 90H 91H 92H … 97H
// 88H 88H 8AH … 8FH
// 98H 99H 9AH … 9FH

switch (X)
{
  case 0 : address = 0x80+Y;
	       break;
  case 1 : address = 0x90+Y;
	       break;
  case 2 : address = 0x88+Y;
	       break;
  case 3 : address = 0x98+Y;
	       break;
  default :
	       address = 0x80+Y;
	       break;
}
Write_Char(0,address);

}

扫描二维码关注公众号,回复: 12449172 查看本文章

void LEDWriteString (u8 X, u8 Y ,uc8 *s)
{
LED_Set_XY(X,Y);//选择位置
while (*s)//取出数据
{
WriteChar(1,*s);
s++;
Delay_ms(50);
}
}

void Dispaly_Img(u8 constimg)
{
u8 i,j;
for (i = 0; i < 32; i++)//32行
for(j = 0; j < 8; j++)//8块(64列)
{
WriteChar(comm,0x34);//扩展指令
Delay_ms(10);
WriteChar(comm, y + j);
Delay_ms(10);
WriteChar(comm, x1 + i);
Delay_ms(10);
WriteChar(comm,0x30);//基本指令
Delay_ms(10);
WriteChar(dat,img[j
16+i2]);
Delay_ms(10);
WriteChar(dat,img[j
16+i2+1]);
Delay_ms(10);
}
for (i = 32; i < 64; i++)//另外32行
for(j = 0; j < 8; j++)//8块
{
WriteChar(comm,0x34);
Delay_ms(10);
WriteChar(comm, y + j - 32);
Delay_ms(10);
WriteChar(comm, x2 + i);
Delay_ms(10);
WriteChar(comm,0x30);
Delay_ms(10);
WriteChar(dat,img[j
16+i2]);
Delay_ms(10);
WriteChar(dat,img[j
16+i*2+1]);
Delay_ms(10);
}
Delay_ms(10);
WriteChar(comm,0x36);
}

u8 BMP[] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xD0,0x00,0x00,0x00,0x00,0x0F,0x00,
0x00,0x90,0x07,0x00,0x00,0xC0,0x08,0x00,0x00,0x08,0x0F,0x00,0x00,0x70,0x10,0x00,0x00,0x08,0x1E,0x00,0x00,0x38,0x10,0x00,0x00,0x08,0x7D,0x00,0x00,0x1E,0x10,0x00,
0x00,0x08,0xFA,0x00,0x00,0x0F,0x30,0x00,0x00,0x04,0xF4,0x03,0xC0,0x07,0x20,0x00,0x00,0x04,0xE8,0x07,0xE0,0x13,0x30,0x00,0x00,0x04,0xFA,0xFF,0xFF,0x0B,0x20,0x00,
0x00,0x44,0xD4,0xFF,0xFF,0x27,0x20,0x00,0x00,0x84,0xE8,0xFF,0xFF,0x9F,0x30,0x00,0x00,0x04,0xF1,0xFF,0xFF,0x5F,0x30,0x00,0x00,0x04,0xFA,0x3F,0xFE,0x3F,0x10,0x00,
0x00,0x80,0xFC,0x1F,0xFC,0x7F,0x10,0x00,0x00,0x08,0xFE,0x0F,0xF8,0x7F,0x10,0x00,0x00,0x08,0xFF,0x07,0xF0,0xFF,0x10,0x00,0x00,0x88,0xFF,0x03,0xE0,0xFF,0x11,0x00,
0x00,0x88,0x0F,0x03,0xE0,0xF0,0x09,0x00,0x00,0xD0,0x03,0x00,0x00,0xC0,0x0B,0x00,0x00,0xD0,0x01,0x00,0x00,0x80,0x07,0x00,0x00,0xE0,0xE1,0x09,0x80,0x07,0x07,0x00,
0x00,0xE0,0xF0,0x03,0xC0,0x0F,0x07,0x00,0x00,0xE0,0xF8,0x17,0xE0,0x1F,0x07,0x00,0x00,0xE0,0xF8,0x07,0xE0,0x1F,0x07,0x00,0x00,0xE0,0xF8,0x07,0xE0,0x1F,0x07,0x00,
0x00,0xE0,0xF8,0x07,0xE0,0x1F,0x07,0x00,0x00,0xE0,0xF1,0x03,0xC0,0x8F,0x07,0x00,0x00,0xE0,0xE1,0x91,0x11,0xC3,0x07,0x00,0x00,0xE0,0x03,0x00,0x20,0xE0,0x07,0x00,
0x00,0xC0,0x0F,0x00,0x00,0xF0,0x03,0x00,0x00,0xC0,0x07,0x00,0x00,0x00,0x02,0x00,0x00,0x40,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x40,0x00,0x00,0x00,0x00,0x02,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x20,0x00,0x00,
0x00,0x00,0x08,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x10,0x00,0x00,0x0C,0x00,0x00,0x00,0x00,0xC0,0x00,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x70,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};

int main(void)
{
u32 i;
u8 Table[] = “我爱你中国!”;
GPIO_Config();
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Lcd_Init();
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Clr_Ser();
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
Delay_ms(5000);
WrietChar(0,0x30);
WrietChar(0,0x01);
Delay_ms(50);
WrietChar(0,0x80);
Delay_ms(50);
LEDWriteString(0,0,Table);
Delay_ms(50);
LEDWriteString(1,0,Table);
Delay_ms(50);
LEDWriteString(2,0,Table);
Delay_ms(50);
LEDWriteString(3,0,Table);
Delay_ms(50);
i = 200;
while(i–)
Delay_ms(50);
Clr_Ser();
Delay_ms(50);
Dispaly_Img(BMP);
i = 200;
Delay_ms(50);
while(i–)
WriteChar(0,0x30);
Clr_Ser();
Delay_ms(50);
while(1);
}

猜你喜欢

转载自blog.csdn.net/News53231323/article/details/113506893
今日推荐