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.

Trouble with mikro c code

Status
Not open for further replies.

Beloved

New Member
I used the following proteus design and Mikro c code to build a 6 digit 7segment count down timer using pic16f887A and ic 4026
The proteus design is named Zacch in the attached file and the mikro c is in the code tag. the issue am having is that my proteus simulation isn't working as it supposed to. Can anyone pls help me trouble shoot the code. It is urgent
C:
int i = 0;
int j = 0;
int var = 0;
int var1 = 0;
int var2 = 0;
int var3 = 0;
int var4 = 0;
int secR = 0;
int secL = 0;
int MinL = 0;
int MinR = 0;
int X = 0;
int a = 0;
int b = 0;
int k = 0;
int m = 0;
int n = 0;
int Setup = 1;
int Stop = 0;
int e=0;
int h=0;




void all_rst()
{
  PORTD.F0=1;
  PORTD.F1=1;
  PORTD.F2=1;
  PORTD.F3=1;
  PORTD.F4=1;
  PORTD.F5=1;

  PORTD.F0=0;
  PORTD.F1=0;
  PORTD.F2=0;
  PORTD.F3=0;
  PORTD.F4=0;
  PORTD.F5=0;

}


 void Display_Test()
{
  all_rst();
  var = 10;
  for (j = 0; j < 10; j++)
  {
    var = var - 1;
    for (i = 0; i < var; i++)
    {
      PORTB.F0=0;
      PORTB.F1=0;
      PORTB.F2=1;
      PORTB.F3=1;
      PORTB.F4=1;
      PORTB.F5=1;
      PORTB.F0=0;
      PORTB.F1=0;
      PORTB.F2=0;
      PORTB.F3=0;
      PORTB.F4=0;
      PORTB.F5=0;

    }
    }
    }

    void c_inc()
{
  X=1  ;
  X=0 ;
}
void buzz()
{
  for (e = 0; e < 10; e++)
  {
    PORTA.F2=1;
    delay_ms(100);
    PORTA.F2=0;
    delay_ms(100);
  }
  }




void main() {
  TRISA=0b11111111;
  TRISB=0b00000000;
  TRISC=0b00000000;
  TRISD=0b00000000;
  all_rst();
PORTC.F0=1;
PORTC.F1=1;
PORTC.F2=1;
PORTC.F3=1;
PORTC.F4=1;
PORTC.F5=1;
 delay_ms(500);
  Display_Test();

  while(1) {

     if (Setup==1)
  {
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=0;
PORTC.F4=0;
PORTC.F5=1;

    while (PORTA.F1 != 0)
    {
      if (PORTA.F0 == 0)
      {
        X = PORTB.F5;
        secR = secR + 1;
        c_inc();
        delay_ms(250);
        if (secR > 9)
        {
          secR = 0;
        }
      }
    }
    delay_ms(250);
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=0;
PORTC.F4=1;
PORTC.F5=0;
    while (PORTA.F1 != 0)
    {
      if (PORTA.F0 == 0)
      {
        X = PORTB.F4;
        secL = secL + 1;
        c_inc();
        if (secL > 5)
        {
          secL = 0;
          PORTD.F4=1;
          PORTD.F4=0;

        }
        delay_ms(250);
      }
    }
    delay_ms(250);
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=1;
PORTC.F4=0;
PORTC.F5=0;
    while (PORTA.F1 != 0)
    {
      if (PORTA.F0 == 0)
      {
        X = PORTB.F3;
        MinR = MinR + 1;
        c_inc();
        delay_ms(250);
        if (MinR > 9)
        {
          MinR = 0;
        }
      }
    }
    delay_ms(250);
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=1;
PORTC.F3=0;
PORTC.F4=0;
PORTC.F5=0;
    while (PORTA.F1 != 0)
    {
      if (PORTA.F0 == 0)
      {
        X = PORTB.F2;
        MinL = MinL + 1;
        c_inc();
        delay_ms(250);
        if (MinL > 9)
        {
          MinL = 0;
        }
      }
    }
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=1;
PORTC.F4=1;
PORTC.F5=1;
    var1 = secR;
    var2 = secL;
    var3 = MinR;
    var4 = MinL;
    Setup = 0;

  }

  var1 = var1 - 1;
  if (var1 == -1 && var2 == 0 && Stop == 0)
  {
    var1 = 9;
    var2 = 5;
    var3 = var3 - 1;
  }
  if (var1 == -1 && Stop == 0)
  {
    var2 = var2 - 1;
    var1 = 9;
  }
  if (var3 == -1)
  {
    var3 = 9;
    var4 = var4 - 1;
  }
  if (var1 == 9   && var2 == 5 && var3 == 9 && var4 == -1)
  {
    Stop = 1;
    buzz();
  }
  while(stop)
      {
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=0;
PORTC.F3=0;
PORTC.F4=0;
PORTC.F5=0;
    delay_ms(300);
PORTC.F0=0;
PORTC.F1=0;
PORTC.F2=1;
PORTC.F3=1;
PORTC.F4=1;
PORTC.F5=1;
    delay_ms(300);
  }
  delay_ms(1000);
  PORTD.F5=1;
  PORTD.F5=0;
  for (a = 0; a < var1; a++)
  {
    PORTB.F5=1;
    PORTB.F5=0;

  }
  PORTD.F4=1;
  PORTD.F4=0;
  for (b = 0; b < var2; b++)
  {
    PORTB.F4=1;
    PORTB.F4=0;
  }
  PORTD.F3=1;
  PORTD.F3=0;


  for (h = 0; h < var3; h++)
  {
    PORTB.F3=1;
    PORTB.F3=0;
  }
  PORTD.F2=1;
  PORTD.F2=0;

   for (k = 0; k < var4; k++)
  {
    PORTB.F2=1;
    PORTB.F2=0;
  }
















  }





}
 
