/***************************************/ /* Target : avr單片機 ATmega8l */ /* Crystal: 3.6864Mhz */ /* 2007.8.20 */ /* 外國人寫的學習型空調紅外遙控器的源程序*/ /* AT24C512里存儲2個鍵的時間數據, */ /* 學習和發射時都通過串口輸出 */ /* 按鍵學習和串口學習都可以 /*帶多鍵盤通訊接口 /***************************************/ //IRD-1V20 模塊地址寫EEPROM(2) #include <iom8v.h> #include <macros.h> #include <string.h> #include <eeprom.h> #define SYSCLK 3686400UL #define uchar unsigned char #define uint unsigned int #define ulint unsigned long #define IRD_START TCCR2=0X19 #define IRD_STOP TCCR2=0X00 /* I2C device slave address 1 A0、A1、A2--GND == 0xa0 2 A0、A1、A2--VCC == 0xa6 */ /* 24c512 =65536 bytes,anykey include <1000 bytes */ #define devicecode 0x01 #define TWI_SLA_24C512 0xa0 //#define address 0x02//遙控器地址 //#define TWI_SLA_PCF8563 0xa2 #define MAX_ITER 200 #define TW_WRITE 0 #define TW_READ 1 // MT #define TW_REP_START 0x10 #define TW_START 0x08 #define TW_MT_ARB_LOST 0x38 #define TW_MT_SLA_ACK 0x18 #define TW_MT_SLA_NACK 0x20 #define TW_MT_DATA_ACK 0x28 #define TW_MT_DATA_NACK 0x30 // MR #define TW_MR_SLA_ACK 0x40 #define TW_MR_SLA_NACK 0x48 #define TW_MR_ARB_LOST 0x38 #define TW_MR_DATA_NACK 0x58 #define TW_MR_DATA_ACK 0x50 #define led_off PORTC|=(1<<PORTC2) #define led_on PORTC&=(~(1<<PORTC2)) #define TX485 PORTD|=(1<<PORTD7); #define RX485 PORTD&=(~(1<<PORTD7)); uchar end,study_key; uchar data_buf[700]; //uchar data_buf11[100]; uchar modify_buf[6];//鍵值特征暫存區 uchar study_key_value=0; uchar send_key_value=0; uchar scon_key_value=0; uchar rece_begin=0; uint timer_over_cnt,pulse_in_cnt; uchar send_end; uint save_start,send_start; //uint pp; uchar study_num,send_num; uint num,lenth; uchar sum=0,rec_end;//len1; uint s_save_start; uchar s_study_num; uint count0,count1; uint rece_num=0;//接受數據計數; uchar rece_buf[8]; uchar rece_ok=0; uchar ctrl_flag=0; uchar get_flag=0; uchar set_flag=0; uchar amend_flag=0; uchar scon_save[8]; uchar scon_err=0; uchar scon_err_flag=0; uchar key_on_flag=0; uchar key_off_flag=0; uchar ord_flag=0; uchar MKaddress; void EEPROM_write( unsigned int Address, unsigned char UcData) { while(EECR & (1<<EEWE)); EEAR=Address; EEDR=UcData; EECR|=(1<<EEMWE); EECR|=(1<<EEWE); } unsigned char EEPROM_read(unsigned int Address) { while(EECR & (1<<EEWE)); EEAR=Address; EECR|=(1<<EERE); return EEDR; } void watchdog_init(void) { WDR(); //this prevents a timout on enabling WDTCR=0x1F; NOP(); WDTCR=0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs } void Delay_mSec(uint n) { uchar i; while(n--){for(i=0;i<125;i++);WDR();} } void Delay_us(uchar n) { uchar i; for(i=0;i<n;i++); } void int0_init(void) { MCUCR=0x01; // MCUSR=0; } void uart_init(void)//bps=38400,UCSRA=0X02;bps=19200,UCSRA=0; { UCSRB = 0x00; //disable while setting baud rate UCSRA = 0x01; UCSRC = 0x86;//0x06; UBRRL = 0x0b; //set baud rate lo UBRRH = 0x00; //set baud rate hi UCSRB = 0x9c; } int TWI_read_bytes(uchar SLAADDR,uint eeaddr, int len, uchar *buf,uchar slavtype) { uchar sla, twcr,n = 0; int rv = 0; /* patch high bits of EEPROM address into SLA */ WDR(); sla = SLAADDR ; restart: if (n++ >= MAX_ITER) return -1; begin: TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); /* send start condition */ while ((TWCR & (1<<TWINT)) == 0) ; /* wait for transmission */ switch ((TWSR&0xf8)) { case TW_REP_START: /* OK, but should not happen */ case TW_START: break; case TW_MT_ARB_LOST: goto begin; /* Note [7] */ default: return -1; /* error: not in start condition */ /* NB: do /not/ send stop condition */ } WDR(); TWDR = sla | TW_WRITE; TWCR = (1<<TWINT)|(1<<TWEN); /* clear interrupt to start transmission */ while ((TWCR & (1<<TWINT)) == 0) ; /* wait for transmission */ switch ((TWSR&0xf8)) { case TW_MT_SLA_ACK: break; case TW_MT_SLA_NACK: goto restart; /* nack during select: device busy writing */ case TW_MT_ARB_LOST: goto begin; /* re-arbitrate */ default: goto error; /* must send stop condition */ } if(slavtype==2) { TWDR = eeaddr>>8; /* high 8 bits of addr */ TWCR = (1<<TWINT) | (1<<TWEN); /* clear interrupt to start transmission */ while ((TWCR & (1<<TWINT)) == 0) ; /* wait for transmission */ switch ((TWSR&0xf8)) { case TW_MT_DATA_ACK: break; case TW_MT_DATA_NACK: goto quit; case TW_MT_ARB_LOST: goto begin; default: goto error; /* must send stop condition */ } } WDR(); TWDR = eeaddr; /* low 8 bits of addr */ TWCR = (1<<TWINT) | (1<<TWEN); /* clear interrupt to start transmission */ while ((TWCR & (1<<TWINT)) == 0) ; /* wait for transmission */
由于代碼太長,本文篇幅有限,完整本版的代碼從這里下載http://www.zg4o1577.cn/ziliao/file/IRD-1V21.rar
這是從一個外國論壇上搞下來的,已經測試通過,晶振使用外接的3.6864Mhz,內部rc晶振可能不太穩定。