打印
[应用相关]

单片机STM32的LCD12864显示(1)

[复制链接]
1132|16
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
初级工程渣|  楼主 | 2021-7-30 22:29 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "includes.h"
#include "12864.h"

unsigned char table[]="重庆大学电子竞赛";

/*************************************************************/
/*************LCD显示函数************************************/

/*******ns级别延时********************************************/
void Delay_nus(unsigned int nCount)
{
unsigned int j;
while(nCount--)
{
    j=8;
    while(j--);
}
}
/*******ms级别延时********************************************/
void Delay_mus(unsigned int nCount)
{
    while(nCount--)
    Delay_nus(1100);
}
/*******转接函数********************************************/
void data_change(unsigned char u){
    if(u&0x01)GPIO_SetBits(GPIOC, GPIO_Pin_0); else GPIO_ResetBits(GPIOC, GPIO_Pin_0);
    if(u&0x02)GPIO_SetBits(GPIOC, GPIO_Pin_1); else GPIO_ResetBits(GPIOC, GPIO_Pin_1);
    if(u&0x04)GPIO_SetBits(GPIOC, GPIO_Pin_2); else GPIO_ResetBits(GPIOC, GPIO_Pin_2);
    if(u&0x08)GPIO_SetBits(GPIOC, GPIO_Pin_3); else GPIO_ResetBits(GPIOC, GPIO_Pin_3);
    if(u&0x10)GPIO_SetBits(GPIOC, GPIO_Pin_4); else GPIO_ResetBits(GPIOC, GPIO_Pin_4);
    if(u&0x20)GPIO_SetBits(GPIOC, GPIO_Pin_5); else GPIO_ResetBits(GPIOC, GPIO_Pin_5);
    if(u&0x40)GPIO_SetBits(GPIOC, GPIO_Pin_6); else GPIO_ResetBits(GPIOC, GPIO_Pin_6);
    if(u&0x80)GPIO_SetBits(GPIOC, GPIO_Pin_7); else GPIO_ResetBits(GPIOC, GPIO_Pin_7);
}

使用特权

评论回复
沙发
初级工程渣|  楼主 | 2021-7-30 22:31 | 只看该作者
/**************数据口浮空输入************************************/
void DDRC_IN(){
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
      |GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

使用特权

评论回复
板凳
初级工程渣|  楼主 | 2021-7-30 22:32 | 只看该作者
/**************数据口推挽输出************************************/
void DDRC_OUT(){
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
      |GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

使用特权

评论回复
地板
初级工程渣|  楼主 | 2021-7-30 22:34 | 只看该作者
/*******读忙态函数********************************************/
void check_busy(){
unsigned int busy;
  DDRC_IN();
  RS_CLR;
  RW_SET;
  EN_SET;
  //busy=GPIO_ReadInputData(GPIOC) & 0x0080;
  while(GPIO_ReadInputData(GPIOC)&0x0080);
  EN_CLR;
  DDRC_OUT();
}

使用特权

评论回复
5
初级工程渣|  楼主 | 2021-7-30 22:34 | 只看该作者
/*******写命令函数********************************************/
void write_12864com(unsigned char com)
{  check_busy();
   RW_CLR;
   RS_CLR;
   Delay_nus(500);
   //DATA_IO=com;
   data_change(com);
   EN_SET;
   Delay_nus(1000);
   EN_CLR;
   Delay_nus(1000);
}

使用特权

评论回复
6
初级工程渣|  楼主 | 2021-7-30 22:35 | 只看该作者
/*******写数据函数********************************************/
void write_12864dat(unsigned char dat)
{  check_busy();
   RW_CLR;
   RS_SET;
   Delay_nus(500);
   //DATA_IO=dat;
   data_change(dat);
   EN_SET;
   Delay_nus(1000);
   EN_CLR;
   Delay_nus(1000);
}

使用特权

评论回复
7
初级工程渣|  楼主 | 2021-7-30 22:36 | 只看该作者
/****读数据函数*************************************************/
unsigned char u8_Lcd12864ReadByte_f( void )
{
    unsigned char byReturnValue ;
    check_busy();
    DDRC_IN();
    RS_SET;
    RW_SET;
    EN_CLR;
    Delay_nus(10);
    EN_SET;
    byReturnValue=(unsigned char)(GPIO_ReadInputData(GPIOC)&0x0080);
    EN_CLR;
    DDRC_OUT();

    return byReturnValue ;   
}

使用特权

评论回复
8
初级工程渣|  楼主 | 2021-7-30 22:39 | 只看该作者
/*******12864初始化函数********************************************/
void init12864lcd(void)
{
   Delay_mus(500);
   write_12864com(0x30);
   Delay_nus(500);
   write_12864com(0x30);
   Delay_nus(500);
   write_12864com(0x0c);
   Delay_nus(500);
   write_12864com(0x01);
   Delay_nus(25);
  // write_12864com(0x06);
//  Delay_nus(2500);
  // write_12864com(0x0c);
   Delay_nus(500);
}

使用特权

评论回复
9
初级工程渣|  楼主 | 2021-7-30 22:41 | 只看该作者
/*******显示函数********************************************/
void display(void)
{
   unsigned char i;
    write_12864com(0x80);
   for(i=0;i<14;i++)
    {
       write_12864dat(table[i]);
       Delay_mus(50);
      }
}

使用特权

评论回复
10
初级工程渣|  楼主 | 2021-7-30 22:48 | 只看该作者
/****清除显示*************************************************/
void Clean_12864_GDRAM(void)
{
    unsigned char x,y;
    write_12864com(0x34);
    write_12864com(0x36);        //需要两次,本次设置扩展指令集。
    for (y=0;y<32;y++)
    {
        write_12864com(0x80+y);  //设置y=1000+00xx,y+1则往下一行
        write_12864com(0x80);        //设置x=1000 0000
        for (x=0;x<16;x++)
        {
            write_12864dat(0x00);   //高字节数据
            write_12864dat(0x00);        //低字节数据
        }
    }
}

使用特权

评论回复
11
初级工程渣|  楼主 | 2021-7-30 22:50 | 只看该作者
/****画点函数1*************************************************/
void v_Lcd12864DrawPoint_f(unsigned char x,unsigned char y,unsigned char Color )
{
    unsigned char Y,Row , Tier , Tier_bit    ;
    unsigned char  ReadOldH, ReadOldL  ;
    write_12864com(0x34);
    Y=y;
    Tier=x>>4;   
    Tier_bit=x&0x0f;
    if(Y<32)
    {
        Row=Y;
    }
    else
    {
        Row=Y-32;
        Tier+=8;
    }

使用特权

评论回复
12
初级工程渣|  楼主 | 2021-7-30 22:58 | 只看该作者
/****画点函数1*************************************************/
void v_Lcd12864DrawPoint_f(unsigned char x,unsigned char y,unsigned char Color )
{
    unsigned char Y,Row , Tier , Tier_bit    ;
    unsigned char  ReadOldH, ReadOldL  ;
    write_12864com(0x34);
    Y=y;
    Tier=x>>4;   
    Tier_bit=x&0x0f;
    if(Y<32)
    {
        Row=Y;
    }
    else
    {
        Row=Y-32;
        Tier+=8;
    }
    write_12864com( Row+0x80);
    write_12864com( Tier+0x80);
    u8_Lcd12864ReadByte_f();
    ReadOldH=u8_Lcd12864ReadByte_f();
    ReadOldL=u8_Lcd12864ReadByte_f();
    write_12864com(Row+0x80);
    write_12864com(Tier+0x80);
    if(Tier_bit<8)
    {
        switch(Color)
        {
            case 0 : ReadOldH&=(~(0x01<<(7-Tier_bit))); break;
            case 1 : ReadOldH|=(0x01<<(7-Tier_bit));  break;
            case 2 : ReadOldH^=(0x01<<(7-Tier_bit)); break;
            default : break ;   
        }
        write_12864dat( ReadOldH ) ;
        write_12864dat( ReadOldL ) ;
    }
    else
    {
        switch(Color)
        {
            case 0 : ReadOldL &= (~( 0x01 << ( 15 - Tier_bit ))) ;  break ;
            case 1 : ReadOldL |= ( 0x01 << ( 15 - Tier_bit ))    ;  break ;
            case 2 : ReadOldL ^= ( 0x01 << ( 15 - Tier_bit ))  ;  break ;
            default : break ;
        }
        write_12864dat( ReadOldH ) ;
        write_12864dat( ReadOldL ) ;
    }
   
    write_12864com(0x36);
    write_12864com(0x30);
}

使用特权

评论回复
13
初级工程渣|  楼主 | 2021-7-30 23:00 | 只看该作者
/****画点函数2*************************************************/
void LCD_set_dot(unsigned char px,unsigned char py) {
unsigned char x,y,x_byte,x_bit,y_byte,y_bit,tmph,tmpl;
  x=px&0x7f;
  y=py&0x3f;
  x_byte=x/16;
  x_bit=x&0x0f;
  y_byte=y/32;
  y_bit=y&0x1f;
  write_12864com(0x36);

  write_12864com(0x80+y_bit);
  write_12864com(0x80+x_byte+8*y_byte);
  
  u8_Lcd12864ReadByte_f();
  //Delay(2);
  tmph=u8_Lcd12864ReadByte_f();
  tmpl=u8_Lcd12864ReadByte_f();
  //Delay(20);
  write_12864com(0x80+y_bit);
  write_12864com(0x80+x_byte+8*y_byte);
  //Delay(20);
  if(x_bit<8)
{ write_12864dat(tmph|(0x01<<(7-x_bit)));
           write_12864dat(tmpl);
}
else
{ write_12864dat(tmph);
          write_12864dat(tmpl|(0x01<<(15-x_bit)));
}
// write_12864com(0x36);
write_12864com(0x30);
}

使用特权

评论回复
14
初级工程渣|  楼主 | 2021-7-30 23:02 | 只看该作者
/*******画横线函数*******************************************/
void draw_Rline(unsigned char px,unsigned char py,unsigned char n,unsigned char xu){
      unsigned char i,j,x,y,x_byte,x_bit,y_byte,y_bit;
      write_12864com(0x36);
          x=px&0x7f;                           //可选择起始点,长度,虚线或者实线
          y=py&0x3f;
          x_byte=x/16;
          x_bit=x&0x0f;
          y_byte=y/32;
          y_bit=y&0x1f;
  write_12864com(0x80+y_bit);
  write_12864com(0x80+8*y_byte+x_byte);
  for(i=0;i<n;i++){
    if(xu) write_12864dat(0x55);
      else  write_12864dat(0xff);
  }
}

使用特权

评论回复
15
初级工程渣|  楼主 | 2021-7-30 23:03 | 只看该作者
/*******画竖线函数*******************************************/
void draw_Rline(unsigned char px,unsigned char py,unsigned char n,unsigned char xu){
      unsigned char i,j,x,y,x_byte,x_bit,y_byte,y_bit;
      write_12864com(0x36);
          x=px&0x7f;                           //可选择起始点,长度,虚线或者实线
          y=py&0x3f;
          x_byte=x/16;
          x_bit=x&0x0f;
          y_byte=y/32;
          y_bit=y&0x1f;
  write_12864com(0x80+y_bit);
  write_12864com(0x80+8*y_byte+x_byte);
  for(i=0;i<n;i++){
    if(xu) write_12864dat(0x55);
      else  write_12864dat(0xff);
  }
}

使用特权

评论回复
16
初级工程渣|  楼主 | 2021-7-30 23:04 | 只看该作者
/*******画边框函数*******************************************/
void draw_side(){
  unsigned char i,j;
    write_12864com(0x80);
    for(i=0;i<16;i++){write_12864dat(0x55);}   //上边框边框
   
    draw_Rline(0,63,16,1);   //下边框
   
    write_12864com(0x98);
    for(i=0;i<16;i++){write_12864dat(0x55);}   //下边框
  
}   

使用特权

评论回复
17
初级工程渣|  楼主 | 2021-7-30 23:06 | 只看该作者
/*******12864端口配置函数*******************************************/
void LCD12864_Config(void)
{  
  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOC , ENABLE);
  //E口2 3 4 6口做RS PSB RW EN信号线   推挽输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_6;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOE, &GPIO_InitStructure);  
  //C口0~7 做8位数据线  推挽输出
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3
      |GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;  
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
  
  
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

51

主题

553

帖子

0

粉丝