
/
// nokia 5110 arduino驅動程序(中文,圖形顯示。超聲波測距)
// 版本1.1
// 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);
}
//
unsigned char boy_bmp[]=
{
0xff,0x01,0x81,0xc1,0xf1,0xf9,0xf9,0xfd,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xfd,0xf9,0xf1,0xe1,0x81,0x01,0xff,0xff,0x00,0x1f,0x7f,0xff,0x3f,0x27,0x27,
0x27,0x27,0x3f,0x17,0x3f,0x27,0x27,0x27,0x27,0x3f,0xff,0x3f,0x1f,0x0f,0x00,0xff,
0xff,0xf0,0xfc,0xfc,0xfe,0xff,0xfe,0xfc,0xec,0xf8,0xfa,0x5a,0xfa,0xf8,0xec,0xfc,
0xfe,0xff,0xfe,0xfe,0xfc,0xf8,0xf0,0xff,
};
//
const unsigned char hzk[][24]={
//深
{0x08,0xf1,0x1a,0x40,0x56,0xca,0x46,0xf2,0xc6,0x4a,0x56,0x00,0x01,0x07,0x00,0x02,0x01,0x00,0x00,0x07,0x00,0x01,0x02,0x00,},
//入
{0x00,0x00,0x00,0xc1,0x31,0x0e,0x30,0xc0,0x00,0x00,0x00,0x00,0x04,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x02,0x04,0x00,},
//互
{0x02,0x02,0x82,0xfe,0x8a,0x8a,0x8a,0xfa,0x02,0x03,0x02,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,0x00,},
//動
{0x92,0x52,0x32,0x52,0x92,0x10,0x08,0xff,0x08,0x08,0xf8,0x00,0x03,0x01,0x01,0x01,0x05,0x02,0x01,0x04,0x04,0x06,0x01,0x00,},
//科
{0x8a,0x6a,0xfe,0x29,0x49,0x80,0x92,0xa4,0x80,0xff,0x40,0x00,0x01,0x00,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x07,0x00,0x00,},
//技
{0x44,0x44,0xff,0x24,0x00,0x64,0xa4,0x3f,0xa4,0x64,0x04,0x00,0x04,0x04,0x07,0x00,0x04,0x04,0x02,0x01,0x02,0x04,0x04,0x00,},
};
//
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 }, // <<SPAN class=apple-converted-space>
{ 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
};
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;
}
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
}
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);
}
}
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++;
}
}
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 lcd_write_chinese_string(unsigned char x, unsigned char y,
unsigned char ch_with,unsigned char num,
unsigned char line,unsigned char row)
{
unsigned char i,n;
lcd_set_xy(x,y); //設置初始位置
for (i=0;i
{
for (n=0; n
{
if (n==ch_with) //寫漢字的下半部分
{
if (i==0) lcd_set_xy(x,y+1);
else
{
lcd_set_xy((x+(ch_with+row)*i),y+1);
}
}
lcd_write_byte(hzk[line+i][n],1);
}
i++;
lcd_set_xy((x+(ch_with+row)*i),y);
}
}
void lcd_draw_bmp_pixel(unsigned char x,unsigned char y,unsigned char *map,
unsigned char pix_x,unsigned char pix_y)
{
unsigned int i,n;
unsigned char row;
if (pix_y%8==0)
{
row=pix_y/8; //計算位圖所占行數
}
else
{
row=pix_y/8+1;
}
for (n=0;n
{
lcd_set_xy(x,y);
for(i=0; i
{
lcd_write_byte(map[i+n*pix_x], 1);
}
y++; //換行
}
}
void loop()
{
lcd_init();//初始化液晶
lcd_clear();//清屏
lcd_draw_bmp_pixel(30,0,boy_bmp,24,24); //調入上面的圖像
lcd_write_english_string(4,2,"----"); //寫入ascii字符
lcd_write_english_string(55,2,"----");
lcd_write_chinese_string(6,3,12,6,0,0); //寫入中文字符
lcd_write_english_string(20,5,"arduino");
delay(5000);
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) //如果距離大于120cm
{
lcd_write_english_string(35,2,"???"); //在(35, 2)坐標上顯示“???”
}
else
{
lcd_write_english_string(60,2,"cm"); //如果小于120cm
lcd_set_xy(35, 2); //在(35, 2)坐標上顯示測出數值
lcd_write_char( 0x30+distance00/100); //顯示百位數
lcd_write_char( 0x30+distance0/10); //顯示十位數
lcd_write_char( 0x30+distance); //顯示個位數
}
}
}