touch switch dimmer


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2007
    Posts
    6

    Default touch switch dimmer

    hello everyone,
    i am new in picbasic pro,recently i work on a project <touch switch dimmer>
    i make 9 switches and connect them into pic 16f876,each output of the switch will give 0v or 5v.
    1 switch is connected in portc.6 act as a selection button, when i touch it once,it control my LED brightness
    second touch will goes to control my servo motor's speed

    i arrange the rest of the 8 switches in circular shape and their output is connected to port B of my pic as a intensity controller. when i move clockwise, it will increase the brightness of the LED or the speed of the motor (depend on which 1 selected). If i move counter-clockwise,it will decrease the intensity.
    and my output is pin CCP1 and CCP2,the output is connected to a RC filter then to the transistor and control the LED or the Motor circuit.
    In this project, i try to use pwm to control the intensity,i need some idea to write the code...

    before this,i try a code as below to test the pwm output,but it seem not work:
    -----------------------------------------------------------------------------------------

    ' Output is a 1Khz signal with duty cycle sweeping
    ' from 20% to 80% once per second

    ' Note: PWM output will be on the CCP1 pin. Register
    ' names are for the PIC16F87x devices.

    ' PR2 = Timer2 period register, controls PWM period.
    ' CCPR1L and CCP1CON<5:4>bits control the duty cycle,
    ' and should be treated as a 10-bit word

    ' Fosc = Clock Frequency (4MHz)
    ' PS = Timer2 Prescale Value (T2CON<1:0>)
    ' Freq = PWM output frequency
    ' Duty% = Duty cycle (20% = 0.2)

    ' formulas:
    ' PR2=(Fosc/(4*PS*Freq))-1
    ' CCPR1L:CCP1CON<5:4>=(PR2+1)*4*Duty%

    duty VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)


    TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
    CCP1CON = %00001100 ' Set CCP1 to PWM
    T2CON = %00000101 ' Turn on Timer2, Prescale=4

    ' Use formula to determine PR2 value for a 1KHz signal,
    ' 4MHz clock, and prescale=4. (4E6/(4*4*1E3))-1=249

    PR2 = 249 ' Set PR2 to get 1KHz out

    ' Use formula to determine CCPR1L:CCP1CON<5:4> value for
    ' ends of range 20% to 80%. (249+1)*4*0.2=200 (20% value)
    ' (249+1)*4*0.8=800 (80% value)


    duty = 200 ' Set duty cycle to 20%

    loop: CCP1CON.4 = duty.0 ' Store duty to registers as
    CCP1CON.5 = duty.1 ' a 10-bit word
    CCPR1L = DUTY >> 2

    duty = duty + 10 ' Increase duty cycle

    ' Since the total sweep of duty is 600 (800-200) and
    ' we are adding 10 for each loop, that results in 60
    ' steps min to max. 1 second divided by 60 = 16.67mS

    Pause 17 ' Pause 1/60 of second

    IF (duty < 800) Then loop ' Do it again unless 80% duty cycle

    duty = 200 ' Reset to 20% duty cycle

    GoTo loop ' Do it forever

    ----------------------------------------------------------------------------------------
    thank for your helps

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Quote Originally Posted by helmut View Post
    before this,i try a code as below to test the pwm output,but it seem not work:
    Your code looks fine (to me) - what is not working?
    Do you get PWM out? Is the timing off?
    Is the 1 second off?
    Anything connected to RC2 during this test?
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

  3. #3
    Join Date
    Aug 2007
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    i put a LED as the output in RC2
    i c no different for the intensity of the LED after i run this pwm code....
    did i missed some step?

  4. #4
    Join Date
    Sep 2006
    Location
    Mexico
    Posts
    47


    Did you find this post helpful? Yes | No

    Default

    You need to add DEFINE OSC 4, try with HPWM command.

  5. #5
    Join Date
    Aug 2007
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    thank you guys,i solve the pwm problems.that's minor mistake in my hardware.
    In addtion, in my project, i move clockwise will increase intensity of my output.
    and move counter clockwise will decrease the intensity.
    so i try the shifting instruction
    the code i used as shown:

    if portb == portb>>1 then
    high porta.0
    endif
    if portb == portb<<1 then
    high porta.1
    endif
    end

    if my 1st input is high in portb.0, it's consider shifting right or shifting left??

Similar Threads

  1. Need a cheap touch sensor idea.. here it is
    By mister_e in forum Code Examples
    Replies: 20
    Last Post: - 16th April 2016, 22:42
  2. Dimmer switch
    By iugmoh in forum Off Topic
    Replies: 4
    Last Post: - 20th March 2009, 16:02
  3. Replies: 14
    Last Post: - 26th September 2007, 05:41
  4. Touch Sensitive Switch
    By BobP in forum General
    Replies: 8
    Last Post: - 23rd August 2007, 22:27
  5. Newbie - 16F628A and switch latching
    By malc-c in forum mel PIC BASIC Pro
    Replies: 45
    Last Post: - 19th May 2006, 02:35

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts