本文包含原理图、PCB、源代码、封装库、中英文PDF等资源
您需要 登录 才可以下载或查看,没有账号?注册会员
×
PC0接复位、PC1接时钟、PC2输出数据、PC3接D/C、PC5测试电压
$regfile = "m88def.dat"
$crystal = 1000000
'-------------------------------------------------------------------------------
Dc Alias Portc.3
Ddrc = &B111011111 : Portc = 0
'-------------------------------------------------------------------------------
Config Adc = Single , Prescaler = Auto , Reference = Internal
Config Spi = Soft , Din = Portc.6 , Dout = Portc.2 , Ss = None , Clock = Portc.1
Spiinit
'-------------------------------------------------------------------------------
Dim A As Byte , I As Word , X As Byte , Y As Byte , C As Byte
Dim Ac As Long , Zf As String * 4 , Gs As Byte , Zf1 As String * 1
'-------------------------------------------------------------------------------
Portc.0 = 0 : Waitms 10 : Portc.0 = 1 : Waitms 10
Do
'初始化3310LCD
Dc = 0
Restore Begin
Gosub Out_data : Gosub Out_data : Gosub Out_data : Gosub Out_data
'清屏,光标回位
Dc = 1
For I = 1 To 4032
A = 0 : Spiout A , 1
Next
Dc = 0
X = 128 : Gosub Out_x
Y = 64 : Gosub Out_y
'显示"电压"
Dc = 1
X = 128 : Y = 64
Restore Aa
For I = 1 To 32
Gosub Out_data : Gosub Out_data
Dc = 0
X = X + 1 : Gosub Out_x
Y = 64 : Gosub Out_y
Dc = 1
Next
'检测电压
Ac = Getadc(5)
Ac = Ac * 1100
Ac = Ac / 1023
'把电压数值转变为字符
Zf = Str(ac)
'求字符的个数
Gs = Len(zf)
'显示电压值的字符
For I = 1 To Gs
Zf1 = Mid(zf , I , 1)
Select Case Zf1
Case "0" : Restore A0
Case "1" : Restore A1
Case "2" : Restore A2
Case "3" : Restore A3
Case "4" : Restore A4
Case "5" : Restore A5
Case "6" : Restore A6
Case "7" : Restore A7
Case "8" : Restore A8
Case "9" : Restore A9
End Select
For C = 1 To 8
Gosub Out_data : Gosub Out_data
Dc = 0
X = X + 1 : Gosub Out_x
Gosub Out_y
Dc = 1
Next
Next
'显示单位"mV"
Dc = 1
Restore Ab
For I = 1 To 16
Gosub Out_data : Gosub Out_data
Dc = 0
X = X + 1 : Gosub Out_x
Gosub Out_y
Dc = 1
Next
Waitms 3000
Loop
End
'-------------------------------------------------------------------------------
Out_x:
Spiout X , 1
Return
'-------------------------------------------------------------------------------
Out_y:
Spiout Y , 1
Return
'-------------------------------------------------------------------------------
Out_data:
Read A : Spiout A , 1
Return
'*******************************************************************************
'----------------------------------初始化数据-----------------------------------
Begin:
Data &B00100001 , &B10010000 , &B00100010 , &B00001100
'-------------------------------------电压 -------------------------------------
Aa:
Data 0 , 0 , 0 , 0 , 248 , 15 , 72 , 4 , 72 , 4 , 72 , 4 , 72 , 4 , 255 , 63
Data 72 , 68 , 72 , 68 , 72 , 68 , 72 , 68 , 248 , 79 , 0 , 64 , 0 , 112 , 0 , 0
Data 0 , 32 , 0 , 24 , 254 , 39 , 2 , 32 , 66 , 32 , 66 , 32 , 66 , 32 , 66 , 32
Data 250 , 63 , 66 , 32 , 66 , 33 , 66 , 46 , 98 , 36 , 66 , 32 , 2 , 32 , 0 , 0
'--------------------------------------mV --------------------------------------
Ab:
Data 128 , 32 , 128 , 63 , 128 , 32 , 128 , 0 , 128 , 63 , 128 , 32 , 128 , 0 , 0 , 63
Data 8 , 0 , 120 , 0 , 136 , 7 , 0 , 56 , 0 , 14 , 200 , 1 , 56 , 0 , 8 , 0
'---------------------------------------0---------------------------------------
A0:
Data 0 , 0 , 224 , 15 , 16 , 16 , 8 , 32 , 8 , 32 , 16 , 16 , 224 , 15 , 0 , 0
'---------------------------------------1---------------------------------------
A1:
Data 0 , 0 , 16 , 32 , 16 , 32 , 248 , 63 , 0 , 32 , 0 , 32 , 0 , 0 , 0 , 0
'---------------------------------------2---------------------------------------
A2:
Data 0 , 0 , 112 , 48 , 8 , 40 , 8 , 36 , 8 , 34 , 136 , 33 , 112 , 48 , 0 , 0
'---------------------------------------3---------------------------------------
A3:
Data 0 , 0 , 48 , 24 , 8 , 32 , 136 , 32 , 136 , 32 , 72 , 17 , 48 , 14 , 0 , 0
'---------------------------------------4---------------------------------------
A4:
Data 0 , 0 , 0 , 7 , 192 , 4 , 32 , 36 , 16 , 36 , 248 , 63 , 0 , 36 , 0 , 0
'---------------------------------------5---------------------------------------
A5:
Data 0 , 0 , 248 , 25 , 8 , 33 , 136 , 32 , 136 , 32 , 8 , 17 , 8 , 14 , 0 , 0
'---------------------------------------6---------------------------------------
A6:
Data 0 , 0 , 224 , 15 , 16 , 17 , 136 , 32 , 136 , 32 , 24 , 17 , 0 , 14 , 0 , 0
'---------------------------------------7---------------------------------------
A7:
Data 0 , 0 , 56 , 0 , 8 , 0 , 8 , 63 , 200 , 0 , 56 , 0 , 8 , 0 , 0 , 0
'---------------------------------------8---------------------------------------
A8:
Data 0 , 0 , 112 , 28 , 136 , 34 , 8 , 33 , 8 , 33 , 136 , 34 , 112 , 28 , 0 , 0
'---------------------------------------9---------------------------------------
A9:
Data 0 , 0 , 224 , 0 , 16 , 49 , 8 , 34 , 8 , 34 , 16 , 17 , 224 , 15 , 0 , 0
还是那句话:写程序不仅靠经验、弄出来就行了,还要整理,要规范,或者说要有一些好的习惯,例如:
Restore Begin
Gosub Out_data : Gosub Out_data : Gosub Out_data : Gosub Out_data
什么意思?人家搞不懂,过一段时间自己也搞不懂。既然是初始化,就写成:
Gosub InitLCDxx
多好?
X = 128 : Gosub Out_x
Y = 64 : Gosub Out_y
这样就没必要了,写一个过程,直接:
Moveto 128, 64 或 Moveto X, Y
是否更好?
当然,要和D/C配套,可以只用一个函数发送数据, 一类是data, 另一类是command,例如:
Const CmdByte = 0
Const DataByte = 1
Sub SendByte(Byval CmdType as Byte, Byval vData As Byte)
Dc = CmdType
Spiout vDate , 1
End Sub
如果写成:
Sub SendByte(Byval CmdType as Byte)
Dc = CmdType
Read A : Spiout A , 1
End Sub
看起来省了事,其实不大好,因为函数是强耦合的,程序要改动或Copy到其他地方使用就比较麻烦,整个环境都得改。
当然,这只是例子,在你的程序中,写数据到LCD无非就是两种文本:一种是中文,一种是Ascii字符,这样程序一共用到的过程是:
InitLCD '初始化LCD
MoveTo/Locate '定位光标
WriteCH '写一个中文字符
WriteAsc '写一个Asc字符(数字)
写中文时不要把[电压]两个字放到一起,这样的程序很难作他用,例如下次要写3个字或1个字得改代码部分,而一个写中文字符的函数给出不同的数据就可以了。 |