Regarding ICP1 pin

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hello i am working on atmega16 micro-controller, i am trying to interface the color sensor whos output is the pulse, i am want to connect it to the ICP1 pin (input capture) mode, here is my timer 1 configuration but i am not able to get any sort of results, please look into it and correct the code

 

void TIMER_1(void)
{
// Set up input capture, no prescaler
TCCR1B |= (1<<ICES1); // get rising edge first
TCCR1B |= (1<<CS10); // no prescaler
TIMSK |= (1<<TICIE1); // enable ICP interrupt

sei(); // enable global interrupts

 

}

ISR (TIMER1_CAPT_vect)
{
// Read ICR1
// Flip edge trigger
// Clear ICR1

localICR=ICR1;
TCCR1B ^= (1<<ICES1);
TCNT1=0;
pulse++;
}

i am shirish_avrlover

Last Edited: Wed. Feb 7, 2018 - 07:09 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don’t touch TCNT1. Store the capture values and do a subtract to get the period.
Note variables shared between isrs and other code must be declared volatile. You’ve not shown us this part.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

thank you sir i will try with volatile variable and send the complete code

i am shirish_avrlover

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sir i was trying to interface the color sensor 

who output is pulse 

 

here is the code for it, but not able to get any output 

 

the code consist of input capture 

 

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile unsigned long int pulse = 0; //to keep the track of the number of pulses generated by the color sensor
volatile unsigned long int red; // variable to store the pulse count when read_red function is called
volatile unsigned long int blue; // variable to store the pulse count when read_blue function is called
volatile unsigned long int green; // variable to store the pulse count when read_green function is called

volatile uint16_t localICR=0;

 

void color_sensor_pin_config(void)
{
DDRD = DDRD | 0x00;// setting all as input pins for here output pin of color sensor will be connected
PORTD= PORTD | 0x00; // initially all are set to 0
DDRC = DDRC | 0b11110111; // we have set all as output pin accept the pin 3
PORTC = PORTC | 0x00; // initially all are set to 0 this port is used to connect S3 and S2 pins

}

void port_init(void)
{
color_sensor_pin_config();//color sensor pin configuration
}

void TIMER_1(void)
{
// Set up input capture, no prescaler
TCCR1B |= (1<<ICES1); // get rising edge first
TCCR1B |= (1<<CS10); // no prescaler
TIMSK |= (1<<TICIE1); // enable ICP interrupt

sei(); // enable global interrupts

 

}

ISR (TIMER1_CAPT_vect)
{
// Read ICR1
// Flip edge trigger
// Clear ICR1

localICR=ICR1;
TCCR1B ^= (1<<ICES1);
TCNT1=0;
pulse++;
}

void init_devices(void)
{
cli(); //Clears the global interrupt
port_init(); //Initializes all the ports
TIMER_1();
sei(); // Enables the global interrupt
}

//Color Sensing Scaling
/// note that s0 and s1 are connected VCC ie all time s1 = s0= 1;
//Filter Selection s2=pc0 s3=pc1
void filter_red(void) //Used to select red filter
{
//Filter Select - red filter
PORTC = PORTC & 0b00000000; //set S2 low //set S3 low
}

void filter_green(void) //Used to select green filter
{
//Filter Select - green filter
PORTC = PORTC | 0b00000011; //set S2 High //set S3 High

}

void filter_blue(void) //Used to select blue filter
{
//Filter Select - blue filter
PORTC = PORTC & 0b00000010 ; //set S2 low //set S3 High

}

void filter_clear(void) //select no filter
{
//Filter Select - no filter
PORTC = PORTC & 0x00;
PORTC = PORTC |0x01;//set S2 High //set S3 Low
}

 

void red_read(void) // function to select red filter and display the count generated by the sensor on LCD. The count will be more if the color is red. The count will be very less if its blue or green.
{
//Red
filter_red(); //select red filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
red = pulse; //store the count in variable called red
}

void green_read(void) // function to select green filter and display the count generated by the sensor on LCD. The count will be more if the color is green. The count will be very less if its blue or red.
{
//Green
filter_green(); //select green filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
green = pulse; //store the count in variable called green

}

void blue_read(void) // function to select blue filter and display the count generated by the sensor on LCD. The count will be more if the color is blue. The count will be very less if its red or green.
{
//Blue
filter_blue(); //select blue filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
blue = pulse; //store the count in variable called blue

}

int main(void)
{
init_devices();
int r=0,g=0,b=0;

for(int i=0;i<2;i++) // testing for 2 times
{
red_read(); //display the pulse count when red filter is selected
_delay_ms(500);
green_read(); //display the pulse count when green filter is selected
_delay_ms(500);
blue_read(); //display the pulse count when blue filter is selected
_delay_ms(500);

r=r+red;
g=g+green;
b+=blue;
}
if (r>g && r>b)
{
PORTC = PORTC | 0b10000000; // pc7 red led is connected
_delay_ms(1000);
}

if (g>r && g>b)
{
PORTC = PORTC & 0b00000000;
PORTC = PORTC | 0b01000000; // pc6 greeen led is connected
_delay_ms(10000);
}

if(b>r && b>g)
{
PORTC = PORTC & 0b00000000;
PORTC = PORTC | 0b00100000; // pc5 blue led is connected
_delay_ms(10000);
}
}

i am shirish_avrlover

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

hello i was trying to interface the color sensor 

who output is pulse with atmega 16, my int0,int1,int2 are busy so forced to used input capture mode

 

here is the code for it, but not able to get any output 

 

the code consist of input capture 

pin connection

 

color sensor pin micro-controller pin 
o/p PD6
s2 PC0
s3 PC1
s0 VCC(logic 1)
S1 VCC(logic 1)
EN' GND

// code//

#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

