|
|
View previous topic :: View next topic |
Author |
Message |
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Mon Feb 17, 2014 12:19 pm |
|
|
Quote: | All waiting for button press to go true then false does is go through there many times when the switch bounces (which most of them do). Switch bounces have driven many people screaming into the night - you either need to debounce it in software or hardware (something like an RC network can work well although it introduces a slight delay in sensing switch closure). Multiple closures in just a few ms (typical) causes most software to act goofy and the user does not realize that they have effectively pushed the button multiple times in just a few ms.
mikey |
I dont have the option of making a harware change. so it will have to be in software... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Feb 17, 2014 12:40 pm |
|
|
Which is why a search here will give numerous examples of debounce code.
A good example is the one in this thread, and the link to 'button.c':
<http://www.ccsinfo.com/forum/viewtopic.php?t=47510> |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Mon Feb 17, 2014 1:20 pm |
|
|
Quote: |
trouble with the PWM
|
i suspect that after you get your switch issues corrected, more trouble awaits.
let me guess: the PWM output drives some form of buck modulator, sans
integrating capacitor?
or
just direct switches the LED ??
you need to keep in mind that PERCEIVED brightness of the LED is based on peak current through it, not the RMS energy supplied.
(its the reason LED automotive tail lights can strobe so annoyingly on certain models. )
if your circuit is what i suspect, then you should expect to see very little change in intensity over the full range of the PWM duty cycle.
got a schematic of what you built ? |
|
|
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Mon Feb 17, 2014 1:32 pm |
|
|
Quote: | Which is why a search here will give numerous examples of debounce code.
A good example is the one in this thread, and the link to 'button.c':
<http://www.ccsinfo.com/forum/viewtopic.php?t=47510> |
I am going to have a look at this now...
Quote: | got a schematic of what you built ? |
I am working on a DC-DC Constant Current Step-Down LED driver. |
|
|
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Mon Feb 17, 2014 2:45 pm |
|
|
Ttelmah
New code......no joy
Code: |
#include <16F87.h>
#fuses INTRC,NOWDT
#use delay(clock=1000000)
int count=0;
// These are the "Bvar" variables.
int8 A0 = 0; // For the button on pin A0
#include <Button.c>
// Function parameters:
//
// button(pin, DownState, Delay, Rate, BVar, Action)
//
// Returns: The value of the Action variable (normally
// set = 1) is returned when the button is
// pressed. When the button isn't pressed,
// the opposite value is returned (normally 0).
//======================================
void demo_sequence(int Demo_Number){
long led_duty =0;
int led_set=0;
// variables used to set by PWM duty cycle
//----------------------------------------------------------------------------
// PWM Setup
//----------------------------------------------------------------------------
switch(Demo_Number){
case 0 :
while (count==0)
{
led_set=255;//Demo 0 while count zero, set LED to full brightness
if(led_duty<led_set)
led_duty++;
if(led_duty>led_set)
led_duty--;
set_pwm1_duty(led_duty);//ramp up to 255, full brightness
delay_ms(10);
}
break;
case 1 :
while (count==1)
{
led_set=55;//Demo 1 while count 1, set LED to full brightness
if(led_duty<led_set)
led_duty++;
if(led_duty>led_set)
led_duty--;
set_pwm1_duty(led_duty);//ramp up to 255, full brightness
delay_ms(10);
}
break;
}
}
void main()
{
while(TRUE)
{
demo_sequence(count);
if(button(PIN_A0, 0, 50, 10, A0, 1))
count++;
// You must use a delay statement in the loop.
// A good value to use is 10 ms.
delay_ms(10);
}
}
.
|
I have a normally open push button located on pins 3 an 4 on the "main", shown in the image below...
[IMG]http://i.imgur.com/5LByHKd.png?1[/IMG] |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Mon Feb 17, 2014 2:50 pm |
|
|
Do some actual _thinking_. If count==0, how is the while loop ever going to exit?. |
|
|
ezflyr
Joined: 25 Oct 2010 Posts: 1019 Location: Tewksbury, MA
|
|
Posted: Mon Feb 17, 2014 2:54 pm |
|
|
Zombie,
That image doesn't even show pins 3 & 4! This thread is becoming a huge waste of time!
Do you even have any idea what your problem really is? Is it a button problem? Is it a PWM problem? Is it both? You really need to separate the two functions, and test them independently! Then, you must come back here and provide us with *meaningful* feedback. Telling us that a past suggestion yields 'No Joy' is really absurd.
Edit: Other than the LED that you are brightening and dimming, what other feedback are you getting during your program development? You should really dedicate a pin to send out diagnostic messages to your PC so that you can actually see what's going on......
John |
|
|
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Mon Feb 17, 2014 3:59 pm |
|
|
Hi ezflyr,
Here is all the knowledge I have for you....(correct me where i am wrong)
I want to control a 1Khz PWM signal for LED light dimming..
To achieve the 1Khz i am using
Code: | setup_oscillator(OSC_1MHZ);
setup_ccp1(CCP_PWM);setup_timer_2(T2_DIV_BY_4, 64, 1); |
which i believe will give me 961.5hz... close enough.
Now i am developing using the ICD-U64 which needs to be set up at 2400baud for diagnostic messages. On a 1mhz clock thats not going to happen or am i wrong ??.
So basically when I combine the pwm code with the push button code i have no ability to diagnose due to baud rates.... so i am blind
This PWM code here works perfect on its own..
push A0 down and light will dim then after 5 sec return to full
Code: | setup_oscillator(OSC_1MHZ);
setup_ccp1(CCP_PWM);
setup_timer_2(T2_DIV_BY_4, 64, 1);
While(TRUE)
{
if(!input(PIN_A0))
{
led_set=20;
led_timer=500;
}
if(led_timer>0)
{
led_timer--;
}
else
{
led_set=255;
}
if(led_duty<led_set){led_duty++;}
if(led_duty>led_set){led_duty--;}
set_pwm1_duty(led_duty);
delay_ms(10);
}
}
|
I want to demo an LED Luminarie with different light output modes, and i want to switch through the modes using a push button...
Looks like i will have to start from scratch... and work on the debounce.
Will have to change the clock speed for, and add a pin for diagnostics and so on..
I am new to all this
sorry for been absurd
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Feb 18, 2014 2:12 am |
|
|
2400bps, is perfectly doable at 1MHz clock.
Why not have your PWM at exactly 1KHz?.
setup_timer_2(T2_DIV_BY_1, 249, 1);
gives 1KHz.
You don't show the declarations for led_timer etc.. Remember an int can only hold 255 max, so if this is an int, the code is not going to work (500 won't go into an int, needs an int16). Also, if led_set, and led_duty are int8, even with the /4 timer2 setup, these need to be int16, since otherwise the duty value allowed is 64 max.... |
|
|
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Tue Feb 18, 2014 4:57 pm |
|
|
Hi guys guys thanks for all the help....
I got out the soldering iron to add an option to debug on pin B3....
Every thing is working well now debounce problem sorted thanks to the code supplied here....
Quote: | Which is why a search here will give numerous examples of debounce code.
A good example is the one in this thread, and the link to 'button.c':
<http://www.ccsinfo.com/forum/viewtopic.php?t=47510> |
My switch statement is working very well (might need a few adjustments but i am very happy) and i can loop/exit/enter through each case statement with ease.
Quote: | You don't show the declarations for led_timer etc.. |
my code..
Code: | long led_duty =0;
int led_set=0;
long led_timer=0; |
Quote: | Why not have your PWM at exactly 1KHz?.
setup_timer_2(T2_DIV_BY_1, 249, 1);
gives 1KHz.
You don't show the declarations for led_timer etc.. Remember an int can only hold 255 max, so if this is an int, the code is not going to work (500 won't go into an int, needs an int16). Also, if led_set, and led_duty are int8, even with the /4 timer2 setup, these need to be int16, since otherwise the duty value allowed is 64 max.... |
I have no scope to check PWM but i have a multimeter....
When i use
Code: | setup_timer_2(T2_DIV_BY_1, 249, 1); |
the current from the LED is at 0.080mA with 255 duty cycle.
When i use
Code: | setup_timer_2(T2_DIV_BY_4, 64, 1); |
the curreny from the LED is at 0.278mA with 255 duty cycle. appears much brighter..
Just wondering why is there a difference when i use 255 for both? |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Tue Feb 18, 2014 5:30 pm |
|
|
Assuming you've defined duty as bigger than int8.
With this code :-
Code: | setup_timer_2(T2_DIV_BY_1, 249, 1); | The PWM perod is (249+1)*4 = 1000 clock cycles, on period is 255 clock cycles.
With this code :- Code: | setup_timer_2(T2_DIV_BY_4, 64, 1); | The PWM perod is (64+1)*4*4 = 1040 clock cycles, on period is 255*4 = 1020 clock cycles.
Duty RATIO is nearly four time bigger for second case than first.
Hence your meter readings and brightness levels.
You've used T2_DIV_BY_4 in second case and not the first.
Mike |
|
|
zombiePIC
Joined: 08 Feb 2014 Posts: 23
|
|
Posted: Wed Feb 19, 2014 2:41 pm |
|
|
Thanks for explaing Mike!! |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|