Split from: Measuring Pulse Width with Input Capture

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

Hi everybody,

I am new in AVR and now I am learning Input Capture. but I have a Problem with code examples. I like to see the result in LCD. but it dosnt show anything.

I feel that ISR never called. for example I changed vietnamforever code to avr128 but it is like other examples I have and it Shows nothing from ISR in LCD.

 

 

extern "C"
{
#include "lcd.h"	
	};

#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>

#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;
	
	lcd_init(LCD_DISP_ON_CURSOR_BLINK);
	//clr_LCD();
	
	lcd_puts("vi tri:");
	
	lcd_gotoxy(1,11);
	lcd_putc('.');
	lcd_gotoxy(1,15);
	lcd_putc('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 (TIMER1_CAPT_vect){
	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);
	lcd_gotoxy(1,8);
	lcd_puts("   ");
	lcd_gotoxy(1,11-len(disa));
	lcd_puts(disa);
	
	b=data_temp-a*100;
	sprintf(disb,"%i",b);
	lcd_gotoxy(1,12);
	if (len(disb)==1){
		lcd_putc('0');
		lcd_gotoxy(1,13);
	}
	lcd_puts(disb);
	
	//_delay_ms(1);
}

Can you pls help me?

This topic has a solution.

Last Edited: Wed. Oct 28, 2015 - 03:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

I haven't had time to fully examine the code, but it does stand out that you are modifying the global variable 'data' inside an ISR, and 'data' is not declared to be 'volatile'.

 

Try declaring data (and datardy) as ...

 

volatile uint8_t datardy=0;
volatile uint16_t data;
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thanks for the Reply. I was forgotten to change variable to volatile but it dosnt change anything. I still have nothing in LCD. 

and I can not exactly undrestand "modifying data". i initialise data in ISR. and I add a new  command "disp(datardy);" to make sure etwas should be written in LCD but no change!!!

ISR (TIMER1_CAPT_vect){
	TCNT1=0;
	datardy=1-datardy;
	disp(datardy);
	if(datardy){
		cbi(TCCR1B,ICES1);
		data=ICR1;
		//disp(data);
		disp(data);
	}
	else sbi(TCCR1B,ICES1);
}

I had copy this code from hier:

https://www.avrfreaks.net/forum/m...

because I thought it is correct. I will give other examples that I test.

 

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

this is other example:

it is a test Program to make sure ISR(TIMER1_CAPT_vect) execute correctly.  I have still nothing in LCD from this interrupt but I have the counter X from ISR(TIMER1_OVF_vect) in LCD.

when I make this command SchauinLCD(x,"\nja:"); comment, LCD is empty.

it is a exception: rarely when I make power of puls generator off and wait a while and then make it on I have the data in LCD. but it is unexpectedly and irregular.  

 


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

#include "lcd.h"


void _Sei() ;
void SchauinLCD( int,char[16]);
void setLED();
void SetInputCapture();
void Showdirekt(int);

volatile unsigned currentTime =0;


int main(void)
{
	
	lcd_init(LCD_DISP_ON_CURSOR_BLINK);
	int count = 0;
	setLED();
	SetInputCapture();							
	
	while(1)
	{
		if (TCNT1 % 2232 == 0)// each 7 mili second? chang the situation
		{
			//TCNT1 = 0;// should manually set to zero
			count++;
			if (count >50)
			{
				count =0;
				if (PORTG ==0b00000000)
				{
					PORTG = 0b00000010;
					//SchauinLCD(Flag,"\nFlag");
					//SchauinLCD(CC,"\nmCC:");
										
					}
				else 
				{
					PORTG =0b00000000;
					//SchauinLCD(currentTime,"\nTime");
										
				}
			}	
		}
	}
	
	return 0;
}


void _Sei()  {
	sei();
}


ISR(TIMER1_CAPT_vect)
{
static int Flag;
Flag++;
currentTime = ICR1;
//Showdirekt(ICR1);

SchauinLCD(Flag,"\nF:");
SchauinLCD(currentTime,"\nICR:");

}
ISR(TIMER1_OVF_vect)
{
static int x;
x++;
SchauinLCD(x,"\nja:");
}

void SchauinLCD( int x,char b[16])
{	char a[16]="0" ;
	itoa(x,a,10);
	lcd_puts(b);
	lcd_puts(a);
}

void setLED()
{
	DDRG = 0b01111111;//set as output
	PORTG = 0b00000000;
	TCCR1B |= 1<<CS10 | 1<<CS11;// set 1 in bit 0 and 1 of CS1.
								// CS1 or CS11 = 1
								//xxxxxx11 or TCCRB1 = xxxxxx11
								// TCCRB = xxxxxx11
	
	};
	
void SetInputCapture()
{
	// init Timer 1
	
	TCNT1 = 0; // initial Timer Value
	
	// First Capture on rising edge
	TCCR1B |= (1<< ICES1);
	
	// Enable input Capture intruppt enable and overflow interrupt enable
	TIMSK |= (1<<TICIE1) | (1<<TOIE1);
	
	// Start Timer 1- CS10 = no prescaling
	TCCR1B |= (1<< CS10); // Start Timer
	_Sei();
	
}
void Showdirekt(int a)
{
	static counterr;
	SchauinLCD(counterr,"\nOF:");
	counterr = a;
	SchauinLCD(counterr,"\nNF:");
}

 my Puls generator information are:

1Hz-50Mhz

repetition Time = 1 ms

Delay = 1 ms

duration = 1 ms

 

I am really confused.

sad

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have found the answer.

Amplitude was too low. i change it from 0.5 v to 10 V.cheeky

 

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

So now you are putting a 10V signal into an AVR8 port pin with an Absolute Maximum Rating of about 6V?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Next thread by @samsoft:

My AVR stopped working.  Why?

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

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

GOD forgive me. surprise

I think yes.

it is atmega128

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

laugh

I am lucky! it is still working!wink