Last edited:
No file attached.

Mike.
BTW, don't attach the code as a file but between [code] and [/code] tags.
 
You really need to comment your code, explanations at each point of what sections are intended to do.

It takes far longer to work out someone else's program without knowing what they were intending when they wrote it!

It also helps you (the author) if you need to go back and do mods in a few years! With enough internal comments, you don't need any separate notes about it.
 
It also helps if the indenting is correct. You may even find that if you correctly indent your code you will spot your error.

With regards to comments, I recently read "a comment is a love letter to your future self" - definitely agree.

Mike.
Edit also give your variables meaningful names. It's impossible to work out what is intended when the variables are called var1, var2 etc.
 
I ran your code through a tidier that I have and it produced this.
Code:
int i = 0;
int j = 0;
int var = 0;
int var1 = 0;
int var2 = 0;
int var3 = 0;
int var4 = 0;
int secR = 0;
int secL = 0;
int MinL = 0;
int MinR = 0;
int X = 0;
int a = 0;
int b = 0;
int k = 0;
int m = 0;
int n = 0;
int Setup = 1;
int Stop = 0;
int e=0;
int h=0;

void all_rst(){
  PORTD.F0=1;
  PORTD.F1=1;
  PORTD.F2=1;
  PORTD.F3=1;
  PORTD.F4=1;
  PORTD.F5=1;

  PORTD.F0=0;
  PORTD.F1=0;
  PORTD.F2=0;
  PORTD.F3=0;
  PORTD.F4=0;
  PORTD.F5=0;
}

void Display_Test(){
  all_rst();
  var = 10;
  for (j = 0; j < 10; j++){
    var = var - 1;
    for (i = 0; i < var; i++){
      PORTB.F0=0;
      PORTB.F1=0;
      PORTB.F2=1;
      PORTB.F3=1;
      PORTB.F4=1;
      PORTB.F5=1;
      PORTB.F0=0;
      PORTB.F1=0;
      PORTB.F2=0;
      PORTB.F3=0;
      PORTB.F4=0;
      PORTB.F5=0;
    }
  }
}

void c_inc(){
  X=1;
  X=0;
}

void buzz(){
  for (e = 0; e < 10; e++){
    PORTA.F2=1;
    delay_ms(100);
    PORTA.F2=0;
    delay_ms(100);
  }
}

