注册 登录
电子工程世界-论坛 返回首页 EEWORLD首页 频道 EE大学堂 下载中心 Datasheet 专题
jxb01033016的个人空间 https://home.eeworld.com.cn/space-uid-445.html [收藏] [复制] [分享] [RSS]
日志

基于LCD1602的人机界面设计

已有 2564 次阅读2010-3-5 16:09 |

 
                                 系统启动界面。
 
                                               RTC 界面
                        设置ID界面
 
 
背景:左边的CPU模拟DSP,右边的CPU模拟EM78P5840。
      DSP扫描按键,(分别是MENU,UP,DOWN,ENTER)
      MENU为显示页面切换。
      ENTER为参数设置按键。
      UP用来增加参数。
      DOWN用来减少参数。
     DSP通过I2C总线传输给MCU,由MCU驱动LCD显示相应界面。
 
 
接收部分:
 
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define KEY_MENU   0xFE
#define KEY_ENTER  0xFB
#define KEY_UP     0XFD
#define KEY_DOWN   0XF7
#define page_time_set        1
#define page_ir_set          2
#define page_mode_set        3
#define page_quality_set     4
#define page_delay_set       5
#define page_sequence_set    6
#define page_erase_set       7
#define page_id_set          8
#define page_sureerase_set   9
#define page_clrmemo_set     10
sbit SDA = P0^7;
sbit SCL = P0^6;
sbit i2c_ctl =P0^5;
uint  data time_counter=0;
uchar data second=0;
uchar data minute=20;
uchar data hour=01;
uchar data day=11;
uchar data month=04;
uchar data year=10;
uchar data time_point;
uchar image_delay_time=0;
uchar data page=0;
uchar idata id_num[8]={0x30,0x30,0x30,0x30,0x30,0x30};
uchar id_point=0;
uchar page5_sw=0;
uchar key_temp=0xff;
uchar key_code=0xff;
uchar key_value[2]={0xff,0xff};
uchar enter_counter=0;
bit ms20_ready=0;
bit minute_flag=0;
bit id_set_flag=0;
//uchar  const  code  page1_up[16]="  System v2.32  ";
//uchar  const  code page1_down[16]=" Starting......";
//uchar  const  code page2_up[16]= " Date and Time ";
//uchar  const  code page2_down[16]="10/04/11 01:20AM";
//uchar  data  key_value[3]={0xff,0xff,0xff};
//uchar const  all_page_up[][16]=  {{ " Date and Time"},{"IR Event Count"},{"  Image Mode"},{"Image Quallity"},{"  Image delay"},{" Image sequence"},{"Eraser all Image"},{"Set Camera ID"},{" Enter = Erase"},{" Memory Clear "}};
//uchar const  all_page_down[][16]={{"10/04/11 01:20AM"},{"   0   Reset?"},{">still<  video "},{">Standard< High "},{" 1 min    [  ]"},{"  >1<  2   3"},{"0    Erase?"},{"ID-[________]"},{" [  ] = Cancel"},{" 0000 images"}};
uchar const  page2_select[4]={0,6,8,14};
uchar const  page3_select[4]={0,9,10,15};
uchar const  page5_select[6]={2,4,6,8,10,12};
uchar const  lcd_command[4]={0x38,0x0c,0x06,0x01};
 

#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define AddWr 0xa0 /*器件地址选择及写标志*/
#define AddRd 0xa1 /*器件地址选择及读标志*/

sbit Sda= P0^7; /*串行数据*/
sbit Scl= P0^6; /*串行时钟*/
sbit spi_ctl= P0^5; /*硬件写保护*/
void mDelay(uint j)
   {
      uint i;
      for(;j>0;j--)
         {
          for(i=100;i>0;i--)
             {
               ;       
             }
         }  
    }
void delay( )
  {
    uchar i=20;
    for(;i>0;i--)
        {
          _nop_();
        }
  }
