⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ir.c

📁 一款居于世纪民生CS6208的带TCP-IP网络功能的8位MCU内核的芯片的应用代码。
💻 C
📖 第 1 页 / 共 4 页
字号:
#include "CS6208.H"
#include "REG.H"
#include <intrins.h>
#include <stdlib.h>
#include <math.h>
#include <absacc.h>

#include "NIC.H"
#include "IR.H"

#define  IR38KHz //use 38khz crastal,else use timer0 generate 38khz
#define  TIMER0_RELOAD 0x7C

#define  TRUE   1
#define  FALSE  0

unsigned char __wsystemtimer = 0;
#define TIME_1S  100

unsigned char __wsend = 0;
#define TIME_ARPREQ   100
#define TIME_SEND     200

unsigned char idata __wmeterquantity;
unsigned char idata __warmed;
unsigned char idata ALARM_BUFFER[4];
unsigned char xdata TIME[7];
unsigned char xdata CONTROL[4];
struct METER{
unsigned char TYPE;
unsigned char PULSE;
unsigned char WIDTH;
unsigned char COUNTER0;
unsigned char COUNTER1;
unsigned long AMOUNT;
unsigned char WRITE; 
}METER0,METER1,METER2,METER3,METER4,METER5;

void PARA_INIT();
void CS6208_INIT();

/*************************/

/************************/
main()
{

    PBOE = 0xFF;//PB as output
    PCOE = 0xFF;//PC as output
	BREG = 0xFF;
    CREG = 0xFF;
    
	PARA_INIT();
	CHECKSUM_UDP();

	Init_NIC();
    CS6208_INIT();
    ARPREQ();

	while(1)
	     {

            TA = 0xAA;
	        TA = 0x55;
	        WDCON = WDCON|0x01;//clear watchdog timer
            
            #ifdef IR38KHz
            if (__wirtime >= TIME_IR)
               {
                  __wirtime = 0;

                  if (!__blearn)
                     {
                        __blearn = TRUE;
                        __sbbusy = 0;
                        IR_CLEAR();
                        EX0 = 1;//enable ir interrupt
                     }
                  else
                     {
                        __blearn = FALSE;
                        if (__dir)
                           {
                              __dirlong = 0;
                              EX0 = 1;//enable ir interrupt
                           }
                     }
               }
            #endif

			Deal_Packet();
            
		    if (__wsend == TIME_ARPREQ)
               {
                  __wsend = TIME_ARPREQ + 30;
                  ARPREQ();
               }
			if (__wsend == TIME_SEND)
               {
                  __wsend = TIME_SEND + 30;
                  UDP(R_BROARDCAST);
               }            
		 }
}
/**********************************************************/

