新闻中心

EEPW首页 > 嵌入式系统 > 设计应用 > AVR学习型空调红外遥控器代码

AVR学习型空调红外遥控器代码

作者:时间:2012-09-18来源:网络收藏
/***************************************//* Target : avr单片机                    */               /* 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|=(1PORTC2)#define led_on         PORTC=(~(1PORTC2))#define TX485    PORTD|=(1PORTD7);#define RX485    PORTD=(~(1PORTD7));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  (1EEWE));EEAR=Address;EEDR=UcData;EECR|=(1EEMWE);EECR|=(1EEWE);}unsigned char EEPROM_read(unsigned int Address){while(EECR  (1EEWE));EEAR=Address;  EECR|=(1EERE);return EEDR;}void watchdog_init(void){WDR(); //this prevents a timout on enablingWDTCR=0x1F;NOP();WDTCR=0x0F; //WATCHDOG ENABLED - dont forget to issue WDRs}void Delay_mSec(uint n){uchar i;    while(n--){for(i=0;i125;i++);WDR();}   }void Delay_us(uchar n){uchar i;    for(i=0;in;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 rateUCSRA = 0x01;UCSRC = 0x86;//0x06;UBRRL = 0x0b; //set baud rate loUBRRH = 0x00; //set baud rate hiUCSRB = 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 = (1TWINT)|(1TWSTA)|(1TWEN); /* send start condition */while ((TWCR  (1TWINT)) == 0) ; /* wait for transmission */switch ((TWSR0xf8)){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 = (1TWINT)|(1TWEN); /* clear interrupt to start transmission */while ((TWCR  (1TWINT)) == 0) ; /* wait for transmission */switch ((TWSR0xf8)){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 = (1TWINT) | (1TWEN); /* clear interrupt to start transmission */while ((TWCR  (1TWINT)) == 0) ; /* wait for transmission */switch ((TWSR0xf8)){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 = (1TWINT) | (1TWEN); /* clear interrupt to start transmission */while ((TWCR  (1TWINT)) == 0) ; /* wait for transmission */

由于代码太长,本文篇幅有限,完整本版的代码从这里下载http://www.51hei.com/ziliao/file/IRD-1V21.rar

这是从一个外国论坛上搞下来的,已经测试通过,晶振使用外接的3.6864Mhz,内部rc晶振可能不太稳定。



评论


相关推荐

技术专区

关闭