So far it allows you to write strings of text to any position on the screen. Missing functions at the moment include custom-generated characters, and reading data back from the display data although these aren't essential for me at the moment.
So, hopefully someone will find this code useful? Let me know if you do.
Module code
Code: Select all
{
*****************************************************************************
* Name : MAX7456.BAS *
* Author : Ed Williamson-Brown *
* Notice : Copyright (c) 2009 MadInventions *
* : All Rights Reserved *
* Date : 25/06/2009 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
Module MAX7456
Include "string.bas"
Include "SPI.bas"
Const MAXREG_VM0 = $0
Const MAXREG_VM1 = $1
Const MAXREG_HOS = $2
Const MAXREG_VOS = $3
Const MAXREG_DMM = $4
Const MAXREG_DMAH =$5
Const MAXREG_DMAL = $6
Const MAXREG_DMDI = $7
Const MAXREG_CMM = $8
Const MAXREG_CMAH = $9
Const MAXREG_CMAL = $A
Const MAXREG_CMDI = $B
Const MAXREG_OSDM = $C
Const MAXREG_RB0 = $10
Const MAXREG_RB1 = $11
Const MAXREG_RB2 = $12
Const MAXREG_RB3 = $13
Const MAXREG_RB4 = $14
Const MAXREG_RB5 = $15
Const MAXREG_RB6 = $16
Const MAXREG_RB7 = $17
Const MAXREG_RB8 = $18
Const MAXREG_RB9 = $19
Const MAXREG_RB10 = $1A
Const MAXREG_RB11 = $1B
Const MAXREG_RB12 = $1C
Const MAXREG_RB13 = $1D
Const MAXREG_RB14 = $1E
Const MAXREG_RB15 = $1F
Const MAXREG_OSDBL = $6C
//Read only registers
Const MAXREG_STAT = $A0
Const MAXREG_DMDO = $B0
Const MAXREG_CMDO = $C0
Const CharTable() As Char =
(
" ","1","2","3","4","5","6","7","8","9","0",
"A","B","C","D","E","F","G","H","I","J","K",
"L","M","N","O","P","Q","R","S","T","U","V",
"W","X","Y","Z","a","b","c","d","e","f","g",
"h","i","j","k","l","m","n","o","p","q","r",
"s","t","u","v","w","x","y","z","(",")",".",
"?",";",":",",",$27,"/","'","-","<",">","@" )
Const char_to_char_code_table() As Byte =
(
$00,$01,$02,$03,$04,$05,$06,$07,$08,$09,$0A,
$0B,$0C,$0D,$0E,$0F,$10,$11,$12,$13,$14,$15,
$16,$17,$18,$19,$1A,$1B,$1C,$1D,$1E,$1F,$20,
$21,$22,$23,$24,$25,$26,$27,$28,$29,$2A,$2B,
$2C,$2D,$2E,$2F,$30,$31,$32,$33,$34,$35,$36,
$37,$38,$39,$3A,$3B,$3C,$3D,$3E,$3F,$40,$41,
$42,$43,$44,$45,$46,$47,$48,$49,$4A,$4B,$4C)
//*****************************************************************************
Sub init_SPI()
SPI.SetAsMaster(spiOscDiv16)
SPI.SetSample(spiSampleMiddle)
SPI.SetClock(spiIdleHigh,spiRisingEdge )
SPI.Enabled = true
End Sub
Sub select_Max()
PORTC.0=0
DelayUS(1)
End Sub
Sub deselect_Max()
PORTC.0=1
DelayUS(1)
End Sub
//*****************************************************************************
// MAX7456 low level code
Sub MAXWrite(addr As Byte, data As Byte)
select_Max()
SPI.WriteByte(addr)
DelayUS(1)
SPI.WriteByte(data)
deselect_Max()
End Sub
//-----------------------------------------------------
sub ResetMAX7456()
maxwrite(MAXREG_VM0,%01000110) // Software reset
delayms(1)
end sub
//-----------------------------------------------------
Sub EnableMAX7456()
MAXWrite(MAXREG_VM0,%01000100) // PAL, autosync, OSD off, VSYNC, buffer enabled
End Sub
//-----------------------------------------------------
Sub DisableMAX7456()
MAXWrite(MAXREG_VM0,%01000101) // PAL, disabled
End Sub
//-----------------------------------------------------
Sub EnableMAX7456_with_OSD()
MAXWrite(MAXREG_VM0,%01001100) // PAL, enabled with OSD enabled
End Sub
//-----------------------------------------------------
Function Get_Charcode_From_Table(c As String) As Byte
Dim count As Byte
Dim rv_char As Byte
rv_char=0
count = 0
For count=0 To Bound(CharTable)
If c = CharTable(count) Then
rv_char = char_to_char_code_table(count)
Break
End If
Next
If count=Bound(CharTable) Then
rv_char=1 //Should be '0' later...
End If
Get_Charcode_From_Table = rv_char
End Function
//-----------------------------------------------------
Sub InitAutoIncrement()
MAXWrite(MAXREG_DMM,%00000001)
End Sub
//-----------------------------------------------------
Sub OffAutoIncrement()
MAXWrite(MAXREG_DMM,0)
End Sub
//-----------------------------------------------------
Sub SendStopAutoAddressIncrementData()
select_Max()
SPI.WriteByte($ff)
deselect_Max()
End Sub
//-----------------------------------------------------
Sub WriteText(txt As String)
Dim data As Byte
Dim i As Byte
i=0
InitAutoIncrement()
While txt(i) <> null
data = Get_Charcode_From_Table(mid(txt,i,1))
select_Max()
SPI.WriteByte(data)
deselect_Max()
i=i+1
Wend
SendStopAutoAddressIncrementData()
OffAutoIncrement()
End Sub
//-----------------------------------------------------
Sub SetCursor(x As Byte, y As Byte)
Dim msb As Byte
Dim lsb As Byte
Dim temp_line_num As Integer
If (x>30 Or x=0 Or y>30 Or y=0) Then
lsb=4
msb=0
Else
temp_line_num = ((y-1)*30)+x
lsb = temp_line_num And 255
temp_line_num = temp_line_num >> 8
msb = temp_line_num And 255
msb=msb And 1 //Ensure we're writing to the Data memory...
End If
select_Max()
MAXWrite(MAXREG_DMAH,msb)
MAXWrite(MAXREG_DMAL,lsb)
deselect_Max()
End Sub
//-----------------------------------------------------
//*****************************************************************************
// MAX7456 high level code
//*****************************************************************************
// public routines
Public Sub Init_MAX7456()
init_SPI()
DelayMS(1)
ResetMAX7456()
delayms(1)
EnableMAX7456()
DelayMS(1)
DisableMAX7456()
DelayMS(1)
EnableMAX7456()
DelayMS(1)
EnableMAX7456_with_OSD()
DelayMS(1)
End Sub
//-----------------------------------------------------
Public Sub SetXY(x As Byte, y As Byte)
SetCursor(x,y)
End Sub
//-----------------------------------------------------
Public Sub Print(text As String)
// DisableMAX7456()
WriteText(text)
// EnableMAX7456()
End Sub
//-----------------------------------------------------
Public Sub Clearline(y As Byte)
SetCursor(0,y)
// 123456789012345678901234567890
WriteText(" ")
End Sub
//-----------------------------------------------------
// initialise module
//Initialize
To use the module, use the following code in your main bas file (note that I'm using a PIC18F2610)
Ed.
Code: Select all
{
*****************************************************************************
* Name : GPS_OBD.BAS *
* Author : Ed Williamson-Brown *
* Notice : Copyright (c) 2009 MadInventions *
* : All Rights Reserved *
* Date : 25/06/2009 *
* Version : 1.0 *
* Notes : *
* : *
*****************************************************************************
}
// device and clock
Device = 18F2610
Clock = 8
Config osc = INTIO67, WDT = OFF, MCLRE = ON , PWRT = off
#option ICD = false
// import modules...
Include "ICD_debug.bas"
Include "MAX7456.bas"
Include "Convert.bas"
// local variables
Dim counter As Integer
OSCCON = %01110010
counter=0
Output (PORTC.0)
Output (PORTC.1)
PORTC.0=1
Init_MAX7456()
DelayMS(1)
SetXY(9,2)
Print("MadInventions!")
While True
SetXY(1,15)
Print("Count = " + DecToStr(counter)+" ")
counter=counter+1
If counter<25 Then
PORTC.1=1
setxy(20,15)
print ("LED ON ")
Else
PORTC.1=0
setxy(20,15)
print ("LED OFF")
End If
If counter>50 Then counter = 1 End If
DelayMS(100)
Wend
Code: Select all
Include "MAX7456.bas"
Output (PORTC.0)
PORTC.0=1
Init_MAX7456()
DelayMS(1)
SetXY(9,2)
Print("MadInventions!")