/*发送起始条件*/
 void start(void) /*起始条件*/
    {
    delay( );
    Sda=1;
    Scl=1;
    delay( );
   Sda=0;
   delay( );
  }
  void stop(void) /*停止条件*/
    {
      delay();
     Sda=0;
    Scl=1;
    delay( );  
    Sda=1;
    delay( );
  }
  void Ack(void) /*应答位*/
  {
  //Sda=0;
  delay( );
  Scl=1;
  delay( );
  Scl=0;
  delay( );
  }
 // void NoAck(void) /*反向应答位*/
 //  {
   // Sda=1;
  // delay( );
 //  Scl=1;
  // delay( );
 // Scl=0;
 // }
 void send(uchar Data) /*发送数据子程序,Data为要求发送的数据*/
   {
    uchar BitCounter=8; /*位数控制*/
    uchar temp=Data; /*中间变量控制*/
   do
    {
     delay( );
     Scl=0;
     //delay( );
    if((temp&0x80)==0x80)/* 如果最高位是1*/
    Sda=1;
   else
   Sda=0;
   delay( );
  Scl=1;
   temp=temp<<1; /*RLC*/
   //Data=temp;
  BitCounter--;
   delay( );
  }
  while(BitCounter);
  Scl=0;
  delay( );
  }

  //uchar Read(void) /*读一个字节的数据,并返回该字节值*/
 // {
 // uchar temp=0;
 // uchar temp1=0;
  //uchar BitCounter=8;
  //Sda=1;
 // do{
 // Scl=0;
 // _nop_ ();
//
//  _nop_ ();
 // _nop_ ();
  //_nop_ ();
  //Scl=1;
 //_nop_ ();
 //_nop_ ();
 //_nop_ ();
 //
 //_nop_ ();
 //if(Sda) /*如果Sda=1;*/
 //temp=temp|0x01; /*temp的最低位置1*/
 //else
 //temp=temp&0xfe; /*否则temp的最低位清0*/
 //if(BitCounter-1)
 //{ temp1=temp<<1;
 //temp=temp1;
 //}
 //BitCounter--;
 //}while(BitCounter);
 //return(temp);
 //}

 //void send_mcu_page(uchar page)
 // {
  // uchar i;
   //EA=0;
   //i2c_ctl=0;
   //delay( );
   //delay( );
 //  start(); /*发送启动信号*/
  // send(0xa0); /*发送SLA+W*/
  //Ack();
 // for(i=0;i<16;i++)
 // {
 
 // send(all_page_up[page][i]);
 // Ack();
 
  //mDelay(20);
 //}
 //for(i=0;i<16;i++)
 //  {
 //    send(all_page_down[page][i]);
 //    Ack( );
 //   }
 //stop( );
 // }
 
  void send_mcu_info(uchar page,uchar key_code)
       {
           EA=0;
           i2c_ctl=0;
           delay( );
           start( );
           send(0xa0);
           Ack();
           send(page);
           Ack( );
           send(key_code);
           Ack( );
           stop( );
          i2c_ctl=1;
           delay( );
           delay( );
           EA=1;
        }
          
 // void read_from_mcu(uchar Data[],uchar Num)
  //{
  // uchar i;
   //uchar *PData;
  // PData=Data;
 //for(i=0;i<Num;i++)
 // {
 // start();
 // send(0xa0);
 // Ack();
  //Send(Address+i);
 // Ack();
 // start();
 // send(0xa1);
 // Ack();
  //*(PData+i)=Read();
 // Scl=0;
 // NoAck();
 // stop();
 // }
// }
void timer0( void ) interrupt 1
 {
   TH0=(65536-20000)/256;
   TL0=(65536-20000)%256;
   P1=0XFF;
    if(P1!=0xff)
    {
      key_temp=P1;
        }
  else
  {
       key_temp=0xff;
     }
   ms20_ready=1;
   time_counter++;
        if(time_counter>=25)
         {
           time_counter=0;
           second++;
           if(second>=120)
              {
                 second=0;
                 minute++;
                 minute_flag=1;
                 if(minute>=60)
                    {
                       minute=0;                     
                       hour++;
                       if(hour>=24)
                          {
                            hour=0;
                            day++;
                            if(day>=30)
                              {
                                day=0;
                                month++;
                               if(month>=12)
                                  {
                                    month=0;
                                    year++;
                                  }
                               }
                           }
                     }
             }
      }
  }
