Code:
'****************************************************************************
'* Name : pic18f2520_tft_demo.pbp #### semi-Working Code #### * but not pretty, needs lots of work.
'* Author : RICHAED *
'* Notice : Copyright (c) 2015 *
'* : All Rights Reserved *
'* Date : 12/09/2015 *
'* Device : 18F2520 (32k) *
'* Version : ? 25295 bytes (PBP 3.1.4.4) *
'* Notes : sleep, pwm brightness (10 bit pwm) *
'* : new way to define tft pins *
'* : 24 BIT ADDRESS FOR STRINGS AND BUTTON TEXT *
'* : *
'* : See: www.picbasic.co.uk/forum/showthread.php?t=20115 *
'* : *
'* ======> : 3v3 screen! Draws ~ 80mA. *
'* : *
'* : TFT ILI9431 works okay but not pretty, needs lots of work. *
'* : *
'* : ST7735 (KMR-1.8" and Touch version each 128x160) work but the *
'* : code needs fix - only 1/4 of TFT size screen shown instead of *
'* : a reduced scale version to fit into 128x160 frame. *
'* : *
'* : #### How to put into Portrait mode? #### *
'* : *
'****************************************************************************
;
; 07Feb22 Huge trawling exercise to sort the many variations of this evolution on the Forum.
; Includes are the last/latest versions found in the thread (could this be the problem?).
;
; Testing with 2.4" 240x320 TFT is functional using ILI9431 and also ST7735 (1.8" 128x160).
; Had some frustrating moments programming this, MeLabs usb changed for PicKit3 confirmed it
; was the problem, using Test 18f2520.pbp code also proved it.
#CONFIG
CONFIG OSC = INTIO67
CONFIG FCMEN = OFF
CONFIG IESO = OFF
CONFIG PWRT = OFF
CONFIG BOREN = SBORDIS
CONFIG BORV = 3
CONFIG WDT = OFF
CONFIG WDTPS = 512
CONFIG CCP2MX = PORTC
CONFIG PBADEN = OFF
CONFIG LPT1OSC = OFF
CONFIG MCLRE = ON
CONFIG STVREN = ON
CONFIG LVP = OFF
CONFIG XINST = OFF
CONFIG DEBUG = OFF
CONFIG CP0 = OFF
CONFIG CP1 = OFF
CONFIG CP2 = OFF
CONFIG CP3 = OFF
CONFIG CPB = OFF
CONFIG CPD = OFF
CONFIG WRT0 = OFF
CONFIG WRT1 = OFF
CONFIG WRT2 = OFF
CONFIG WRT3 = OFF
CONFIG WRTC = OFF
CONFIG WRTB = OFF
CONFIG WRTD = OFF
CONFIG EBTR0 = OFF
CONFIG EBTR1 = OFF
CONFIG EBTR2 = OFF
CONFIG EBTR3 = OFF
CONFIG EBTRB = OFF
#ENDCONFIG
; TFT in landscape has conn LHS, KMR RHS, 1.8 Touch RHS
; TFT in portrait has conn ?, KMR ?, 1.8 Touch ?
;
; Led- Gnd 2x8 pin hdr
' PIC(18F2520) OTHER KMR-1.8" 1.8" Touch
' | | |
' MCLR PortE.3 (pin 1) Pull High 10k.
' C1- PortA.0 (pin 2)
' PortA.1 (pin 3)
' C1+ PortA.2 (pin 4)
' PortA.3 (pin 5)
' C1OP PortA.4 (pin 6) T_DIN Pin TFT. nc
' PortA.5 (pin 7)
' Ground (pin 8)
' PortA.7 (pin 9) T_CLK Pin TFT.
' PortA.6 (pin 10) T_DO Pin TFT. not tested XPT2046 chip
' PortC.0 (pin 11)
' CCP2 PortC.1 (pin 12) 9600 True via 1k.
' CCP1 PortC.2 (pin 13) BL drive-1k-PNP. Led+ BLK(7) Led A already to +, rejig PNP to switch ground.
' SCL PortC.3 (pin 14) SCK Pin TFT. SCL CLK(3)
' SDA PortC.4 (pin 15)
' SDO PortC.5 (pin 16) SDI(MOSI)Pin TFT. SDA MOS(4)
' Tx PortC.6 (pin 17)
' Rx PortC.7 (pin 18)
' Ground (pin 19) Gnd Gnd Gnd(1)
' +Supply (pin 20) +3v3 +3v3 +3v3(2)
' INT0 PortB.0 (pin 21) T_IRQ Pin TFT.
' PortB.1 (pin 22)
' PortB.2 (pin 23) T_CS Pin TFT.
' PortB.3 (pin 24) DC Pin TFT. A0 DC(6)
' PortB.4 (pin 25) RESET Pin TFT. RESET RES(5)
' T1G PortB.5 (pin 26) CS Pin TFT. CS CS1(9), CS2(10) works w/o either????
' PGD PortB.6 (pin 27)
' PGC PortB.7 (pin 28)
DEFINE NO_CLRWDT 1
#DEFINE TOUCH_SCREEN 1
#DEFine dbug 1 ; Comment if not req'd.
;-----------connection----------
; pic tft
; <tft_port.tft_dc_bit >-------< dc >
; <tft_port.tft_cs_bit >-------< cs >
; <tft_port.tft_rst_bit>-------< rst >
; <msspx.sdo >-----------------< mosi >
; <msspx.sck >-----------------< sck >
; <back_light>-----------------< led > via pnp cct
;*****************NEW EASY PIN DEFINES*********************************
tft_dc_bit con 3 ;ie bit 3
tft_cs_bit con 5 ;ie bit 5
tft_rst_bit con 4 ;ie bit 4
;*****************NEW EASY PIN DEFINES*********************************
tft_port var latb
back_light var latc.2 ; portc.2
cont var word ; backlight level
; WIDTH con 320 ; X MUST MATCH TFT MODULE
; HEIGHT con 240 ; Y
WIDTH con 160 ; X
HEIGHT con 128 ; Y
' my TOUCH_SCREEN connection definitions
T_CLK VAR PORTa.7
T_INPin VAR PORTa.4
T_OUTPin VAR PORTa.6
T_INT VAR PORTb.0
T_CS VAR LATb.2
number_of_buttons con 6
number_of_sliders con 1
INCLUDE "TFT_SPI.pbpmod" ;glcdc
; include "tft-ILI9431.bas"
include "tft-ST7735.bas"
INCLUDE "DT_INTS-18.bas"
INCLUDE "ReEnterPBP-18.bas"
INCLUDE "my_Elapsed_INT-18.bas"
include "TOUCH_TFT.PBPMOD" ;uncomment to use TOUCH_TFT
INCLUDE "font7x8.bas"
goto demostart
upbutton:
@ db 131,0 ; chr 131 is AN up arrow in this font THAT I'M USING
demostart:
ASM
INT_LIST macro ; IntSource, Label, Type, ResetFlag?
INT_Handler TMR1_INT, _ClockCount, PBP, yes
endm
INT_CREATE
ENDASM
@ INT_ENABLE TMR1_INT
led_on var bit
led_on_time var word
time_now var word
BOXX VAR BYTE
BOXY VAR BYTE
BOXZ VAR BYTE
cnt var byte
clear
' latb=255 ; make sure tft etc are not selected ie all cs high *************NOW IN TFT INIT********************
TRISA=%00111111
TRISB=%11000011
TRISC=%11010011
ADCON0 = 0 ' Disable A/D.
ADCON1 = %00000110 ' All digital.
OSCCON = %01110000 ' Internal 8 Mhz Osc
OSCTUNE=%01000000 ' PLL ON
cont=500 ; half brightness
DEFINE OSC 32
buff var byte[32]
t2con=5
PR2 = 255;
CCPR1L = cont>>2; ;set pwm
ccp1con=12|((cont&3)<<4);
SSPCON1=$20 ;$20-21-22 works 20 IS FASTEST
SSPSTAT=$40
TEXTSIZE=2
gosub tft_init
gosub TOUCH_INIT
GOSUB ResetTime
GOSUB StartTimer
' #ifdef dbug
' trisb.7=0 ;DEBUG OUT
' latb.7=1
' pause 4000
' Serout2 PORTb.7,84,["ready ", 13,10]
' #ENDIF
#ifdef dbug
trisc.1=0 ; DEBUG OUT
; latc.1=1 ; Float high for True.
latc.1=0 ; Float low for Inverted. The Nortek white usb serial adapter used here.
pause 4000
; Serout2 PORTc.1,84,["ready True ", 13,10]
Serout2 PORTc.1,16468,["ready Inverted ", 13,10]
#ENDIF
glcdc font7x8 ;SET FONT
bg=$ffff
fg=0
fillrect 0,0,WIDTH,height ;cls
TEXTSIZE=4
GLCDc 50,103
GLCDC "READY"
pause 400
bg=$ffff
fg=0
fillrect 0,0,WIDTH,height ;cls
MAKEBUTTON 53,170,0,92 ,40,"Clear" ;
MAKEBUTTON 276,220,1,56,30,"Sleep"
MAKEBUTTON 280,150,2,60,"Led"
MAKEBUTTON 242,52,3,40 ; no text SEE BELOW FOR Another way to attach text
MAKEBUTTON 140,150,4,50,30," +"
MAKEBUTTON 205,150,5,50,30," -"
button_action.0[1]=1 ; flash text for this button
button_action.0[2]=1 ; flash text for this button
button_rpt.0[4]=1 ; auto rpt on button 4
button_rpt.0[5]=1 ; auto rpt on button 5
;another way to attach text to a button [HANDY FOR CHRS NOT IN ASCII 7 BIT PRINTABLE RANGE] EG THERE IS NO OTHER WAY TO GET CHR 131 IN A PBP STRING
@ GetAddress21 _upbutton ,_tft_bigaddress ;macro in dt_ints-18
button_Taddr[3*3] = tft_bigaddress[2]
button_Taddr[3*3+1]= tft_bigaddress[1] ;assign a new text to button 3
button_Taddr[3*3+2]= tft_bigaddress[0]
button_Tsize[3] =1 ;UPDATE NUMBER OF text CHRS for this button
MAKESLIDER 100,80,0
slider_index =0
the_slider = slider_index *SL_SZ ; set slider indexing properly
slider_FG[the_slider ]=$ffe0 ;set slider bar colour if default not desired
slider_count[the_slider]=cont/10
bg=0
INTCON2.6=0
led_on=0
main:
fg=$7FF
DRAWBUTTON 0
DRAWBUTTON 1
DRAWBUTTON 2
DRAWBUTTON 3
DRAWBUTTON 4
DRAWBUTTON 5
slider_index =0
the_slider = slider_index *SL_SZ
slider_MODE[the_slider]=0 ;FORCE DRAW OF SLIDER
DRAWSLIDER 0
fg=$ffFF
DRAWcircle 60 , 60 , 20
fillcircle 60 , 60 , 20
LOP:
GOSUB CK_BUTTON
IF BUTTON_STATE[0]==2 THEN
BUTTON_STATE[0]=0
fg=0
fillrect 0,0,WIDTH,height ;cls
goto main
elseIF BUTTON_STATE[1] THEN
if BUTTON_STATE[1]==2 then ; shut it all down
gosub tft_sleep
ccp1con=0
back_light=1
pause 100
intcon=$10
@ sleep
@ nop
intcon=$c0
gosub tft_wake
CCPR1L = cont>>2;
ccp1con=12|((cont&3)<<4);
endif
BUTTON_STATE[1]=0
elseIF BUTTON_STATE[2] THEN ;short or long press
drawimage 220,30,45,45,led
led_on=1
gosub get_now
led_on_time=time_now
BUTTON_STATE[2]=0
elseIF BUTTON_STATE[3] THEN
if BUTTON_STATE[3]==2 then
drawimage 220,30,45,45,led
else
DRAWBUTTON 3
fg=0
fillrect 220,30,45,45
drawimage 220,30,45,45,led2
endif
BUTTON_STATE[3]=0
elseIF BUTTON_STATE[4] THEN ; short , long press or RPT
CNT=CNT+1
BUTTON_STATE[4]=0
gosub s_cont
elseIF BUTTON_STATE[5] THEN ; short , long press or RPT
CNT=CNT-1
BUTTON_STATE[5]=0
gosub s_cont
endif
slider_index =0
the_slider = slider_index *SL_SZ
IF slider_MODE[the_slider]&1 THEN ; adjust brightness
gosub set_cont
ENDIF
if led_on then
gosub get_now
if abs(time_now-led_on_time) > 100 then
DRAWBUTTON 3
fg=0
' fillrect 220,30,45,45
' drawimage 220,30,45,45,led2
led_on=0
endif
endif
if SecondsChanged then
fg=$F800
bg=$ffff
SecondsChanged=0
TEXTSIZE=2
BOXX=6*7*TEXTSIZE
BOXY=HEIGHT-(TEXTSIZE*8+1)
ARRAYWRITE buff,[dec2 seconds,0]
GLCDSTR BOXX,BOXY ,buff
endif
if hoursChanged then
hoursChanged=0
TEXTSIZE=2
BOXY=HEIGHT-(TEXTSIZE*8+1)
ARRAYWRITE buff,[dec2 hours,0]
GLCDSTR 1,BOXY ,buff
endif
if minutesChanged then
TEXTSIZE=2
minutesChanged=0
BOXX=3*7*TEXTSIZE
BOXY=HEIGHT-(TEXTSIZE*8+1)
ARRAYWRITE buff,[dec2 minutes,0]
GLCDSTR BOXX,BOXY,buff
endif
goto LOP
set_cont:
cont = 1000 - slider_COUNT[0]*10
CCPR1L = cont>>2;
ccp1con=12|((cont&3)<<4);
slider_MODE[the_slider]=slider_MODE[the_slider]&$FE
cnt= slider_COUNT[the_slider]
s_cont:
TEXTSIZE=3
bg=0
fg=$7FF
ARRAYWRITE buff,[dec3 cnt,0]
GLCDSTR 50,10 ,buff
return
get_now :
@ INT_DISABLE TMR1_INT ;disable int to get value of ticks WITHOUT ROLLOVER ISSUES [TICKS IS NOT ATOMIC]
time_now=ticks ; 1 tick==10mS
@ INT_ENABLE TMR1_INT
return
#ifdef dbug
db_prn:
intcon.7=0
Serout2 PORTb.7,84,["t ",#TU_TMP3 , 13,10]
intcon.7=1
return
#ENDIF
INCLUDE "led2.bas"
INCLUDE "led.bas"
Bookmarks