热卖开发板:
  • 51单片机开发板
  • USB开发板
  • AVR单片机开发板
  • LCD1602程序设计代码实例


      //-----------------------------------------------------------------------------
      // Includes
      //-----------------------------------------------------------------------------

      #include <c8051f020.h>                 // SFR declarations
      #include <stdio.h>

      //-----------------------------------------------------------------------------
      // 16-bit SFR Definitions for 'F02x
      //-----------------------------------------------------------------------------

      sfr16 DP       = 0x82;                 // data pointer
      sfr16 TMR3RL   = 0x92;                 // Timer3 reload value
      sfr16 TMR3     = 0x94;                 // Timer3 counter
      sfr16 ADC0     = 0xbe;                 // ADC0 data
      sfr16 ADC0GT   = 0xc4;                 // ADC0 greater than window
      sfr16 ADC0LT   = 0xc6;                 // ADC0 less than window
      sfr16 RCAP2    = 0xca;                 // Timer2 capture/reload
      sfr16 T2       = 0xcc;                 // Timer2
      sfr16 RCAP4    = 0xe4;                 // Timer4 capture/reload
      sfr16 T4       = 0xf4;                 // Timer4
      sfr16 DAC0     = 0xd2;                 // DAC0 data
      sfr16 DAC1     = 0xd5;                 // DAC1 data

      //-----------------------------------------------------------------------------
      // Global CONSTANTS
      //-----------------------------------------------------------------------------

      #define SYSCLK       22118400          // SYSCLK frequency in Hz


      /*sbit YJRS = P4^0;
      sbit YJRW = P4^1;                      
      sbit YJE = P4^2;
      sbit YJDB4 = P5^4;
      sbit YJDB5 = P5^5;
      sbit YJDB6 = P5^6;                      
      sbit YJDB7 = P5^7;
      */
      sbit SW1 = P3^7;                       // SW1='0' means switch pressed

      //-----------------------------------------------------------------------------
      // Function PROTOTYPES
      //-----------------------------------------------------------------------------

      void SYSCLK_Init (void);
      void PORT_Init (void);
      void Write_COM(unsigned yjcom );
      void Write_CHAR(unsigned char yjchar);
      void Write_STR(unsigned char *s);
      void YJ_Init(void);
      void YJ_Mode1(void);
      void YJ_Mode2(void);
      void YJ_Mode3(void);
      void CG_Write(void);
      void MYCHAR_Disp(void);
      //-----------------------------------------------------------------------------
      // Global VARIABLES
      //-----------------------------------------------------------------------------
      int code  tab1[12]={0X43,0X75,0X72,0X73,0X6f,0X72,0X20,    //Cursor
           0X57,0X72,0X69,0X74,0X65};      //write
      int code  tab2[16]={0X43,0X75,0X72,0X73,0X6f,0X72,      //Cursor
              0X4C,0X65,0X66,0X74,       //Left
           0X53,0X63,0X72,0X6F,0X6C,0X6C};     //Scroll
      int code  tab3[19]={0X43,0X75,0X72,0X73,0X6f,0X72,0x20,    //Cursor
            0X52,0X69,0X67,0X68,074,0x20,     //Left
           0X53,0X63,0X72,0X6F,0X6C,0X6C};     //Scroll
      int code cgtab[64]={0x08,0x0f,0x12,0x0f,0x0a,0x1f,0x02,0x02,  //年
           0x0f,0x09,0x0f,0x09,0x0f,0x09,0x11,0x00,  //月
           0x1f,0x11,0x11,0x1f,0x11,0x11,0x1f,0x00,  //日
           0x11,0x0a,0x04,0x1f,0x04,0x1f,0x04,0x00,  //$
           0x0e,0x00,0x1f,0x0a,0x0a,0x0a,0x13,0x00,  //元
           0x18,0x18,0x07,0x08,0x08,0x08,0x07,0x00,  //℃
           0x04,0x0a,0x15,0x04,0x04,0x04,0x04,0x00,  //10
           0x17,0x15,0x15,0x15,0x15,0x15,0x17,0x00};   //↑
      int code tabdy[36]={0x4d,0x44,0x4c,0x53,0x34,0x30,0x32,0x36,0x36,
           0x20,0x03,0x32,0x35,0x35,0x2e,0x30,0x30,0x04,
           0x31,0x39,0x39,0x37,0x00,0x37,0x01,0x31,0x02,
           0x20,0x54,0x3d,0x33,0x35,0x05,0x06,0x20,0x07};
      //-----------------------------------------------------------------------------
      // MAIN Routine
      //-----------------------------------------------------------------------------

      void main (void) {
        int i,j,k;

         unsigned char code test[]="0123456789ABCDEF";                                     // temperature

         WDTCN = 0xde;                       // disable watchdog timer
         WDTCN = 0xad;

         SYSCLK_Init ();                     // initialize oscillator
         PORT_Init ();                       // initialize crossbar and GPIO
         YJ_Init(); 
       P4 = 0;
       P5 = 0xff;
      /*   while (1) {
        
        Write_COM(0x80);
       for(k=0;k<2500;k++);
       Write_STR(test);
       Write_COM(0xc0);
       for(k=0;k<2500;k++);
       Write_STR("abcdefghijklmnop");
       }*/
       //YJ_Mode1();
        //CG_Write();
        MYCHAR_Disp();
       for(i=0;i<2000;i++)
        {
         for(j=0;j<20;j++)
         {
          for(k=0;k<240;k++);
         }
        };
      }

      //-----------------------------------------------------------------------------
      // Initialization Subroutines
      //-----------------------------------------------------------------------------

      //-----------------------------------------------------------------------------
      // SYSCLK_Init
      //-----------------------------------------------------------------------------
      //
      // This routine initializes the system clock to use an 22.1184MHz crystal
      // as its clock source.

      void SYSCLK_Init (void)
      {
         int i;                              // delay counter
         OSCXCN = 0x67;                      // start external oscillator with
                                             // 22.1184MHz crystal
         for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)
         while (!(OSCXCN & 0x80)) ;          // Wait for crystal osc. to settle
         OSCICN = 0x88;                      // select external oscillator as SYSCLK
                                             // source and enable missing clock
                                             // detector
      }

      //-----------------------------------------------------------------------------
      // PORT_Init
      //-----------------------------------------------------------------------------
      //
      // Configure the Crossbar and GPIO ports
      //
      void PORT_Init (void)
      {
          XBR0 = 0x00; // XBAR0: Initial Reset Value
       XBR1 = 0x00; // XBAR1: Initial Reset Value
       XBR2 = 0x40; // XBAR2: Initial Reset Value
      // Select Pin I/0

      // NOTE: Some peripheral I/O pins can function as either inputs or
      // outputs, depending on the configuration of the peripheral. By default,
      // the configuration utility will configure these I/O pins as push-pull
      // outputs.
                            // Port configuration (1 = Push Pull Output)
          P0MDOUT = 0x00; // Output configuration for P0
          P1MDOUT = 0x00; // Output configuration for P1
          P2MDOUT = 0x00; // Output configuration for P2
          P3MDOUT = 0x00; // Output configuration for P3
          P74OUT = 0x01;  // Output configuration for P4-7

          P1MDIN = 0xFF;  // Input configuration for P1

      }

      //写指令代码子程序
      void Write_COM(unsigned yjcom )
      {
       int bf_sta,k;

        P4&=0xfe;   //RS=0
        P4|=0x02;     //R/W=1
       do {
         P5 = 0xff;
         P4|= 0x04;
         bf_sta = P5;
         P4 &= 0xfb;
         P4 |= 0x04;
         P4 &= 0xfb;
        }
       while (bf_sta & 0x80);

          P4 &= 0xfd;   //R/W=0
        P5 = yjcom;
        P4 |= 0x04;   //E=1
        P4 &= 0xfb;   //E=0
        P5 = yjcom << 4;
        P4 |= 0x04;
        P4 &= 0xfb;
       for(k=0;k<250;k++);
      }
      //写显示数据子程序(单个字符)
      void Write_CHAR(unsigned char yjchar)
      {
      int bf_sta,k;

         P4&=0xfe;
        P4|=0x02;
       do {
         P5 = 0xff;
         P4 |= 0x04;
         bf_sta = P5;
         P4 &= 0xfb;
         P4 |= 0x04;
         P4 &= 0xfb;
        }
       while (bf_sta & 0X80);
        
        P4 |= 0x01;   //RS=1
        P4 &= 0xfd;   //R/W=0
        P5 = yjchar;
        P4 |= 0x04;
        P4 &= 0xfb;
        P5=yjchar << 4;
        P4 |= 0x04;
        P4 &= 0xfb;
      // for(k=0;k<2500;k++);
      }
      //写字符串
      void Write_STR(unsigned char *str1)
      {
      int k;
       unsigned char yjchar1;
       while(*str1 != 0x0)
        {
         yjchar1=*str1;
         Write_CHAR(yjchar1);
         str1++;
         for(k=0;k<2500;k++);
        }
      }
      //液晶初始化
      void YJ_Init(void)
      {
       int i,j;

        P5=0X30;
        P4&=0XFE;
        P4&=0XFD;  //RS=R/W=E=0
       for(i=0;i<3;i++){
         P4|=0X04;
         P4&=0XFB;
         for(j=0;j<250;j++);
        }

       Write_COM(0x28); //4位数据接口,两行显示5×7点阵字符
       Write_COM(0x01); //清屏
       Write_COM(0x06); //数据读、写操作后,AC自动增一
            //数据读、写操作,画面不动
       Write_COM(0x0f); //显示开、光标开、闪烁开
      }
      //逐字依次输入方式演示程序段
      void YJ_Mode1(void)
      {
      int i,j;
       Write_COM(0x06); //数据读、写操作后,AC自动增一
            //数据读、写操作,画面不动
       Write_COM(0x80);
       for(j=0;j<250;j++);
       for(i=0;i<12;i++){
        Write_CHAR(tab1[i]);
        for(j=0;j<250;j++);
        };
       Write_COM(0xC0);
       for(j=0;j<250;j++);
       for(i=0;i<12;i++){
        Write_CHAR(tab1[i]);
        for(j=0;j<250;j++);
        };
      }
      //光标左移输入方式演示程序段
      void YJ_Mode2(void) 
      {
      int i,j,k;
       Write_COM(0x04); //数据读、写操作后,AC自动增一
            //数据读、写操作,画面不动
       Write_COM(0x90);
       for(i=0;i<12;i++){
        Write_CHAR(tab1[12-i]);
        for(j=0;j<1000;j++)
        for(k=0;k<250;k++);
        };
       Write_COM(0xd0);
       for(j=0;j<250;j++);
       for(i=0;i<16;i++){
        Write_CHAR(tab2[16-i]);
        for(j=0;j<1000;j++)
        for(k=0;k<250;k++);
        };
      }
      //光标右移输入方式演示程序段
      void YJ_Mode3(void) 
      {
      int i,j,k;
       Write_COM(0x06); //数据读、写操作后,AC自动增一
            //数据读、写操作,画面不动
       Write_COM(0x80);
       for(i=0;i<19;i++){
        Write_CHAR(tab1[i]);
        for(j=0;j<1000;j++)
        for(k=0;k<250;k++);
        };
      }
      /*自定义字符显示演示程序段*/
      void MYCHAR_Disp(void)
      {
      int i,j;
       CG_Write();
       Write_COM(0x80);
       for(i=0;i<16;i++){
        Write_CHAR(tabdy[i]);
        for(j=0;j<250;j++);
        };
       Write_COM(0xc0);
       for(i=0;i<16;i++){
        Write_CHAR(tabdy[i+18]);
        for(j=0;j<100;j++);
        };
      }
      //建立自定义字符块
      void CG_Write(void)
      {
      int i,j;
       Write_COM(0x40);
       for(j=0;j<250;j++);
       for(i=0;i<64;i++){
        Write_CHAR(cgtab[i]);
        for(j=0;j<250;j++);
        };
      }

    声明:CEPARK已尽力确保内容的准确性,但因业务发展和技术服务的需要,有关内容可能会根据实际情况随时更新或者修改,恕不另行通知,不便之处敬请谅解!
      商城推荐
      社区指南
    CEPARK电子园-电子工程师自己的网络家园!
      迷你博客,轻松完成日志发布,管理相册;展示个性自我
      兴趣小组,热点话题,加入您感兴趣的话题讨论小组,一起交流吧
      最新资讯,热门话题与好友一起分享信息,他评论感兴趣的话题
      实名认证,真实交友,还可以轻松快捷的了解好友动态
      邀请您身边的好友,朋友,同事一起加入欢乐圈