lua—GPRS模块开发之I2C OLED模块驱动

来源:互联网 发布:2083欧洲独立宣言 知乎 编辑:程序博客网 时间:2024/05/16 09:21
 终于要从学校滚蛋了,前几天TB上发现一个神器,一种可以用来编程的GPRS模块,哈哈,感觉物联网时代已经到来,牛掰!多的不说啦,什么MQTT之类,socket之类的demo luat团队已经给的很完善了。我就做demo没有的,驱动个I2C OLED .当然首先得有一点单片机基础了啦!之前大学四年就搞了点单片机,感觉自己好废,不过这个对以后工作还是有用呢。个人感觉学单片机学的就是接口技术(拙见),因为以前弄过相似的产品,SO,搞定这个现实还是很轻松的,当然来,要谢谢luat团队对底层代码的维护升级! 选择这个作为我的第一个博客就是因为AIR200这个产品让我看见物联网的半边天!多的不说啦,什么是I2c协议啥啥啥的就不多说了,默认你已经懂I2C、已经熟读这个OLED的驱动芯片资料啦,我还是个小学生,代码可能有bug,还请多多指教

本着开源精神,贡献代码如下,可以显示6x8字符,可以显示图片,其他自己去研究啦,下面这个链接是代码源文件下载链接,手头宽裕就给几个积分呗,不给也行,我已经附上源代码啦
http://download.csdn.net/detail/a13698709128/9850149

module(…,package.seeall)

local F6x8 =–6x8字库
{
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
}

local xinhaomange=
{0x07,0x09,0x11,0x21,0x41,0x41,0xFF,0x41,0x41,0x21,0x11,0x09,0x87,0x00,0xE0,0x00,0xF8,0x00,0xFE,
0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x0C,0x00,0x0F,0x00,0x0F,0x00,0x0F,0x00,0x0F,0x00,0x0f}

local xinhaoyiban={
0x07,0x09,0x11,0x21,0x41,0x41,0xFF,0x41,0x41,
0x21,0x11,0x09,0x87,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,
0x00,0x00,0x0F,0x00,0x0F,0x00,0x00,0x00,0x00,
0x00,0x00}

local i2cid = 1
–local i=1

local function print(…) –串口打印函数,输出时候加上test
_G.print(“test”,…)
end
– 写命令
local function write_cmd(cmd)
local back=i2c.write(i2cid,0x00,cmd)
print(“writecmd “)
print(back)
return back
end

local function write_dat(dat)

local back=i2c.write(i2cid,0x40,dat)
return back

end

local function Set_Postion(a,b)
–write_cmd(0xb0+b)
local result

result= i2c.write(i2cid,0x00,0xb0+b)
print(“result——->>>”)
print(result)
result= i2c.write(i2cid,0x00,bit.bor(0x10,bit.rshift(bit.band(a,0xf0),4)))
print(“result——->>>”)
print(result)
result= i2c.write(i2cid,0x00,bit.bor(0x01,(bit.band(a,0x0f))))
print(“result——->>>”)
print(result)
–write_cmd(bit.bor(0x10,bit.rshift(bit.band(a,0xf0),4)))
–write_cmd(bit.bor(0x01,(bit.band(a,0x0f))))
end
local teststring=”123456”
local function OLED_P6x8Str(x, y,ch)
– i2c.setup(i2cid,i2c.SLOW,Address)
local c,i,j=0,0,1
for d=1,string.len(ch),1 do
print(“———————>>>>>>”)
c=string.byte(ch,d)-32–获取当前的字符ASCII
print(string.char(string.byte(ch,d)))
if(x>127)then
x=0
y=y+1
end

Set_Postion(x,y)for i=1,6,1 do  -- write_dat(F6x8[c*6+i])  i2c.write(i2cid,0x40,F6x8[c*6+i])  print("   char n ascii\r\n")  print( string.format("%02X",F6x8[c*6+i]))endx=x+6j=j+1

end
–i2c.close(i2cid)
end

local function OLED_FILL(bmpdat)
local x,y=0,0
for y=1,8,1 do
i2c.write(i2cid,0x00,0xb0+y-1)
i2c.write(i2cid,0x00,0x01)
i2c.write(i2cid,0x00,0x10)

for x=0,127,1 do  i2c.write(i2cid,0x40,bmpdat)  -- print("init",string.format("%02X",bmpdat))end

end

end
local function Device_Init()
local i2cslaveaddr =0x3c –芯片地址
local Brightness=0xcf

local cmd = {0xAE,0x00,0x10,0x40,0x81,Brightness,0xA1,0xC8,0xA6,0xA8,0x3F,0xD3,0x00,0xD5,0x80,
0xD9,0xF1,0xDA,0x12,0xDB,0x40,0x20,0x02,0x8D,0x14,0xA4,0xA6,0xAF}

fre=i2c.setup(i2cid,i2c.SLOW,i2cslaveaddr)
print(fre)–100kHz频率
if fre ~= i2c.SLOW then
print(“init fail !”)
return
end

print(“IIC Init Successful!”,rtos.poweron_reason())

– for i=1,table.getn(cmd),1 do
– result= write_cmd(cmd[i])
– i2c.setup(i2cid,i2c.SLOW,Address)
print(“wrote >>”)
wrote_result= i2c.write(i2cid,0x00,cmd)
print(wrote_result)
– print(“init”,string.format(“%02X”,cmd[i]))
– end
OLED_FILL(0x00)–清屏
–Set_Postion(0,0)

end

local function test()

print(“———————>>>>>>”)
– print(‘f’)
–c=string.byte(‘f’)
print(c)
local i=0
for d=1,string.len(teststring),1 do
c=string.sub(teststring,d)
print(c)

end

end

local function showpic(x0,y0,x1,y1,pic)–起始地址,图片长度,宽度
local pic_back
local j,x,y=1,0,0
local cyctime,t=0,0–cyctime用来记录当前图片需要占用几行来显示
local length=table.getn(pic)
if((y1%8)==0) then –说明还要占一行
cyctime=y1/8
else
cyctime=y1/8+1 –此时cyctime记录的是需要写几行
end

– y1=y

for t=1,cyctime,1 do –一行一行的来写,竖着的8
Set_Postion(x0,y0+t-1)–因为t的初始值为1,所以要减去一才能够从0,0坐标开始显示,才是正确位置
print(“xxxxxxxx0”,x0)
for x=x0,x1+x0-1,1 do–循环x1-x0次,也就是图片的长度
print(“j”)
i2c.write(i2cid,0x40,pic[j])
print(j)
if(j

原创粉丝点击