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.

DELAY and DISPLAY in microcontroller programming

Status
Not open for further replies.

mailus

New Member
i have difficulty in programming with delays and 7 segment display routines;
now my doubt is,

i have data(in bytes) in a variable, i need to check the location of bit(from byte mentioned) which is zero. then i need to display in a single seven segment display(1~8). here i use for loop and bit manipulation function to find the location,on the same time i need to display which location is zero.for this i use delay function in for loop.how to avoid this delay function in for loop and the same time display the output

i put my code here:

Code:
#include <16f877a.h>
#fuses HS,PUT,NOWDT,NOPROTECT,NOBROWNOUT 

#use delay(clock=4000000)

#define crystal 4000000
#define time 750
#define relay_time 500
#define debounce 50


unsigned int8 curr_ipt=0xff,i,sli=0,temp=0;
int1 rd_ipt=1;



int conv(unsigned int8 data)           //function with return value
{
   switch(data)                        //return the converted data to display in the 7-segment
   {
      case 1:
         return(0xf9);
         break;
      case 2:
         return(0xa4);
         break;
      case 3:
         return(0xb0);
         break;
      case 4:
         return(0x99);
         break;
      case 5:
         return(0x92);
         break;
      case 6:
         return(0x82);
         break;
      case 7:
         return(0xf8);
         break;
      case 8:
         return(0x80);
         break;
      case 9:
         return(0x90);
         break;
      default:
         return(0xff);
         break;

    }

}


void output()
{
   if((curr_ipt==0xff) && sliver==1)        //if all pins in portc is high
   {
      output_low(pin_A1);   
      output_low(pin_A2);
      output_high(pin_A3); //relay,error_led signal are low and ready signal is high
      delay_ms(relay_time);
      
   } else if((curr_ipt!=0xff) || sliver==0)    //any of the input is low
   {
      rd_ipt=0;
      output_high(pin_A1);   
      output_high(pin_A2);
      output_low(pin_A3);     //relay,error_led signal are high and ready signal is low
      delay_ms(relay_time);
   }

}




void display()
{
   if(curr_ipt!=0xff)         //check input if any change enter loop
   {
      for(i=0;i<=7;i++)
      {
         if((curr_ipt &(1<<i))==0)     //for each value of i bit location is change to ith location
         {                              //if any particular bit is zero means enter   
            delay_ms(debounce);        //debounce time
            if((curr_ipt &(1<<i))==0)  //check again if still bit 0
            {
            temp=i;                    //save the location i in temp variable
            output_B(conv(temp+1));    //call function "conv" and display in the portB
            delay_ms(time);            //time for display the content
            }
         }
      }
   }
   
   if(sli==0)                       //if sli input is zero means enter here
   {
      delay_ms(debounce);              //debounce time
      if(sli==0)                      //check again still input is zero enter
      {
      output_b((conv(9)));             //call fn"conv" and display digit '9' on display
      delay_ms(time);
      }
   }
   if((curr_ipt==0xff) && sli==1)     //if all inputs are high display empty
   {
      output_b((conv(0)));
   }

}


void main()
{
   while(TRUE)
   {
      start:
      if(rd_ipt==1)        //read port when rd_ipt is enabled
      {
         curr_ipt=input_c();   //assign port c value to variable curr_ipt
         sli=input(pin_a0);
      }
      output();               //call output function
      display();              //call display function
      if(input(pin_A5))
      {
      delay_ms(20);
      if(input(pin_A5)) 
      {
        rd_ipt=1;
        goto start;
        output_high(pin_E0);
      }
      }
   }

}



problems in my code:

"The goto and continue keywords shall not be used. The break keyword shall not be used outside
of a switch statement. These keywords lead to spaghetti code.
"

"i have implemented delay in a closed loop so it make my code to too lazy one,so controller wait some time until the closed loop has to complete"


so i keep on searching i found that RTOS concept.
that means time slicing or time slot method by using cooperative tasking and round-robin technique.is this technique suitable for my problem?

please give some solution to solve this problem
 
Just get rid of the label and the goto. Plus the line after the goto cannot be reached so delete it,
Code:
void main(){
   while(TRUE){
      if(rd_ipt==1){        //read port when rd_ipt is enabled
         curr_ipt=input_c();   //assign port c value to variable curr_ipt
         sli=input(pin_a0);
      }
      output();               //call output function
      display();              //call display function
      if(input(pin_A5)){
         delay_ms(20);
         if(input(pin_A5)){
            rd_ipt=1;
         }
      }
   }
}

BTW, I changed your indenting so it made sense to me.

Mike.
 
