lcd.c

来自「c8051f020模块.rar」· C语言 代码 · 共 524 行

C
524
字号
#include <c8051f020.h>
#include<intrins.h>

#define uint8 unsigned char
#define uint16 unsigned int

uint8 j;

uint8 YE[8]={4,5,6,7,0,1,2,3};

//uint8 xdata DrawCur[96];
uint8 xdata DrawCur[128];

sbit SDA=P3^7;
sbit SCK=P3^6;
sbit D_C=P3^5;
sbit CSB=P3^4;



uint8 xdata num[32][32]={
/*--  文字:  0  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x07,0x08,0x10,0x10,0x08,0x07,0x00,0x00,0xF0,0x08,0x04,0x04,0x08,0xF0,0x00},

/*--  文字:  1  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x08,0x08,0x1F,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0xFC,0x04,0x04,0x00,0x00},

/*--  文字:  2  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x0E,0x10,0x10,0x10,0x11,0x0E,0x00,0x00,0x0C,0x14,0x24,0x44,0x84,0x0C,0x00},

/*--  文字:  3  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x0C,0x10,0x11,0x11,0x12,0x0C,0x00,0x00,0x18,0x04,0x04,0x04,0x88,0x70,0x00},

/*--  文字:  4  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x00,0x03,0x04,0x08,0x1F,0x00,0x00,0x00,0xE0,0x20,0x24,0x24,0xFC,0x24,0x00},

/*--  文字:  5  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x1F,0x10,0x11,0x11,0x10,0x10,0x00,0x00,0x98,0x84,0x04,0x04,0x88,0x70,0x00},

/*--  文字:  6  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x07,0x08,0x11,0x11,0x18,0x00,0x00,0x00,0xF0,0x88,0x04,0x04,0x88,0x70,0x00},

/*--  文字:  7  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x1C,0x10,0x10,0x13,0x1C,0x10,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00},

/*--  文字:  8  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x0E,0x11,0x10,0x10,0x11,0x0E,0x00,0x00,0x38,0x44,0x84,0x84,0x44,0x38,0x00},

/*--  文字:  9  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x07,0x08,0x10,0x10,0x08,0x07,0x00,0x00,0x00,0x8C,0x44,0x44,0x88,0xF0,0x00},

/*--  文字:  -  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80},

/*--  文字:  +  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0xF8,0x80,0x80,0x80,0x00},

/*--  文字:  H  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x10,0x1F,0x10,0x00,0x00,0x10,0x1F,0x10,0x04,0xFC,0x84,0x80,0x80,0x84,0xFC,0x04},

/*--  文字:  Z  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x08,0x10,0x10,0x10,0x13,0x1C,0x10,0x00,0x04,0x1C,0x64,0x84,0x04,0x04,0x18,0x00},

/*--  文字:  :  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x6C,0x6C,0x00,0x00,0x00,0x00}};





uint8 xdata a[32][32]={
/*--  文字:  度  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x00,0x3F,0x20,0x24,0x24,0x3F,0xA5,0x65,0x25,0x3F,0x24,0x24,0x24,0x20,0x00,
0x01,0x06,0xF8,0x01,0x01,0x42,0x62,0x54,0x48,0x48,0x54,0x64,0x42,0x03,0x02,0x00},

/*--  文字:  频  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x04,0x3C,0x04,0x04,0xFD,0x24,0x24,0x00,0x4F,0x58,0x6B,0x48,0x48,0x4F,0x40,0x00,
0x22,0xC2,0x04,0x08,0xD0,0x20,0xC2,0x02,0xE4,0x18,0xE0,0x10,0x08,0xE4,0x06,0x00},

/*--  文字:  率  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x30,0x28,0x25,0x20,0x26,0xAC,0x75,0x26,0x24,0x28,0x25,0x28,0x20,0x20,0x00,
0x10,0x50,0x90,0x10,0x10,0x50,0xD0,0x7F,0x50,0xD0,0x30,0x10,0x90,0x50,0x10,0x00},

/*--  文字:  为  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x08,0x08,0x48,0x28,0x38,0x08,0x0F,0xF9,0x08,0x08,0x08,0x08,0x1F,0x08,0x00,
0x00,0x00,0x02,0x04,0x08,0x10,0x60,0x80,0x00,0x88,0x64,0x02,0x04,0xF8,0x00,0x00},

/*--  文字:  相  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x08,0x08,0x0B,0xFF,0x0C,0x0A,0x09,0x00,0x7F,0x44,0x44,0x44,0x44,0x7F,0x00,0x00,
0x20,0xC0,0x00,0xFF,0x00,0x00,0x80,0x00,0xFF,0x42,0x42,0x42,0x42,0xFF,0x00,0x00},

/*--  文字:  位  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x03,0x0C,0x37,0xC0,0x54,0x13,0x90,0x50,0x70,0x10,0x17,0x12,0x10,0x00,0x00,
0x80,0x00,0x00,0xFE,0x04,0x04,0x04,0xE4,0x04,0x0C,0x74,0x84,0x04,0x04,0x04,0x00},

/*--  文字:  差  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x01,0x11,0x15,0x15,0x95,0x75,0x57,0x1D,0x15,0x35,0xD5,0x55,0x15,0x11,0x01,0x00,
0x02,0x04,0x08,0x12,0x62,0xA2,0x22,0x22,0x3E,0x22,0x22,0x22,0x22,0x02,0x02,0x00},

/*--  文字:  第  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x08,0x10,0x20,0xCB,0x6A,0x5A,0x4A,0x5A,0x2F,0xCA,0x6A,0x5A,0x4E,0x40,0x40,0x00,
0x00,0x04,0x04,0xC4,0x48,0x48,0x50,0x60,0xFF,0x40,0x40,0x48,0x44,0x78,0x00,0x00},

/*--  文字:  小  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x00,0x00,0x03,0x0E,0x04,0x00,0xFF,0x00,0x08,0x04,0x03,0x01,0x00,0x00,0x00,
0x20,0x40,0x80,0x00,0x00,0x02,0x01,0xFE,0x00,0x00,0x00,0x00,0x80,0xE0,0x40,0x00},

/*--  文字:  组  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x04,0x1C,0xE5,0x46,0x18,0x00,0x7F,0x44,0x44,0x44,0x44,0x7F,0x00,0x00,0x00,
0x00,0x44,0xC4,0x44,0x48,0x48,0x04,0xFC,0x44,0x44,0x44,0x44,0xFC,0x04,0x04,0x00},

/*--  文字:  制  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x00,0x0A,0xF2,0x52,0x12,0xFF,0x12,0x12,0x12,0x00,0x3F,0x00,0x00,0xFF,0x00,0x00,
0x00,0x00,0xFC,0x80,0x80,0xFF,0x84,0x86,0xFC,0x00,0xF0,0x02,0x01,0xFE,0x00,0x00},

/*--  文字:  作  --*/
/*--  宋体12;  此字体下对应的点阵为:宽x高=16x16   --*/
{0x01,0x02,0x04,0x1F,0xE0,0x44,0x18,0x30,0xDF,0x12,0x12,0x12,0x16,0x12,0x10,0x00,
0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0xFF,0x20,0x20,0x20,0x20,0x60,0x20,0x00}};



