CC2530基于寄存器实现无线收发

发布于 / 学习
/*
  当光照强的时候关闭LED灯  手捂着传感器跑马灯
*/
#include<iocc2530.h>

int count=0;
char output[8];

void Delay(unsigned char m)
{
  int i=0,j=0;
  for(i=0;i<1000;i++)
    for(j=0;j<m;j++);
}

//定义一个函数用来进行adc转换
unsigned short get_adc()
{
  APCFG|=1;
  P0SEL |=0x01;
  P0DIR &=~0x01;
  ADCCON3=(0X80|0X10|0X00);
  ADCIF=0;
  while(!ADCIF);
  unsigned long value;
  value = ADCH;
  value = value<<8;
  value |=ADCL; 
  value = value*330;
  value = value>>15;
  return value;
}

void initUART0()
{
  PERCFG|= 0x00;
  P0SEL |=0X0C;
  U0CSR |=0X80;
  U0GCR = 10;
  U0BAUD = 216;
  U0UCR |= 0X80;
  U0CSR |=0X40;

  UTX0IF = 0;
  URX0IF = 0;
  EA = 1;
  URX0IE = 1;
}
void initT1()
{
  CLKCONCMD &=~0x40;
  while(CLKCONSTA &0X40);
  CLKCONCMD &=~0X47;

  T1CTL |= 0X05;
  IEN1 |=0X02;
  EA=1;
}

void led()
{
  P1 = 0x02;
  Delay(100);
  P1 =0x01;
  Delay(100);
  P1=0x10;
  Delay(100);
  P1=0x08;
  Delay(100);
  P1=0x10;
  Delay(100);
  P1 =0x01;
  Delay(100);
  P1 = 0x02;
  Delay(100);
  P1 = 0x00;
  Delay(100);

}
void UART0SendString(char *str)
{
  while(1)
  {
    if(*str=='\0')
      break;
    U0DBUF = *str++;
    while(!UTX0IF);
    UTX0IF = 0;
  }
}
int isRun = 0;
#pragma vector = T1_VECTOR
__interrupt void t1()
{
    count++;

   if(count==30)
    {
      count = 0;
      unsigned short gz_val = get_adc();
      output[0] = gz_val/100+'0';
      output[1] = '.';
      output[2] = gz_val/10%10+'0';
      output[3] = gz_val%10+'0';
      output[4] = 'V';
      output[5] = '\r';
      output[6] = '\n';
      output[7] ='\0';
      UART0SendString("光照值:");
      UART0SendString(output);
    }
  T1IF = 0;
}

void tx()
{
  unsigned char i;
  unsigned char mac[]="ww";
  RFD=5;   //?
  for(i=0;i<3;i++)
    RFD=mac[i];   
     //ISTXON 
     //校准之后使能TX
     //描述:校准之后ISTXON指令立即使能TX。在执行下一条   
  RFST=0xe9;   //?
  while(!(RFIRQF1&(1<<1)));
  RFIRQF1=~(1<<1);
  Delay(200);
}
#ifdef RX
#pragma vector=RF_VECTOR
__interrupt void rf_isr(void) 
{
  char len;
  unsigned char  i; 
  unsigned char buf[12];
  //关中断
  EA = 0;    
  //接收帧结束
       if (RFIRQF0 & (0x40))  //
       {
               //接收帧长度
        len = RFD;
        len &= 0x7f;

                //将接收的数据写入buf中
        for (i = 0; i < len; i++) 
                {
              buf[i] = RFD;
                 Delay(5);
        }
               // 清RF中断
        S1CON = 0; 
               //清除 RXPKTDONE中断  
        RFIRQF0 &= ~(0x40); 

            if((buf[0]=='w')&&(buf[1]=='w'))
            {
              if(isRun==1)
                {
                  led();
                  isRun=0;
                }
            }
       } 
       EA = 1; 
}

#endif


void main()
{
  P1SEL = 0x00;
  P1DIR = 0x1B;
  P1=0;

  initUART0();
  initT1();

FREQCTRL=0X0B;

//SHORT_ADDR0 = 0x05;
//SHORT_ADDR1 = 0x00;
//PAN_ID0 = 0x22; 
//PAN_ID1 = 0x00;


#ifdef RX
       //RXPKTDONE 中断位使能
     RFIRQM0 |= (1<<6);
                    //  RF 中断使能
     IEN2 |= (1<<0);
                       //开中断
         EA = 1;
                       //清除RXFIFO缓冲区并复位解调器  
         RFST = 0xed; 
                       //为RX使能并校准频率合成器
     RFST = 0xe3;
                       //禁止帧过滤
         FRMFILT0 &= ~(1<<0);

#endif

  while(1)
  {
    unsigned short gz_val = get_adc();
    if(gz_val<150)
      isRun=1;
    else
      isRun=0;
    if(isRun==1)
    {
      tx();
      isRun=0;
    }
  }

}
本文采用 CC BY-NC-SA 3.0 Unported 协议进行许可
本文链接: https://www.ahwgs.cn/cc2530jiyujicunqishixianwuxianshoufa.html