با عرض سلام خدمت همگی دوستان. یک کد مربوط به ریموت مادون قرمز هست که نوشتم و هنوز نتونستم ایرادش را رفع کنم. در این برنامه قرار هست توسط یک کنترل مادون قرمز پلییر ماشین ،یک رله و دو ال ای دی را در گیرنده روشن و خاموش کنم. نمیدونم ایرادش کجاست ، لطف می کنید راهنمایی بفرمائید.
#include <mega32a.h>
#include <alcd.h>
#include <delay.h>
#include <stdio.h>
#define green PORTC.1
#define yel PORTC.0
#define rly PORTD.5
#define bl PORTC.2
#define LCD_CONTROL 0x08
#define LCD_DISPLAY 0x04
#define LCD_CURSOR 0x02
#define LCD_BLINK 0x01
#define LCD_CURSOR_ON (LCD_CONTROL | LCD_DISPLAY | LCD_CURSOR | LCD_BLINK)
#define LCD_CURSOR_OFF (LCD_CONTROL | LCD_DISPLAY)
// Declare your global variables here
unsigned int tik;
char byt;
bit repeat_flag;
bit start_flag;
char address;
char command;
char address_1;
char command_1;
char address_0;
char command_0;
char a;
char b;
char z;
bit xz;
char s[20] = "MAN ";
char lcd_buffer[16];
void ok()
{
if (z == 1)
{
TCNT1 = 18650;
TCCR1B=0x05;
rly = 0;
}
if (z== 2)
{
TCNT1 = 18650;
TCCR1B=0x05;
rly = 1;
}
}
void Man_aut()
{
z=z+1;
if (z> 2) z = 0;
if (z==1) sprintf( s , "Off_T");
if (z==2) sprintf( s , "On_T");
if (z==0) sprintf( s , "Man");
delay_ms(300);
}
void Rly_off()
{
rly = 1;
delay_ms(300);
}
void Green_led()
{
if (green==1)
{
green=0;
}
else
{
green=0;
}
delay_ms(300);
}
void Yel_led()
{
if (yel==1)
{
yel=0;
}
else
{
yel=1;
}
delay_ms(300);
}
void On_off()
{
dd:
if (command == 162)
{
xz=~xz;
if (xz == 1)
{
bl = 1;
lcd_clear();
lcd_puts("ON");
}
else
{
lcd_clear();
lcd_puts( " Good By ");
TCCR1B=0x00;
TCNT1 = 0;
delay_ms(2000);
a = 1;
bl = 0;
rly = 1;
green = 0;
yel = 0;
}
delay_ms(300);
}
_lcd_write_data (LCD_CURSOR_OFF);
if (xz != 1) goto dd;
}
void Vol_up()
{
a=a+1;
delay_ms(120);
if (a > 120) a = 120;
}
void Vol_down()
{
a=a-1;
delay_ms(120);
if (a < 1) a=1;
if (a > 120) a=1;
}
void Rly_on()
{
if (z==2)
{
TCCR1B=0x00;
rly = 0;
}
if(z==1)
{
TCCR1B=0x05;
}
if (z==0)
{
TCCR1B=0x00;
rly = 0;
}
delay_ms(300);
}
void Read_command()
{
if (command==162) On_off();
if (command==168) Vol_up();
if (command==224) Vol_down();
if (command==144) Man_aut();
if (command==104) ok();
if (command==194) Rly_on();
if (command==226) Rly_off();
if (command==2) Yel_led();
if (command==34) Green_led();
}
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
TCCR0=0x04;
if (tik >= 139)
{
if (tik < 150)
{
address = 1;
repeat_flag = 0;
start_flag = 1;
address_1 = 0;
command_1 = 0;
address_0 = 0;
command_0 = 0;
}
}
if (tik >= 116)
{
if (tik < 139)
{
address = 2;
repeat_flag = 1;
start_flag = 0;
}
}
if (tik >= 22)
{
if (tik < 116)
{
if (start_flag == 1)
{
byt=byt+1;
if (byt < 9)
{
address_1=address_1 * 2;
address_1 = address_1 + 1 ;
}
if (byt >= 9)
{
if (byt < 17)
{
address_0=address_0 * 2;
address_0 = address_0 + 1;
}
}
if (byt >= 17)
{
if (byt < 25)
{
command_1=command_1 * 2;
command_1 = command_1 + 1;
}
}
if (byt >= 25)
{
command_0=command_0 * 2;
command_0 = command_0 + 1;
}
}
}
}
if (tik >= 10)
{
if (tik < 22)
{
if (start_flag == 1)
{
byt=byt+1;
if (byt < 9)
{
address_1=address_1 * 2;
}
if (byt >= 9)
{
if (byt < 17)
{
address_0 =address_0 * 2;
}
}
if (byt >= 17)
{
if (byt < 25)
{
command_1 =command_1 * 2;
}
}
if (byt >= 25)
{
command_0 =command_0 * 2;
}
}
}
}
tik = 0;
if (byt == 32)
{
address = address_1;
command = command_1;
byt = 0;
repeat_flag = 0;
start_flag = 0;
TCCR0=0x00;
}
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0 = 253;
tik=tik+1;
if (tik >= 1200)
{
tik = 0;
repeat_flag = 0;
start_flag = 0;
address_1 = 0;
command_1 = 0;
address_0 = 0;
command_0 = 0;
address = 0;
command = 0;
TCCR0=0x00;
}
}
// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
TCCR1B=0x00;
b=b+1;
if (b > 9)
{
a=a-1;
b = 0;
}
if (a < 1)
{
if(z== 1)
{
a = 0;
b = 0;
Rly_off();
}
if (z==2)
{
a = 0;
b = 0;
Rly_on();
}
}
if (a >= 1)
{
TCNT1 = 18650;
TCCR1B=0x05;
}
}
void main(void)
{
PORTC=0x00;
DDRC=0xFF;
PORTD=0x00;
DDRD=0xA0;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Falling Edge
// INT1: Off
// INT2: Off
GICR|=0x40;
MCUCR=0x02;
MCUCSR=0x00;
GIFR=0x40;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x05;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTA Bit 2
// RD - PORTA Bit 1
// EN - PORTA Bit 3
// D4 - PORTA Bit 4
// D5 - PORTA Bit 5
// D6 - PORTA Bit 6
// D7 - PORTA Bit 7
// Characters/line: 16
lcd_init(16);
// Global enable interrupts
#asm("sei")
rly=1;
lcd_puts (" IR Receiver");
delay_ms(2000);
lcd_clear();
On_off();
while (1)
{
if (address > 0)
{
lcd_gotoxy(1,0);
sprintf(lcd_buffer,"%d ", command);
lcd_puts(lcd_buffer);
if (xz == 1) Read_command();
}
lcd_gotoxy(0,5);
sprintf(lcd_buffer,"Mode=%d ", s);
lcd_puts(lcd_buffer);
lcd_gotoxy(1,5);
sprintf(lcd_buffer,"Time=%d ", a);
lcd_puts(lcd_buffer);
lcd_gotoxy(1,14);
sprintf(lcd_buffer,"%d ", b);
lcd_puts(lcd_buffer);
}
}