打印

有关51单片机与rf903的程序问题

[复制链接]
1524|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
陈158|  楼主 | 2009-7-23 21:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
温度传感器程序
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit DS=P2^2;//温度接口
sbit wela=P2^7;
sbit dula=P2^6;
void rest();//唤醒温度传感器ds18b20
void write_bit(bit);//写一个比特率
bit read_bit();//返回值
void write_byte(uchar);// 写字节
uchar read_byte();//返回值
void tmpchang();//温度转换
void delay(uchar);//延时函数
uint get_tmp(); //获得温度
void display(uint z);//显示函数
void main()//主函数
{
    uchar i;
    tmpchang();
    for(i=0;i<8;i++)
    {
        display(get_tmp());    
    }
}
void delay(uchar z)//延时z毫秒       
{
    uchar i,j;
    for(i=0;i<110;i++)
        for(j=0;j<z;j++);
}
void rest()
{
    uchar i;
    DS=0;
    i=100;
    while(i--);//延时约600us
    DS=1;
    i=20;
    while(i--);
}
void write_bit(bit a)
{
    uchar i;
    DS=0;
    if(a==0)
    {
        i=10;
        while(i--);//延时75us写0
        DS=1;
    }
    else
    {
        i=1;
        while(i--);//延时17us
        DS=1;
        i=5;
        while(i--);//延时40us写1      ????    
    }
    i=8;
    while(i--);
}
bit read_bit()
{
    uchar i;
    bit a;
    DS=0;
    i=1;
    while(i--);//延时17us
    DS=1;
    a=DS;        //master采样   时序图要看认真点
    i=1;
    while(i--);//延时17us
    return(a);
}
void write_byte(uchar a)//写一个字节
{
    uchar i;
    bit b;
    for(i=0;i<8;i++)
    {
        b=a&0x01;
        a=a>>1;
        if(b==0)
            write_bit(0);//写0
        else
            write_bit(1);//写1
    }
}
uchar read_byte()    //读一个字节
{
    uchar i,j,dat;
      dat=0;
      for(i=0;i<8;i++)
      {
        j=read_bit();
        dat=(j<<7)|(dat>>1);   //读出的数据最低位在最前面,这样刚好一个字节在DAT里
      }
      return(dat);
}
void tmpchang()
{
    rest();
      write_byte(0xcc);  // address all drivers on bus
      write_byte(0x44);
}
uint get_tmp()//得到温度
{
      uchar a,b,c;     
    uint temp=0;
      rest();
      write_byte(0xcc);
      write_byte(0xbe);
      a=read_byte();
      b=read_byte();
    b=b&0x03;        //减小开始误差
    temp=temp|b;
    temp=temp<<8;    //移向高位
    temp=temp+a;    //放入两个字节里面
    temp=temp*10/16;//十六到十进制转换
    return temp;
}
void display(uint z)//显示函数精确到小数点后一位  数码管上显示三位数字
{
    uchar table1[]={0x3f,0x06,0x5b,0x4f,
    0x66,0x6d,0x7d,0x07,0x7f,0x6f};//数码管能显示的数字从0-9
    uchar i,j,k;
    i=z/100;//十位
    j=z%100/10;//个位
    k=z%100%10;//小数位
    wela=1;//打开位选
    P0=0xfe;//赋值给第一个数码管
    wela=0;//关闭位选
    dula=1;//打开段选
    P0=table1;//显示十位数值
    dula=0;//关闭段选
    delay(4);
    wela=1;
    P0=0xfd;
    wela=0;
    dula=1;
    P0=table1[j]+0x80;//显示个位要加点
    dula=0;
    delay(4);
    wela=1;
    P0=0xfb;
    wela=0;
    dula=1;
    P0=table1[k];//显示小数位
    dula=0;
    delay(4);
}
rf903程序

