分享

一网友的作品:Arduino驱动NOKIA5110,超声波测距网购论坛http://www.bay2009.cn

 开启美好每一天 2014-03-02
个人觉得5110模块非常好用,于是写了一个驱动,让好用的arduino驱动好用的lcd。程序写得比较初级,只有ascii字符显示,还有很多修改空间,比如还少了中文显示函数和图形显示函数,还有任意位置打点函数,可能过两天整理一下,编写一个5110的函数库,大家只要include一下就可以使用了:)
欢迎大家使用这个范例来和arduino进行更深层次的互动
(!注意事项:lcd5110 vcc接口只能输入3v电压,接入arduino的3v端,过高的电压可能会导致lcd烧毁,背景led灯也是标准3v输入,其余接口的逻辑电压可以使用5v)
更多深入了解http://code./learningaboutarduinonokialcd

//**************************************************************/
// nokia 5110 arduino驱动程序(超声波测距)
// 程序设计:kent
// 版本1.0
// gongzheng19881116@126.com
// 2010.6
//**************************************************************/

//端口定义

int lcd_ce=2;
int lcd_rst=3;
int sclk=4;
int sdin=5;
int lcd_dc=6;
int inputpin=8;  // 定义超声波信号接收接口
int outputpin=9; // 定义超声波信号发出接口
/****************************************************************/
void setup()
{
  pinmode(inputpin, input);
  pinmode(outputpin, output);
}
//****************************定义ascii字符**********************//

