0

Good night! To everyone seeing this, Im an student and I have a problem here, hope someone can help me out. Thanks

So Im using a micro PIC16F877A to make a digital counter that shows on a display, so my goal in this code is to make this counter count to 36 and then restart from 0. So I did an If statement at the bottom where if tens(de) reach 3 and units(un) reach 6, it starts from 0 again.

More data: Currently working on MPLAB-X and Proteus, using C language, the Bit config of the pic will be attach here and the proteus proyect as well. I suppose there is better ways to make this on a PIC, but all this is really new to me, so I'll be glad to learn about it.

Here is the code:

#include <xc.h>
#define _XTAL_FREQ 4000000

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)


int ar = 0;
int de = 0, un = 0, A = 0;
int Display[10] = {0XC0, 0XF9, 0XA4, 0XB0, 0X99, 0X92, 0X82, 0XF8, 0X80, 0X98};
void visualizacion(void);
void incremento(void);

void main(void)
{
    TRISC = 0X00;         // PUERTO C COMO SALIDA
    TRISB = 0X00;         // PUERTO B SALIDA
    TRISDbits.TRISD0 = 1; // PUERTO D0 COMO ENTRADA
    
    while (1)
    {
        if (PORTDbits.RD0 == 1)
        {
            ar = 1;
        }
        if (PORTDbits.RD0 == 0 && ar == 1)
        {
            un = un+1;
            ar = 0;
        }
        visualizacion();
        incremento();
    }
}

void visualizacion(void)
{
    PORTC = 0X01;
    PORTB = Display[un];
    __delay_us (500);
    
    PORTC = 0X02;
    PORTB = Display[de];
    __delay_us (500);
    
}

void incremento(void)
{
    if (un >= 10)
    {
        un = 0;
        de = de+1;
    }
    if(de >= 3 && un >= 6)
    {
        de = 0;
    }
}

This is the bit config

And this is the error: output error image

Thanks!

3
  • replace if(de>=3) && (un>=6){ with : if (de >= 3 && un >= 6) { Commented Jun 13, 2022 at 1:26
  • 1
    Also change de=0; to de=0; un=0; otherwise you rest to 6. Commented Jun 13, 2022 at 1:41
  • Thanks guys for replying here, it worked!!! also I replied to thank earlier but seems that my comment dissapeared and i don't know why, so ima @ u Commented Jun 13, 2022 at 9:16

1 Answer 1

2

The question is answered in the comments to the question (by mlwn & stark):

replace if(de>=3) && (un>=6){ with : if (de >= 3 && un >= 6) {

Also change de=0; to de=0; un=0; otherwise you rest to 6.

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

Comments

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.