#include <reg52.h>
#include <intrins.h>
#define     uchar    unsigned char
#define     uint     unsigned int
#define     C_FIFO_Byte           0x20
#define     C_Tx_Frequency        0xcccc                    #define     C_Rx_Frequency        0xd0cb 
//---------------------------RF903引脚映射(用户移植时请根据具体硬件修改) ---------------------------------------
sbit        RF903_Scs         =   P1^0    ;    
sbit         RF903_Sck        =   P1^1    ;   
sbit         RF903_Sdi        =   P3^4    ;                    
sbit         RF903_Sdo        =   P3^4    ;                       
sbit         RF903_PowerOn    =   P1^6    ;      
sbit         RF903_Trs        =   P1^4    ;       
sbit         RF903_Tre        =   P1^5      ;
sbit         RF903_Irq        =  P3^2      ;
//--------------------------- 以下部分无需修改 -------------------------------------------------------------------
#define     declare_sdo_input()          RF903_Sdi=1
#define     declare_sdo_output()         RF903_Sdi=0
//-----------------------------宏定义IO初始化--------------------------------------------------------------------
#define     RF903_IO_INITIALIZTION() 
RF903_Scs         =1;
RF903_Sck         =0;
RF903_Sdi         =1;
RF903_PowerOn     =1;
RF903_Trs         =0;
RF903_Tre         =0;
RF903_Irq         =1
//------------------------------基本延时函数---------------------------------------------------------------------
#define     delay_RF903_us()  (_nop_(),_nop_()    )
//---------------------------------------------------------------------------------------------------------------
#define     positive              1
#define     negative              0
//-------------------------------RF903寄存器宏定义---------------------------------------------------------------
#define     Reg_SystemClock       0x00
#define     Reg_PLL_I             0x01
#define     Reg_PLL_II            0x02
#define     Reg_PLL_III           0x03
#define     Reg_PLL_IV            0x04
#define     Reg_Crystal           0x05
#define     Reg_TXI               0x06
#define     Reg_TXII              0x07
#define     Reg_RXI               0x08
#define     Reg_RXII              0x09
#define     Reg_ADC               0x0a
#define     Reg_FIFO              0x0b
#define     Reg_Code              0x0c
#define     Reg_PinControl        0x0d
#define     Reg_Calibration       0x0e
#define     Reg_ModeControl       0x0f
static     uint  TX_Freq_Calibration;
static     uint  RX_Freq_Calibration;
uchar      id_buff[0x04] = {0x54,0x75,0xC5,0x2A};    //地址
#define TxRxBuf_Len 32
uchar     TxRxBuf[TxRxBuf_Len]=
{

0x01,0x02,0x03,0x4,0x05,0x06,0x07,0x08,
0x09,0x10,0x11,0x12,0x13,0x14,0x15,0x16,
0x17,0x18,0x19,0x20,0x21,0x22,0x23,0x24,
0x25,0x26,0x27,0x28,0x29,0x30,0x31,0x32,
};

void     RF903_Register_Write(uchar addr,uint parameter);
uint     RF903_Register_Read(uchar addr);
void     RF903_Status_Receiver(void);
void     RF903_Status_Transmit(void);
uchar    RF903_Fifo_Read(uchar *read_buff);
void     RF903_Fifo_Write(uchar *write_buff);
static void delay(uint n)
{
    uint i;
    while(n--)
    for(i=0;i<40;i++);
}

void    Byte_Write(uchar byte)
{
        uchar i;
        declare_sdo_output();
        for(i = 0x00; i < 0x08; i++)
        {
            if(byte&0x80)
                RF903_Sdi = positive;
            else
                RF903_Sdi  = negative;
            delay_RF903_us();
            RF903_Sck = positive;
            delay_RF903_us();
            RF903_Sck = negative;
            delay_RF903_us();            
            byte <<= 0x01;
        }
}

uchar   Byte_Read(void)
{
        uchar i;
        uchar byte;
        declare_sdo_input();
        for(i = 0x00; i < 0x08; i++)
        {
            byte<<= 0x01;
            delay_RF903_us(); 
            if(RF903_Sdo)
                byte|= 0x01;
            else
                byte&= 0xfe;
            RF903_Sck= positive;           
            delay_RF903_us();
            RF903_Sck= negative;              
        }
        return    (byte);
}

