- 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);
- }
- }