alexxx
Advanced Member level 4
Please post the complete c file.varunme said:The code in post #53 , keeps all lights in portd "on" always
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Please post the complete c file.varunme said:The code in post #53 , keeps all lights in portd "on" always
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
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;
//.....................
}
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.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.
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
}
Try this code and post some feedback because it is not testedvarunme 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% ?
#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
}
That's strange, according to my simulator it goes off.The light not going off after 1 minitue
can you give some guidance in this ?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.
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;
}
}
}
}
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:if certain time condition is satisfied then b0,
an another time condition is satisfied then b1
and so on
No you change pwm frequency in this way, not duty cycle.varunme said:In the second if , there should be 100% intensity in the lights , so there needs no delay
varunme said:bo, b1 ,b2 is activated by the timing condition from the first pic
while(1)
{
if(portb.f0==1)
{
TimerISR();
}
else if (portb.f1==1)
{
portd=0xFF;
}
else if (portb.f2==1)
{
portd=0x00;
}
delay_ms(1);
}
Comment out the following lines and post some feedback with the results.varunme said:the last light does not hold high for some time
//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);
After commenting,
The lights doesn't holds high
port_D |= (1<<i); //Then keep light opened
if (i)
port_D |= (1<<(i-1)); //together with light next to it