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.

[SOLVED] How can we change the state of 3 adjescent

Status
Not open for further replies.
varunme said:
The code in post #53 , keeps all lights in portd "on" always
Please post the complete c file.
 

the complete c code is as follows

Code:
void TimerISR (void);
void main (void)
{

  trisc=0xff;
  trisd=0x00;
  // portd=0x00;
  while (1)
  {
    TimerISR();
    delay_ms(1);
  }
}
unsigned char i;
volatile unsigned char port_C;
volatile unsigned char port_D = 0;
volatile unsigned char count = 0;
volatile unsigned char previous_port_D = 0;
void TimerISR (void){

  if (count >= 1)
    count = 0;
  else
    count++;

  port_C = PORTC; //save PORTC
  for(i = 0; i < 8; i++)
  {
    if ((port_C&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_D |= (1<<i);
      if (i)
        port_D |= (1<<(i-1));
    }
    else
    {
      if (!(port_C&(1<<i)))  //if sensor is not intercepted
      {
        if (i<8)
        {
          if (previous_port_D & (1<<i))  //and previous situation was light on
          {
            if (!(port_C&(1<<(i+1))))  //but the next sensor is still not activated
            {
              port_D |= (1<<i);  //Then keep light opened
              if (i)
                port_D |= (1<<(i-1));  //together with light next to it
          }
        }
      }
    }
  }

  previous_port_D = port_D;  //update previous
  PORTD = port_D;  //update PORTD
  }
  }
 

varunme said:
the complete c code is as follows

Hello again Varun!
This is not the code as I posted it. My post was as follows:

Code:
volatile unsigned char count = 0;
volatile unsigned char previous_port_D = 0;
void TimerISR (void) 
{
  unsigned char i;
  volatile unsigned char port_C;
  volatile unsigned char port_D = 0;
//.....................
}

I didn't bother checking the code in your previous post, change it as the original posted code and then if it has any bugs we can check it again.


Alexandros
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
I have made the code as you have given,
but now too some problems, the other lights other than the activated sensor's light goes to high frequency as soon as the sensor got activated.

and the lights doesn't keep high till the next sensor is activated.
 

varunme said:
the other lights other than the activated sensor's light goes to high frequency as soon as the sensor got activated.

and the lights doesn't keep high till the next sensor is activated.
Yes you are right. I miscalculated the fact that the previous situation could be a result not only of sensor, but of 50% duty cycle as well. Try the following code and post back the results. It looks OK, I tested it on simulator.

Code:
volatile unsigned char count = 0;
volatile unsigned char previous_port_C = 0;
void TimerISR (void)
{
  unsigned char i;
  volatile unsigned char port_C;
  volatile unsigned char port_D = 0;

  if (count >= 1)
    count = 0;
  else
    count++;

  port_C = PORTC; //save PORTC
  for(i = 0; i < 8; i++)
  {
    if ((port_C&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_D |= (1<<i);
      if (i)
        port_D |= (1<<(i-1));
			
      if (port_C&(1<<i))	//If sensor is intercepted
      {
        previous_port_C |= (1<<i);	//update variable
	if (i)
	{
	  if (previous_port_C & (1<<(i-1)))	//and previous value variable for next time
	    previous_port_C &= ~((1<<(i-1)));
        }
      }
    }
    else
    {
      if (!(port_C&(1<<i)))  //if sensor is not intercepted
      {
        if (i<8)
        {
          if (previous_port_C & (1<<i))  //and previous situation was light on
          {
            if (!(port_C&(1<<(i+1))))  //but the next sensor is still not activated
            {
	      if (i<7)	//if not the last sensor
	      {
	        port_D |= (1<<i);  //Then keep light opened
		if (i)
		  port_D |= (1<<(i-1));  //together with light next to it
              }
	      else  //for last sensor just turn off light
	        previous_port_C &= ~(1<<7);
	    }
          }
        }
      }
    }
  }
  PORTD = port_D;  //update PORTD
}
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes, this is the code that i want.. ,
working like charm,

can we modify the code so that if the next sensor is not activated for 1 minitues, the previously activated sensor's light goes 50% ?
 

varunme said:
can we modify the code so that if the next sensor is not activated for 1 minitues, the previously activated sensor's light goes 50% ?
Try this code and post some feedback because it is not tested

Code:
#define _SENSOR_TIMEOUT		6000-2  //According to the code, -2 is needed from the desired value(10ms*6000=1min)
volatile unsigned char count = 0;
volatile unsigned char previous_port_C = 0;
volatile unsigned int portC_sensor_timeout[8]={0,0,0,0,0,0,0,0};
void TimerISR (void)
{
  unsigned char i;
  volatile unsigned char port_C;
  volatile unsigned char port_D = 0;

  if (count >= 1)
    count = 0;
  else
    count++;

  port_C = PORTC; //save PORTC
  for(i = 0; i < 8; i++)
  {
    if ((port_C&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_D |= (1<<i);
      if (i)
        port_D |= (1<<(i-1));
	
      if (port_C&(1<<i))	//If sensor is intercepted
      {
        previous_port_C |= (1<<i);	//update variable
        portC_sensor_timeout[i] = 0;	//reset timeout
        if (i)
        {
          if (previous_port_C & (1<<(i-1)))	//also update previous value variable for next time
            previous_port_C &= ~((1<<(i-1)));
        }
      }
    }
    else	//sensor not intercepted
    {
      if (previous_port_C & (1<<i))  //if previous situation was light on
      {
        if (!(port_C&(1<<(i+1))))  //but the next sensor is still not activated
        {
          portC_sensor_timeout[i] += 2;	//+2 for the period of light on (because of 50%)
          if (portC_sensor_timeout[i] >= _SENSOR_TIMEOUT)
          {
            portC_sensor_timeout[i] = 0;
            previous_port_C &= ~(1<<i);	//at next cycle light will go off
          }
					
          if (i<7)	//if not the last sensor
          {
            port_D |= (1<<i);  //Then keep light opened
            if (i)
              port_D |= (1<<(i-1));  //together with light next to it
          }
          else  //for last sensor just turn off light
            previous_port_C &= ~(1<<7);
        }
      }
    }
  }

  PORTD = port_D;  //update PORTD
}


Alexandros
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
The light not going off after 1 minitue
 

The light not going off after 1 minitue
That's strange, according to my simulator it goes off.

I just noticed that your delay is 1 and not 10ms, so the correct value would be 60000-2, but this could not be a problem in any case.

Try change the _SENSOR_TIMEOUT value to 1000, so that the timeout will become shorter to 1sec. It will be easier to debug in this way. Then drop it to 100, then to 10. Will it work with values like that?

Furthermore, doublecheck that your delay() function works properly, because that could be a reason of failure.

If problem insists post the complete file.


Alexandros
 
Last edited:
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes, that works perfectly, thank you very much

Actually the code works with 60000 itself,

i think some problems with the simulator kept me out of success
After a system restart all went fine.


thank you.jpg
 
  • Like
Reactions: alexxx

    alexxx

    Points: 2
    Helpful Answer Positive Rating
So you are saying that one pic measures time and depending on the timing condition it drives pins of the other pic to command him to do something with the lights?
If this is the case, then things are much more simpler. When you read the inputs from the lights pic, if the time pic commanded something, then lights pic will follow it. Thus you can drive lights from the inputs read function. Or even better, read the inputs, update your flags, and from another function drive lights according to the values of those flags, this is much more structured.
If I misunderstood, please explain the scenario in details.
can you give some guidance in this ?
 

can i do like this ?
in the lighting pic ?


Code:
void main() {

     trisc=0xff;        //input
     trisd=0x00;        //output
     trisb=0xff;        //input
                while(1)
                        {
                        if(portb.f0==1)
                        {
                                       while (1)
                                       {
                                        TimerISR();
                                        delay_ms(1);
                                        }
                        }
                        else if (portb.f1==1)
                        {
                                        while (1) {
                        
                                        TimerISR();
                                        }
                        }
                        else if (portb.f2==1)
                        {
                                        while (1) {
                                        portd=0x00;
                                        }
                        }
}
}
 

This doesn't seem to be correct. If portb.f0=1, then the code will never get out of this if (because of the while(1) loop). Same stands for portb.f1 and f2 as well. Furthermore I can't imagine the purpose of not keeping delay in the second if. Please give detailed description. What exactly b0, b1 and b2 represent and what is the scenario with those signals.
 

bo, b1 ,b2 is activated by the timing condition from the first pic

if certain time condition is satisfied then b0,
an another time condition is satisfied then b1
and so on

In the second if , there should be 100% intensity in the lights , so there needs no delay
 

varunme said:
if certain time condition is satisfied then b0,
an another time condition is satisfied then b1
and so on
I assume then that those ports should be read in every cycle and not just once, so those while(1) must be gone.


varunme said:
In the second if , there should be 100% intensity in the lights , so there needs no delay
No you change pwm frequency in this way, not duty cycle.


varunme said:
bo, b1 ,b2 is activated by the timing condition from the first pic
Code:
while(1)
{
  if(portb.f0==1)
  {
    TimerISR();
  }
  else if (portb.f1==1)
  {
    portd=0xFF;
  }
  else if (portb.f2==1)
  {
    portd=0x00;
  }
  
  delay_ms(1);
}

I think this is what you're looking for.
Just keep in mind that there is a priority in this way from higher to lower cases. If another condition is the priority than B0, it should be placed in the highest if().

Alexandros
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
As soon as the last sensor is activated, the last light also goes off,
so the last light does not hold high for some time,
can we work around this ?
 

varunme said:
the last light does not hold high for some time
Comment out the following lines and post some feedback with the results.

Code:
          //if (i<7)	//if not the last sensor
          //{
            port_D |= (1<<i);  //Then keep light opened
            if (i)
              port_D |= (1<<(i-1));  //together with light next to it
          //}
          //else  //for last sensor just turn off light
          //  previous_port_C &= ~(1<<7);


Alexandros
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
After commenting,
The lights doesn't holds high
 

After commenting,
The lights doesn't holds high

Did you make sure that you left the following part out of the comments?

Code:
            port_D |= (1<<i);  //Then keep light opened
            if (i)
              port_D |= (1<<(i-1));  //together with light next to it
 
  • Like
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
uhhh,

no,
i will check and reply

---------- Post added at 12:30 ---------- Previous post was at 12:28 ----------

yess

its working
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top