void delay(uint8 m)
{
  while(m--);

}



void lcd_SendByte(uint8 x)
{
  uint8 i,n=0x80;   
  for(i=0;i<8;i++)
  {
    if (x&n) 
      SDA=1;
    else 
      SDA=0;
    n=n>>1;
    delay(5);
    SCK=1;
    delay(5);
    SCK=0;
    delay(5);
    SCK=1;    
  }
}


/* 发命令x到主窗口*/
void lcd_MC(uint8 x)
{
  SCK=1;
  CSB=0;
  D_C=0;
  lcd_SendByte(x);
  CSB=1;
}

/*发数据到主窗口*/
void lcd_MD(uint8 x)
{
  SCK=1;
  CSB=0;
  D_C=1;
  lcd_SendByte(x);
  CSB=1;
}

void lcd_clear(void)  //清屏程序
{
  uint8 i,page;
 for (page=0;page<8;page++)
  {
    lcd_MC(0xB0|page); // 设置显示页面
    lcd_MC(0x10); // 设置列地址高位
    lcd_MC(0x01); // 设置列地址低位
 for (i=0;i<128;i++)
    {
      lcd_MD(0x00);//lcd_SD(0x00);
    }
  }
}



void LcdIni(void)  //液晶屏初始化
{ 
  //P3DIR |=0xFF;
  lcd_MC(0xE2);     //液晶屏复位
  lcd_MC(0x2F);  //工作状态设置
  lcd_MC(0x81);  //电压模式设定
  lcd_MC(0x31);  //电压寄存器设定
  lcd_MC(0xA2);  //bias=1/9(11)
  lcd_MC(0xA0);  //ADC,clo0 maps seg0(8)
  lcd_MC(0xC0);  //remapped mode.com0-->comn(15)
  lcd_MC(0x40);  //设定起始行地址
  lcd_MC(0xAF);   //启动LCD
  lcd_clear();// 清屏
}


