找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1994|回复: 0
收起左侧

MEGA8红外学习程序下载

[复制链接]
ID:252159 发表于 2017-11-22 09:10 | 显示全部楼层 |阅读模式
红外学习程序下载

单片机源程序如下:
  1. ///////////////////////////////////////////////////////////////////////////////////
  2. //        This is the TOP2800.c file.                                              //
  3. //                                                                               //
  4. //        04.05.08        Created by domdom.                                       //
  5. ///////////////////////////////////////////////////////////////////////////////////
  6. #include <MEGA8.h>
  7. #include "TWI.H"
  8. #define uchar unsigned char
  9. #define uint  unsigned int
  10. #define M62429MicClk PORTC.2
  11. #define M62429VolClk PORTC.1
  12. #define M62429Data PORTC.3
  13. #define ir_in PIND.2 //ir_in
  14. #define swsda PINB.3 //Key data in
  15. #define swsck PORTB.2//Key ledsck   
  16. #define swcs PORTB.1 //Key cs
  17. #define ledsck PORTB.4//LED ledsck
  18. #define ledsda PORTB.5//LED data
  19. #define sel_vga0 PORTD.6
  20. #define sel_vga1 PORTD.7
  21. #define sel_v PORTD.3
  22. #define sel_a PORTB.6
  23. #define bp PORTC.0
  24. #define up PORTB.7
  25. #define down PORTD.5
  26. #define power PORTB.0
  27. #define ir_out PORTD.4   
  28. #define rd_device_add 0xa1
  29. #define wr_device_add 0xa0  

  30. void CommSend(uchar data);//串行口发送处理子程序
  31. uint ScanKey();//键盘扫描程序
  32. void SysInit(void);  
  33. void LearnIR(void);
  34. void LearnOK(void);  
  35. void SendIR(void);  
  36. void SendToVGA(unsigned char index);
  37. void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume);
  38. unsigned char EEPROM_read(unsigned int addr);
  39. //void EEPROM_write(unsigned int addr,unsigned char data);

  40. uchar AT24C32_W(uchar mcu_data,uint AT24C32_address);
  41. uchar AT24C32_R(uint AT24C32_address);
  42. void KeyProcess(uint KeyValue);  
  43. void CommandInput(void);
  44. interrupt [USART_RXC] void comm_isr(void);      //串行口中断程序
  45. interrupt [EXT_INT0] void int0_isr(void);        //the IR Input interrupt
  46. interrupt [TIM1_OVF] void timer1_int(void);  
  47. interrupt [TIM0_OVF] void timer0_int(void);
  48. interrupt [TIM2_OVF] void timer2_int(void);    //used for power delay off   


  49. uchar RxFlag;
  50. uchar RxBuf;  
  51. unsigned char gPWMEn;
  52. unsigned int T2Cnt; //used for Timer2 count
  53. unsigned char gLen,gCount;
  54. unsigned char IrDataIndex;
  55. unsigned char gState,RsFlag;
  56. unsigned char gSending;
  57. unsigned char gTimeoutCount;
  58. unsigned char gLearning;
  59. unsigned char gWaitingIR;
  60. unsigned int gIRData[224];
  61. unsigned char SerialBuf[16];            //串口缓存
  62. const uchar BaudCode[]={7,12,25,51,103}; // 38400(error),19200,9600,4800,2400 bps  
  63. unsigned char VolumeSel,AVCnt,AudioVolume,MicVolume,LedValue,Learncnt,LearnEnCnt,gLearningFail,PowerOffFlag,LearningIREn;


  64. void delay(uint t)//延时
  65. {
  66.    uint i;
  67.    for(i=0;i<t;i++);
  68. }

  69. void Beep(uchar operation)
  70. {
  71.         if(operation) bp=1;
  72.         else bp=0;   
  73. }  

  74. unsigned char EEPROM_read(unsigned int addr)
  75. {
  76.         /* Wait for completion of previous write */
  77.         while(EECR & (1<<1))
  78.         ;
  79.         /* Set up address register */
  80.         EEAR = addr;
  81.         /* Start eeprom read by writing EERE */
  82.         EECR |= (1<<0);
  83.         /* Return data from data register */
  84.         return EEDR;       
  85. }
  86. /*
  87. void EEPROM_write(unsigned int addr,unsigned char data)
  88. {
  89.         //Wait for completion of previous write
  90.         while(EECR & (1<<1))
  91.         ;
  92.         // Set up address and data registers
  93.         EEAR = addr;
  94.         EEDR = data;
  95.         // Write logical one to EEMWE
  96.         EECR |= (1<<2);
  97.         // Start eeprom write by setting EEWE
  98.         EECR |= (1<<1);
  99. }
  100. */
  101. void LED(uchar Data)
  102. {
  103.         uchar i;  
  104.         for(i=0;i<8;i++)
  105.         {               
  106.                 Data<<=1;   
  107.                 ledsda=SREG.0;             
  108.                 ledsck=0;
  109.                 ledsck=1;
  110.         }
  111. }

  112. uint ScanKey()//键盘扫描程序
  113. {
  114.         uchar i;   
  115.         uint KeyVal;
  116.         KeyVal=0x0000;
  117.         delay(20);
  118.         swcs=1;
  119.         delay(20);
  120.         swsck=0;   
  121.         delay(20);
  122.         swsck=1;  
  123.         delay(20);         
  124.         swcs=0;  
  125.         KeyVal=KeyVal|swsda;
  126.         for(i=0;i<15;i++)
  127.         {      
  128.                 swsck=0;
  129.                 delay(20);                          
  130.                 swsck=1;
  131.                 delay(20);
  132.                 KeyVal<<=1;
  133.                 KeyVal=KeyVal|swsda;
  134.         }
  135.         swcs=1;  
  136.         return(KeyVal);
  137. }  

  138. void SysInit(void)
  139. {
  140.         DDRD=0xFB;        /*input:PD2(IR_IN),PD0(RXD)  11111011*/
  141.         PORTD=0xFB;    //0xB3;
  142.         DDRB=0xf7;    /*input:  PB3(swsda)    11110111*/
  143.         PORTB=0xf7;
  144.         DDRC=0xff;
  145.         PORTC=0xfe; //Beep
  146.         
  147.         TWBR=32;   //TWI
  148.         TWSR=0;
  149.         
  150.         TCCR0=1;//stop
  151.         TCNT0=192;
  152.         
  153.         TCCR1A=0x00;
  154.         TCCR1B=0x00;
  155.         TCNT1H=0x00;
  156.         TCNT1L=0x00;
  157.         OCR1AH=0x00;
  158.         OCR1AL=0x00;
  159.         OCR1BH=0x00;
  160.         OCR1BL=0x00;

  161.         TCCR2=0x00;
  162.         ASSR=0x00;//CK
  163.         TCNT2=0;
  164.         OCR2=0;                                
  165.         TCCR2=0;//ctc,No prescaling,No OC2,stop;
  166.         //TCCR2=9;//ctc,No prescaling,No OC2,start;
  167.         GICR= 0x00;        // Enable INT1 interrupt
  168.         MCUCR=1;       //INT0 any change  INT1  //Low LEVEL(SW_IN)          
  169.         TCCR1A=0;
  170.         //TCCR1B=2;   // TIMER1 clock is xtal/8  //start Timer
  171.         TCCR1B = 0; //stop
  172.         // preset TIMER1
  173.         TCNT1=0;
  174.         // clear TIMER1 interrupts flags
  175.         TIFR=0;
  176.         // disable TIMER1 overflow interrupt
  177.         TIMSK=0x00;  
  178.         /*UART初始化*/
  179.         OSCCAL=EEPROM_read(0x1ff); //对内部RC OSC调整       
  180.        
  181.   //        OSCCAL=0xff;
  182.         //UCSRA=0xfc;//0xfc;               
  183.            UBRRH=0;
  184.            UBRRL=BaudCode[3]; //default Baud Rate=4800 (i=3)
  185. //           UBRRL=11;   //19200   
  186. //UBRRL=24;   //19200         
  187.    //        UBRRL=50;   //9600      
  188. //        UBRRL=102;   //4800
  189. //        UBRRL=204;   //1200
  190.       //        UBRL=103;
  191.         UCSRB=0x98;//enable UART receiver and transmitter, and receive interrupt(RXCIE)
  192.         UCSRC=(1<<7)|(1<<2)|(1<<1);//8位数据+1位STOP位        

  193.         SREG=0x80;    //enable interrupt bit in sreg   
  194.   //        CommSend(OSCCAL);

  195.         //initial system Variable  
  196.         if(AT24C32_R(0x00)==0x7e) RsFlag=1;
  197.         else RsFlag=0;
  198.        
  199.         LearningIREn=0;
  200.         RxFlag=0;
  201.         up=1;
  202.         down=1;
  203.         power=0;//Open Projector  

  204.         LedValue=0xff;                         
  205.                                   LedValue&=0xfe;
  206.                                   LedValue|=0x02;         //Off the Notebook LED;
  207.                                       LedValue|=0x04;  //off the DV LED;  
  208.         LedValue&=0xf7;  //On the AV LED
  209.         LedValue|=0x20;         //Off the Screen Down LED;
  210.         LedValue|=0x10;         //Off the Screen Up LED;
  211.         LedValue|=0x40;        //Off the Mic Sel LED       
  212. //        LedValue|=0x80;  //Off the IR Learning Sel LED       
  213.         LED(LedValue);

  214.             sel_a=0; //difference
  215.             sel_v=0;
  216.             sel_vga1=0;
  217.             sel_vga0=0;        
  218.        
  219.         T2Cnt=0; //used for Timer2 count
  220.         Learncnt=0;
  221.         LearnEnCnt=0;   
  222.         VolumeSel=0;
  223.         AVCnt=0;
  224.         gLearning=0;
  225.         gSending=0;
  226.         gLearningFail=0;  
  227.         AudioVolume=73;   
  228.         PowerOffFlag=1;  
  229.         MicVolume = 73;
  230.             M62429Ctr(0,0,73);  //Audio
  231.         M62429Ctr(0,1,73);
  232.             M62429Ctr(1,0,73);  //Mic
  233.         M62429Ctr(1,1,73);
  234. }

  235. /******************************************
  236.                I2C总线写一个字节
  237.                             返回0:写成功
  238.                                 返回非0:写失败
  239. *******************************************/
  240. uchar AT24C32_W(uchar mcu_data,uint AT24C32_address)
  241. {
  242.           unsigned int addr;
  243.           addr=AT24C32_address;
  244.           Start();//I2C启动   
  245.           Wait();
  246.           if(TestAck()!=START) return 1;//ACK
  247.           Write8Bit(wr_device_add);//写I2C从器件地址和写方式
  248.           Wait();
  249.           if(TestAck()!=MT_SLA_ACK) return 1;//ACK
  250.           Write8Bit(addr>>8);//写24C02的ROM地址h
  251.           Wait();
  252.           if(TestAck()!=MT_DATA_ACK) return 1;//ACK
  253.           Write8Bit(AT24C32_address&0x00FF);//写24C02的ROM地址l
  254.           Wait();
  255.           if(TestAck()!=MT_DATA_ACK) return 1;//ACK
  256.           Write8Bit(mcu_data);//写数据到24C02的ROM
  257.           Wait();
  258.           if(TestAck()!=MT_DATA_ACK) return 1;//ACK       
  259.           Stop();//I2C停止
  260.           delay(2000);//延时等EEPROM写完
  261.           return 0;
  262. }
  263. /******************************************
  264.                I2C总线读一个字节
  265.                            如果读失败也返回0
  266. *******************************************/
  267. uchar AT24C32_R(uint AT24C32_address)
  268.       {
  269.            unsigned char temp;  
  270.            unsigned int addr;
  271.            addr=AT24C32_address;
  272.            Start();//I2C启动
  273.            Wait();
  274.            if (TestAck()!=START) return 0;//ACK          
  275.            Write8Bit(wr_device_add);//写I2C从器件地址和写方式
  276.            Wait();
  277.            if(TestAck()!=MT_SLA_ACK) return 0;//ACK
  278.            Write8Bit(addr>>8);//写24C02的ROM地址h
  279.            Wait();
  280.            if(TestAck()!=MT_DATA_ACK) return 0;//ACK
  281.            Write8Bit(AT24C32_address&0x00FF);//写24C02的ROM地址l
  282.            Wait();          
  283.            if (TestAck()!=MT_DATA_ACK) return 0;
  284.            Start();//I2C重新启动
  285.            Wait();
  286.            if (TestAck()!=RE_START)  return 0;
  287.            Write8Bit(rd_device_add);//写I2C从器件地址和读方式
  288.            Wait();
  289.            if(TestAck()!=MR_SLA_ACK)  return 0;//ACK
  290.            Twi();//启动主I2C读方式
  291.            Wait();
  292.            if(TestAck()!=MR_DATA_NOACK) return 0;//ACK       
  293.            temp=TWDR;//读取I2C接收数据
  294.            Stop();//I2C停止
  295.            return temp;
  296.       }

  297. interrupt [USART_RXC] void comm_isr(void)//串行口中断程序
  298. {
  299.      uchar i,sreg,FunCode;
  300.    //  CommSend(0xF5);
  301.      if ((UCSRA&0x80)==0x80)
  302.      {
  303.          sreg=SREG;
  304.          SREG=0x0; //SREG=0x0;
  305.          RxFlag=0;
  306.          while (!(UCSRA&0x80)); UCSRA&=~0x80;//CommSend(UDR);
  307.          if(UDR!=0xFF) goto EXT;          //同步码
  308.          while (!(UCSRA&0x80)); UCSRA&=~0x80; //功能码
  309.          FunCode=UDR;
  310.          //CommSend(FunCode); }
  311.          if(FunCode==0x7F)
  312.          {
  313.                  while (!(UCSRA&0x80)); UCSRA&=~0x80;
  314.                  RxBuf=UDR;delay(0xff);//CommSend(RxBuf);      //接收数据
  315.                 while (!(UCSRA&0x80)); UCSRA&=~0x80;//结束码
  316.                  if(UDR==0x55) RxFlag=1;      
  317.          }
  318.          else if(FunCode==0x8F)//232 databyte
  319.          {
  320.                 while (!(UCSRA&0x80));SerialBuf[0]=UDR;UCSRA&=~0x80;//数据索引  
  321.                 
  322.                 while (!(UCSRA&0x80));SerialBuf[1]=UDR;UCSRA&=~0x80;//Baud Rate
  323.                 
  324.                 while (!(UCSRA&0x80));
  325.                 SerialBuf[2]=UDR;
  326.                 UCSRA&=~0x80;//RsDataLength   
  327.                
  328.                 for(i=0;i<SerialBuf[2];i++)
  329.                 {
  330.                         while (!(UCSRA&0x80));
  331.                         SerialBuf[i+3]=UDR;
  332. //                        AT24C32_W(UDR,RsDataIndex*512+3+i);UCSRA&=~0x80;//RsDataCode
  333.                 }                 
  334.                 
  335.                 while (!(UCSRA&0x80));
  336.                  if(UDR==0x55)
  337.                  {     //  CommSend(SerialBuf[0]);
  338.                         AT24C32_W(SerialBuf[1],SerialBuf[0]*512+1);//CommSend(SerialBuf[1]);//Baud Rate
  339.                         AT24C32_W(SerialBuf[2],SerialBuf[0]*512+2);//CommSend(SerialBuf[2]);//RsDataLength      
  340.                         for(i=0;i<SerialBuf[2];i++)
  341.                         {
  342.                                 //CommSend(SerialBuf[i+3]);
  343.                                 AT24C32_W(SerialBuf[i+3],SerialBuf[0]*512+3+i);//Write RsDataCode TO IIC
  344.                         }
  345.                          AT24C32_W(0x7e,0); //Rs232 Flag
  346.                          RsFlag=1;
  347.                  }
  348.                  else AT24C32_W(0,0); UCSRA&=~0x80;//结束码
  349.                   
  350.          }
  351.          else goto EXT;
  352.      }   
  353. EXT:
  354.          SREG=sreg;     
  355. //    CommSend(0x77);
  356. }
  357.             
  358. void CommSend(uchar data)  //串行口发送处理子程序
  359. {
  360.    /*        UCSRA |= (1<<6);//TXC  
  361.         UDR = data;
  362.         do{} while ((UCSRA&(1<<6)) == 0); */
  363.      while (!(UCSRA&(1<<5)));
  364.          UDR=data;         
  365. }
  366.   
  367. void M62429WriteByte(unsigned char VolumeSel,unsigned char i,unsigned char Data)
  368. {
  369.     if(VolumeSel%2) M62429MicClk = 0;   //Mic
  370.     else M62429VolClk = 0;   //Audio
  371.     do
  372.     {
  373.         Data = Data >> 1;
  374.         M62429Data = SREG.0;
  375.         delay(2);
  376.         if(VolumeSel%2) M62429MicClk = 1;
  377.         else M62429VolClk = 1;
  378.         delay(2);
  379.         M62429Data = 0;
  380.         delay(2);
  381.         if(VolumeSel%2) M62429MicClk = 0;
  382.         else M62429VolClk = 0;
  383.     } while (--i != 0);
  384. }

  385. void M62429Ctr(unsigned char VolumeSel,unsigned char Mod,unsigned char Volume)
  386. {
  387.     unsigned char temp;
  388.     if (Volume < 84)
  389.     {
  390.         Mod = Mod & 0x03;
  391.         temp = (Volume & 0x03) | 0x0c;
  392.         Volume = Volume + 4;
  393.         Volume = (Volume & ~0x03) | Mod;
  394.         M62429WriteByte(VolumeSel,7,Volume);
  395.         M62429WriteByte(VolumeSel,3,temp);
  396.         M62429Data = 1;
  397.         delay(3);
  398.         if(VolumeSel%2) M62429MicClk = 1;
  399.         else M62429VolClk = 1;
  400.         delay(4);
  401.         if(VolumeSel%2) M62429MicClk = 0;
  402.         else M62429VolClk = 0;
  403.         M62429Data = 0;
  404.     }
  405. }

  406. ///////////////////////////////
  407. //IR Learn
  408. void LearnIR(void)
  409. {
  410. //        unsigned int i;
  411. //        for(i=0;i<192;i++)
  412. //        gIRData[i]=0;   
  413.         gCount=0;
  414.         gWaitingIR=1;   
  415.         gState=0;
  416.    //        gTimeoutCount=0;

  417.         GICR= 0x40;        // Enable INT0 interrupt
  418.         MCUCR=0x01;       //toggle  
  419.            SREG=0x80;    //Enable interrupt bit in sreg   
  420.         gLearning=1;//Must add it again  
  421.        
  422.         TIMSK=0x40;// enable TIMER2 overflow
  423.         TCNT2=0;
  424.         TCCR2=0x07;//start timer2/1024  电源控制带延时
  425.        
  426. ……………………

  427. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
top2800-all-ok-1006.rar (7.17 KB, 下载次数: 7)


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表