Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! 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

Full Member level 4
Full Member level 4
Joined
Mar 6, 2012
Messages
234
Helped
7
Reputation
16
Reaction score
7
Trophy points
1,308
Location
-
Visit site
Activity points
2,706
hi.,
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 coding here:
Code:
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
            }
         }
      }


i looking for your reply.....
 

You must avoid to use Delay this maner, that is implemented intrinsicly on closed loop.
Another approach is perform a time slot, which releases program to keep runing.

+++
 

You must avoid to use Delay this maner, that is implemented intrinsicly on closed loop.
Another approach is perform a time slot, which releases program to keep runing.

+++

please explain with example,i am new to microcontroller

- - - Updated - - -

You want to check which bit in the byte is 0 and then display the position of those bits on 7 Segment display? If yes, what is the problem you are facing?

if i use above mentioned code,an another input from a pin force the task to read the input again.now due to the above mentioned code the loop will not exit at (20-30)ms period.
it took addition of delay_ms(time)(time=750 defined in a macro). so this a series problem in my code.
 

Post the full code so that it can be fixed.

Here the code:

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);
      }
      }
   }

}
 

Mention the problem area of the code. You said that another pin is forcing to read something again. Which pin is that. Point to that pin.

input(pin_A5),by using goto statement.

Code:
if(input(pin_A5))
      {
      delay_ms(20);
      if(input(pin_A5)) 
      {
        rd_ipt=1;
        goto start;
        output_high(pin_E0);
      }
      }
 

Is the display() function called again. If yes, before goto start; set a flag and use this flag to execute display() function i.e., if(flag==1)//then don't execute display() if(flag == 0)display();

If that doesn't help then clearly explain step by step the flow of program needed so that program flow can be modified as needed.
 

Is the display() function called again. If yes, before goto start; set a flag and use this flag to execute display() function i.e., if(flag==1)//then don't execute display() if(flag == 0)display();

If that doesn't help then clearly explain step by step the flow of program needed so that program flow can be modified as needed.

andre_teprom correctly understand my problem(i put delay on a closed loop). The member suggest "Another approach is perform a time slot, which releases program to keep runing."
I want to know about this method please give some examples or explain the concept
 

mailus,

...I want to know about this method please give some examples or explain the concept...
This is more commonly known as Timeslicing.
Basically, you must to use Interrupt handler to generate time base to slot each routine according its time requirements.
...input(pin_A5),by using goto statement...
Other point : You must strongly avoid to use goto statement in C.
It become the program into a non structured one.


+++
 
  • Like
Reactions: mailus

    mailus

    Points: 2
    Helpful Answer Positive Rating
mailus,


This is more commonly known as Timeslicing.
Basically, you must to use Interrupt handler to generate time base to slot each routine according its time requirements.

yes,now i got some ideas about time slot method.i will re-modify my code.

- - - Updated - - -

please suggest me(links or reference books),
some topics related to programming to improve my skills(for ex: time slot method,as you suggested).
 

can i implement these tasking methods for my problem?
Cooperative scheduling,Round-robin scheduling
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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;
            break;
            output_high(pin_E0);
            }
      }
}




See if you have to make rd_ipt=0; before the break; statement.
 

please forgot my coding because it has many problems.i need error free code.
i mention the 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"
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top