0
/*
 * File:   proje_6.c
 * Author: ayanoglu
 *
 * Created on 08 Nisan 2023 Cumartesi, 12:14
 */


#include <xc.h>

void delayFunction(unsigned int);

#define Button PORTBbits.RB0   //RB0 button

#define Led PORTBbits.RB1       //RB1 led

char Counter = 0;



void main(void) {
  
    TRISBbits.TRISB0 = 1;
    TRISBbits.TRISB1 = 0;


    
    if (Button==1){
        Counter++;
        
        
        if ((Counter !=0) && (Counter %10 == 0)){
            Led = 1;
            delayFunction(200);
            Led = 0;

        }
        
    }
    while(1);
    
    
    
    
    
}

void delayFunction(unsigned int itime){
    unsigned int i;
    unsigned char j;
    for (i=0;i<itime;i++){
        for(j=0;j<165;j++);
    }
}

I use P18F45K22

I want the led to turn on and turn off after a while when the number reaches tens(10,20,30..) but I can't get into the if statement.

pin RB0 as a button and pin RB1 as a led

counter should increase as I press the button

1
  • 1
    There isn't an activity loop. The code executes once, and then hangs at while(1);. Even if the if (Button==1) branch had been taken, then Counter == 1 and won't satisfy the condition to turn on the LED. Commented Apr 8, 2023 at 13:06

1 Answer 1

1

You would need something like:

#include <xc.h>
#include <libpic30.h>  //For __delay32(...)

#define Button PORTBbits.RB0   //RB0 button

#define Led PORTBbits.RB1       //RB1 led

void main(void) {
    INT8 Counter = 0; //char can be used on PIC's but nicer with an int  

    TRISBbits.TRISB0 = 1; //Input
    TRISBbits.TRISB1 = 0; //Output

    while(1) { //Loop forever....

     if (Button==1){ //Assuming here that the button toggles, no bounce, etc.

      Counter++;
             
        if ((Counter !=0) && (Counter %10 == 0)){
            Led = 1;
            __delay32(80000); //Delay 20 milliseconds to stabilise power supply 
            //*See note below... 
            Led = 0;

        }
    }    
}

*The __delay32() is a Microchip delay function. The parameter of 80000 was calculated for my system, yours might be different.

Sign up to request clarification or add additional context in comments.

2 Comments

it built succesfully but on proteus the button doesnt work
It shouldn't really matter but try replacing the PORTBbits.RB0 and PORTBbits.RB1 to LATBbits.LATB0 and LATBbits.LATB1.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.