void main(){
  TRISA=0b11111111;
  TRISB=0b00000000;
  TRISC=0b00000000;
  TRISD=0b00000000;
  all_rst();
  PORTC.F0=1;
  PORTC.F1=1;
  PORTC.F2=1;
  PORTC.F3=1;
  PORTC.F4=1;
  PORTC.F5=1;
  delay_ms(500);
  Display_Test();
  while(1){
    if (Setup==1){
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=0;
      PORTC.F4=0;
      PORTC.F5=1;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F5;
          secR = secR + 1;
          c_inc();
          delay_ms(250);
          if (secR > 9){
            secR = 0;
          }
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=0;
      PORTC.F4=1;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F4;
          secL = secL + 1;
          c_inc();
          if (secL > 5){
            secL = 0;
            PORTD.F4=1;
            PORTD.F4=0;

          }
          delay_ms(250);
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=1;
      PORTC.F4=0;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F3;
          MinR = MinR + 1;
          c_inc();
          delay_ms(250);
          if (MinR > 9){
            MinR = 0;
          }
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=1;
      PORTC.F3=0;
      PORTC.F4=0;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F2;
          MinL = MinL + 1;
          c_inc();
          delay_ms(250);
          if (MinL > 9){
            MinL = 0;
          }
        }
      }
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=1;
      PORTC.F4=1;
      PORTC.F5=1;
      var1 = secR;
      var2 = secL;
      var3 = MinR;
      var4 = MinL;
      Setup = 0;
    }
    var1 = var1 - 1;
    if (var1 == -1 && var2 == 0 && Stop == 0){
      var1 = 9;
      var2 = 5;
      var3 = var3 - 1;
    }
    if (var1 == -1 && Stop == 0){
      var2 = var2 - 1;
      var1 = 9;
    }
    if (var3 == -1){
      var3 = 9;
      var4 = var4 - 1;
    }
    if (var1 == 9   && var2 == 5 && var3 == 9 && var4 == -1){
      Stop = 1;
      buzz();
    }
    while(stop){
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=0;
      PORTC.F4=0;
      PORTC.F5=0;
      delay_ms(300);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=1;
      PORTC.F3=1;
      PORTC.F4=1;
      PORTC.F5=1;
      delay_ms(300);
    }
    delay_ms(1000);
    PORTD.F5=1;
    PORTD.F5=0;
    for (a = 0; a < var1; a++){
      PORTB.F5=1;
      PORTB.F5=0;
    }
    PORTD.F4=1;
    PORTD.F4=0;
    for (b = 0; b < var2; b++){
      PORTB.F4=1;
      PORTB.F4=0;
    }
    PORTD.F3=1;
    PORTD.F3=0;
    for (h = 0; h < var3; h++){
      PORTB.F3=1;
      PORTB.F3=0;
    }
    PORTD.F2=1;
    PORTD.F2=0;
    for (k = 0; k < var4; k++){
      PORTB.F2=1;
      PORTB.F2=0;
    }
  }
}
However, I've still no idea what it is supposed to do due to the lack of functions, variable names and comments.

For an example of a function, you have the following code,
Code:
    if (Setup==1){
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=0;
      PORTC.F4=0;
      PORTC.F5=1;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F5;
          secR = secR + 1;
          c_inc();
          delay_ms(250);
          if (secR > 9){
            secR = 0;
          }
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=0;
      PORTC.F4=1;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F4;
          secL = secL + 1;
          c_inc();
          if (secL > 5){
            secL = 0;
            PORTD.F4=1;
            PORTD.F4=0;

          }
          delay_ms(250);
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=1;
      PORTC.F4=0;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F3;
          MinR = MinR + 1;
          c_inc();
          delay_ms(250);
          if (MinR > 9){
            MinR = 0;
          }
        }
      }
      delay_ms(250);
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=1;
      PORTC.F3=0;
      PORTC.F4=0;
      PORTC.F5=0;
      while (PORTA.F1 != 0){
        if (PORTA.F0 == 0){
          X = PORTB.F2;
          MinL = MinL + 1;
          c_inc();
          delay_ms(250);
          if (MinL > 9){
            MinL = 0;
          }
        }
      }
      PORTC.F0=0;
      PORTC.F1=0;
      PORTC.F2=0;
      PORTC.F3=1;
      PORTC.F4=1;
      PORTC.F5=1;
      var1 = secR;
      var2 = secL;
      var3 = MinR;
      var4 = MinL;
      Setup = 0;
    }
This is obviously your setup code - make it a function and call it before your while(1) line,
Code:
  Display_Test();
  setup();
  while(1){
and put all the above code in the setup function where it belongs.
Do the same for other bits of code and give each function a descriptive name.
I note you do have some functions and this one,
Code:
void buzz(){
  for (e = 0; e < 10; e++){
    PORTA.F2=1;
    delay_ms(100);
    PORTA.F2=0;
    delay_ms(100);
  }
}
I can pretty much guess what it's doing.
But this one,
Code:
void c_inc(){
  X=1;
  X=0;
}
Couldn't even have a guess.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top