void    Word_Write(uint word)
{
        uchar i;
        declare_sdo_output();
        for(i = 0x00; i < 0x10; i++)
        {           
            if(word&0x8000)
                RF903_Sdi= positive;
            else
                RF903_Sdi= negative;  
            delay_RF903_us();
            RF903_Sck = positive;
            delay_RF903_us();
            RF903_Sck = negative;
            delay_RF903_us();                 
            word<<= 0x01;
        }
}

uint    Word_Read(void)
{
        uchar i;
        uint  word;
        declare_sdo_input();//申明sdo输入函数
        for(i = 0x00; i < 0x10; i++)
        {
            word<<= 0x01;
            
            delay_RF903_us();
            
            if(RF903_Sdo)
                word|= 0x0001;
            else
                word&= 0xfffe;
            RF903_Sck= positive;        
            delay_RF903_us();
            RF903_Sck= negative;
        }
        return(word);
}

void    RF903_Reset_Chip(void)
{
        RF903_Scs = negative;  
        Byte_Write(0x7a);      /* 写RF903-Reset命令 */
        RF903_Scs = positive;
}

void    RF903_Reset_FifoTX(void)
{
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0x6a); /* 写RF903-FifoTX命令 */
        RF903_Scs = positive;
}

void    RF903_Reset_FifoRX(void)
{
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0xea);   /* 写RF903-FifoRX命令 */
        RF903_Scs = positive;
}

uchar  RF903_Fifo_Read(uchar *read_buff)
{
        uchar i;
         RF903_Irq =1;
         _nop_();
        if(RF903_Irq==0)
          return 0;
        if( RF903_Register_Read(Reg_ModeControl)&0X0200 ) //检测crc
        {
          RF903_Reset_FifoRX(); //RF903_Status_Receiver();
          return 0;
        }
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0xca);              /* 读RF903-FIFO命令 */
        declare_sdo_input();         /* 读RF903-FIFO数据 */
        for(i = 0x00;i < C_FIFO_Byte;i++)
        {
            *read_buff  = Byte_Read();
            read_buff ++;
        }
        declare_sdo_output();
        RF903_Scs = positive;
        return 1;
}

void    RF903_Fifo_Write(uchar *write_buff)
{
        uchar i;
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0x5a);                          /* 写RF903-FIFO命令 */
        for(i = 0x00;i < C_FIFO_Byte;i++)        /* 写RF903-FIFO数据 */
        {
            Byte_Write(*write_buff);
            write_buff ++;
        }
        RF903_Scs = positive;
}

void    RF903_Id_Write(unsigned char *id_buff)
{
        uchar i;
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0x2a);             /* 写RF903-ID命令 */
        for(i = 0x00;i < 0x04;i++)  /* 写RF903-ID数据 */
        {
            Byte_Write(*id_buff);
            id_buff ++;
        }
        RF903_Scs = positive;
}

void    RF903_Id_Read(uchar *id_buff)
{
        uchar i;
        RF903_Sck = negative;
        RF903_Scs = negative;
        Byte_Write(0xaa);     
        declare_sdo_input();     
        for(i = 0x00;i < 0x04;i++)
        {
            *id_buff= Byte_Read();
            id_buff ++;
        }
        declare_sdo_output(); 
        RF903_Scs        = positive;
}

void    RF903_Register_Write(uchar addr,uint parameter)
{
        RF903_Sck = negative;
        delay_RF903_us();
        RF903_Scs = negative;
        addr &= 0x0f;
        Byte_Write(addr);         
        Word_Write(parameter); 
        RF903_Scs = positive;
}

uint    RF903_Register_Read(uchar addr)
{
        uint parameter;
        RF903_Sck = negative;
        delay_RF903_us();
        RF903_Scs = negative;
        addr&= 0x0f;
        addr|= 0x80;
        Byte_Write(addr);         /* 读RF903-参数配置命令 */
        declare_sdo_input();
        parameter= Word_Read();    /* 读RF903-参数配置数据 */
        declare_sdo_output();
        RF903_Scs = positive;
        return (parameter); /* 返回读取数值 */
}