timer_init( )
 {
   TMOD=0x01;  //定时器工作模式 方式一
   TH0=(65536-20000)/256;   //
   TL0=(65536-20000)%256;   //
   TR0=1;      //启动定时器一
   ET0=1;      //允许定时器0中断
   EA=0;
 }

   void main()
   {
      //uchar i2c_state=0;
     uchar kkk;
     timer_init( );
    i2c_ctl=1;
    for(kkk=0;kkk<=50;kkk++)
        {
           mDelay(100);
        }
    page=1;
    send_mcu_info(0,KEY_MENU); /*将初始化后的数值写入EEPROM*/      
   
while(1)
 {
   uchar  key_old;
   uchar key_new;
 
      if(page==0)
         {
            if(minute_flag)
               {
                 minute_flag=0;
                // display_normal_time( );
               }
         }
               
      if(ms20_ready)
        {
          ms20_ready=0;
       if(key_temp!=0xff)
         {
             // key_value[2]=key_value[1];
        key_value[1]=key_value[0];
        key_value[0]=key_temp;
        if( key_value[1]==key_value[0] )
           {
              key_new=key_temp;
           if(key_old!=key_new)
              {
                key_old=key_new;
                         switch(key_old)
                               {
                                   case KEY_MENU:
                                                   
                                                  send_mcu_info(page,KEY_MENU);
                                                  page=page+1;
                                                  if(page>8)
                                                    {
                                                      page=1;
                                                    }
                                                  break;
                                  case KEY_ENTER: 
                                                  if(page==page_erase_set)
                                                     { 
                                                                                                     
                                                       //send_mcu_info(page,key_old);
                                                          enter_counter++;
                                                       if(enter_counter<3)
                                                          {
                                                             send_mcu_info(page,key_old);                                                        
                                                           }
                                                       else
                                                         {
                                                            // page=page_time_set;
                                                             //key_old=0xff;
                                                             enter_counter=0;
                                                             send_mcu_info(page,key_old);
                                                             page=page_time_set;
                                                          } 
                                                     } 
                                                   else{
                                                         send_mcu_info(page,key_old);
                                                       }                   
                                                  break;
                                  case KEY_UP:
                                  case KEY_DOWN:  if( page==page_erase_set)
                                                      {
                                                         if(enter_counter==1)
                                                           {
                                                             enter_counter=0;
                                                           }                                                                                     
                                                      }
                                                     send_mcu_info(page,key_old);
                                                  
                                       
                                                  break;
                                  default:  
                                                  key_old=0xff;
                                                  break;
                              
                    
                              }
                       
            }
               
            }
          
          }
          else key_old=0xff;
        }
     }
   }
发送部分:
 #include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define KEY_MENU   0xFE
#define KEY_ENTER  0xFB
#define KEY_UP     0XFD
#define KEY_DOWN   0XF7
#define page_time_set        1
#define page_ir_set          2
#define page_mode_set        3
#define page_quality_set     4
#define page_delay_set       5
#define page_sequence_set    6
#define page_erase_set       7
#define page_id_set          8
#define page_sureerase_set   9
#define page_clrmemo_set     10
sbit SDA = P0^7;
sbit SCL = P0^6;
sbit i2c_ctl =P0^5;
uint  data time_counter=0;
uchar data second=0;
uchar data minute=20;
uchar data hour=01;
uchar data day=11;
uchar data month=04;
uchar data year=10;
uchar data time_point;
uchar image_delay_time=0;
uchar data page=0;
uchar idata id_num[8]={0x30,0x30,0x30,0x30,0x30,0x30};
uchar id_point=0;
uchar page5_sw=0;
uchar key_temp=0xff;
uchar key_code=0xff;
uchar key_value[2]={0xff,0xff};
uchar enter_counter=0;
bit ms20_ready=0;
bit minute_flag=0;
bit id_set_flag=0;
//uchar  const  code  page1_up[16]="  System v2.32  ";
//uchar  const  code page1_down[16]=" Starting......";
//uchar  const  code page2_up[16]= " Date and Time ";
//uchar  const  code page2_down[16]="10/04/11 01:20AM";
//uchar  data  key_value[3]={0xff,0xff,0xff};
//uchar const  all_page_up[][16]=  {{ " Date and Time"},{"IR Event Count"},{"  Image Mode"},{"Image Quallity"},{"  Image delay"},{" Image sequence"},{"Eraser all Image"},{"Set Camera ID"},{" Enter = Erase"},{" Memory Clear "}};
//uchar const  all_page_down[][16]={{"10/04/11 01:20AM"},{"   0   Reset?"},{">still<  video "},{">Standard< High "},{" 1 min    [  ]"},{"  >1<  2   3"},{"0    Erase?"},{"ID-[________]"},{" [  ] = Cancel"},{" 0000 images"}};
uchar const  page2_select[4]={0,6,8,14};
uchar const  page3_select[4]={0,9,10,15};
uchar const  page5_select[6]={2,4,6,8,10,12};
uchar const  lcd_command[4]={0x38,0x0c,0x06,0x01};
 

#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define AddWr 0xa0 /*器件地址选择及写标志*/
#define AddRd 0xa1 /*器件地址选择及读标志*/

