This is something your experiments will tell. The only thing you have to change for such a test, is the frequency of timer interrupt.varunme said:Its to control LEDs so, is the frequency really accounts ?
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.varunme said:can you give a skeleton of using the timer interrupt ?
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
}
Varun,varunme said:I am getting some compiler errors, am working on it
Well this couldn't work because you have a function body inside main() and this is illegal. Do the following:
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
}
You could store the previous light status and compare it with the current one: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%.
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
}
You mean that you have inputs not only from PORTC but from other ports as well?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 ?
What about #53? You didn't gave feedback about it. It should work as well.varunme said:#51, works excellently
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
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
}
Yes you are right.varunme said:and cant change the two adjescent bits in the case of E1 and E2
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));
}
i just deleted it[/COLOR]Oooops... What happened to varunme's post?
OK.varunme said:i just deleted it
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?