STM32开发SI4432
单片机源程序如下(发送):
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "spi.h"
- // 0321 注意1:SPI 空闲时为低电平,第二跳边沿生效 //注意2:SDN直接接DGND算了
- // 注意3:SI4432和STM32要用同一个VCC3.3否则的话收到全为0
- #define SI4432_NSEL PAout(4) //SI4432_NSEL 使能
- #define SI4432_SDN PCout(4) //SI4432_SDN 使能
- #define SI4432_NIRQ PCin(5) //SI4432_NIRQ 中断状态
- void SI4432_Init(void) ;
- u8 SI4432_ReadReg(u8 addr) ;
- void SI4432_WriteReg(u8 addr, u8 value) ;
- u8 i;
- u8 SI4432_RxBUFF[100];
- u8 SI4432_RxCount,SI4432_RxLenth;
- u8 ItStatus1,ItStatus2,res;
- int main(void)
- {
- delay_init(); //延时函数初始化
- NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
- uart_init(9600); //串口初始化为9600
- LED_Init(); //LED端口初始化
- SPI1_Init();
- GPIO_SET();
- SI4432_NSEL=0;
- delay_ms(600);
- res = SI4432_ReadReg(0x00);
- delay_ms(20); printf("res =%02x \n",(unsigned short)res );
- res = SI4432_ReadReg(0x01);
- delay_ms(20); printf("res =%02x \n",(unsigned short)res );
- res = SI4432_ReadReg(0x02);
- delay_ms(20); printf("res =%02x \n",(unsigned short)res );
- delay_ms(1000);
- //读取中断状态 清除中断 释放 SI4432_NIRQ
- ItStatus1 = SI4432_ReadReg(0x03);
- ItStatus2 = SI4432_ReadReg(0x04);
- //软件复位
- SI4432_WriteReg(0x07, 0x80); //向0X07地址 写入0X80 软件复位
- while ( SI4432_NIRQ == 1);
- //读取中断状态 清除中断 释放 SI4432_NIRQ
- ItStatus1 = SI4432_ReadReg(0x03);
- ItStatus2 = SI4432_ReadReg(0x04);
- /////////////////////////开始设置
- // 频率设置 434
- SI4432_WriteReg(0x75, 0x53);
- SI4432_WriteReg(0x76, 0x64); //
- SI4432_WriteReg(0x77, 0x00);
- // 1.2K bps 发射速率
- SI4432_WriteReg(0x2a, 0x14);
- SI4432_WriteReg(0x6e, 0x09);
- SI4432_WriteReg(0x6f, 0xd5);
- SI4432_WriteReg(0x70, 0x2c);//
- //SpiWriteRegister(0x72, 0x48); //(9.6kbps)
- SI4432_WriteReg(0x72, 0x38); //频率偏差(1.2kbps)
- // 下面的设置根据Silabs 的Excel (9.6 kbps, deviation: 45 kHz, channel filter BW: 102.2 kHz
- SI4432_WriteReg(0x1C, 0x1b); //write 0x1E to the IF Filter Bandwidth register
- SI4432_WriteReg(0x20, 0x83); //write 0xD0 to the Clock Recovery Oversampling Ratio register
- SI4432_WriteReg(0x21, 0xc0); //write 0x00 to the Clock Recovery Offset 2 register
- SI4432_WriteReg(0x22, 0x13); //write 0x9D to the Clock Recovery Offset 1 register
- SI4432_WriteReg(0x23, 0xa9); //write 0x49 to the Clock Recovery Offset 0 register
- SI4432_WriteReg(0x24, 0x00); //write 0x00 to the Clock Recovery Timing Loop Gain 1 register
- SI4432_WriteReg(0x25, 0x03); //write 0x24 to the Clock Recovery Timing Loop Gain 0 register
- SI4432_WriteReg(0x1D, 0x40); //write 0x40 to the AFC Loop Gearshift Override register
- SI4432_WriteReg(0x1E, 0x0A); //write 0x0A to the AFC Timing Control register
- SI4432_WriteReg(0x2A, 0x14); //write 0x20 to the AFC Limiter register
- //前导码 同步字
- SI4432_WriteReg(0x34, 0X0A); // 发射5字节的Preamble
- SI4432_WriteReg(0x35, 0x2A); // 需要检测 Preamble
- SI4432_WriteReg(0x33, 0x02);// 同步字3,2 是同步字
- SI4432_WriteReg(0x36, 0x2d); // 同步字为 0x2dd4
- SI4432_WriteReg(0x37, 0xd4);
- SI4432_WriteReg(0x30, 0x8D); // 使能PH+ FIFO模式,高位在前面,使能CRC校验 CCITTT
- SI4432_WriteReg(0x32, 0x00 );// 禁止帧头
- SI4432_WriteReg(0x71, 0x63); // 发射不需要 CLK,FiFo , FSK模式
- //GPIO
- SI4432_WriteReg(0x0b, 0x12); //
- SI4432_WriteReg(0x0c, 0x15); //
- //其他设置
- SI4432_WriteReg(0x09, 0xD7); // 负载电容
- SI4432_WriteReg(0x69, 0x60); //AGC过载
- //发射功率
- SI4432_WriteReg(0x6d, 0x1e);
- //手动打开接收
- SI4432_WriteReg(0x07, 0x05);
- //打开 接收中断
- SI4432_WriteReg(0x05, 0x03);
- SI4432_WriteReg(0x06, 0x00);
- //清中断
- ItStatus1 = SI4432_ReadReg(0x03); //read the Interrupt Status1 register
- ItStatus2 = SI4432_ReadReg(0x04); //read the Interrupt Status2 register
- //接收设置
- SI4432_WriteReg(0x08, 0x02);
- SI4432_WriteReg(0x08, 0x00);
- SI4432_WriteReg(0x07, 0x05); //手动打开接收
- /////////////////////////////////
- while(1)
- {
- if( SI4432_NIRQ == 0 ) //如果发生中断
- {
- delay_ms(50);
- ItStatus1 = SI4432_ReadReg(0x03); //read the Interrupt Status1 register
- ItStatus2 = SI4432_ReadReg(0x04); //read the Interrupt Status2 register
-
- if ( (ItStatus1 & 0x02) == 0x02 )
- {
- SI4432_RxLenth = SI4432_ReadReg(0x4B);
- for (SI4432_RxCount=0;SI4432_RxCount < SI4432_RxLenth;SI4432_RxCount++)
- {
- SI4432_RxBUFF[SI4432_RxCount] = SI4432_ReadReg(0x7F);
- }
- SI4432_RxCount=0;
- SI4432_WriteReg(0x08, 0x02);
- SI4432_WriteReg(0x08, 0x00);
- SI4432_WriteReg(0x07, 0x05); //手动打开接?
- }
- }
- res = SI4432_ReadReg(0x02); delay_ms(50);
- delay_ms(20); printf("res =%02x \n",(unsigned short)res );
- }
- }
- // 读取寄存器
- u8 SI4432_ReadReg(u8 addr) //读取寄存器的值
- {
- uint8_t temp=0;
-
- SI4432_NSEL=0;
- SPI1_ReadWriteByte(addr); //发送读取寄存器地址
- temp=SPI1_ReadWriteByte(0Xff);
- SI4432_NSEL=1;
-
- return temp;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
si4432.rar
(546.01 KB, 下载次数: 131)
|