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

Status
Not open for further replies.
OK, since 50% and 100% pwm is required, things are not so complicated.
Set a timer interrupt. If you need 1KHz for example, the interrupt must be coming every 500us. Inside timer interrupt declare a flag. Each time interrupt comes, this flag will be toggled.
Now you said that the solution of post #27 worked well. With small effort and this timer interrupt involved, you will do this quickly.
If sensor is intercepted, then the corresponding output will be driven with 1 (100%), no changes on that. But if it is not intercepted, then it will not be 0 in any case. It will depend on the value of the flag toggling inside timer interrupt. If it is 1, then the output will go to 1. Else, it will go to 0.
In this simple way, you will quickly adjust the output to be 50% or 100%, instead of 0 and 100% as it is now.

varunme said:
Its to control LEDs so, is the frequency really accounts ?
This is something your experiments will tell. The only thing you have to change for such a test, is the frequency of timer interrupt.


Hope that helped,
Alexandros
 

    V

    Points: 2
    Helpful Answer Positive Rating
I am working on this,
can you give a skeleton of using the timer interrupt ?
 

dear varun..

Sorry for late reply.

Your requirement is to glow/increase the intensity of the adjacent lights when a vehicle or more passes the sensor.

"Here is the explanation which i have posted in brief (post no 33). When vehicle passes the sensor, either high to low or low to high will be the output of sensor. So consider this signal as a clock for a jk flip flop and tie the input pins of JK flip flop with VCC. Now when ever a vehicle passes, flip flop will get a pulse at the clock pin and toggles the state of output from high to low or low to high. Connect this output to port pin (input port). Now until another vehicle crosses, the state of the flip flop does not change as well as u are going to read same result at input port.

Now in your program instead of reading logic high to verify the vehicle interruption, you can verify the logic state change from the previous read data and new read data. If the logic changes from one state to another state, then you can increase the intensity of the light otherwise give some delay to glow the adjacent lights with intensity and then reduce them gradually. During this period, if another vehicle passes, you can identify the change of state and your program will glow the light with intensity

I have you one more point too. I think it is better to understand this logic first. If it suits to your application and your requirement, i tell you that point too"

Thanks and sorry for late reply

Have a nice day
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
varunme said:
can you give a skeleton of using the timer interrupt ?
I will not use a flag but a counter, in this way you could adjust later on to other duty cycles than 50%. All the algorithm is running inside timer interrupt. I think it is better this way, because from outside you may lose counts. If in the future you see that no counts are lost, then you can keep only the counter part inside interrupt (the if-else part with count variable) and all the others from outside.

Code:
volatile unsigned char count = 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));
    }
  }

  PORTD = port_D;  //update PORTD
}

Try this code and post back for the results.

Hope this solves it,
Alexandros
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
I am getting some compiler errors, am working on it
 

varunme said:
I am getting some compiler errors, am working on it
Varun,

the TimerISR() function name is random. If this is the fault, then see how pic defines that name. I don't use pic, so I gave this random name.
 

not just timer

 

Please make line numbers visible to understand wich error goes where. I cannot see it clearly, but under PORTD = port_d command there are 2 '}' in a row? If so this is a mistake.
I think that the mistake will be easy to find, it is syntax or something like that. As I said post another screenshot with all function visible to the end plus line nubmers. Remember that this code run just fine, the only thing changed is the if () under the for() loop.
 

ok , i will check once again and reply
 

I have attached the errors with line numbers

 

I have attached the errors with line numbers

View attachment 67537
Well this couldn't work because you have a function body inside main() and this is illegal. Do the following:

Code:
void TimerISR (void);
void main (void) 
{
  trisc=0xff;
  trisd=0x00;
  while (1)
  {
    TimerISR();
  }
}

volatile unsigned char count = 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));
    }
  }

  PORTD = port_D;  //update PORTD
}

Does your compiler has a built-in function for delay? If yes, put an 1ms delay below TimerISR(); line inside main, so to have a frequency of 1ms. If this works OK, almost all the job is done.

Alexandros
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
yes, this is working.

But as soon as the sensor is activated, the two lights is 100% ( The two adjescent lights are going 100%),
The moment it deactivates, it goes 50%.

I am having a small distance from one sensor to another, The light has to be at 100% until the adjacent sensor is activated,
means, at the small distance i have mentioned, the light has to be at 100%.

I am working on that.
 

varunme said:
I am having a small distance from one sensor to another, The light has to be at 100% until the adjacent sensor is activated,
means, at the small distance i have mentioned, the light has to be at 100%.
You could store the previous light status and compare it with the current one:

Code:
void TimerISR (void);
void main (void) 
{
  trisc=0xff;
  trisd=0x00;
  while (1)
  {
    TimerISR();
  }
}

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;

  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
}

I think this should work.
 
Reactions: varunme

    varunme

    Points: 2
    Helpful Answer Positive Rating
#51, works excellently,
in #51,
how can i combine the inputs from port A and Port E and output in port B,
Together with this condition of, the inputs from C to D ?
 

varunme said:
how can i combine the inputs from port A and Port E and output in port B,
Together with this condition of, the inputs from C to D ?
You mean that you have inputs not only from PORTC but from other ports as well?


varunme said:
#51, works excellently
What about #53? You didn't gave feedback about it. It should work as well.
 

#53 also working ,
but , i think need to tweak a bit, its 100% initially.


but my concept is more suited for #51

but now i need more sensors and lights, so i have to take port A and E as input
as output to port B

means

portc -> portd

and

portA + port E -> portB

E is having 3bits
A having 0 to 5
B having 8bits
 
Last edited:

varunme said:
but now i need more sensors and lights, so i have to take port A and E as input
as output to port B

means

portc -> portd

and

portA + port E -> portB

E is having 3bits
A having 0 to 5
B having 8bits


Expanding post #51 code:

Code:
void TimerISR (void) 
{
  volatile unsigned char port_A;
  volatile unsigned char port_E;
  volatile unsigned char port_B = 0;
  unsigned char i;

  //............................ the rest of the code remains as is.

  PORTD = port_D;  //update PORTD


  port_A = PORTA; //save PORTA
  for(i = 0; i < 5; i++)
  {
    if ((port_A&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_B |= (1<<i);
      if (i)
        port_B |= (1<<(i-1));
    }
  }

  port_E = PORTE; //save PORTE
  for(i = 0; i < 3; i++)
  {
    if ((port_E&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_B |= (1<<(i+5));
      if (!i)
        port_B |= 0x10;  //Previous light input is from PORTA
    }
  }

  PORTB = port_B;  //update PORTB
}


See if this works.
 

    V

    Points: 2
    Helpful Answer Positive Rating
varunme said:
and cant change the two adjescent bits in the case of E1 and E2
Yes you are right.

Code:
    if ((port_E&(1<<i)) || (count >= 1))  //pin will be set in any case if count > 0
    {
      port_B |= (1<<(i+5));
      port_B |= (1<<(i+4));
    }


---------- Post added at 10:08 ---------- Previous post was at 10:07 ----------

Oooops... What happened to varunme's post?
 
Last edited:

[/COLOR]Oooops... What happened to varunme's post?
i just deleted it
because, port A and E are having so much peripherals,
i want to disable all , by checking with datasheet,
As for doing that i have deleted the post,


As for now the program starts with all output pins high, not flickering
 

varunme said:
i just deleted it
OK.
By the way I deleted two lines in post #58, I think they are not necessary any more. When you 're done with the datasheet, I think the code is OK now.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…