volatile unsigned long int pulse = 0; //to keep the track of the number of pulses generated by the color sensor
volatile unsigned long int red; // variable to store the pulse count when read_red function is called
volatile unsigned long int blue; // variable to store the pulse count when read_blue function is called
volatile unsigned long int green; // variable to store the pulse count when read_green function is called

volatile uint16_t localICR=0;

 

void color_sensor_pin_config(void)
{
DDRD = DDRD | 0x00;// setting all as input pins for here output pin of color sensor will be connected
PORTD= PORTD | 0x00; // initially all are set to 0
DDRC = DDRC | 0b11110111; // we have set all as output pin accept the pin 3
PORTC = PORTC | 0x00; // initially all are set to 0 this port is used to connect S3 and S2 pins

}

void port_init(void)
{
color_sensor_pin_config();//color sensor pin configuration
}

void TIMER_1(void)
{
// Set up input capture, no prescaler
TCCR1B |= (1<<ICES1); // get rising edge first
TCCR1B |= (1<<CS10); // no prescaler
TIMSK |= (1<<TICIE1); // enable ICP interrupt

sei(); // enable global interrupts

 

}

ISR (TIMER1_CAPT_vect)
{
// Read ICR1
// Flip edge trigger
// Clear ICR1

localICR=ICR1;
TCCR1B ^= (1<<ICES1);
TCNT1=0;
pulse++;
}

void init_devices(void)
{
cli(); //Clears the global interrupt
port_init(); //Initializes all the ports
TIMER_1();
sei(); // Enables the global interrupt
}

//Color Sensing Scaling
/// note that s0 and s1 are connected VCC ie all time s1 = s0= 1;
//Filter Selection s2=pc0 s3=pc1
void filter_red(void) //Used to select red filter
{
//Filter Select - red filter
PORTC = PORTC & 0b00000000; //set S2 low //set S3 low
}

void filter_green(void) //Used to select green filter
{
//Filter Select - green filter
PORTC = PORTC | 0b00000011; //set S2 High //set S3 High

}

void filter_blue(void) //Used to select blue filter
{
//Filter Select - blue filter
PORTC = PORTC & 0b00000010 ; //set S2 low //set S3 High

}

void filter_clear(void) //select no filter
{
//Filter Select - no filter
PORTC = PORTC & 0x00;
PORTC = PORTC |0x01;//set S2 High //set S3 Low
}

 

void red_read(void) // function to select red filter and display the count generated by the sensor on LCD. The count will be more if the color is red. The count will be very less if its blue or green.
{
//Red
filter_red(); //select red filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
red = pulse; //store the count in variable called red
}

void green_read(void) // function to select green filter and display the count generated by the sensor on LCD. The count will be more if the color is green. The count will be very less if its blue or red.
{
//Green
filter_green(); //select green filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
green = pulse; //store the count in variable called green

}

void blue_read(void) // function to select blue filter and display the count generated by the sensor on LCD. The count will be more if the color is blue. The count will be very less if its red or green.
{
//Blue
filter_blue(); //select blue filter
pulse=0; //reset the count to 0
_delay_ms(100); //capture the pulses for 100 ms or 0.1 second
blue = pulse; //store the count in variable called blue

}

int main(void)
{
init_devices();
int r=0,g=0,b=0;

for(int i=0;i<2;i++) // testing for 2 times
{
red_read(); //display the pulse count when red filter is selected
_delay_ms(500);
green_read(); //display the pulse count when green filter is selected
_delay_ms(500);
blue_read(); //display the pulse count when blue filter is selected
_delay_ms(500);

r=r+red;
g=g+green;
b+=blue;
}
if (r>g && r>b)
{
PORTC = PORTC | 0b10000000; // pc7 red led is connected
_delay_ms(1000);
}

if (g>r && g>b)
{
PORTC = PORTC & 0b00000000;
PORTC = PORTC | 0b01000000; // pc6 greeen led is connected
_delay_ms(10000);
}

if(b>r && b>g)
{
PORTC = PORTC & 0b00000000;
PORTC = PORTC | 0b00100000; // pc5 blue led is connected
_delay_ms(10000);
}
}

i am shirish_avrlover

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You chose a rather difficult nethod to measure pulses. It makes little sense using the input capture with your method. Why would you not load a timer with the required period and loop count the input change whilst checking to see if the timer has timed out. You could probably count all three inputs at once - no need for interrupts.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Next time before you post code please get into the habit of using this button:

When you do that this:

void TIMER_1(void)
{
// Set up input capture, no prescaler
TCCR1B |= (1<<ICES1); // get rising edge first
TCCR1B |= (1<<CS10); // no prescaler
TIMSK |= (1<<TICIE1); // enable ICP interrupt

sei(); // enable global interrupts

 

}

ISR (TIMER1_CAPT_vect)
{
// Read ICR1
// Flip edge trigger
// Clear ICR1

localICR=ICR1;
TCCR1B ^= (1<<ICES1);
TCNT1=0;
pulse++;
}

will become:

void TIMER_1(void)
{
    // Set up input capture, no prescaler
    TCCR1B |= (1<<ICES1); // get rising edge first
    TCCR1B |= (1<<CS10); // no prescaler
    TIMSK |= (1<<TICIE1); // enable ICP interrupt
    
    sei(); // enable global interrupts

 

}

ISR (TIMER1_CAPT_vect)
{
    // Read ICR1
    // Flip edge trigger
    // Clear ICR1
    
    localICR=ICR1;
    TCCR1B ^= (1<<ICES1);
    TCNT1=0;
    pulse++;
}

which is much more readable (though I'm not entirely sure why it needs so many blank lines? Also when you use an operator like "<<" or "=" put spaces on either side to make the code easier to read)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ok sir , i am new here from next time i will, thank you

i am shirish_avrlover