/********************************** 
6 x 8 font 
1 pixel space at left and bottom 
index = ascii - 32 
***********************************/ 
const unsigned char font6x8[][6] = 

    { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },   // sp 
    { 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 },   // ! 
    { 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 },   // " 
    { 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 },   // # 
    { 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 },   // $ 
    { 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 },   // % 
    { 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 },   // & 
    { 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 },   // ' 
    { 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 },   // ( 
    { 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 },   // ) 
    { 0x00, 0x14, 0x08, 0x3e, 0x08, 0x14 },   // * 
    { 0x00, 0x08, 0x08, 0x3e, 0x08, 0x08 },   // + 
    { 0x00, 0x00, 0x00, 0xa0, 0x60, 0x00 },   // , 
    { 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 },   // - 
    { 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 },   // . 
    { 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 },   // / 
    { 0x00, 0x3e, 0x51, 0x49, 0x45, 0x3e },   // 0 
    { 0x00, 0x00, 0x42, 0x7f, 0x40, 0x00 },   // 1 
    { 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 },   // 2 
    { 0x00, 0x21, 0x41, 0x45, 0x4b, 0x31 },   // 3 
    { 0x00, 0x18, 0x14, 0x12, 0x7f, 0x10 },   // 4 
    { 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 },   // 5 
    { 0x00, 0x3c, 0x4a, 0x49, 0x49, 0x30 },   // 6 
    { 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 },   // 7 
    { 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 },   // 8 
    { 0x00, 0x06, 0x49, 0x49, 0x29, 0x1e },   // 9 
    { 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 },   // : 
    { 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 },   // ; 
    { 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 },   // < 
    { 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 },   // = 
    { 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 },   // > 
    { 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 },   // ? 
    { 0x00, 0x32, 0x49, 0x59, 0x51, 0x3e },   // @ 
    { 0x00, 0x7c, 0x12, 0x11, 0x12, 0x7c },   // a 
    { 0x00, 0x7f, 0x49, 0x49, 0x49, 0x36 },   // b 
    { 0x00, 0x3e, 0x41, 0x41, 0x41, 0x22 },   // c 
    { 0x00, 0x7f, 0x41, 0x41, 0x22, 0x1c },   // d 
    { 0x00, 0x7f, 0x49, 0x49, 0x49, 0x41 },   // e 
    { 0x00, 0x7f, 0x09, 0x09, 0x09, 0x01 },   // f 
    { 0x00, 0x3e, 0x41, 0x49, 0x49, 0x7a },   // g 
    { 0x00, 0x7f, 0x08, 0x08, 0x08, 0x7f },   // h 
    { 0x00, 0x00, 0x41, 0x7f, 0x41, 0x00 },   // i 
    { 0x00, 0x20, 0x40, 0x41, 0x3f, 0x01 },   // j 
    { 0x00, 0x7f, 0x08, 0x14, 0x22, 0x41 },   // k 
    { 0x00, 0x7f, 0x40, 0x40, 0x40, 0x40 },   // l 
    { 0x00, 0x7f, 0x02, 0x0c, 0x02, 0x7f },   // m 
    { 0x00, 0x7f, 0x04, 0x08, 0x10, 0x7f },   // n 
    { 0x00, 0x3e, 0x41, 0x41, 0x41, 0x3e },   // o 
    { 0x00, 0x7f, 0x09, 0x09, 0x09, 0x06 },   // p 
    { 0x00, 0x3e, 0x41, 0x51, 0x21, 0x5e },   // q 
    { 0x00, 0x7f, 0x09, 0x19, 0x29, 0x46 },   // r 
    { 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 },   // s 
    { 0x00, 0x01, 0x01, 0x7f, 0x01, 0x01 },   // t 
    { 0x00, 0x3f, 0x40, 0x40, 0x40, 0x3f },   // u 
    { 0x00, 0x1f, 0x20, 0x40, 0x20, 0x1f },   // v 
    { 0x00, 0x3f, 0x40, 0x38, 0x40, 0x3f },   // w 
    { 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 },   // x 
    { 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 },   // y 
    { 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 },   // z 
    { 0x00, 0x00, 0x7f, 0x41, 0x41, 0x00 },   // [ 
    { 0x00, 0x55, 0x2a, 0x55, 0x2a, 0x55 },   // 55 
    { 0x00, 0x00, 0x41, 0x41, 0x7f, 0x00 },   // ] 
    { 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 },   // ^ 
    { 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 },   // _ 
    { 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 },   // ' 
    { 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 },   // a 
    { 0x00, 0x7f, 0x48, 0x44, 0x44, 0x38 },   // b 
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 },   // c 
    { 0x00, 0x38, 0x44, 0x44, 0x48, 0x7f },   // d 
    { 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 },   // e 
    { 0x00, 0x08, 0x7e, 0x09, 0x01, 0x02 },   // f 
    { 0x00, 0x18, 0xa4, 0xa4, 0xa4, 0x7c },   // g 
    { 0x00, 0x7f, 0x08, 0x04, 0x04, 0x78 },   // h 
    { 0x00, 0x00, 0x44, 0x7d, 0x40, 0x00 },   // i 
    { 0x00, 0x40, 0x80, 0x84, 0x7d, 0x00 },   // j 
    { 0x00, 0x7f, 0x10, 0x28, 0x44, 0x00 },   // k 
    { 0x00, 0x00, 0x41, 0x7f, 0x40, 0x00 },   // l 
    { 0x00, 0x7c, 0x04, 0x18, 0x04, 0x78 },   // m 
    { 0x00, 0x7c, 0x08, 0x04, 0x04, 0x78 },   // n 
    { 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 },   // o 
    { 0x00, 0xfc, 0x24, 0x24, 0x24, 0x18 },   // p 
    { 0x00, 0x18, 0x24, 0x24, 0x18, 0xfc },   // q 
    { 0x00, 0x7c, 0x08, 0x04, 0x04, 0x08 },   // r 
    { 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 },   // s 
    { 0x00, 0x04, 0x3f, 0x44, 0x40, 0x20 },   // t 
    { 0x00, 0x3c, 0x40, 0x40, 0x20, 0x7c },   // u 
    { 0x00, 0x1c, 0x20, 0x40, 0x20, 0x1c },   // v 
    { 0x00, 0x3c, 0x40, 0x30, 0x40, 0x3c },   // w 
    { 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 },   // x 
    { 0x00, 0x1c, 0xa0, 0xa0, 0xa0, 0x7c },   // y 
    { 0x00, 0x44, 0x64, 0x54, 0x4c, 0x44 },   // z 
    { 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }    // horiz lines 
}; 
/************************lcd初始化函数********************************/  
void lcd_init(void) 

    //先设置为输出 
     pinmode(sclk,output); 
     pinmode(sdin,output);
     pinmode(lcd_dc,output); 
     pinmode(lcd_ce,output);
     pinmode(lcd_rst,output);
    
    // 产生一个让lcd复位的低电平脉冲 
     digitalwrite( lcd_rst, low); 
    
     delaymicroseconds(1);
     digitalwrite( lcd_rst, high); 
     
    // 关闭lcd 
     digitalwrite( lcd_ce, low); 
     delaymicroseconds(1);

    // 使能lcd 
     digitalwrite( lcd_ce, high); //lcd_ce = 1; 
     delaymicroseconds(1); 
    lcd_write_byte(0x21, 0); // 使用扩展命令设置lcd模式 
    lcd_write_byte(0xc8, 0); // 设置偏置电压 
    lcd_write_byte(0x06, 0); // 温度校正 
    lcd_write_byte(0x13, 0); // 1:48 
    lcd_write_byte(0x20, 0); // 使用基本命令 
    lcd_clear();             // 清屏 
    lcd_write_byte(0x0c, 0); // 设定显示模式,正常显示 
         
    // 关闭lcd 
   digitalwrite( lcd_ce, low);  //lcd_ce = 0; 

