Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic16f84a led frequency

Status
Not open for further replies.

danielec

New Member
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);



}

}
}
 
because you are not trapping the rollover from 255 to zero or the other way round, 0 to 255 in delay_ms (x)
 
X is an integer. it can be any number between 0 to 65353. Please explain furthermore your point.
Thanks,
Daniel.
 
You need to debounce the switches in software. As you are reading it a few times per second. Also make sure your delay_MS takes an integer.
 
i would check that "unsigned int" is actualy a 16 bit type as you have asumed in your code. In most c-compilers ive worked with for microcontrollers, int is actualy only 8 bit (0-256). This is becaus the cpu in a microcontroller is 8 bit arcitechute, and by standards, int is then 8 bit.

On a 16 bit cpu (old days) it should be 16 bit, on a 32 bit it should be 32 bit and so on...

Altho i know this is not realy true for c on compuiters (int kinda stayed 16 bit in most c compilers for pc, when 32 bit pc cpu's ,arived), it is true for example in the ccs compiler.

Try defining your "x" variable as an "unsigned long" and that should fix your problem.
 
your code should not going to rollover bacause of (x>=200) and (x<=800) test conditions but since you say so, unsigned int x is probably below the range from 200 to 800, meaning its 8 bit, try zero as the lowest value and 255 the highest and multiply x to some value to achieve longer or shorter delay

or as ombrastein said try unsigned long data type
 
He seeds the variable x with 400. So the code will fire up and be in his range checks. And thanks to all for not commenting on the code.

Make sure the int x is 16 bits unsigned. Not a byte (0-255; who said 0-256).

Also, make sure you are not holding the button too long. On the one end (lower x) it will do the 5 math a lot faster as the delay speeds up.

If you are having a major jump in the speed in any direction, x is probably a byte (8 bit word; or byte). Word (in the old days) meant 16 bits to me (but it was open then and word was global).
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top