Capacitive Touch Sense Çalışıyor

Başlatan myders, 25 Aralık 2014, 15:20:09

myders

Dokunmatik buton
---By Oguz Akkus---myders------------------25.12.2014-----------------------

-- Capacitive Touch Sense
include 16f1937                    -- target PICmicro
--
-- This program assumes that a 20 MHz resonator or crystal
-- is connected to pins OSC1 and OSC2.
-- (unspecified configuration bits may cause a different frequency!)
pragma target clock 1000_000     -- oscillator frequency
-- configuration memory settings (fuses)
pragma target OSC      INTOSC_NOCLKOUT               -- HS crystal or resonator
pragma target PLLEN    P1         -- PLL off
pragma target WDT      disabled     -- no watchdog
pragma target DEBUG    disabled     -- no debugging
pragma target LVP      disabled     -- no Low Voltage Programming
pragma target MCLR     external     -- reset externally
--pragma target PWRTE    Enabled
-- These configuration bit settings are only a selection, sufficient for
-- this program, but other programs may need more or different settings.
--
--enable_digital_io()                -- make all pins digital I/O
include delay                         -- fetch delay library
include print

--    Directions for use of this library in application programs
--    (in this sequence):
--    1. Declare the following constants:
          const byte LCD_ROWS     = 2              -- 1, 2 or 4 lines
          const byte LCD_CHARS    = 16             -- 8, 16 or 20 chars per line
--       and variables (aliases):
         var   bit  lcd_en        is  pin_d4      -- trigger
         var   bit  lcd_rs        is  pin_d5      -- cmd/data select
--       and variables (aliases):
--          var   byte lcd_dataport  is  portA_low   -- 4 data pins
--       or
          var   bit  lcd_d4        is  pin_d0      -- databit d4 pin
          var   bit  lcd_d5        is  pin_d1      -- databit d5 pin
          var   bit  lcd_d6        is  pin_d2     -- databit d6 pin
          var   bit  lcd_d7        is  pin_d3      -- databit d7 pin
          pin_d0_direction       = output
          pin_d1_direction       = output
          pin_d2_direction       = output
          pin_d3_direction       = output
          pin_d4_direction       = output
          pin_d5_direction       = output

include lcd_hd44780_4
--
lcd_init()

lcd_clear_screen()
lcd = "T" lcd = "o" lcd = "u"  lcd = "c"  lcd = "h"
lcd_cursor_position(1,0)
lcd = "S" lcd = "e" lcd = "n"  lcd = "s" lcd = "e"
delay_1s(1)


--

-- ok, now let's configure ADC
-- we want to measure using low resolution
-- (that's our choice, we could use high resolution as well)
const bit ADC_HIGH_RESOLUTION = false
-- we said we want 1 analog channel...
--const byte ADC_NCHANNEL = 2
-- and no external voltage reference
const byte ADC_NVREF = ADC_NO_EXT_VREF
-- now we can include the library
-- note it s now named "adc", not "adc_hardware" anymore
include adc
-- and run the initialization step
adc_init()

var byte Alt = 56
var byte SINIR = 100
var byte Sure
var bit  Ters
--OPTION_REG=0b01001111
--ANSELA = 0b01011111
--ADCON0=0b00001001
--OPTION_REG_NWPUEN = 0
pin_C2_direction = output
pin_c2 = low
procedure TUSBAK is
          pin_A2 = LOW
          pin_A2_direction = output
          pin_A2 = high
          ADCON0=0b00001001
          delay_10us(1)
          ADCON0 = 0b00001000
          JANSEL_ANS0 = 0
          pin_A0_direction = output
          pin_A0 = low
          pin_A0_direction = input
          JANSEL_ANS0 = 1
          Alt = adc_read_low_res(0)
          
          pin_A0_direction = output
          JANSEL_ANS0 = 0
          pin_A0 = low
end procedure

lcd_clear_screen()
forever loop
   TUSBAK
   lcd_cursor_position(0,0)
   print_byte_dec (lcd,Alt) lcd =" " lcd =" "
   if Alt < SINIR & Ters == 0 then
      pin_c2 = high
      Sure = 0 Ters = 1
      While Alt<SINIR | Sure<SINIR loop
            TUSBAK
            lcd_cursor_position(1,0)
            print_byte_dec (lcd,Alt) lcd =" " lcd =" "
                  if Alt < SINIR then
                     Sure = 0
                  else
                     Sure = Sure+16
                     if Sure > SINIR then
                        Exit loop
                     end if
                  end if
      End loop
      delay_10us(5)
      Sure = 0
   End if
   delay_10us(1)
   Ters = 0
   pin_c2 = low

end loop