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

📄 ex1c.c

📁 24e1的无线通讯原程序,可以从该原码中,学习到怎样做利用24E1来做无线开发
💻 C
📖 第 1 页 / 共 2 页
字号:
    CE = 1;    
    ResetTimer(1);
    while(DR1 == 0)
    {
        if ((rxTout != 0) && (GetTimer(1) > rxTout))
        {
            CE = 0;
            return 0;
        }
    }
    cmd = SpiReadWrite(0);
    i = 0;
    while(DR1)
    {
        pBuf[i++] = SpiReadWrite(0);
        if (i == nBytes)
            break;
    }
    while(DR1)
    {
        SpiReadWrite(0);
    }
    CE = 0;
    return cmd;
}

static unsigned char WaitAck(void)
{
    unsigned char ack;
    SetRxMode();
    CE = 1;
    ResetTimer(0);
    while(DR1 == 0)
    {
        if (GetTimer(0) > 10)                    // 3ms time-out/10MS
        {
            CE = 0;
            return 0;
        }
    }
    ack = SpiReadWrite(0);
    while(DR1)
    {
        SpiReadWrite(0);
    }
    CE = 0;
    return ack;
}

unsigned char TransmitPacket(unsigned char *pBuf)
{
    unsigned int i;
    unsigned char cmdt, cmdr, j;

    seq++;
    cmdt = 0x10 | (seq & 0x0f);
    cmdr = 0;
    ResetTimer(1);
    for(;;)
    {
        for (i=0;i<3;i++)          //JUST 6 TIME
        {
            // Repeat two times on all channels if no ACK is received
            for (j=0;j<2;j++)
            {
                TransmitBytes(pBuf, cmdt);
                cmdr = WaitAck();
                if ((cmdr != 0) && ((cmdr & 0x0f) == (cmdt & 0x0f)))
                {
                    //hopIdx++;    NOT HOP
                    return 1;
                }
            }
           // hopIdx++;     NOT HOP
            if ((txTout != 0) && (GetTimer(1) > txTout))
                return 0;
        }
    }
    return 0;
}

unsigned char ReceivePacket(unsigned char *pBuf)
{
    unsigned char cmd;

    //done = 0;
    while(1)
    {
        cmd = ReceiveBytes(pBuf);
         if (cmd == 0)
            return 0;
         cmd = (cmd & 0x0f) | 0x20;
         SendAck(cmd);
         Delay100us(1);
         SendAck(cmd);
         Delay100us(10);
         SendAck(0X20);
        //hopIdx++;
        if ((cmd & 0x0f) != seq)
        {
            seq = cmd & 0x0f;
            //done = 1;
             break;
        }
    }
    return 1;
}

//void ReceiverTimeout(unsigned tOut)
//{
	// Set the receiver timeout in ms.
	// 1ms = min timeout, 65536ms is max, 0 means wait forever
//	rxTout = tOut;
//}

//void TransmitterTimeout(unsigned tOut)
//{
	// Set the transmitter timeout in ms
	// 1ms is min timeout, 65536ms is max, 0 means wait forever
//	txTout = tOut;
//}

void InitReceiver(unsigned char n, unsigned char *pAddr)
{
    seq = 15;
    rxTout = 1000;								// Default 3s timeout    
    nBytes = n;                                 // Number of data bytes (0-25)

    SPICLK = 0;                                 // Max SPI clock
    SPI_CTRL = 0x02;                            // Connect internal SPI controller to Radio
    CE = 0;
    CS = 1;                                     // RF SPI CS = 1
    Delay100us(0);

    SpiReadWrite(0x00);                         // We are not using the DuoCeiver functionallity
    SpiReadWrite((n+1)*8);                      // Number of Rx data bits
    InitCommon(pAddr);
}

void InitTransmitter(unsigned char n, unsigned char *pAddr)
{
    seq = 0;
    txTout = 1000;								// Default 3s timeout
    nBytes = n;                                 // Number of data bytes (0-25)
    SPICLK = 0;                                 // Max SPI clock
    SPI_CTRL = 0x02;                            // Connect internal SPI controller to Radio
    CE = 0;
    CS = 1;                                     // RF SPI CS = 1
    Delay100us(0);
    SpiReadWrite(0x00);                         // We are not using the DuoCeiver functionallity
    SpiReadWrite(8);                            // Number of Rx data bits is 8 for the transmitter
    InitCommon(pAddr);
}
void ReceiveFile(void)
{
   // unsigned char xdata i;

    InitReceiver(25, 0);
    while(1)
    {
       if (ReceivePacket(buf) != 0)
                {
                     
		           LEDG=0;
				   LEDR=1;
				
				   //for (i=0;i<25;i++)             //if use ISD debug ,dont use this one
				       //   { PutChara(buf[i]);}
               //for (j=0;j<20;j++)
	           //{Delay100us(200);}      //LEDG  400mS
			                        						 
				 }
                else
                {
                    LEDR=0; 
				    LEDG=1;   // Indicates an error
               //for (j=0;j<20;j++)
	           //{Delay100us(200);}      //LEDG  400mS

                }
            //PutChar(buf[n++]);
    }
}


void TransmitFile(void)
{
    unsigned int xdata temp,j;
    unsigned char xdata  n;
	n=0;
    InitTransmitter(25, 0);
    while(1)
    {   
         for (aaa=0;aaa<25;aaa++)
          {buf[aaa]=ID[aaa];} 
         n=n+1;
	     buf[24]=n;
    
	    temp= ( TransmitPacket(buf));
		           
		    if (temp==1)
			    {  LEDR=0;
 			       //s=s-1;
				  // buf[19]=s;
				  // speeker();
				}
			else   {LEDG=0;                //Y LED OFF
			       // i=4;
				   //	s=4;
			      // speeker1();
			       }
			 if (temp==1)
           {  for (j=0;j<25;j++)
	           {Delay100us(200);}      //LEDG  20mS
		     //LEDR=0;
             LEDR=1;
		     }
			 else {
			     for (j=0;j<25;j++)
	           {Delay100us(200);}      //LEDG  400mS
		       //LEDR=0;
                LEDG=1;
			       }
	      for (bbb=0;bbb<2;bbb++)           //2s
          {     
		  for (aaa=0;aaa<100;aaa++)        //delay 1s
          {Delay100us(100);}               //delay10ms
		  }
    }
}
int main(void)
{	unsigned char aaa;
    Init();
	
    //ISDwait();
    EA=1;
	LEDR=0;
	LEDG=0;
	for (aaa=0;aaa<100;aaa++)
          {Delay100us(50);}
  
    LEDR=1;
	LEDG=1;
   	for (aaa=0;aaa<100;aaa++)
          {Delay100us(100);}
    LEDR=0;
	LEDG=0;
    while(1)
    {

      /*  //    case 'R':
		    if (KEYR==0)
			  {
			    LEDR=1;
	            LEDG=0;
               ReceiveFile();
                }
                
            //case 'T':
			if (KEYT==0)
			  {
			    LEDR=0;
	            LEDG=1;
                TransmitFile();
			   }*/
          PutChara(0xaa);
		    	for (aaa=20;aaa<100;aaa++)
          {Delay100us(100);}
    }

    return 0;
}

⌨️ 快捷键说明

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