sbit Sda= P0^7; /*串行数据*/
sbit Scl= P0^6; /*串行时钟*/
sbit spi_ctl= P0^5; /*硬件写保护*/
void mDelay(uint j)
   {
      uint i;
      for(;j>0;j--)
         {
          for(i=100;i>0;i--)
             {
               ;       
             }
         }  
    }
void delay( )
  {
    uchar i=20;
    for(;i>0;i--)
        {
          _nop_();
        }
  }
/*发送起始条件*/
 void start(void) /*起始条件*/
    {
    delay( );
    Sda=1;
    Scl=1;
    delay( );
   Sda=0;
   delay( );
  }
  void stop(void) /*停止条件*/
    {
      delay();
     Sda=0;
    Scl=1;
    delay( );  
    Sda=1;
    delay( );
  }
  void Ack(void) /*应答位*/
  {
  //Sda=0;
  delay( );
  Scl=1;
  delay( );
  Scl=0;
  delay( );
  }
 // void NoAck(void) /*反向应答位*/
 //  {
   // Sda=1;
  // delay( );
 //  Scl=1;
  // delay( );
 // Scl=0;
 // }
 void send(uchar Data) /*发送数据子程序,Data为要求发送的数据*/
   {
    uchar BitCounter=8; /*位数控制*/
    uchar temp=Data; /*中间变量控制*/
   do
    {
     delay( );
     Scl=0;
     //delay( );
    if((temp&0x80)==0x80)/* 如果最高位是1*/
    Sda=1;
   else
   Sda=0;
   delay( );
  Scl=1;
   temp=temp<<1; /*RLC*/
   //Data=temp;
  BitCounter--;
   delay( );
  }
  while(BitCounter);
  Scl=0;
  delay( );
  }

  //uchar Read(void) /*读一个字节的数据,并返回该字节值*/
 // {
 // uchar temp=0;
 // uchar temp1=0;
  //uchar BitCounter=8;
  //Sda=1;
 // do{
 // Scl=0;
 // _nop_ ();
//
//  _nop_ ();
 // _nop_ ();
  //_nop_ ();
  //Scl=1;
 //_nop_ ();
 //_nop_ ();
 //_nop_ ();
 //
 //_nop_ ();
 //if(Sda) /*如果Sda=1;*/
 //temp=temp|0x01; /*temp的最低位置1*/
 //else
 //temp=temp&0xfe; /*否则temp的最低位清0*/
 //if(BitCounter-1)
 //{ temp1=temp<<1;
 //temp=temp1;
 //}
 //BitCounter--;
 //}while(BitCounter);
 //return(temp);
 //}

 //void send_mcu_page(uchar page)
 // {
  // uchar i;
   //EA=0;
   //i2c_ctl=0;
   //delay( );
   //delay( );
 //  start(); /*发送启动信号*/
  // send(0xa0); /*发送SLA+W*/
  //Ack();
 // for(i=0;i<16;i++)
 // {
 
 // send(all_page_up[page][i]);
 // Ack();
 
  //mDelay(20);
 //}
 //for(i=0;i<16;i++)
 //  {
 //    send(all_page_down[page][i]);
 //    Ack( );
 //   }
 //stop( );
 // }
 
  void send_mcu_info(uchar page,uchar key_code)
       {
           EA=0;
           i2c_ctl=0;
           delay( );
           start( );
           send(0xa0);
           Ack();
           send(page);
           Ack( );
           send(key_code);
           Ack( );
           stop( );
          i2c_ctl=1;
           delay( );
           delay( );
           EA=1;
        }
          
 // void read_from_mcu(uchar Data[],uchar Num)
  //{
  // uchar i;
   //uchar *PData;
  // PData=Data;
 //for(i=0;i<Num;i++)
 // {
 // start();
 // send(0xa0);
 // Ack();
  //Send(Address+i);
 // Ack();
 // start();
 // send(0xa1);
 // Ack();
  //*(PData+i)=Read();
 // Scl=0;
 // NoAck();
 // stop();
 // }
// }
void timer0( void ) interrupt 1
 {
   TH0=(65536-20000)/256;
   TL0=(65536-20000)%256;
   P1=0XFF;
    if(P1!=0xff)
    {
      key_temp=P1;
        }
  else
  {
       key_temp=0xff;
     }
   ms20_ready=1;
   time_counter++;
        if(time_counter>=25)
         {
           time_counter=0;
           second++;
           if(second>=120)
              {
                 second=0;
                 minute++;
                 minute_flag=1;
                 if(minute>=60)
                    {
                       minute=0;                     
                       hour++;
                       if(hour>=24)
                          {
                            hour=0;
                            day++;
                            if(day>=30)
                              {
                                day=0;
                                month++;
                               if(month>=12)
                                  {
                                    month=0;
                                    year++;
                                  }
                               }
                           }
                     }
             }
      }
  }
