本帖最后由 donatello1996 于 2023-2-28 23:26 编辑
简介:使用CH32V307-EVT评估板接上必要传感器外设,监测老人居家的环境温度、大气压、有害气体浓度、心率、血样、血氧、三轴加速度、三轴陀螺仪等指标,其中三轴加速度和三轴陀螺仪是为了监控老人是否发生摔倒的情况,通过400M LORA数字信号发送于老人子女手上的另一块电路板(以下以子女电路板代称),LORA通信距离视乎发射器功率,使老人子女可以通过子女电路板在一公里以内实时监测老人居家的上述指标,同时,CH32V307-EVT评估板还通过板上TTL串口控制一个VHF语音对讲模块,子女电路板上也接入一个相同型号,相同频率的VHF语音对讲模块,VHF在城市内可进行一公里内的无衰减异步语音通信,进行三公里内的衰减异步语音通信,老人和老人子女均可通过VHF语音对讲模块上的PTT按钮进行语音通话,而无需使用网络流量,开机即用,老人子女也可以通过串口方式对VHF语音对讲模块写频。除了上述功能外,老人手上的CH32V307-EVT评估板还带有手势识别模块和RDA5807收音机模块,以手势控制收音机模块的输出音量和自动搜索可用电台,用于老人日常消遣。此系统相比起之前兴起的4G、WIFI、NBIOT物联网智能家居系统,优势在于完全脱离网络流量限制,LORA通信和VHF通信都是高稳定性、超远距离、开机即用无需登录云平台的通信手段,140MHz和400MHz这两个频段使用大功率发射天线,可以满足一公里内的无衰减通信和三公里内的衰减通信,此系统一般用于老人与老人子女同住处,老人子女上班与出租屋距离不远的场合,若是老人与老人子女远于三公里,那就只能使用网络流量的方式通过云平台进行通信了。
硬件说明:
此系统使用到了CH32V307-EVT板上的I2C、SPI、串口、外部中断、AD接口、DA接口等外设,监测老人居家的环境温度和大气压所使用的模块为I2C接口的BMP280模块,该模块可以检测环境温度和大气压。监测有害气体浓度所使用的模块为电压输出和电平输出的MQ135模块,当MQ135模块检测到一氧化碳,甲醛等有害气体浓度大于阈值时,会在D0接口输出下降沿和持续的低电平,CH32V307-EVT评估板可通过外部中断进行监测。监测老人心率和血氧的模块是串口的MAX30102心率血氧采集模块,此模块需要使用手指尖进行检测,厂家后续第二代产品可升级为直接接触式检测,但输出方式不变,仍为串口。监测老人三轴加速度和三轴陀螺仪所使用的模块为I2C接口MPU6050模块。收音机模块为I2C接口的RDA5807模块,需要外接天线和扬声器使用。老人可通过I2C接口的PAJ7620模块进行手势控制,该模块支持八个手势控制,分别为前、后、左、右、上、下、顺时针、逆时针,检测灵敏度尚可,但肯定比不了独立按键直接控制,该模块厂家也在后续升级中。VHF语音对讲模块上集成了麦克风、扬声器和PTT发射按钮,CH32V307-EVT板也是通过串口进行VHF语音对讲模块控制。发射LORA数字信号所使用的模块为SPI接口+外部中断接口安信可RA02模块,主控为SX1278,需要配备发射天线。VHF语音对讲模块和安信可RA02模块都必须成对使用,也就是另外的一个VHF语音对讲模块和安信可RA02模块需要接到另外的电路板上进行无线通信。VHF语音对讲模块和安信可RA02模块的频段是错开的,VHF语音对讲模块可用频段为140-170MHz,安信可RA02模块可用频段为400-470MHz,两个模块一起使用不会造成任何干扰,但安信可RA02模块不可以与UHF模块同时使用(400-470MHz)。由于MPU6050模块对I2C硬件要求过高,所以使用独立的I2C总线,不与BMP280模块、PAJ7620模块、RDA5807模块同时使用。
软件说明:
开发平台为VSCode,编译使用官方编译链riscv64-unknown-elf-toolchain-10.2.0-2020.12.8-x86_64-w64-mingw32,命令行编译:
<div>riscv64-unknown-elf-gcc -g -O3 --specs=nosys.specs --specs=nano.specs -msave-restore -march=rv32imafc -mabi=ilp32 -msmall-data-limit=8 -fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -Wunused -Wuninitialized -nostartfiles -Xlinker -T ./Link/Link1.ld ./Startup/startup_ch32v30x_D8C1.S -I./Bsp -I./Core -I./Driver -I./Startup -I./User -I./Eth -L./Lib ./Startup/syscalls.c ./Startup/system_ch32v30x.c ./Driver/ch32v30x_adc.c ./Driver/ch32v30x_rcc.c ./Driver/ch32v30x_dac.c ./Driver/ch32v30x_misc.c ./Driver/ch32v30x_gpio.c ./Driver/ch32v30x_exti.c ./Driver/ch32v30x_spi.c ./Driver/ch32v30x_dma.c ./Driver/ch32v30x_tim.c ./Driver/ch32v30x_usart.c ./Driver/ch32v30x_i2c.c ./Bsp/debug.c ./Bsp/spi1_spi2.c ./Bsp/i2c.c ./Bsp/uart.c ./Bsp/adc_pa0.c ./Bsp/lora.c ./Bsp/bmp280.c ./Bsp/paj7620.c ./Bsp/mpu6050.c ./User/main.c ./User/ch32v30x_it.c -lm -o main</div><div>
</div><div>riscv64-unknown-elf-objcopy -O binary main main.bin</div>
此系统不带RTOS,完全由裸机实现,CH32V307设置主频为140MHz,初始化所有必要外设后进入主循环,主循环不间断读取AD电压值和手势传感器值,手势传感器值用于控制RDA5807模块调频和调音量,以及VHF语音模块。200个循环读取一次BMP280的电压值、温度值、MPU6050的三轴加速度值和三轴陀螺仪值,250个循环写入一次血氧和心跳的AT请求指令,300个循环读取一次AT返回数值,1000个循环进行一次LORA数字信号发送,10000个循环将有害气体告警字节清零:
Delay_Init();
UART1_Init(115200);
printf("SystemClk:%d\n",SystemCoreClock);
printf("Leader Qiao is niubility.\n");
printf("Brother Long is lihaility.\n");
printf("Master XU is dalaoility.\n");
UART2_Init(9600 , 1);
UART2_Send_String("UART2\n");
ADC_Function_Init();
DAC1_Init();
//W5500_Init();
SPI1_Init();
SPI2_Init();
RA02_GPIO_Init();
RA02_EXTI8_Int_Init();
SX1276_Init(400 , 7);
I2C_Init_PB6_PB7();
BMP280_Init();
PAJ7620_Init();
I2C_Init_PD8_PD9();
MPU6050_Init();
printf("RDA5807P_Intialization = %d\n" , RDA5807P_Intialization());
while(1)
{
// if(mpu_dmp_get_data(&pitch , &roll , &yaw) == 0)
// {
// MPU_Get_Accelerometer(&aacx,&aacy,&aacz);
// MPU_Get_Gyroscope(&gyrox,&gyroy,&gyroz);
// printf("pitch = %d.%-3d roll = %d.%-3d yaw = %d.%-3d\n" ,
// (int)pitch , abs(((int)(pitch * 1000)) % 1000) ,
// (int)roll , abs(((int)(roll * 1000)) % 1000) ,
// (int)yaw , abs(((int)(yaw * 1000)) % 1000)
// );
// }
ret = I2C_Read_Addr_PB6_PB7(PAJ7620_ID , 0x43);
if(ret != 0)
{
switch(ret)
{
case 16:
{
printf("Approach.\n");
break;
}
case 32:
{
printf("Away.\n");
break;
}
case 4:
{
printf("Forward.\n");
break;
}
case 8:
{
printf("Backword.\n");
break;
}
case 2:
{
printf("Left.\n");
break;
}
case 1:
{
printf("Right.\n");
break;
}
case 64:
{
printf("Clockwise.\n");
break;
}
case 128:
{
printf("Anti-clockwise.\n");
break;
}
}
}
if(timer_count % 200 == 0)
{
adc_val = Get_ADC_Val(1);
printf("adc_val = %d\n" , adc_val);
lora_tx_buffer[2] = adc_val >> 8;
lora_tx_buffer[3] = adc_val & 0xff;
lora_tx_buffer[4] = 0;
BMP280_Read_Pressure_Tempature(&pres , &temp);
asl = BMP280PressureToAltitude(&pres);
printf("pres = %d.%-3d , temp = %d.%-3d , asl = %d.%-3d\n" ,
(int)pres , abs(((int)(pres * 1000)) % 1000) ,
(int)temp , abs(((int)(temp * 1000)) % 1000) ,
(int)asl , abs(((int)(asl * 1000)) % 1000)
);
lora_tx_buffer[6] = ((int)pres) >> 8;
lora_tx_buffer[7] = ((int)pres) & 0xff;
lora_tx_buffer[8] = (((int)(pres * 65536)) & 65536) >> 8;
lora_tx_buffer[9] = ((int)(pres * 65536)) & 0xff;
lora_tx_buffer[10] = ((int)temp) >> 8;
lora_tx_buffer[11] = ((int)temp) & 0xff;
aacx = I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_XOUT_H) << 8 | I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_XOUT_H + 1);
aacy = I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_YOUT_H) << 8 | I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_YOUT_H + 1);
aacz = I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_ZOUT_H) << 8 | I2C_Read_Addr_PD8_PD9(0x68 , ACCEL_ZOUT_H + 1);
printf("%d %d %d\n" , aacx , aacy , aacz);
lora_tx_buffer[12] = aacx >> 8;
lora_tx_buffer[13] = aacx & 0xff;
lora_tx_buffer[14] = aacy >> 8;
lora_tx_buffer[15] = aacy & 0xff;
lora_tx_buffer[16] = aacz >> 8;
lora_tx_buffer[17] = aacz & 0xff;
}
if(timer_count % 250 == 0)
{
printf("timer_count = %d\n" , timer_count);
flag_spo2_heart = 1 - flag_spo2_heart;
if(flag_spo2_heart == true)
UART2_Send_String("AT+HEART\r\n");
else
UART2_Send_String("AT+SPO2\r\n");
}
if(timer_count % 300 == 0)
{
printf("spo2 = %d heart = %d\n" , spo2 , heart);
lora_tx_buffer[18] = spo2 >> 8;
lora_tx_buffer[19] = spo2 & 0xff;
lora_tx_buffer[20] = heart >> 8;
lora_tx_buffer[21] = heart & 0xff;
}
// if(rec_start && !rec_stop)
// {
// rec_adc_val[rec_count++] = Get_ADC_Val(0);
// printf("%d\n" , rec_adc_val[rec_count - 1]);
// DAC_SetChannel1Data(DAC_Align_12b_R, rec_adc_val[rec_count - 1]);
// }
// else if(!rec_start && rec_stop)
// {
// rec_stop = false;
// printf("rec_count = %d\n" , rec_count);
// for(i = 0 ; i < rec_count ; i ++)
// {
// DAC_SetChannel1Data(DAC_Align_12b_R, rec_adc_val[i]);
// }
// rec_count = 0;
// }
if(timer_count % 1000 == 0)
{
printf("timer_count = %d\n" , timer_count);
Lora_TxData(lora_tx_buffer , 50);
}
if(timer_count % 10000 == 0)
{
lora_tx_buffer[4] = 0;
}
timer_count ++;
if(timer_count >= 1000000000)
timer_count = 0;
}
BMP280模块初始化:
uint8_t BMP280_Init(void)
{
uint8_t bmp280_id;
uint8_t tmp[1];
I2C_Read_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS, BMP280_CHIPID_REG, 1, &bmp280_id);
printf("bmp280_id = 0x%x\n" , bmp280_id);
I2C_Read_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS, BMP280_DIG_T1_LSB_REG , 24 , (u8 *)&bmp280Cal);
//printf("%d %d %d %d %d %d %d %d %d %d %d %d\n" ,
// bmp280Cal.dig_T1 , bmp280Cal.dig_T2 , bmp280Cal.dig_T3 ,
// bmp280Cal.dig_P1 , bmp280Cal.dig_P2 , bmp280Cal.dig_P3 ,
// bmp280Cal.dig_P4 , bmp280Cal.dig_P5 , bmp280Cal.dig_P6 ,
// bmp280Cal.dig_P7 , bmp280Cal.dig_P8 , bmp280Cal.dig_P9);
tmp[0] = BMP280_MODE;
I2C_Write_Reg_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS , BMP280_CTRLMEAS_REG , 1 , tmp);
tmp[0] = 5 << 2;
I2C_Write_Reg_Datas_PB6_PB7(BMP280_SLAVE_ADDRESS , BMP280_CONFIG_REG , 1 , tmp);
return bmp280_id;
}
PAJ7620初始化:
void PAJ7620_Init()
{
int i;
I2C_Write_Reg_Data_PB6_PB7(PAJ7620_ID , PAJ7620_REGITER_BANK_SEL , PAJ7620_BANK0);
I2C_Write_Reg_Data_PB6_PB7(PAJ7620_ID , PAJ7620_REGITER_BANK_SEL , PAJ7620_BANK0);
printf("PAJ7620 Init 0 = 0x%x\n" , I2C_Read_Addr_PB6_PB7(PAJ7620_ID , 0));
printf("PAJ7620 Init 1 = 0x%x\n" , I2C_Read_Addr_PB6_PB7(PAJ7620_ID , 1));
for(i = 0 ; i < INIT_REG_ARRAY_SIZE ; i ++)
I2C_Write_Reg_Data_PB6_PB7(PAJ7620_ID , initRegisterArray[i][0] , initRegisterArray[i][1]);
I2C_Write_Reg_Data_PB6_PB7(PAJ7620_ID , PAJ7620_REGITER_BANK_SEL , PAJ7620_BANK0);
}
MPU6050初始化:
void MPU6050_Init()
{
int ret;
ret = I2C_Read_Addr_PD8_PD9(0x68 , 0x75);
printf("MPU6050_Init = 0x%x\n" , ret);
I2C_Write_Reg_Data_PD8_PD9(0x68 , SMPLRT_DIV , 7);
//Write to sample rate register
printf("SMPLRT_DIV = 0x%x\n" , I2C_Read_Addr_PD8_PD9(0x68 , SMPLRT_DIV));
I2C_Write_Reg_Data_PD8_PD9(0x68 , PWR_MGMT_1 , 1);
// Write to power management register
printf("PWR_MGMT_1 = 0x%x\n" , I2C_Read_Addr_PD8_PD9(0x68 , PWR_MGMT_1));
I2C_Write_Reg_Data_PD8_PD9(0x68 , CONFIG , 0);
// Write to Configuration register
printf("CONFIG = 0x%x\n" , I2C_Read_Addr_PD8_PD9(0x68 , CONFIG));
I2C_Write_Reg_Data_PD8_PD9 (0x68 , GYRO_CONFIG , 24);
// Write to Gyro Configuration register
printf("GYRO_CONFIG = 0x%x\n" , I2C_Read_Addr_PD8_PD9(0x68 , GYRO_CONFIG));
I2C_Write_Reg_Data_PD8_PD9 (0x68 , INT_ENABLE , 1);
//Write to interrupt enable register
printf("INT_ENABLE = 0x%x\n" , I2C_Read_Addr_PD8_PD9(0x68 , INT_ENABLE));
}
RDA5807初始化:
bool RDA5807P_Intialization(void)
{
uint8_t error_ind = 0;
uint8_t RDA5807P_REGR[10]={0x0};
uint8_t i = 0;
RDA5807P_REGW[0] = 0x00;
RDA5807P_REGW[1] = 0x02;
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807P_REGW[0], 2);
Delay_ms(50);
error_ind = OperationRDAFM_2w(READ, (uint8_t *)&RDA5807P_REGR[0], 10);
Delay_ms(50);
gChipID = RDA5807P_REGR[8];
gChipID = ((gChipID<<8) | RDA5807P_REGR[9]);
printf("gChipID = %d\n" , gChipID);
if (gChipID == 0x5808) //RDA5807N
{
for (i=0;i<8;i++)
RDA5807P_REGW[i] = RDA5807N_initialization_reg[i];
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807N_initialization_reg[0], 2);
Delay_ms(600);
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807N_initialization_reg[0], sizeof(RDA5807N_initialization_reg));
}
else if (gChipID == 0x5804) //RDA5807PE,RDA5807SP
{
for (i=0;i<8;i++)
RDA5807P_REGW[i] = RDA5807PE_initialization_reg[i];
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807PE_initialization_reg[0], 2);
Delay_ms(600);
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807PE_initialization_reg[0], sizeof(RDA5807PE_initialization_reg));
}
else if (gChipID == 0x5801) //RDA5807H,RDA5807HP
{
for (i=0;i<8;i++)
RDA5807P_REGW[i] = RDA5807PH_initialization_reg[i];
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807PH_initialization_reg[0], 2);
Delay_ms(600);
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807PH_initialization_reg[0], sizeof(RDA5807PH_initialization_reg));
Delay_ms(100);
do
{
i++;
if(i>10)
return 0;
RDA5807P_SetFreq(8750);
Delay_ms(10);
//read REG0A&0B
OperationRDAFM_2w(READ,&(RDA5807P_REGR[0]), 4);
if((RDA5807P_REGR[3]&0x80)==0)
{
RDA5807P_REGW[1] &= 0xFE;
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807P_REGW[0], 2);
Delay_ms(50);
RDA5807P_REGW[1] |= 0x01;
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807P_REGW[0], 2);
Delay_ms(50);
}
}while((RDA5807P_REGR[3]&0x80)==0);
}
else
{
gChipID = RDA5807P_REGR[4];
gChipID = ((gChipID<<8) | RDA5807P_REGR[5]);
}
if ((gChipID == 0x5802) || (gChipID == 0x5803))
{
gChipID = 0x5802;
for (i=0;i<8;i++)
RDA5807P_REGW[i] = RDA5807P_initialization_reg[i];
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807P_initialization_reg[0], 2);
Delay_ms(600);
error_ind = OperationRDAFM_2w(WRITE, (uint8_t *)&RDA5807P_initialization_reg[0], sizeof(RDA5807P_initialization_reg));
}
Delay_ms(50);
if (error_ind )
return 0;
else
return 1;
}
读取血氧和心跳值使用串口空闲中断:
__attribute__((interrupt(), weak)) void USART2_IRQHandler(void)
{
int i = 0;
if(USART_GetITStatus(USART2 , USART_IT_IDLE) != RESET)
{
USART_ReceiveData(USART2);
DMA_Cmd(DMA1_Channel6 , DISABLE);
com2_rx_count = COM_RX_BUFFER_SIZE - DMA_GetCurrDataCounter(DMA1_Channel6);
DMA1_Channel6->CNTR = COM_RX_BUFFER_SIZE;
DMA_Cmd(DMA1_Channel6 , ENABLE);
for(i = 0 ; i < com2_rx_count ; i ++)
{
if(com2_rx_buffer[i] == '+' && com2_rx_buffer[i + 1] == 'S' && com2_rx_buffer[i + 2] == 'P'
&& com2_rx_buffer[i + 3] == 'O' && com2_rx_buffer[i + 4] == '2' && com2_rx_buffer[i + 5] == '='
&& com2_rx_buffer[i + 6] != 'N'
)
{
spo2 = (com2_rx_buffer[i + 6] - '0') * 10 + com2_rx_buffer[i + 7] - '0';
}
if(com2_rx_buffer[i] == '+' && com2_rx_buffer[i + 1] == 'H' && com2_rx_buffer[i + 2] == 'E'
&& com2_rx_buffer[i + 3] == 'A' && com2_rx_buffer[i + 4] == 'R' && com2_rx_buffer[i + 5] == 'T'
&& com2_rx_buffer[i + 6] == '=' && com2_rx_buffer[i + 7] != 'N'
)
{
heart = com2_rx_buffer[i + 7] - '0';
if('0' <= com2_rx_buffer[i + 8] && com2_rx_buffer[i + 8] <= '9')
{
heart = (com2_rx_buffer[i + 7] - '0') * 10 + com2_rx_buffer[i + 8] - '0';
}
if('0' <= com2_rx_buffer[i + 9] && com2_rx_buffer[i + 9] <= '9')
{
heart = (com2_rx_buffer[i + 7] - '0') * 100 + (com2_rx_buffer[i + 8] - '0') * 10 + com2_rx_buffer[i + 9] - '0';
}
}
}
}
}
SX1276初始化:
SX1276初始化:
void SPI1_Init()
{
GPIO_InitTypeDef GPIO_InitStructure = {0};
SPI_InitTypeDef SPI_InitStructure = {0};
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE );
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
uint8_t SPI1_ReadWriteByte(uint8_t data)
{
uint8_t retry = 0;
while ((SPI1->STATR & SPI_I2S_FLAG_TXE) == RESET)
{
retry ++;
if(retry > 200)return 0;
}
SPI1->DATAR = data;
retry=0;
while ((SPI1->STATR & SPI_I2S_FLAG_RXNE) == RESET)
{
retry ++;
if(retry > 200)return 0;
}
return SPI1->DATAR;
}
void SX1276_Init(uint32_t fre , uint8_t bw)
{
xLORA.InitOK = 0;
LoRaSettings.RFFrequency = fre * 1000 * 1000;
LoRaSettings.SpreadingFactor = 11;
LoRaSettings.SignalBw = bw;
SX1276_Reset();
SX1276Read(0x06, ®Temp ,1);
while(regTemp != 0x6C)
{
printf("SX1276_Init error.\n");
Delay_ms(300);
}
SX1276_SetOpMode(RFLR_OPMODE_SLEEP);
SX1276Read(0x01, ®Temp , 1);
printf("0x01 regTemp = 0x%x\n" , regTemp);
regTemp |= 0x80 ;
SX1276Write( 0x01, ®Temp , 1);
SX1276Read (0x01, ®Temp , 1);
printf("0x01 regTemp = 0x%x\n" , regTemp);
regTemp = RFLR_LNA_GAIN_G1;
SX1276Write ( REG_LR_LNA , ®Temp , 1);
SX1276LoRaSetRFFrequency(LoRaSettings.RFFrequency);
SX1276LoRaSetSpreadingFactor(LoRaSettings.SpreadingFactor);
SX1276LoRaSetErrorCoding(LoRaSettings.ErrorCoding);
SX1276LoRaSetPacketCrcOn(LoRaSettings.CrcOn);
SX1276LoRaSetSignalBandwidth(LoRaSettings.SignalBw);
SX1276LoRaSetImplicitHeaderOn(LoRaSettings.ImplicitHeaderOn );
SX1276LoRaSetSymbTimeout(0x3FF);
SX1276Write( 0x22, &LoRaSettings.PayloadLength , 1 );
SX1276LoRaSetLowDatarateOptimize( true );
SX1276LoRaSetPAOutput( RFLR_PACONFIG_PASELECT_PABOOST );
regTemp = 0x87;
SX1276Write(REG_LR_PADAC , ®Temp , 1);
regTemp = 0x8F;
SX1276Write(REG_LR_PACONFIG , ®Temp , 1);
//SX1276Read (0x26, ®Temp , 1);
//regTemp = (regTemp & 0xF7) | ( 1<<3);
//SX1276Write (0x26, ®Temp, 1);
regTemp = 0xFF;
SX1276Write(REG_LR_IRQFLAGS ,®Temp, 1);
xLORA.InitOK = 1;
Lora_RxMode(0);
printf("SX1276_Init OK.\n");
}
SX1276使用外部中断进行发送缓冲区清零:
__attribute__((interrupt(), weak)) void LORA_DIO0_HANDLER(void)
{
EXTI_ClearITPendingBit(LORA_DIO0_PIN);
printf("LORA_DIO0_HANDLER xLORA.TxMode = %d xLORA.RxMode = %d\n" , xLORA.TxMode , xLORA.RxMode);
if(xLORA.TxMode == 1)
{
printf("LORA_DIO0_HANDLER xLORA.TxMode = 1\n");
regTemp = 0xFF;
SX1276Write( 0x12, ®Temp , 1);
xLORA.TxMode = 0;
Lora_RxMode(0);
xLORA .RxMode = 1;
return;
}
if(xLORA.RxMode == 1)
{
regTemp = 0xFF;
SX1276Write( 0x12, ®Temp ,1 );
if( LoRaSettings.ImplicitHeaderOn == true )
RxSize = LoRaSettings.PayloadLength ;
else
SX1276Read( REG_LR_NBRXBYTES, &RxSize, 1 );
SX1276Read( REG_LR_FIFORXCURRENTADDR, ®Temp ,1);
SX1276Write( REG_LR_FIFOADDRPTR, ®Temp ,1 );
SX1276Read(0, RxBuffer, RxSize );
printf("RxSize %d RxBuffer: %s\r" , RxSize , RxBuffer);
}
}
|
|