void Init_PORT(void)
{
  XBR2=0X40;
  P3MDOUT=0XFF;
}


void Init_SYSCLK(void)
{ 
  uint8 i;
  OSCXCN = 0X67;//开始外部22.1184M晶振
  for(i = 0;i < 128;i++); //等待外部起振
  while(!(OSCXCN & 0X80));	//查询标志位
  OSCICN = 0X88;//用外部晶振,并使能始终丢
}


void lcd_hanzi(uint8 x0,uint8 page,uint8 width)  //单个汉字显示
{
  uint8 x,i=0;
  
for (x=x0+1;x<x0+width+1;x++)    //起始列地址x0,页地址page,数据宽度width
 {
  	
    // 一次显示两页
    
    lcd_MC(0xB0|page);             //设置页地址    
    lcd_MC(((x>>4)&0x0f)|0x10);    // 设置列地址高位
    lcd_MC(x&0x0F);                // 设置列地址低位
    lcd_MD(a[j][i]);             // 画上层 
      
    lcd_MC(0xB0|(page-1));         //设置页地址
    lcd_MC(((x>>4)&0x0f)|0x10);    // 设置列地址高位
    lcd_MC(x&0x0F);               //设置列地址低位   
    lcd_MD(a[j][i+width]);              // 画下层 
    i++;
  }
}


void lcd_zifu(uint8 x0,uint8 page,uint8 width)  //单个字符显示
{
  uint8 x,i=0;
  
for (x=x0+1;x<x0+width+1;x++)    //起始列地址x0,页地址page,数据宽度width
 {
  	
    // 一次显示两页
    
    lcd_MC(0xB0|page);             //设置页地址    
    lcd_MC(((x>>4)&0x0f)|0x10);    // 设置列地址高位
    lcd_MC(x&0x0F);                // 设置列地址低位
    lcd_MD(num[j][i]);             // 画上层 
      
    lcd_MC(0xB0|(page-1));         //设置页地址
    lcd_MC(((x>>4)&0x0f)|0x10);    // 设置列地址高位
    lcd_MC(x&0x0F);               //设置列地址低位   
    lcd_MD(num[j][i+width]);              // 画下层 
    i++;
  }
}


void lcd_hanzichuan(uint8 s,uint8 y,uint8 k)   //一页汉字显示
{
  uint8 i=0;
  for(i=0;i<1;i++)
    {  lcd_hanzi((s+i*k),y,k); j++;   }
}


void lcd_zifuchuan(uint8 s,uint8 y,uint8 k)   //一页字符显示
{
  uint8 i=0;
  for(i=0;i<1;i++)
    {  lcd_zifu((s+i*k),y,k); j++;   }
}






