红外学习程序下载
单片机源程序如下:
- ///////////////////////////////////////////////////////////////////////////////////
- // This is the TOP2800.c file. //
- // //
- // 04.05.08 Created by domdom. //
- ///////////////////////////////////////////////////////////////////////////////////
- #include <MEGA8.h>
- #include "TWI.H"
- #define uchar unsigned char
- #define uint unsigned int
- #define M62429MicClk PORTC.2
- #define M62429VolClk PORTC.1
- #define M62429Data PORTC.3
- #define ir_in PIND.2 //ir_in
- #define swsda PINB.3 //Key data in
- #define swsck PORTB.2//Key ledsck
- #define swcs PORTB.1 //Key cs
- #define ledsck PORTB.4//LED ledsck
- #define ledsda PORTB.5//LED data
- #define sel_vga0 PORTD.6
- #define sel_vga1 PORTD.7
- #define sel_v PORTD.3
- #define sel_a PORTB.6
- #define bp PORTC.0
- #define up PORTB.7
- #define down PORTD.5
- #define power PORTB.0
- #define ir_out PORTD.4
- #define rd_device_add 0xa1
- #define wr_device_add 0xa0
- void CommSend(uchar data);//串行口发送处理子程序
- uint ScanKey();//键盘扫描程序
- void SysInit(void);
- void LearnIR(void);
- void LearnOK(void);
- void SendIR(void);
- void SendToVGA(unsigned char index);
- void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume);
- unsigned char EEPROM_read(unsigned int addr);
- //void EEPROM_write(unsigned int addr,unsigned char data);
- uchar AT24C32_W(uchar mcu_data,uint AT24C32_address);
- uchar AT24C32_R(uint AT24C32_address);
- void KeyProcess(uint KeyValue);
- void CommandInput(void);
- interrupt [USART_RXC] void comm_isr(void); //串行口中断程序
- interrupt [EXT_INT0] void int0_isr(void); //the IR Input interrupt
- interrupt [TIM1_OVF] void timer1_int(void);
- interrupt [TIM0_OVF] void timer0_int(void);
- interrupt [TIM2_OVF] void timer2_int(void); //used for power delay off
- uchar RxFlag;
- uchar RxBuf;
- unsigned char gPWMEn;
- unsigned int T2Cnt; //used for Timer2 count
- unsigned char gLen,gCount;
- unsigned char IrDataIndex;
- unsigned char gState,RsFlag;
- unsigned char gSending;
- unsigned char gTimeoutCount;
- unsigned char gLearning;
- unsigned char gWaitingIR;
- unsigned int gIRData[224];
- unsigned char SerialBuf[16]; //串口缓存
- const uchar BaudCode[]={7,12,25,51,103}; // 38400(error),19200,9600,4800,2400 bps
- unsigned char VolumeSel,AVCnt,AudioVolume,MicVolume,LedValue,Learncnt,LearnEnCnt,gLearningFail,PowerOffFlag,LearningIREn;
- void delay(uint t)//延时
- {
- uint i;
- for(i=0;i<t;i++);
- }
- void Beep(uchar operation)
- {
- if(operation) bp=1;
- else bp=0;
- }
- unsigned char EEPROM_read(unsigned int addr)
- {
- /* Wait for completion of previous write */
- while(EECR & (1<<1))
- ;
- /* Set up address register */
- EEAR = addr;
- /* Start eeprom read by writing EERE */
- EECR |= (1<<0);
- /* Return data from data register */
- return EEDR;
- }
- /*
- void EEPROM_write(unsigned int addr,unsigned char data)
- {
- //Wait for completion of previous write
- while(EECR & (1<<1))
- ;
- // Set up address and data registers
- EEAR = addr;
- EEDR = data;
- // Write logical one to EEMWE
- EECR |= (1<<2);
- // Start eeprom write by setting EEWE
- EECR |= (1<<1);
- }
- */
- void LED(uchar Data)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- Data<<=1;
- ledsda=SREG.0;
- ledsck=0;
- ledsck=1;
- }
- }
- uint ScanKey()//键盘扫描程序
- {
- uchar i;
- uint KeyVal;
- KeyVal=0x0000;
- delay(20);
- swcs=1;
- delay(20);
- swsck=0;
- delay(20);
- swsck=1;
- delay(20);
- swcs=0;
- KeyVal=KeyVal|swsda;
- for(i=0;i<15;i++)
- {
- swsck=0;
- delay(20);
- swsck=1;
- delay(20);
- KeyVal<<=1;
- KeyVal=KeyVal|swsda;
- }
- swcs=1;
- return(KeyVal);
- }
- void SysInit(void)
- {
- DDRD=0xFB; /*input:PD2(IR_IN),PD0(RXD) 11111011*/
- PORTD=0xFB; //0xB3;
- DDRB=0xf7; /*input: PB3(swsda) 11110111*/
- PORTB=0xf7;
- DDRC=0xff;
- PORTC=0xfe; //Beep
-
- TWBR=32; //TWI
- TWSR=0;
-
- TCCR0=1;//stop
- TCNT0=192;
-
- TCCR1A=0x00;
- TCCR1B=0x00;
- TCNT1H=0x00;
- TCNT1L=0x00;
- OCR1AH=0x00;
- OCR1AL=0x00;
- OCR1BH=0x00;
- OCR1BL=0x00;
- TCCR2=0x00;
- ASSR=0x00;//CK
- TCNT2=0;
- OCR2=0;
- TCCR2=0;//ctc,No prescaling,No OC2,stop;
- //TCCR2=9;//ctc,No prescaling,No OC2,start;
- GICR= 0x00; // Enable INT1 interrupt
- MCUCR=1; //INT0 any change INT1 //Low LEVEL(SW_IN)
- TCCR1A=0;
- //TCCR1B=2; // TIMER1 clock is xtal/8 //start Timer
- TCCR1B = 0; //stop
- // preset TIMER1
- TCNT1=0;
- // clear TIMER1 interrupts flags
- TIFR=0;
- // disable TIMER1 overflow interrupt
- TIMSK=0x00;
- /*UART初始化*/
- OSCCAL=EEPROM_read(0x1ff); //对内部RC OSC调整
-
- // OSCCAL=0xff;
- //UCSRA=0xfc;//0xfc;
- UBRRH=0;
- UBRRL=BaudCode[3]; //default Baud Rate=4800 (i=3)
- // UBRRL=11; //19200
- //UBRRL=24; //19200
- // UBRRL=50; //9600
- // UBRRL=102; //4800
- // UBRRL=204; //1200
- // UBRL=103;
- UCSRB=0x98;//enable UART receiver and transmitter, and receive interrupt(RXCIE)
- UCSRC=(1<<7)|(1<<2)|(1<<1);//8位数据+1位STOP位
- SREG=0x80; //enable interrupt bit in sreg
- // CommSend(OSCCAL);
- //initial system Variable
- if(AT24C32_R(0x00)==0x7e) RsFlag=1;
- else RsFlag=0;
-
- LearningIREn=0;
- RxFlag=0;
- up=1;
- down=1;
- power=0;//Open Projector
- LedValue=0xff;
- LedValue&=0xfe;
- LedValue|=0x02; //Off the Notebook LED;
- LedValue|=0x04; //off the DV LED;
- LedValue&=0xf7; //On the AV LED
- LedValue|=0x20; //Off the Screen Down LED;
- LedValue|=0x10; //Off the Screen Up LED;
- LedValue|=0x40; //Off the Mic Sel LED
- // LedValue|=0x80; //Off the IR Learning Sel LED
- LED(LedValue);
- sel_a=0; //difference
- sel_v=0;
- sel_vga1=0;
- sel_vga0=0;
-
- T2Cnt=0; //used for Timer2 count
- Learncnt=0;
- LearnEnCnt=0;
- VolumeSel=0;
- AVCnt=0;
- gLearning=0;
- gSending=0;
- gLearningFail=0;
- AudioVolume=73;
- PowerOffFlag=1;
- MicVolume = 73;
- M62429Ctr(0,0,73); //Audio
- M62429Ctr(0,1,73);
- M62429Ctr(1,0,73); //Mic
- M62429Ctr(1,1,73);
- }
- /******************************************
- I2C总线写一个字节
- 返回0:写成功
- 返回非0:写失败
- *******************************************/
- uchar AT24C32_W(uchar mcu_data,uint AT24C32_address)
- {
- unsigned int addr;
- addr=AT24C32_address;
- Start();//I2C启动
- Wait();
- if(TestAck()!=START) return 1;//ACK
- Write8Bit(wr_device_add);//写I2C从器件地址和写方式
- Wait();
- if(TestAck()!=MT_SLA_ACK) return 1;//ACK
- Write8Bit(addr>>8);//写24C02的ROM地址h
- Wait();
- if(TestAck()!=MT_DATA_ACK) return 1;//ACK
- Write8Bit(AT24C32_address&0x00FF);//写24C02的ROM地址l
- Wait();
- if(TestAck()!=MT_DATA_ACK) return 1;//ACK
- Write8Bit(mcu_data);//写数据到24C02的ROM
- Wait();
- if(TestAck()!=MT_DATA_ACK) return 1;//ACK
- Stop();//I2C停止
- delay(2000);//延时等EEPROM写完
- return 0;
- }
- /******************************************
- I2C总线读一个字节
- 如果读失败也返回0
- *******************************************/
- uchar AT24C32_R(uint AT24C32_address)
- {
- unsigned char temp;
- unsigned int addr;
- addr=AT24C32_address;
- Start();//I2C启动
- Wait();
- if (TestAck()!=START) return 0;//ACK
- Write8Bit(wr_device_add);//写I2C从器件地址和写方式
- Wait();
- if(TestAck()!=MT_SLA_ACK) return 0;//ACK
- Write8Bit(addr>>8);//写24C02的ROM地址h
- Wait();
- if(TestAck()!=MT_DATA_ACK) return 0;//ACK
- Write8Bit(AT24C32_address&0x00FF);//写24C02的ROM地址l
- Wait();
- if (TestAck()!=MT_DATA_ACK) return 0;
- Start();//I2C重新启动
- Wait();
- if (TestAck()!=RE_START) return 0;
- Write8Bit(rd_device_add);//写I2C从器件地址和读方式
- Wait();
- if(TestAck()!=MR_SLA_ACK) return 0;//ACK
- Twi();//启动主I2C读方式
- Wait();
- if(TestAck()!=MR_DATA_NOACK) return 0;//ACK
- temp=TWDR;//读取I2C接收数据
- Stop();//I2C停止
- return temp;
- }
- interrupt [USART_RXC] void comm_isr(void)//串行口中断程序
- {
- uchar i,sreg,FunCode;
- // CommSend(0xF5);
- if ((UCSRA&0x80)==0x80)
- {
- sreg=SREG;
- SREG=0x0; //SREG=0x0;
- RxFlag=0;
- while (!(UCSRA&0x80)); UCSRA&=~0x80;//CommSend(UDR);
- if(UDR!=0xFF) goto EXT; //同步码
- while (!(UCSRA&0x80)); UCSRA&=~0x80; //功能码
- FunCode=UDR;
- //CommSend(FunCode); }
- if(FunCode==0x7F)
- {
- while (!(UCSRA&0x80)); UCSRA&=~0x80;
- RxBuf=UDR;delay(0xff);//CommSend(RxBuf); //接收数据
- while (!(UCSRA&0x80)); UCSRA&=~0x80;//结束码
- if(UDR==0x55) RxFlag=1;
- }
- else if(FunCode==0x8F)//232 databyte
- {
- while (!(UCSRA&0x80));SerialBuf[0]=UDR;UCSRA&=~0x80;//数据索引
-
- while (!(UCSRA&0x80));SerialBuf[1]=UDR;UCSRA&=~0x80;//Baud Rate
-
- while (!(UCSRA&0x80));
- SerialBuf[2]=UDR;
- UCSRA&=~0x80;//RsDataLength
-
- for(i=0;i<SerialBuf[2];i++)
- {
- while (!(UCSRA&0x80));
- SerialBuf[i+3]=UDR;
- // AT24C32_W(UDR,RsDataIndex*512+3+i);UCSRA&=~0x80;//RsDataCode
- }
-
- while (!(UCSRA&0x80));
- if(UDR==0x55)
- { // CommSend(SerialBuf[0]);
- AT24C32_W(SerialBuf[1],SerialBuf[0]*512+1);//CommSend(SerialBuf[1]);//Baud Rate
- AT24C32_W(SerialBuf[2],SerialBuf[0]*512+2);//CommSend(SerialBuf[2]);//RsDataLength
- for(i=0;i<SerialBuf[2];i++)
- {
- //CommSend(SerialBuf[i+3]);
- AT24C32_W(SerialBuf[i+3],SerialBuf[0]*512+3+i);//Write RsDataCode TO IIC
- }
- AT24C32_W(0x7e,0); //Rs232 Flag
- RsFlag=1;
- }
- else AT24C32_W(0,0); UCSRA&=~0x80;//结束码
-
- }
- else goto EXT;
- }
- EXT:
- SREG=sreg;
- // CommSend(0x77);
- }
-
- void CommSend(uchar data) //串行口发送处理子程序
- {
- /* UCSRA |= (1<<6);//TXC
- UDR = data;
- do{} while ((UCSRA&(1<<6)) == 0); */
- while (!(UCSRA&(1<<5)));
- UDR=data;
- }
-
- void M62429WriteByte(unsigned char VolumeSel,unsigned char i,unsigned char Data)
- {
- if(VolumeSel%2) M62429MicClk = 0; //Mic
- else M62429VolClk = 0; //Audio
- do
- {
- Data = Data >> 1;
- M62429Data = SREG.0;
- delay(2);
- if(VolumeSel%2) M62429MicClk = 1;
- else M62429VolClk = 1;
- delay(2);
- M62429Data = 0;
- delay(2);
- if(VolumeSel%2) M62429MicClk = 0;
- else M62429VolClk = 0;
- } while (--i != 0);
- }
- void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume)
- {
- unsigned char temp;
- if (Volume < 84)
- {
- Mod = Mod & 0x03;
- temp = (Volume & 0x03) | 0x0c;
- Volume = Volume + 4;
- Volume = (Volume & ~0x03) | Mod;
- M62429WriteByte(VolumeSel,7,Volume);
- M62429WriteByte(VolumeSel,3,temp);
- M62429Data = 1;
- delay(3);
- if(VolumeSel%2) M62429MicClk = 1;
- else M62429VolClk = 1;
- delay(4);
- if(VolumeSel%2) M62429MicClk = 0;
- else M62429VolClk = 0;
- M62429Data = 0;
- }
- }
- ///////////////////////////////
- //IR Learn
- void LearnIR(void)
- {
- // unsigned int i;
- // for(i=0;i<192;i++)
- // gIRData[i]=0;
- gCount=0;
- gWaitingIR=1;
- gState=0;
- // gTimeoutCount=0;
- GICR= 0x40; // Enable INT0 interrupt
- MCUCR=0x01; //toggle
- SREG=0x80; //Enable interrupt bit in sreg
- gLearning=1;//Must add it again
-
- TIMSK=0x40;// enable TIMER2 overflow
- TCNT2=0;
- TCCR2=0x07;//start timer2/1024 电源控制带延时
-
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
top2800-all-ok-1006.rar
(7.17 KB, 下载次数: 7)
|