led blinking speed using up/down push buttons ( pic16f883).

Status
Not open for further replies.

zain kaleem

New Member
Hi
I am working on simple project. I just want to create the blinking led with variable delay. I have connected two push buttons for increment and decrement the delay value to blink the led. I am using PIC16F883 on proteus simulation and for programming MPLAB IDE. My code is not working correctly. The value for the delay is not changing.

Can anybody help me with this ?


Here is the complete code.


#include <xc.h>
#include "confi.h" // header file for configuration settings.

void delay(unsigned int x);
unsigned int speedstep=0;
void main(void) {
unsigned int x,y;
TRISC0=1;
TRISC1=1;
TRISC2=0;
PORTCbits.RC2=0;

while(1)
{
RC2=1;
delay(y);
RC2=0;
delay(y);

if (PORTCbits.RC0 == 0) //was the increment button pushed?
{
for(x=0; x<500; x++) //debounce switch
;

speedstep++; //increment speedstep variable by 1

y = speedstep;
while(PORTCbits.RC0 == 0);//show variable value on port c
}

if (PORTCbits.RC1 == 0) //was the decrement button pushed?
{
for(x=0; x<500; x++) //debounce switch
;

speedstep--; //deccrement speedstep variable by 1

y = speedstep; //show variable value on port c
while(PORTCbits.RC1 == 0);
}
}
}
void delay(unsigned int x){
for(int i=0; i!=x; i++)
{
NOP();
}
}
 
I updated the code as you said


void delay(unsigned int x){
for(int i=0; i!=x; i++)
{
__delay_ms(1);
}
}


It worked.
when I increment the delay it also increases but when I decrement the delay the led just stays ON.
how can i know that how much delay is used by the led??
 
EDIT:
serial.print( y, DEC);
serial.print( speedstep,DEC );
is a great way to evaluate the variables
 
Last edited:
If you add indentation to your code and then use code tags, you get this,
Code:
#include <xc.h>
#include "confi.h" // header file for configuration settings.

void delay(unsigned int x);
unsigned int speedstep=0;

void main(void){
unsigned int x,y;
TRISC0=1;
TRISC1=1;
TRISC2=0;
PORTCbits.RC2=0;
    while(1){
        RC2=1;
        delay(y);
        RC2=0;
        delay(y);

        if (PORTCbits.RC0 == 0){ //was the increment button pushed?
            for(x=0; x<500; x++); //debounce switch
            speedstep++; //increment speedstep variable by 1
            y = speedstep;
            while(PORTCbits.RC0 == 0);//show variable value on port c
        }

        if (PORTCbits.RC1 == 0){ //was the decrement button pushed?
            for(x=0; x<500; x++); //debounce switch
            speedstep--; //deccrement speedstep variable by 1
            y = speedstep; //show variable value on port c
            while(PORTCbits.RC1 == 0);
        }
    }
}

void delay(unsigned int x){
    for(int i=0; i!=x; i++)
    {
        NOP();
    }
}

It's much more readable and easier for people to follow.

I can't see why you have the problem you do.

Mike.
Edit, speedstep is unsigned int initialised as zero. It get decremented to 65535. Are you waiting long enough? That's 65 seconds - over a minute.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…