Measuring Pulse Width with Input Capture

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

Hi, I used MCU atmega32 to measuring Pulse width. But it's seem something wrong with Input capture initial. Because I figure out that input capture interrupt service function didnt run. Please help me correct this code. I'm very grateful for your help :D

Attachment(s): 

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

The code:

#include 
#include 
#include "myLCD.h"
#include 

#ifndef cbi 
	#define cbi(port, bit) 	   (port) &= ~(1 << (bit))
#endif
#ifndef sbi 
	#define sbi(port, bit) 	   (port) |=  (1 << (bit))
#endif


void disp(uint16_t data);


//ham tim do dai chuoi
char len(char s[])
{
    int x=0;
    while (s[x]!='\0')
        x++;
    return x;
}

uint8_t datardy=0;
uint16_t data;

int main (void){
	DDRD=0x00;
	PORTD=0xFF;
	
	init_LCD(); 
	clr_LCD();
	
	print_LCD("vi tri:");
	move_LCD(1,11);
	putchar_LCD('.');
	move_LCD(1,15);
	putchar_LCD('d');	
	
	//set up input capture mode 12. Noise canceler,input capture rising edge
	TCCR1B=(1<<ICNC1)|(1<<WGM13) |(1<<WGM12) |(1<<CS11) ;//prescaler = 8,
	TCCR1B=(1<<ICES1);
	TIMSK=(1<<TICIE1);//enable input capture interrupt
	TCNT1=0;
	sei();
	//--------------------------------------------------------------------------------    
	while(1){
	//do nothing
}	
	return 0;
}
//input capture interrupt service function
ISR (SIG_INPUT_CAPTURE1){
TCNT1=0;
datardy=1-datardy;

if(datardy){
	cbi(TCCR1B,ICES1);
	data=ICR1;
	//disp(data);
	disp(data);
	}
else sbi(TCCR1B,ICES1);
}

//ham hien thi len LCD
void disp(uint16_t data){
uint16_t data_temp;
uint8_t a,b; 
char disa[3];
char disb[2];
data_temp=data/10;
data_temp*=18;

a=data_temp/100;
sprintf(disa,"%i",a);
move_LCD(1,8);
print_LCD("   ");
move_LCD(1,11-len(disa));
print_LCD(disa);
			
b=data_temp-a*100;
sprintf(disb,"%i",b);
move_LCD(1,12);
if (len(disb)==1){
	putchar_LCD('0');
	move_LCD(1,13);
}
print_LCD(disb);
	
_delay_ms(1);
}

As the question seems more general than just something to do with GCC I'll move this to AVR Forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
   //set up input capture mode 12. Noise canceler,input capture rising edge 
   TCCR1B=(1<<ICNC1)|(1<<WGM13) |(1<<WGM12) |(1<<CS11) ;//prescaler = 8,
   TCCR1B=(1<<ICES1); 

What is the value of x after these two lines:

x = 8;
x = 2;

?

And what should "input capture mode 12" be?

Stefan Ernst

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#define cbi(port, bit)       (port) &= ~(1 << (bit))

How could this ever compile ? (Unbalanced parentheses.)

Quote:
And what should "input capture mode 12" be?
Mode12 is CTC with ICR1 as TOP, which would make it useless for input capture. Use Normal mode.

Regards,
Steve A.

The Board helps those that help themselves.

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

sternst wrote:

   //set up input capture mode 12. Noise canceler,input capture rising edge 
   TCCR1B=(1<<ICNC1)|(1<<WGM13) |(1<<WGM12) |(1<<CS11) ;//prescaler = 8,
   TCCR1B=(1<<ICES1); 

What is the value of x after these two lines:

x = 8;
x = 2;

?

And what should "input capture mode 12" be?


I think i replied you yesterday :D It's a crazy mistake, I adjusted this line to

TCCR1B=(1<<ICNC1)|(1<<WGM13)|(1<<WGM12)|(1<<CS11)|(1<<ICES1);

But it take no effect :(

Koshchi wrote:
How could this ever compile ? (Unbalanced parentheses.)

oh! when i changed my code to debug, I forgot to delete this line. But I think This is no problem here.
Quote:
Mode12 is CTC with ICR1 as TOP, which would make it useless for input capture. Use Normal mode.

I dont know much about Input capture. This is my 1st time I use it. can i use Input capture interrupt function in normal mode, Koshchi?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
(1<<WGM13)|(1<<WGM12)|

This sets Mode 12. You do >>NOT<< want Mode 12, you want Mode 0.

Quote:
This is my 1st time I use it. can i use Input capture interrupt function in normal mode
Yes, but that is Mode 0.

Regards,
Steve A.

The Board helps those that help themselves.

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

Thanks Koshchi! the code now running perfectly :) thanks all to read my problem:)