/************************lcd清屏函数*******************************/ 
void lcd_clear(void) 

    unsigned int i; 
    lcd_write_byte(0x0c, 0);  
    lcd_write_byte(0x80, 0); 
    for (i=0; i<504; i++) 
      { 
        lcd_write_byte(0, 1); 
      }    

/*************************设置字符位置函数**************************/
void lcd_set_xy(unsigned char x, unsigned char y) 

    lcd_write_byte(0x40 | y, 0);// column 
    lcd_write_byte(0x80 | x, 0);// row 

/*************************ascii字符显示函数*************************/ 
void lcd_write_char(unsigned char c) 

    unsigned char line; 
    c -= 32; 
    for (line=0; line<6; line++) 
    { 
        lcd_write_byte(font6x8[c][line], 1); 
    } 

/*******************************************************************/ 
/*------------------------------------------------- 
lcd_write_english_string  : 英文字符串显示函数 
输入参数:*s      :英文字符串指针; 
          x、y    : 显示字符串的位置,x 0-83 ,y 0-5 
--------------------------------------------------*/ 
void lcd_write_english_string(unsigned char x,unsigned char y,char *s) 

    lcd_set_xy(x,y); 
    while (*s)  
    { 
     lcd_write_char(*s); 
     s++; 
    } 

/******************************************************************/ 
/*--------------------------------------------- 
lcd_write_byte    : 写数据到lcd 
输入参数:data    :写入的数据; 
          command :写数据/命令选择; 
---------------------------------------------*/ 
void lcd_write_byte(unsigned char dat, unsigned char command) 

    unsigned char i; 
   digitalwrite( lcd_ce, low); // 使能lcd_ce = 0 
    if (command == 0) 
    { 
     digitalwrite( lcd_dc, low);// 传送命令 lcd_dc = 0; 
    } 
    else 
    { 
      digitalwrite( lcd_dc, high);// 传送数据lcd_dc = 1; 
    } 

  for(i=0;i<8;i++) 
  { 
     if(dat&0x80) 
     { 
        digitalwrite( sdin, high);//sdin = 1; 
     } 
    else 
    { 
     digitalwrite( sdin, low);//sdin = 0; 
     } 
   digitalwrite( sclk, low);//sclk = 0; 
   dat = dat << 1; 
   digitalwrite( sclk, high);//sclk = 1; 
  } 
    digitalwrite( lcd_ce, high);//lcd_ce = 1; 
        


/*************************以下为主函数*****************************/

void loop()
{
  lcd_init();//初始化液晶     
  lcd_clear(); 

    lcd_write_english_string(0,0," --arduino-- ");
    lcd_write_english_string(0,2,"renge:");
   
    lcd_write_english_string(0,4,"design by kent"); 
    lcd_write_english_string(0,5,"    2010.6    "); 
    
    while(1)
    {
      digitalwrite(outputpin, low); // 使发出发出超声波信号接口低电平2μs
      delaymicroseconds(2);
      digitalwrite(outputpin, high); // 使发出发出超声波信号接口高电平10μs,这里是至少10μs
      delaymicroseconds(10);
      digitalwrite(outputpin, low);    // 保持发出超声波信号接口低电平
      int distance = pulsein(inputpin, high);  // 读出脉冲时间
      distance= distance/58;           // 将脉冲时间转化为距离(单位:厘米)
         if(distance>120)
       {
      
         lcd_write_english_string(35,2,"???");
        }
         else
        {
          lcd_write_english_string(60,2,"cm");
          lcd_set_xy(35, 2); 
          lcd_write_char( 0x30+distance%1000/100); //显示百位数
          lcd_write_char( 0x30+distance%100/10);   //显示十位数
          lcd_write_char( 0x30+distance%10);       //显示个位数
            
        
         } 
        delay(10);
      }         
   
}





    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约