timer_init( )
 {
   TMOD=0x01;  //定时器工作模式 方式一
   TH0=(65536-20000)/256;   //
   TL0=(65536-20000)%256;   //
   TR0=1;      //启动定时器一
   ET0=1;      //允许定时器0中断
   EA=0;
 }

   void main()
   {
      //uchar i2c_state=0;
     uchar kkk;
     timer_init( );
    i2c_ctl=1;
    for(kkk=0;kkk<=50;kkk++)
        {
           mDelay(100);
        }
    page=1;
    send_mcu_info(0,KEY_MENU); /*将初始化后的数值写入EEPROM*/      
   
while(1)
 {
   uchar  key_old;
   uchar key_new;
 
      if(page==0)
         {
            if(minute_flag)
               {
                 minute_flag=0;
                // display_normal_time( );
               }
         }
               
      if(ms20_ready)
        {
          ms20_ready=0;
       if(key_temp!=0xff)
         {
             // key_value[2]=key_value[1];
        key_value[1]=key_value[0];
        key_value[0]=key_temp;
        if( key_value[1]==key_value[0] )
           {
              key_new=key_temp;
           if(key_old!=key_new)
              {
                key_old=key_new;
                         switch(key_old)
                               {
                                   case KEY_MENU:
                                                   
                                                  send_mcu_info(page,KEY_MENU);
                                                  page=page+1;
                                                  if(page>8)
                                                    {
                                                      page=1;
                                                    }
                                                  break;
                                  case KEY_ENTER: 
                                                  if(page==page_erase_set)
                                                     { 
                                                                                                     
                                                       //send_mcu_info(page,key_old);
                                                          enter_counter++;
                                                       if(enter_counter<3)
                                                          {
                                                             send_mcu_info(page,key_old);                                                        
                                                           }
                                                       else
                                                         {
                                                            // page=page_time_set;
                                                             //key_old=0xff;
                                                             enter_counter=0;
                                                             send_mcu_info(page,key_old);
                                                             page=page_time_set;
                                                          } 
                                                     } 
                                                   else{
                                                         send_mcu_info(page,key_old);
                                                       }                   
                                                  break;
                                  case KEY_UP:
                                  case KEY_DOWN:  if( page==page_erase_set)
                                                      {
                                                         if(enter_counter==1)
                                                           {
                                                             enter_counter=0;
                                                           }                                                                                     
                                                      }
                                                     send_mcu_info(page,key_old);
                                                  
                                       
                                                  break;
                                  default:  
                                                  key_old=0xff;
                                                  break;
                              
                    
                              }
                       
            }
               
            }
          
          }
          else key_old=0xff;
        }
     }
   }
 
       
 
发表评论 评论 (17 个评论)
回复 tiankai001 2010-3-5 16:40
详细介绍以下功能就好了
回复 jxb01033016 2010-3-5 17:24
打猎照相机的,当有猎物经过照相机,自动拍照,这只是个人机界面而已。
回复 soso 2010-3-5 20:51
:)
回复 jxb01033016 2010-3-8 08:41
回复 open82977352 2010-3-10 09:20
回复 天天谈芯 2010-3-14 10:40
感谢博主,很有收获。
回复 天天谈芯 2010-3-14 10:40
回复 jxb01033016 2010-3-15 13:07
REAL?
回复 xiaoyuxiaoer 2010-6-9 22:51
回复 pa2792 2011-3-22 21:41
看到delay( );我就怕。能否把delay( );都减去?
回复 jxb01033016 2011-3-23 08:39
基本不能,这个主要用来保证必要的时序的,否则LCD不能显示
回复 KINGEYE88 2011-4-14 17:37
佩服!
回复 hantao0033 2011-12-8 19:55
LCD都能编成这样,牛成一堆了
回复 jxb01033016 2011-12-11 21:42
回复 buynow 2012-4-23 22:11
这个该怎么认识呢?我知道这是很好的作品,但我还是看不懂,你能说一下这个设计原理吗?用了什么?
回复 小贾 2012-4-25 11:12
回复 张无忌1987 2012-4-25 14:32
如何感应有猎物经过呢?

facelist doodle 涂鸦板

您需要登录后才可以评论 登录 | 注册

热门文章