要把外部中断先关掉,在初始化之后再打开。在gpio.c里面注释掉:
上拉,下跳沿检测
/*Configure GPIO pins : PB0 PB1 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* EXTI interrupt init*/
HAL_NVIC_SetPriority(EXTI0_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI1_IRQn, 0, 0);
//HAL_NVIC_EnableIRQ(EXTI1_IRQn);
接收端主函数:
CC1101Reset(&hspi1);
CC1101WORInit(&hspi1);
CC1101Init(&hspi1);
CC1101WriteCmd( CC1101_SRX ,&hspi1);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
while (1)
{
if (rx[0] %2 == 1)
{
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
}
else {HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);}
}
发送端主函数:
CC1101Reset(&hspi1);
CC1101WORInit(&hspi1);
CC1101Init(&hspi1);
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_Delay(1000);
tx[0] = tx[0]+1;
CC1101SendPacket(tx , sizeof(tx), BROADCAST, &hspi1);
if (flag==0){
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
flag=1;
}
else {
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
flag=0;
}
}
cc1101.c:
#include "cc1101.h"
#include "spi.h"
uint8_t Rx_Flag;
static const uint8_t CC1101InitData[30][2]=
{
/* {CC1101_IOCFG0, 0x06},
{CC1101_FIFOTHR, 0x47},
{CC1101_PKTCTRL0, 0x05},
{CC1101_CHANNR, 0x00},
{CC1101_FSCTRL1, 0x08},
{CC1101_FREQ2, 0x10},//433.92
{CC1101_FREQ1, 0xB0},
{CC1101_FREQ0, 0x3f},
// {CC1101_FREQ2, 0x10},//433.862
// {CC1101_FREQ1, 0xAf},
// {CC1101_FREQ0, 0xdf},
{CC1101_MDMCFG4, 0x5B},
{CC1101_MDMCFG3, 0xF8},
{CC1101_MDMCFG2, 0x03},
{CC1101_DEVIATN, 0x47},
{CC1101_MCSM0, 0x18},
{CC1101_FOCCFG, 0x1D},
{CC1101_WORCTRL, 0xFB},
{CC1101_FSCAL3, 0xEA},
{CC1101_FSCAL2, 0x2A},
{CC1101_FSCAL1, 0x00},
{CC1101_FSCAL0, 0x11},
{CC1101_TEST2, 0x81},
{CC1101_TEST1, 0x35},
{CC1101_MCSM1, 0x3B},*/
//2-FSK
{CC1101_MCSM1, 0x3c},
{CC1101_IOCFG2, 0x06},
{CC1101_IOCFG0, 0x06},
{CC1101_FIFOTHR, 0x47},
{CC1101_PKTCTRL0, 0x05},
{CC1101_CHANNR, 0x01},
{CC1101_FSCTRL1, 0x06},
{CC1101_FREQ2, 0x10}, //433.92
{CC1101_FREQ1, 0xB0},
{CC1101_FREQ0, 0x3f},
{CC1101_MDMCFG4, 0xF6},
{CC1101_MDMCFG3, 0x43}, //2.00224kBaud 58.035714khz
{CC1101_MDMCFG2, 0x03},
{CC1101_MDMCFG1, 0x20},
{CC1101_MDMCFG0, 0x00},
{CC1101_DEVIATN, 0x15}, //0x15:5.157471khz 0x34:19khz, 0x77:380.85khz,0x64:152khz,0x71:228.5khz
{CC1101_MCSM0, 0x18},
{CC1101_FOCCFG, 0x16},
{CC1101_WORCTRL, 0xFB},
{CC1101_FSCAL3, 0xE9},
{CC1101_FSCAL2, 0x2A},
{CC1101_FSCAL1, 0x00},
{CC1101_FSCAL0, 0x1F},
{CC1101_TEST2, 0x81},
{CC1101_TEST1, 0x35},
{CC1101_TEST0, 0x09},
};
uint8_t PaTabel[] = { 0xc0, 0xC8, 0x84, 0x60, 0x68, 0x34, 0x1D, 0x0E};//433M?table
//uint8_t PaTabel[] = { 0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
/*read a byte from the specified register*/
uint8_t CC1101ReadStatus( uint8_t addr , SPI_HandleTypeDef *hspi);
/*Read some bytes from the rigisters continously*/
void CC1101ReadMultiReg( uint8_t addr, uint8_t *buff, uint8_t size , SPI_HandleTypeDef *hspi);
/*Write a byte to the specified register*/
void CC1101WriteReg( uint8_t addr, uint8_t value , SPI_HandleTypeDef *hspi);
/*Flush the TX buffer of CC1101*/
void CC1101ClrTXBuff( SPI_HandleTypeDef *hspi );
/*Flush the RX buffer of CC1101*/
void CC1101ClrRXBuff( SPI_HandleTypeDef *hspi );
/*Get received count of CC1101*/
uint8_t CC1101GetRXCnt( SPI_HandleTypeDef *hspi );
/*Reset the CC1101 device*/
void CC1101Reset( SPI_HandleTypeDef *hspi );
/*Write some bytes to the specified register*/
void CC1101WriteMultiReg( uint8_t addr, uint8_t *buff, uint8_t size , SPI_HandleTypeDef *hspi);
void CC1101WriteCmd( uint8_t command , SPI_HandleTypeDef *hspi);
/*
================================================================================
Function : CC1101WORInit( )
Initialize the WOR function of CC1101
INPUT : None
OUTPUT : None
================================================================================
*/
void CC1101WORInit(SPI_HandleTypeDef *hspi )
{
CC1101WriteReg(CC1101_MCSM0,0x18,hspi);
CC1101WriteReg(CC1101_WORCTRL,0x78,hspi); //Wake On Radio Control
CC1101WriteReg(CC1101_MCSM2,0x00,hspi);
CC1101WriteReg(CC1101_WOREVT1,0x8C,hspi);
CC1101WriteReg(CC1101_WOREVT0,0xA0,hspi);
CC1101WriteCmd( CC1101_SWORRST ,hspi);
}
/*
================================================================================
Function : CC1101ReadReg( )
read a byte from the specified register
INPUT : addr, The address of the register
OUTPUT : the byte read from the rigister
================================================================================
*/
uint8_t CC1101ReadReg( uint8_t addr , SPI_HandleTypeDef *hspi)
{
uint8_t input[1];
uint8_t output[1];
output[0] = addr | READ_SINGLE; //¶Á¼Ä´æÆ÷ÃüÁî
CSN_reset;
while (MISO);
HAL_SPI_TransmitReceive(hspi, output, input, 1, 2000);
HAL_SPI_Receive(hspi, input, 1, 2000);
CSN_set;
return input[0];
}
/*
================================================================================
Function : CC1101ReadMultiReg( )
Read some bytes from the rigisters continously
INPUT : addr, The address of the register