Just get rid of the label and the goto. Plus the line after the goto cannot be reached so delete it,
Code:
void main(){
   while(TRUE){
      if(rd_ipt==1){        //read port when rd_ipt is enabled
         curr_ipt=input_c();   //assign port c value to variable curr_ipt
         sli=input(pin_a0);
      }
      output();               //call output function
      display();              //call display function
      if(input(pin_A5)){
         delay_ms(20);
         if(input(pin_A5)){
            rd_ipt=1;
         }
      }
   }
}

BTW, I changed your indenting so it made sense to me.

Mike.

my condition is when i press the switch pin_A5 goes high,
then my program must execute from the beginning.

Problem in my code is if pin_A5 goes high it does not execute from beginning because of my "for loop" -the closed loop contains delay.
 
Here's my suggestion of a version of your case conv() function. I use MPLABX code formatting feature and XC8. Nice.

Code:
unsigned char conv(unsigned char b) {
    const char c[] = {7, 88, 66, 33}; // array starts at zero
    return b < sizeof (c) ? c[b] : 0; // the fun ternary command
}

call it with this
Code:
boo = conv(value);

I've only been programming for a month or so...
 
Code:
unsigned char conv(unsigned char b) {
    const char c[] = {7, 88, 66, 33}; // array starts at zero
    return b < sizeof (c) ? c[b] : 0; // the fun ternary command
}

call it with this
Code:
boo = conv(value);

what does it mean??? please explain how this value is converted for seven segment.
 
This part of your code
Code:
int conv(unsigned int8 data)           //function with return value
{
   switch(data)                        //return the converted data to display in the 7-segment
   {
      case 1:
         return(0xf9);
         break;
      case 2:
         return(0xa4);
         break;
      case 3:
         return(0xb0);
         break;
      case 4:
         return(0x99);
         break;
      case 5:
         return(0x92);
         break;
      case 6:
         return(0x82);
         break;
      case 7:
         return(0xf8);
         break;
      case 8:
         return(0x80);
         break;
      case 9:
         return(0x90);
         break;
      default:
         return(0xff);
         break;
 
    }
 
}

Could also be done like this. Look up the Ternary C command. See if you can figure out why.
Code:
unsigned char conv(unsigned char b) {
    const char c[] = {0xFF ,0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x90}; // array starts at zero
    return b < sizeof (c) ? c[b] : 0xFF; // the fun ternary command
}

As for the refresh timer interval, TIMER2 makes a pretty handy interrupt timer, TIMER1 too but you need to use the "special" compare mode in the CCP module.

Note: unsigned char is effectively a byte or int8
 
Last edited:
"The goto and continue keywords shall not be used. The break keyword shall not be used outside
of a switch statement. These keywords lead to spaghetti code.
"

Goto and break are perfectly good if you use them the right way. They do not automatically lead to spaghetti code.
 
Goto and break are perfectly good if you use them the right way. They do not automatically lead to spaghetti code.

hi,
I agree with MrT.

There is so much nonsense posted by the programming 'purists' about Goto's being bad practice.

E
 
Code:
return b < sizeof (c) ? c[b] : 0xFF; // the fun ternary command

i did not understand the line

why we need "return b < sizeof(c)" here.
 
why we need "return b < sizeof(c)" here.

"When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier."

The line checks that the b is not out of array bounds.
 
The goto and continue keywords shall not be used. The break keyword shall not be used outside
of a switch statement. These keywords lead to spaghetti code

i am a beginner,while searching about how to program and what are the do's and don't for a program.
I found a PDF file in that file contains list of do's and don't there i found this lines.

i agree with these point:
Goto and break are perfectly good if you use them the right way. They do not automatically lead to spaghetti code.
 
"When the sizeof operator is applied to an array, it yields the total number of bytes in that array, not the size of the pointer represented by the array identifier."

The line checks that the b is not out of array bounds.

so it always yield to " c "; may i correct.
 
i am a beginner,while searching about how to program and what are the do's and don't for a program.
I found a PDF file in that file contains list of do's and don't there i found this lines.

You can follow some guidelines. Nothing wrong with that. When you learn more you need also learn when not to follow guidelines and trust your own judgement. I don't remember ever using goto, but I will when it is needed.

Here is an example of good use of goto:
C:
void someFunction(int *pointer) {
    // do stuff etc.
    if (pointer == NULL) goto error;
    // Error check ok.. do more stuff
    if (pointer == NULL) goto error;
    // Error check still ok.. do more stuff
    return;

    error:
    // clean up and handle error
    return;
}
The point is that you can do many error checks, but you only need to write the error handling code once.
 
how to reduce the controller power by check the input at particular interval.
in my program it continuously check the input.
 
how to reduce the controller power by check the input at particular interval.
in my program it continuously check the input.

Timers and interrupts, depending on the input type edge, level or on change triggers are also possible.

I believe this belongs in the homework section.
 
Status
Not open for further replies.

Latest threads

Back
Top