void PARA_INIT()
{

	MACADDRESS[0] = 0x00;
	MACADDRESS[1] = 0x40;
	MACADDRESS[2] = 0x9D;
	MACADDRESS[3] = 0xFF;
	MACADDRESS[4] = 0xFF;
	MACADDRESS[5] = 0xFF;

	IPADDRESS[0] = 221;
    IPADDRESS[1] = 222;
	IPADDRESS[2] = 222;
	IPADDRESS[3] = 164;

	PC_MACADDRESS[0] = 0x00;
	PC_MACADDRESS[1] = 0x40;
	PC_MACADDRESS[2] = 0x9D;
	PC_MACADDRESS[3] = 0xFF;
	PC_MACADDRESS[4] = 0xFF;
	PC_MACADDRESS[5] = 0x00;

	PC_IPADDRESS[0] = 221;
    PC_IPADDRESS[1] = 222;
	PC_IPADDRESS[2] = 222;
	PC_IPADDRESS[3] = 1;
}
void CS6208_INIT()
{
    PLLFC = 0x28;//PLL CLOCK :20*(8+1)/(2+1)=60MHz
    //PLLFC = 0x5B;//PLL CLOCK :20*(B+1)/(5+1)=40MHz
	//PLLFC = 0x4A;//PLL CLOCK :20*(A+1)/(4+1)=44MHz
	//PLLFC = 0x45;//PLL CLOCK :20*(5+1)/(4+1)=24MHz
    CCR = 0x05;//using fast OSC and disable slow OSC 

    CKCON = 0xC0;
    
    TA = 0xAA;
	TA = 0x55;
	WDCON = 0xC3;//init watchdog timer

    TMOD |= 0x02;//timer0,auto reload, IR 1000/38=26.3us
                 //osc/12 = 5MHz, unit = 200ns; count = 132 = 0x84
                 //TH0 = 0x100 - 0x84 = 0x7C;
    TH0 = TIMER0_RELOAD;
    TL0 = TIMER0_RELOAD;
    #ifndef IR38KHz
    TR0 = 1;
    ET0 = 1;
    #endif

    TMOD |= 0x20;//timer1,auto load,baud rate generation
    TH1 = 0xe8;
	TL1 = 0xe8;
	SCON = 0x50;
	PCON = 0x80;
	TR1 = 1;
    //ET1 = 1;
    ES0 = 1;

    //T2CON |= 0x00;//timer2,timer
    TH2 = 0x3c;
    TL2 = 0xaf;
    TR2 = 1;
    ET2 =1;

	//IT1 = 1;//MAC interrupt
	//EX1 = 1;
    #ifdef IR38KHz
    IT0 = 1;//IR interrupt,falling edge
    //EX0 = 1;
    #endif

    //IP = 0x01;  //INT0 higher
	//IP = 0x04;  //INT1 higher

    EA = 1;
}
void IR_INT0(void) interrupt 0
{
    if (__blearn)
       {
          __wirtime = 0;
          if (__sbirin)
             {
                if (__wirlow)
                   {
                      IR[__dir] = __wirlow;
                      __wirlow = 0;
                      __dir ++;
                   }
                if (__dir) __wirhigh ++;
                if (__wirhigh == 0xFF)
                   {
                      IR[__dir] = 0xFF;//__wirhigh;
                      __wirhighcount ++;
                      if (__wirhighcount >= IR_HIGH_LENGTH)
                         {
                           EX0 = 0;//disable ir interrupt
                           __dir = __dir - (IR_HIGH_LENGTH-1);
                           __sbbusy = 1;
                         }
                      else 
                         {
                           __wirhigh = 0x80;
                           __dir ++;
                         }
                   }
             }
          else
             {
                __wirhighcount = 0;
                if (__wirhigh != 0x80)
                   {
                      IR[__dir] = __wirhigh;
                      __wirhigh = 0x80;
                      __dir ++;
                   }                      
                __wirlow ++;
                if (__wirlow == 0x7F)
                   {
                      IR[__dir] = __wirlow;
                      __wirlow = 0;
                      __dir ++;                      
                   }
             }
       }
     else
       {
          if (__dirlong < __dir)
             {
                if (IR[__dirlong]&0x80)  __sbirout0 = 1;
                else __sbirout0 = 0; 
                IR[__dirlong] --;
                if ((IR[__dirlong]&0x7F) == 0) __dirlong ++;  
             }
           else
             {
                EX0 = 0;//disable ir interrupt
                __sbirout0 = 1;
                __dir = 0;
                __dirlong = 0;
             }
       }
}
void TIMER0(void) interrupt 1
{
     ;
}
void NIC_INT1(void) interrupt 2
{
    //EA = 0;

    ;//Deal_Packet();

    //EA = 1;
}
void SERIAL(void) interrupt 4
{
     if (RI == 1) RI = 0;
     if (TI == 1) TI = 0;
}
void TIMER2(void) interrupt 5
{
    TF2 = 0;//clear interrupt

    TH2 = 0x3c;
    TL2 = 0xaf;

	__wsystemtimer ++;
	if (__wsystemtimer >= TIME_1S)
	   {
	      __wsystemtimer = 0;
          __wsend ++;
          __wirtime ++;                   
	   }
}


/********************cpu end*********************************/

/*************************************************************************/
/***********ethernet begin*******************/
void Deal_Packet()
{
    unsigned char xdata *point;
    unsigned char i;
    //unsigned char xdata __wcurr0,__wcurr1,__wcurr2;
	//unsigned int PacketLength;
	//unsigned int XmitPoint;

    __wisr = NIC_ISR;

	if (__wisr&ISR_RcvOK)
	   {
	      //NIC_ISR = 0xFF;
          NIC_ISR = ISR_RcvOK;

          NIC_CR = CR_Stop|CR_Page1;//change to page 1
		  __wcurr = NIC_CURR;
          //__wcurr0 = NIC_CURR;
          //__wcurr1 = NIC_CURR;
          //__wcurr2 = NIC_CURR;
          //if (__wcurr0 == __wcurr1)  __wcurr = __wcurr0;
          //else if (__wcurr0 == __wcurr2)  __wcurr = __wcurr0;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -