Tuesday, 19 March 2024
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
 
     
 

 
   
     
 
AN #148 - LCD display with touchscreen and AVR Print
Application about how to connect LCD display with touchscreen to M128 AVR chip and make program in BASCOM-AVR.
Author : Evert Dekker  ( lampje at xs4all dot nl )

Intro

I found on the internet  (www.ledsee.com) a very cheap, only € 17,95  128*64 graphic display with touchscreen. It was sold with a controller for the touchscreen, but it would be nicer to read the touchscreen with the ad-converter that’s build in much of the avr processors.

The big advance of a touchscreen is that all is in the software. If you make the traditional keypad and you need an extra key, or maybe change the key layout. It’s not possible, or I will cost you money. For the touchscreen just write some code and finished.

The display has a Ks-108 controller, so controlling the display is easy, Mark has done all the work for this.

For this display a different datasheets on the net all with other connections. After some research the correct connections are;

1

Cs1

11

Db2

2

Cs2

12

Db3

3

Gnd

13

Db4

4

+5V

14

Db5

5

Contrast

15

Db6

6

Cd

16

Db7

7

Read

17

Reset

8

Enable

18

Negative pwr

9

Db0

19

Backlight A

10

Db1

20

Backlight K



I didn’t connect pin 19 & 20 for the backlight. If you close the jumper (figure 1) on the back of the display the backlight it’s connected with the display’s powersupply. Also here you can see how to connect the touch screen with the Avr. In the circuit diagram there are some resistors in series with the touchscreen. This is not really necessary, but only for protecting the avr if you make a program mistake and shorting the io pins.


The basics of a touch screen

The touchscreen that’s used here is a 4 wire resistive touchscreen. It’s not more then 2 big resistors, one with the connection leads to the top and the other with the leads to the side. By pressing on the screen your making contact between the top and bottom layer. To determine the cordinates you read first the x value and then the y value. To say it simple;


Touchscreen x read, Set PF0 high and PF2 low and read PF1 or PF3.
Touchscreen y read, Set PF1 high and PF3 low and read PF0 or PF2.


The application note

First of all i created a virtual keypad on the touchscreen



I have chosen for a 3x4 matrix where the upper row is smaller.

To determine the values as shown in figure 4, unremark the “Print Value” lines in the subroutine “Readtouch”, we need those values in the “Whichkey” subroutine to determine the begin and end of each key.

The buttons that i have used are 32x24 pixels. This is a nice size that you can operate with your finger.

The build in font’s of the display or bascom are too large for my application so i draw my own buttons with build in fonts, see the bmp’s in the download file.

The result

The BIG guys are using a calibration function, temperature compensation, but i don’t think this is necessary for such small screen.

And here is the result created with the source code below:









Source code :

$regfile = "m128def.DAT"
$crystal = 7372800
$baud = 19200
$eepleave
$loadersize = 512
$hwstack = 100
$swstack = 75
$framesize = 40
$lib "glcdKS108.lib" 'Include the library for the KS-108 controler


Config Graphlcd = 128 * 64sed , Dataport = Porta , Controlport = Portc , Ce = 0 , Ce2 = 1 , Cd = 2 , Rd = 3 , Reset = 5 , Enable = 4
'The dataport is the portname that is connected to the data lines of the LCD
'The controlport is the portname which pins are used to control the lcd
'CE =CS1 Chip select
'CE2=CS2 Chip select second chip
'CD=Data/instruction
'RD=Read
'RESET = reset
'ENABLE= Chip Enable


Config Adc = Single , Prescaler = Auto , Reference = Internal 'Setting up the a/d convertor
Config Timer1 = Timer , Prescale = 1024 ' Timer 1 sets the screen back to the mainmenu after 10sec
Const Timer1preload = 58336 'Timer 1 preload for 1 sec interrupt


Speaker
Alias Portf.7 'Buzzer connected to portf.7, see circuit diagram
Dim Temp As Byte , X As Word , Y As Word
Dim Row As Byte , Keyarray(3) As Byte , Col As Byte , Key As Byte , Keylus As Byte
Dim Keypressed As Byte , Menu As Byte
Dim Timecount As Byte
Setfont Font8x8
Enable Ovf1
Enable Interrupts
On Timer1 1secint ' on overflow jump to 1 sec int routine
Start Adc ' Start the ad convertor
Stop Timer1

Gosub Showmainmenu ' Starts with the Mainmenu
' =====Main=====
Main
:
Do
'*************************
' Here your main prog
'*************************
 
Gosub Readtouch  ' Read the touch cordinates
 
Gosub Whichkey ' Which key is pressed
If Menu > 1 Then ' Starts the timer if the menu is not the Mainmenu
  
Start Timer1
Else
 
Stop Timer1
End If

If Keypressed > 0 Then ' Key is pressed
 
Select Case Menu ' Depends on the menu that we are in what to do with the pressed key
 
Case 1 : Select Case Keypressed 'Mainmenu
 
Case 11 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 12 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 13 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 21 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 22 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 23 : Gosub Showlichteettafelmenu ' Shows a sub menu
 
Case 31 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 32 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 33 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 41 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 42 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 43 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
End Select

Case 13 : Select Case Keypressed 'LichtEetafelmenu (menu 13)
 
Case 11 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 12 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 13 : Gosub Showmainmenu ' Back to the mainmenu
 
Case 21 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 22 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 23 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 31 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 32 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 33 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 41 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 42 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
Case 43 : Print " You pressed key: " ; Keypressed ; " in menu: " ; Menu
 
End Select
End Select
 Keypressed
= 0 ' Key is processed so put it back to 0
End If
Loop


'=== Subroutines===
$include "font8x8.font"

1secint
: ' Interrupt routine will set the screen back to the mainmenu in 10 seconds
Incr Timecount ' increment every sec the counter with 1
If Timecount => 10 Then ' here is the 10 sec
 
Stop Timer1 : Timecount = 0
 
Gosub Showmainmenu ' Sets the screen
Else
 
Timer1 = Timer1preload
End If
Return


Showmainmenu
: 'Shows the main menu
Menu
= 1 'Menu number
Cls 'Clears the screen
Showpic 0 , 0 , Headerhoofdmenu ' Draw the 9 pictures on the screen
Showpic 0 , 16 , Jalvoor
Showpic 32 , 16 , Jalachter
Showpic 64 , 16 , Lichtkeuken
Showpic 96 , 16 , Autoprog
Showpic 0 , 40 , Lichtsalontafel
Showpic 32 , 40 , Lichteettafel
Showpic 64 , 40 , Schemerlamp
Showpic 96 , 40 , Pijlrechts
Return

Showlichteettafelmenu
: ' Shows the ShowLichtEetafel menu (sub menu 13)
Menu
= 13 ' Menu number
Cls  'Clears the screen
Showpic 0 , 0 , Headerlichteettafel ' Draw the 9 pictures on the screen
Showpic 0 , 16 , Stopbutton
Showpic 32 , 16 , Pijlomhoog
Showpic 64 , 16 , F1preset
Showpic 96 , 16 , F2preset
Showpic 0 , 40 , Escbutton
Showpic 32 , 40 , Pijlomlaag
Showpic 64 , 40 , F3preset
Showpic 96 , 40 , F4preset
Return


Whichkey
: 'Determins which key is pressed
Select Case X ' For the x value
 
Case 200 To 340 : Col = 10 ' The cordinates on the touchscreen determins which key is pressed for example;
 
Case 341 To 486 : Col = 20  ' 341 to 486 are the cordinates where between the second column is
 
Case 487 To 635 : Col = 30
 
Case 636 To 774 : Col = 40
 
Case Else Col = 0
End Select
Select Case Y  ' For the y value
 
Case 250 To 360 : Row = 1 ' The cordinates on the touchscreen determins which key is pressed for example;
 
Case 361 To 540 : Row = 2 ' 361 to 540 are the cordinates where between the second row is
 
Case 541 To 730 : Row = 3
 
Case Else Row = 0
End Select
Key
= Col + Row ' Add the row and column value so we get 1 key value
If Key > 0 Then ' There is a key pressed
 Keyarray
(keylus) = Key 'Must read the same key 3 times in a row, to prefent bouncing
 
Incr Keylus
 
If Keylus > 3 Then Keylus = 1
 
If Keyarray(1) = Keyarray(2) Then
 
If Keyarray(2) = Keyarray(3) Then ' Key is correct read 3 times the same
 
Sound Speaker , 1 , 65000 ' Give a key beep
 Keypressed
= Key
 Timecount
= 0
 
End If
 
End If
 
End If
Return

Readtouch
:
Config Pinf.0 = Output ' Makes port F.0 output
Config Pinf.2 = Output ' Makes port F.0 output
Set Portf.0 ' Sets port F.0 High
Reset Portf.2 ' Sets port F.2 Low
Ddrf.1 = 0 ' Sets port F.1 as input
Ddrf.3 = 0 ' Sets port F.1 as input because we need it now as ad input
Waitms 20 ' Wait until the port is stable
Y
= Getadc(3)  ' Read the ad value for the y
Y
= 1024 - Y ' Invert the reading
'Print "VALUE Y : " ; Y ' for debugging
Config Pinf.1 = Output  ' Makes port F.1 output
Config Pinf.3 = Output ' Makes port F.3 output
Reset Portf.1 ' Sets port F.1 Low
Set Portf.3 ' Sets port F.3 High
Ddrf.0 = 0 ' Sets port F.0 as input
Ddrf.2 = 0 ' Sets port F.2 as input because we need it now as ad input
Waitms 20 ' Wait until the port is stable
X
= Getadc(2) ' Read the ad value for the x
X
= 1024 - X ' Invert the reading
'Print "VALUE X : " ; X
Return


'=====The buttons images=====
Headerhoofdmenu
:
$bgf "HeaderHoofdmenu.bgf"
Headerlichteettafel
:
$bgf "HeaderLichtEettafel.bgf"
Jalvoor
:
$bgf "Jalvoor.bgf"
Jalachter
:
$bgf "Jalachter.bgf"
Lichtsalontafel
:
$bgf "LichtSalonTafel.bgf"
Lichteettafel
:
$bgf "LichtEetTafel.bgf"
Lichtkeuken
:
$bgf "Lichtkeuken.bgf"
Schemerlamp
:
$bgf "Schemerlamp.bgf"
Escbutton
:
$bgf "Esc.bgf"
Pijlomhoog
:
$bgf "PijlOmhoog.bgf"
Pijlomlaag
:
$bgf "PijlOmlaag.bgf"
Pijlrechts
:
$bgf "PijlRechts.bgf"
Pijllinks
:
$bgf "PijlLinks.bgf"
Autoprog
:
$bgf "Autoprog.bgf"
Stopbutton
:
$bgf "Stop.bgf"
F1preset
:
$bgf "F1preset.bgf"
F2preset
:
$bgf "F2preset.bgf"
F3preset
:
$bgf "F3preset.bgf"
F4preset
:
$bgf "F4preset.bgf"
F3leeg
:
$bgf "F3Leeg.bgf"
F4leeg
:
$bgf "F4Leeg.bgf"
Leeg
:
$bgf "Leeg.bgf"