知識社群登入
位置: 艾鍗學院 Blog > 專業論壇 > 討論
RF資料傳輸疑問
1樓
RF作資料傳輸
不用HT-12E   HT-12D作編碼以及解碼
而用兩個8051來作編碼以及解碼的工作
以下是我用課本範例改的程式碼
可是卻一直試不出來
可以請大大幫我看看有什麼問題嗎?
    
發射端程式
#include <AT89X51.H>
typedef unsigned char tByte;
#define SYNC 0xAA// synchro signal
#define RADDR 0x44
void delayms(unsigned int time)
{
  unsigned int n;
  while(time>0)
    {
     n=120;
     while(n>0)  n--;
     time--;
    }
        
void send_byte(tByte const b)
 {
  SBUF = b;
  while(!TI);
  TI = 0;   
 }
void send_packs(tByte const radd, tByte const rcmd)
 {
  send_byte(SYNC);
  send_byte(radd);
  send_byte(rcmd);
  send_byte((radd+rcmd)); // check sum
 }
 main()
{
  PCON |= 0x00;
  TMOD=0x20;
  TH1=230;
  TL1=230;
  TR1=1;
  SCON=0x40;
  while(1)
    {
     TI=0;
     send_packs(RADDR, P1);
           while(TI==0);
     delayms(100);        
    }
}        

接收端程式
#include <AT89X51.H>
typedef unsigned char tByte;
#define SYNC 0xAA// synchro signal
#define RADDR 0x44
main()
{
PCON |= 0x00;
TMOD = 0X20;
TH1 = 230;
TL1 = 230;
TR1 = 1;
SCON = 0x70;
ES = 1;
EA = 1;
while(1)
{  
   RI = 0;
   while(RI == 0);
}
 
}
 tByte USART_vReceiveByte(void)
{
    // Wait until a byte has been received
    while(!RI);
 RI = 0;
    // Return received data
    return SBUF;
}
 
void UART_ISR() interrupt 4
{
 // a-lu remark: 2011.02.21
 //  No implemented SYNC check before receive the byte of RADDR
 //  Let's to wait and see the test result
 
 //define variables
 tByte raddress, cmd, chk;//transmitter address
 
 // find TX or RX caused interrupt
 if(TI) 
  { 
  // clear TI
  TI = 0;
  }
 else
  { 
  //receive destination address
  raddress = USART_vReceiveByte();
  //receive cmd
  cmd = USART_vReceiveByte();
  //receive checksum
  chk = USART_vReceiveByte();
  //compare received checksum with calculated
  if(chk == (raddress+cmd))//if match perform operations
   {
   //if transmitter address match
   if(raddress == RADDR)
    {
     if(cmd ==  0xfe)
      {
        P1 = 0x3f;         
          }
 
     else if(cmd == 0xfd)
      {
            P1 = 0xcf;
          }
 
     else if(cmd == 0xfb)
          {
        P1 = 0x0f;
          }
 
     else if(cmd == 0xf7)
         {
        P1 = 0xff;
         }     
    }
   }
}
}
2樓

就業班學員

何老師說已於課堂上回覆了