Hi,
I am using pic16f84A and i want to increase and decrease the frequency of a led using 2 push button with pullup resistors. first button on porta.3 will increase the frequency and the second one on porta.2 will decrease the frequency. In case non were pushed the frequency stays as the last one.
it seems it's working but when the frequency is very high its suddenly gets very low and when the frequency is very low its suddenly gets very high.
why?
Daniel.
#include <system.h>
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _XT_OSC & _CP_OFF
#pragma CLOCK_FREQ 4000000
unsigned int x;
void main(void)
{
trisa = 0b11111; //porta in
trisb = 0x00; //portb out
portb=0x00;
x=400;
while (1)
{
portb.3=1; //if none of the buttons are pushed than led frequency is as the last one.
delay_ms (x);
portb.3=0;
delay_ms (x);
while ((porta.3==0) && (x<=800)) //if pushbutton one is pressed and x less than 800 go inside the loop
{
x=x+5; //led frequency is getting lower.
portb.3=1;
delay_ms (x);
portb.3=0;
delay_ms (x);
}
while ((porta.2==0) && (x>=200)) //if pushbutton two is pressed and x greater than 200 go inside the loop
{
x=x-5; //led frequency is getting higher.
portb.3=1;
delay_ms (x);
portb.3=0;
delay_ms (x);
}
}
}
I am using pic16f84A and i want to increase and decrease the frequency of a led using 2 push button with pullup resistors. first button on porta.3 will increase the frequency and the second one on porta.2 will decrease the frequency. In case non were pushed the frequency stays as the last one.
it seems it's working but when the frequency is very high its suddenly gets very low and when the frequency is very low its suddenly gets very high.
why?
Daniel.
#include <system.h>
#pragma DATA _CONFIG, _PWRTE_OFF & _WDT_OFF & _XT_OSC & _CP_OFF
#pragma CLOCK_FREQ 4000000
unsigned int x;
void main(void)
{
trisa = 0b11111; //porta in
trisb = 0x00; //portb out
portb=0x00;
x=400;
while (1)
{
portb.3=1; //if none of the buttons are pushed than led frequency is as the last one.
delay_ms (x);
portb.3=0;
delay_ms (x);
while ((porta.3==0) && (x<=800)) //if pushbutton one is pressed and x less than 800 go inside the loop
{
x=x+5; //led frequency is getting lower.
portb.3=1;
delay_ms (x);
portb.3=0;
delay_ms (x);
}
while ((porta.2==0) && (x>=200)) //if pushbutton two is pressed and x greater than 200 go inside the loop
{
x=x-5; //led frequency is getting higher.
portb.3=1;
delay_ms (x);
portb.3=0;
delay_ms (x);
}
}
}