/*******************************************************
//  划竖直线
//  dat1:起点    dat2:终点  画图是从dat2向dat1
//  VOL:列地址
***********************************************************/
void DrawLine(unsigned int dat1,unsigned int dat2,unsigned int VOL)	 //VOL从1~128,画竖线时dat1和dat2从1~64,画横线时dat1和dat2从0~63
{
  unsigned int DM,numb,m,FP,PP; 
  uint8 LCD_VL,LCD_VH;
  uint8 LCD_D;
  uint8 LCD_L;

  LCD_VH=VOL/16;
  LCD_VL=VOL%16; 
  numb=0;   
  
  DM=dat2-dat1;   
  PP=dat2/8;   
  LCD_L=dat2%8;
  FP=dat2/8; 
  
  LCD_D=0x00;
if(DM>0)
{  
  for(numb=0;numb<DM;)
  {    
    
    if(PP==FP)
    {      
      LCD_D=0x00;
      for(m=(8-LCD_L);m<8;m++)
      {
        if(numb<DM+1)
        {
          LCD_D|=0x01<<m;          
          numb++;                    
        }        
      }
     //LcdNumbers(2,LCD_D,3,0);
      lcd_MC(0x40|0);    //start line
      lcd_MC(0xB0|YE[PP]);
      lcd_MC(0x10|LCD_VH);  //高lie
      lcd_MC(LCD_VL);   //dilie      
      lcd_MD(LCD_D);
      if(PP>0){ PP--;}          
    }     
    else
    {      
      LCD_D=0x00;
      for(m=0;m<8;m++)
      {
        if(numb<DM+1)
        {
          LCD_D|=0x01<<m;          
          numb++;         
        }      
      }      
      lcd_MC(0x40|0);    //start line
      lcd_MC(0xB0|YE[PP]);
      lcd_MC(0x10|LCD_VH);  //高lie
      lcd_MC(LCD_VL);   //dilie 
      lcd_MD(LCD_D);
     // LcdNumbers(2,LCD_D,3,0);     
      if(PP>0) {PP--;}
    }   
  }
}
if(DM==0)
{
  LCD_D=0x00;
  lcd_MC(0x40|0);    //start line
  lcd_MC(0xB0|YE[PP]);
  lcd_MC(0x10|LCD_VH);  //高lie
  lcd_MC(LCD_VL);   //dilie 
  LCD_D|=0x01<<(7-LCD_L);
  lcd_MD(LCD_D);
  
}
} 

void Lcd_DrawCur(void){
int num,k,z;
uint8 dat1,dat2;
unsigned int temp,temp1,temp2;//,dat1,dat2;      //temp1:向前比较;temp2:向后比较;dat1:为该列的起始行;dat2:为该列的终止行
 

//for(k=0;k<6;k++)
for(k=0;k<8;k++)
{
  for(num=0;num<16;num++)
  {
    
    z=num+16*k;
    
    if(z==0)                 //只要画与后面的比较;取两点的中点作为连线的标准
    {
      temp=DrawCur[z];
      temp2=DrawCur[z+1];
      if(temp>=temp2)
      { 
        dat1=temp-(temp-temp2)/2;
        dat2=temp; 
      }
      if(temp<temp2)
      {
        dat1=temp;
        dat2=temp+(temp2-temp)-(temp2-temp)/2;
      }
    }
    //if(z==95)
	if(z==127)
    {
      temp=DrawCur[z];
      temp1=DrawCur[z-1];
      if(temp>=temp1)
      {
        dat1=temp-(temp-temp1)/2;
        dat2=temp;
      }
      if(temp<temp1)
      {
        dat1=temp;
        dat2=temp+(temp1-temp)-(temp1-temp)/2;
      }
    }
   // if((z>0)&&(z<95))
	if((z>0)&&(z<127))
    {
      temp1=DrawCur[z-1];
      temp=DrawCur[z];
      temp2=DrawCur[z+1];
      if((temp>=temp1)&&(temp>=temp2))
      {
        dat2=temp;
        if(temp1>=temp2)
        {
          dat1=temp-(temp-temp2)/2;          
        }
        else
        {
          
          dat1=temp-(temp-temp1)/2;
        }
      }
      if((temp<temp1)&&(temp<temp2))
      {
        dat1=temp;
        if(temp1>=temp2)
        {
          dat2=temp+(temp1-temp)-(temp1-temp)/2;
        }
        else
        {
          dat2=temp+(temp2-temp)-(temp2-temp)/2;
        }
      }
      if((temp>=temp1)&&(temp<temp2))
      {
       dat1=temp-(temp-temp1)/2;
       dat2=temp+(temp2-temp)-(temp2-temp)/2;
      }
      if((temp<temp1)&&(temp>=temp2))
      {
        dat1=temp-(temp-temp2)/2;
        dat2=temp+(temp1-temp)-(temp1-temp)/2;
      }
    }    

     DrawLine(dat1,dat2,z);
   // LcdNumbers(2,num,3,0);
    
    
  }    
  }
   
} 
   



void main(void)
{
  uint8 i,j=43;

  WDTCN = 0xde;
  WDTCN = 0xad;//关闭看门狗
  Init_SYSCLK();
  Init_PORT();
  LcdIni();

  for(i=0;i<128;i++)
  {
    DrawCur[i]=j;
    j++;
	if(j==46)
	j=40;
  }

  while(1)
  {
  	 Lcd_DrawCur();





  
  }


}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?