Aayush Awasthi
Junior Member level 2
- Joined
- Nov 18, 2013
- Messages
- 20
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 128
#include<avr/io.h>
#include<util/delay.h>
//#include<avr/interrupt.h>
#include<stdio.h>
#define TIM1_CAPT 6
#define LCD_RS 0
#define LCD_RW 1
#define LCD_EN 2
#define LCD PORTC
#define F_CPU 1000000UL
void lcd_config();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
int lcd_num(int);
// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void);
int main(void)
{
DDRC=0xFF;
DDRD=0x00;
//DDRB=0xFF;
/* sei();
MCUCR=0b00000010;
GICR=0b010000000;
lcd_config();
*/ while(1)
{
}
}
/*
ISR(INT0_vect)
{
int x;
PORTB=0xFF;
lcd_config();
lcd_cmd(0x01);
_delay_ms(100);
lcd_cmd(0x80);
x=PIND;
lcd_num(x);
_delay_ms(1000);
}
*/
int lcd_num(int n)
{
int r,s=0;
lcd_cmd(0x04);
while(n>0)
{
r=n%10;
n=n/10;
s=(s*10)+r;
lcd_data(s+48);
_delay_ms(100);
}
lcd_cmd(0x06);
return 0;
}
void lcd_config()
{
lcd_cmd(0x02);
lcd_cmd(0x28);
lcd_cmd(0x0C);
lcd_cmd(0x06);
}
void lcd_cmd(unsigned char x)
{
LCD=x&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(x<<4)&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
void lcd_data(unsigned char y)
{
LCD=y&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(y<<4)&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
// Timer 1 input capture interrupt service routine
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
{
unsigned char uc_string[10];
unsigned char uc_i;
unsigned int ui_value=0;
uc_i=TCCR1B;
if((uc_i&0b01000000)==0) // ICP Sensing is at falling edge
{
ui_timer=ICR1L; //ICR1 must be readed sequently
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_value=ui_timer-ui_oldtimer;
lcd_config();
lcd_cmd(0x01);
_delay_ms(100);
lcd_cmd(0x88);
lcd_num(ui_value);
_delay_ms(1000);
//The actual high measurement is in the ui_value variable in micro seconds. Here you have to set your own code
TCCR1B|=0b01000000; //Set ICP Sensing at falling edge
}
else // ICP Sensing is at rising edge
{
TCCR1B&=0b10111111;
ui_timer=ICR1L;
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_oldtimer=ui_timer;
lcd_config();
lcd_cmd(0x01);
_delay_ms(100);
lcd_cmd(0x88);
lcd_num(ui_value);
_delay_ms(1000);
//The actual low measurement is in the ui_value variable in micro seconds. Here you have to set your own code
}
}
interrupt [TIM1_CAPT] void timer1_capt_isr(void)
ISR(TIM1_CAPT_vect)
#include<avr/interrupt.h>
You are mixing avrgcc and codevision syntax.
That is not how you declare an interrupt in avrgcc, the correct syntax isCode:interrupt [TIM1_CAPT] void timer1_capt_isr(void)
Code:ISR(TIM1_CAPT_vect)
You should also include
Code:#include<avr/interrupt.h>
As for the F_CPU not defined error you should define the F_CPU before the inclusion of delay.h so move it at the top
#include<avr/io.h>
[COLOR="#FF0000"]#define F_CPU 1000000UL[/COLOR]
#include<util/delay.h>
#include<stdio.h>
#define LCD_RS 0
#define LCD_RW 1
#define LCD_EN 2
#define LCD PORTC
void lcd_config();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
int lcd_num(int);
int main(void)
{
DDRC=0xFF;
DDRD=0x00;
[COLOR="#FF0000"]// initialization of the timer?[/COLOR]
[COLOR="#FF0000"]sei();[/COLOR] [COLOR="#FF0000"]// You have to set global interrupt enable bit[/COLOR]
/* MCUCR=0b00000010;
GICR=0b010000000;
lcd_config();
*/
while(1)
{
}
[COLOR="#FF0000"]return 0;[/COLOR]
}
int lcd_num(int n)
{
int r,s=0;
lcd_cmd(0x04);
while(n>0)
{
r=n%10;
n=n/10;
s=(s*10)+r;
lcd_data(s+48);
_delay_ms(100);
}
lcd_cmd(0x06);
return 0;
}
void lcd_config()
{
lcd_cmd(0x02);
lcd_cmd(0x28);
lcd_cmd(0x0C);
lcd_cmd(0x06);
}
void lcd_cmd(unsigned char x)
{
LCD=x&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(x<<4)&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
void lcd_data(unsigned char y)
{
LCD=y&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(y<<4)&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
[COLOR="#FF0000"]// TIM1_CAPT_vect only if you have ATtiny24,44,84 device... if you have another one, please check:
// http://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html[/COLOR]
ISR(TIM1_CAPT_vect)
{
unsigned char uc_string[10];
unsigned char uc_i;
unsigned int ui_value=0;
uc_i=TCCR1B;
if((uc_i&0b01000000)==0) // ICP Sensing is at falling edge
{
ui_timer=ICR1L; //ICR1 must be readed sequently
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_value=ui_timer-ui_oldtimer;
lcd_config();
lcd_cmd(0x01);
[COLOR="#FF0000"]//_delay_ms(100);[/COLOR] [COLOR="#FF0000"] // You dont have to wait in interrupts routines, just make some code and go away[/COLOR]
lcd_cmd(0x88);
lcd_num(ui_value);
[COLOR="#FF0000"]//_delay_ms(1000);[/COLOR]
//The actual high measurement is in the ui_value variable in micro seconds. Here you have to set your own code
TCCR1B|=0b01000000; //Set ICP Sensing at falling edge
}
else // ICP Sensing is at rising edge
{
TCCR1B&=0b10111111;
ui_timer=ICR1L;
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_oldtimer=ui_timer;
lcd_config();
lcd_cmd(0x01);
[COLOR="#FF0000"]//_delay_ms(100);[/COLOR]
lcd_cmd(0x88);
lcd_num(ui_value);
[COLOR="#FF0000"] //_delay_ms(1000); [/COLOR]
}
}
#include<avr/io.h>
#define F_CPU 1000000UL
#include<util/delay.h>
#include<stdio.h>
#include<avr/interrupt.h>
#define LCD_RS 0
#define LCD_RW 1
#define LCD_EN 2
#define LCD PORTC
void lcd_config();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
int lcd_num(int);
int main(void)
{
DDRC=0xFF;
DDRD=0x00;
TCCR0=0x69;
TCCR1A=(1<<COM1A1)|(1<<COM1B1)|(1<<WGM10);
TCCR1B=(1<<WGM12)|(1<<CS00); // initialization of timer?
sei(); // You have to set global interrupt enable bit
/* MCUCR=0b00000010;
GICR=0b010000000;
lcd_config();
*/
while(1)
{
ISR(TIM1_CAPT_vect);
_delay_ms(100);
}
return 0;
}
int lcd_num(int n)
{
int r,s=0;
lcd_cmd(0x04);
while(n>0)
{
r=n%10;
n=n/10;
s=(s*10)+r;
lcd_data(s+48);
_delay_ms(100);
}
lcd_cmd(0x06);
return 0;
}
void lcd_config()
{
lcd_cmd(0x02);
lcd_cmd(0x28);
lcd_cmd(0x0C);
lcd_cmd(0x06);
}
void lcd_cmd(unsigned char x)
{
LCD=x&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(x<<4)&0xF0;
LCD&=~(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
void lcd_data(unsigned char y)
{
LCD=y&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
LCD=(y<<4)&0xF0;
LCD|=(1<<LCD_RS);
LCD&=~(1<<LCD_RW);
LCD|=(1<<LCD_EN);
_delay_ms(1);
LCD&=~(1<<LCD_EN);
}
// Timer 1 input capture interrupt service routine
ISR(TIM1_CAPT_vect)
{
unsigned char uc_string[10];
unsigned char uc_i;
unsigned int ui_value=0;
unsigned int ui_timer=0;
unsigned int ui_oldtimer=0;
uc_i=TCCR1B;
if((uc_i&0b01000000)==0) // ICP Sensing is at falling edge
{
ui_timer=ICR1L; //ICR1 must be readed sequently
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_value=ui_timer-ui_oldtimer;
lcd_config();
lcd_cmd(0x01);
//_delay_ms(100); // You dont have to wait in interrupts routines, just make some code and go away
lcd_cmd(0x88);
lcd_num(ui_value);
//_delay_ms(1000);
//The actual high measurement is in the ui_value variable in micro seconds. Here you have to set your own code
TCCR1B|=0b01000000; //Set ICP Sensing at falling edge
}
else // ICP Sensing is at rising edge
{
TCCR1B&=0b10111111;
ui_timer=ICR1L;
ui_value=ICR1H;
ui_value=ui_value<<8;
ui_timer+=ui_value;
ui_oldtimer=ui_timer;
lcd_config();
lcd_cmd(0x01);
//_delay_ms(100);
lcd_cmd(0x88);
lcd_num(ui_value);
//_delay_ms(1000);
}
}
I edited my last post. If you have ATmega16, so you have a bad interrupt vector. Try this:
TIMER1_CAPT_vect
because the vector what you have right now is for ATtiny devices.
ok i done but still not working will you pls tell me the connection of TSOP1738 with atmega16
so what to do in while(1) function and i have posted my code so pls edited it what ever you are saying
Code C - [expand] 1 lcd_config()
Code C - [expand] 1 main()
Code C - [expand] 1 while(1)
void LCD_init( void)
{
// set LCD port
LCDPORTI_C |= 0xfc;
LCDPORTD = 0;
_delay_ms(200);
LCD_half_byte(0x03); // it is needed for 4bit communication
LCD_half_byte(0x03);
LCD_half_byte(0x03);
LCD_half_byte(0x02);
LCD_inst(0x2c); // Function set
LCD_inst(0x0c); // Zapnutie displeja
LCD_inst(0x01); // Zmazanie displeja
LCD_inst(0x06); // Entry mode set
_delay_ms(10); // zmazanie displeja pre uplnu istotu
LCD_inst(0x01);
_delay_ms(10);
}
void LCD_half_byte (unsigned char data)
{
LCDPORTD = 0;
clrbit (LCDPORTI,RS);
setbit(LCDPORTI,EN);
LCDPORTD |= ( (data & 0x0f)<<4 );
clrbit(LCDPORTI,EN);
setbit (LCDPORTI,RS);
_delay_ms(10);
}
// Print char
void LCD_char ( unsigned char znak )
{
setbit (LCDPORTI,RS);
LCDPORTD = 0; // !!!
setbit(LCDPORTI,EN );
LCDPORTD |= ( znak & 0xf0 ); // horne 4 bity
clrbit(LCDPORTI,EN );
LCDPORTD = 0;
setbit (LCDPORTI,RS);
setbit(LCDPORTI,EN);
LCDPORTD |= ( (znak & 0x0f)<<4 ); // dolne 4 bity
clrbit(LCDPORTI,EN);
_delay_us(200);
}
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?