'----------------------------------------------------------------- 'name : TouchDriver.bas 'micro: Mega8 'compiler: Bascom-avr 'author: slyt 'website: http://www.avrbase.com 'date: 2009-09-10 '----------------------------------------------------------------- $regfile = "m8def.dat" $crystal = 4000000 $baud = 9600 $hwstack = 32 $swstack = 32 $framesize = 40 Config Adc = Single , Prescaler = Auto , Reference = Internal Ddrb = &HFF Ddrd.6 = 1 Ddrd.2 = 0 Portb = &B11100 Portd = &HFF Dim V1 As Word , V2 As Word , I As Integer Dim Chan As Byte Dim S1 As Single Dim S As String * 4 Dim X As Word , Y As Word Dim Xstr As String * 4 Dim Ystr As String * 4 On Int0 Int0_int Enable Interrupts Enable Int0 Pe1 Alias Portb.1 Pe2 Alias Portb.2 Pe3 Alias Portb.3 Pe4 Alias Portb.4 Pe5 Alias Portd.2 Pe6 Alias Portd.6 Osccal = &HA0 Start Adc Do Idle Loop Int0_int: Pe5 = 0 : Pe6 = 0 Pe3 = 0 : Pe2 = 1 'X轴加电压 Pe4 = 1 : Pe1 = 0 V1 = Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 + Getadc(0) '读ADC0,取X电压 V1 = V1 / 10 Pe3 = 1 : Pe2 = 0 '关闭X轴电压及接地 Pe4 = 0 : Pe1 = 1 'Y轴加电压 V2 = Getadc(1) '读ADC1,取Y电压 V2 = V2 + Getadc(1) '读ADC1,取Y电压 V2 = V2 + Getadc(1) '读ADC1,取Y电压 V2 = V2 + Getadc(1) '读ADC1,取Y电压 V2 = V2 + Getadc(1) '读ADC1,取Y电压 V2 = V2 / 5 X = 1023 - V1 S = Str(x) Xstr = Format(s , "0000") Y = V2 S = Str(y) Ystr = Format(s , "0000") Portb = &B11100 Pe5 = 1 Pe6 = 1 Print Xstr ; Ystr Return |
'----------------------------------------------------------------- 'name : TFT_Touch.bas 'compiler: Quick Basic 'author: slyt 'website: http://www.avrbase.com 'date: 2009-09-10 '----------------------------------------------------------------- DECLARE SUB delay (dt AS SINGLE) Screen 12 Cls Clear OPEN "COM1:9600,N,8,1,BIN" FOR INPUT AS #1 ON COM(1) GOSUB COM1HANDDLE Com(1) Off Com(1) On DIM x AS INTEGER Dim Y As Integer Dim A(4) As Integer Dim B(4) As Integer Dim Aa(4) As Integer Dim Bb(4) As Integer Dim Intrun As Integer '调入屏幕坐标与触摸屏值换算参数 Open "para.ini" For Input As #2 Line Input #2 , Para$ Kx = Val(para$) Line Input #2 , Para$ Ky = Val(para$) Line Input #2 , Para$ Dx = Val(para$) Line Input #2 , Para$ Dy = Val(para$) Close #2 DO Ak$ = Inkey$ Ak$ = Ucase$(ak$) Select Case Ak$ Case " " Cls Case Chr$(27) Screen 0 End Case "C" '校正坐标转换参数,在屏幕角依次显示十字 A(1) = 10 : B(1) = 10 A(2) = 10 : B(2) = 460 A(3) = 630 : B(3) = 10 A(4) = 630 : B(4) = 460 Locate 5 , 1 For I = 1 To 4 Line(a(i) - 10 , B(i)) -(a(i) + 10 , B(i)) , 15 Line(a(i) , B(i) - 10) -(a(i) , B(i) + 10) , 15 Intrun = 0 Do Loop Until Intrun = 1 Aa(i) = X : Bb(i) = Y Sound 1200 , 1 Print I , X , Y Delay .5 Inrun = 0 Com(1) Off Delay .5 Line(a(i) - 10 , B(i)) -(a(i) + 10 , B(i)) , 0 Line(a(i) , B(i) - 10) -(a(i) , B(i) + 10) , 0 Com(1) On Next Cls '换算参数 Daa =(aa(3) + Aa(4) - Aa(1) - Aa(2)) / 2 Dbb =(bb(2) + Bb(4) - Bb(1) - Bb(3)) / 2 Da = A(3) - A(1) Db = B(2) - B(1) Kx = Da / Daa Ky = Db / Dbb Dx = A(1) - Kx *(aa(1) + Aa(2)) / 2 Dx = Dx + A(3) - Kx *(aa(3) + Aa(4)) / 2 Dx = Dx / 2 Dy = B(1) - Ky *(bb(1) + Bb(3)) / 2 Dy = Dy + B(2) - Ky *(bb(2) + Bb(4)) / 2 Dy = Dy / 2 '存储参数 Open "para.ini" For Output As #2 Print #2 , Str$(kx) Print #2 , Str$(ky) Print #2 , Str$(dx) Print #2 , Str$(dy) Close #2 Intrun = 0 Case Else '在触摸位置画圆点 If Intrun = 1 Then Intrun = 0 Px = Kx * X + Dx Py = Ky * Y + Dy Colr% = 15 Circle(px , Py) , 3 , Colr% Paint(px , Py) , Colr% End If End Select LOOP UNTIL ak$ = CHR$(27) END '=========串口接收中断服务程序========= Com1handdle: Intrun = 1 Com(1) Off Line Input #1 , Dz$ Dz$ = Right$(dz$ , 8) Xy$ = Left$(dz$ , 4) X = Val(xy$) Xy$ = Right$(dz$ , 4) Y = Val(xy$) Com(1) On Return '============================== SUB delay (dt AS SINGLE) T0 = Timer Do T1 = Timer - T0 Loop Until T1 < 0 Or T1 > Dt End Sub |
欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/) | Powered by Discuz! X2.5 |