void    RF903_Status_Transmit(void)
{
  RF903_Register_Write(Reg_ModeControl,0x00d0);
  RF903_Register_Write(Reg_PLL_II,C_Tx_Frequency);
  RF903_Register_Write(Reg_Calibration,TX_Freq_Calibration);
  RF903_Register_Write(Reg_ModeControl,0x00d8); 
  while(RF903_Irq);
  while(!RF903_Irq);                 
}

void    RF903_Status_Receiver(void)
{
    RF903_Register_Write(Reg_ModeControl,0x00c0); 
    RF903_Register_Write(Reg_PLL_II,C_Rx_Frequency);
    RF903_Register_Write(Reg_Calibration,RX_Freq_Calibration);
    //RF903_Register_Write(Reg_ADC, 0x0000);
    //RF903_Register_Write(Reg_ModeControl,0x00c9);
    RF903_Register_Write(Reg_ModeControl,0x00c8);

}

uint    RF903_Calibration_Frequency(uint frequency)
{
        unsigned int temp;
        RF903_Register_Write(Reg_PLL_II,frequency);
        temp = RF903_Register_Read(Reg_ModeControl);  
        temp |= 0x0006;
        RF903_Register_Write(Reg_ModeControl,temp);
        do
        {
            temp = RF903_Register_Read(Reg_ModeControl);
        }while(temp&0x0006);
        temp = RF903_Register_Read(Reg_Calibration);
        temp &= 0x00ef;
        temp |= 0x4910;
        return temp;
}
/
void    RF903_Config_Chip(void)
{
        RF903_Tre = negative;
        RF903_Trs = negative;
        RF903_Register_Write(Reg_SystemClock,0x0079);
        RF903_Register_Write(Reg_PLL_I,0x0043);
        RF903_Register_Write(Reg_PLL_II,0xcfff);
        RF903_Register_Write(Reg_PLL_III,0x0000);
        RF903_Register_Write(Reg_PLL_IV,0x066c);        
        RF903_Register_Write(Reg_TXI,0x1520);
        RF903_Register_Write(Reg_TXII,0x0337);
        RF903_Register_Write(Reg_RXI,0x1813);
        RF903_Register_Write(Reg_RXII,0x500b);
        RF903_Register_Write(Reg_ADC,0x0000);
        RF903_Register_Write(Reg_FIFO,0x4000|(C_FIFO_Byte-1)); 
        RF903_Register_Write(Reg_Code,0x155f);
        RF903_Register_Write(Reg_PinControl,0x0122);
        RF903_Register_Write(Reg_Calibration,0x4886);
           RF903_Register_Write(Reg_ModeControl,0x00e0);     
        delay(80);    //delay 80us
}

void    RF903_Initialisation(void)
{
      RF903_IO_INITIALIZTION();
      RF903_PowerOn = negative;
      delay(500);         /* RF903稳定时间延迟配置 */
      RF903_PowerOn = positive;
      delay(500);
      RF903_Reset_Chip();//软件复位
      delay(500);
      RF903_Register_Write(Reg_Crystal,0x0015);
      delay(500);
      RF903_Register_Write(Reg_ModeControl,0x00c0);
      delay(50);
      RF903_Id_Write(id_buff);
      RF903_Config_Chip();        
      TX_Freq_Calibration = RF903_Calibration_Frequency(C_Tx_Frequency); 
      RX_Freq_Calibration = RF903_Calibration_Frequency(C_Rx_Frequency);
      RF903_Reset_FifoRX();     /* 清洗FIFO */
      RF903_Reset_FifoTX();
      RF903_Status_Receiver();       /* 默认接收状态 */    
}

void    RF903_Send(uchar *txbuf)
{
  RF903_Reset_FifoTX();
  RF903_Fifo_Write(txbuf);
  RF903_Status_Transmit();
  RF903_Status_Receiver();
}

uchar   RF903_Receive(uchar *rxbuf)
{
  if(1==RF903_Fifo_Read(rxbuf))
  {
    RF903_Status_Receiver();
    return 1;
  }
  return 0;
}
main()
{
        RF903_Initialisation();
        while(1)
        {
               RF903_Send(TxRxBuf);
               delay(200);
               delay(200);
        }

}
这两个程序如何嵌套 使903将温度传感器读出的温度发送出去??请求帮助

相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

1

帖子

0

粉丝