PDA

View Full Version : Need help setting up Capacitive Sense 12F1822



Heckler
- 29th November 2013, 21:10
Hey Group,

Below is a bit of code for the 12F1822 that currently relies on a push button for input.
I would like to use one of the Capacitive inputs instead.

I have done some searching here and found some old threads but it is way over my head.
It seams you have to use timers and oscillators and the setup is very confusing.

If anyone has had success setting up the Capacitive Sensing (CPS) module and might share a bit of code that I could drop in, in place of the pushbutton (Btn) code in my program I would greatly appreciate it.

This code will PWM (pulse width modulate) an FET on PortA.2 to adjust the brightness of a 12V LED module connected to a battery. For example it would make a great camping or emergency light and allow one to adjust the brightness from very dim to full on in 5 steps. I would like to simplify the circutry and eliminate the push button.

Thanks in advance



'************************************************* ***************
'* Name : 12F1822 HPWM button *
'* Author : [dwight] *
'* Notice : Copyright (c) 2010 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 8/04/2010 *
'* Version : 1.0 *
'* Notes : this program steps through 5 pwm levels to run *
'* : a cheap Horbor Freight 17 LED worklight. *
'* : *
'************************************************* ***************
'
' Pic Configuration
' =================
#CONFIG
__CONFIG _CONFIG1, _FCMEN_OFF & _IESO_OFF & _BOREN_OFF & _FOSC_INTOSC & _WDTE_OFF & _MCLRE_OFF & _PWRTE_OFF
__CONFIG _CONFIG2, _LVP_OFF
#ENDCONFIG

INCLUDE "ALLDIGITAL.pbp"
DEFINE SHOWDIGITAL 1 'this directive shows the registers that were changed for "alldigital" in the results window


DEFINE OSC 4
OSCCON = %01101010
TRISA = %001000 ' PortA.3 is button input
PORTA = 0 ' all outputs zero
CM1CON0.7=0 ' Disable comparator
CCP1CON = %00001100 ' CCP in PWM mode
'
' Software variables
' ==================
Duty VAR WORD
stepp var byte
' ----------------[ I/O Definitions ]-----------------------------------
FET VAR PORTA.2 ' output to control the FET switch
Btn var PORTA.3 ' push button input
'
' Software/Hardware initialisation
' ================================
PAUSE 50 ' OSC settle delay
Duty = 0
stepp = 6
hpwm 1,255,250 ' Start with Light full ON

'------------------------------< Main program >-------------------------

Start: do While Btn = 1 : loop ' loop here while Btn not pressed

stepp = stepp +1 ' button pressed so increment stepp
if stepp>6 then stepp=0 ' check for rollover
if stepp=6 then blink ' blink to show that you are at the highest level
lookup stepp, [0,1,16,64,128,255,255],duty 'choose level, 5 levels plus OFF
hpwm 1,duty,250 ' else set new level
do while btn = 0 : loop ' wait for Btn to be released
pause 100
goto start

blink: hpwm 1,0,1000 'off ' arrive here when brightness is at its
pause 100 ' highest. So blink to incicate to user.
hpwm 1,255,1000 'on
pause 100
hpwm 1,0,1000 'off
pause 100
hpwm 1,255,1000 'on

do while btn = 0 : loop ' wait for Btn to be released
pause 100
goto start