//char code dx516[3] _at_ 0x003b;//注意在烧写到IC里,要把这句话去掉 void main(void) { initM51CtrlRegs(); ChangToFastMode(); PowerOn_sub(); usbBusReset(); ET1 = 0; //disable timer0 intrrupt TR1 = 0; //timer 0 stop gfFastModeflag = 1; // ToDetectFlashValid(); SET_VIO_50; IIC_Init(); // re = 1; // re = 0; // re = 1; // re = 0; SET_GPIO_PC7; REG1630_PB_GPIO_CON_AB = 0xff; REG1630_PB_GPIO_9B = 0xff; //CLR_PC3; while(REG1630_USB_PLUG_OUT & VDD5V_EXIST_FLG) { PollingUsbCommand(); } }
void board_init(void) { /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); timer2_init(); timer14_init(); Usart_Init(USART2,9600); send_data(USART2, "uart2 is OK!\r\n", strlen("uart2 is OK!\r\n")); //不同的传感器的引脚及用到的板级外设不同 //sensor_init(); #if defined (BEEP) //蜂鸣器用作调试设备 beep_init(); Delay(20); BEEP_OFF(); #endif #if defined (DEBUG) //串口用作调试端口 Usart_Init(DEBUG_UART,115200); send_data(DEBUG_UART, "uart is OK!\r\n", strlen("uart is OK!\r\n")); #endif #if defined (EEPROM) //24cxx IIC接口初始化 IIC_Init(); #endif }
/******************************************** 飞控主函数入口 功能: 1.初始化各个硬件 2.初始化系统参数 3.开定时器4等待数据中断到来 4.开定时器3串口广播实时姿态以及相关信息 ********************************************/ int main(void) { //int i; SystemClock_HSE(9); //系统时钟初始化,时钟源外部晶振HSE //SystemClock_HSI(4); //系统时钟初始化,时钟源内部HSI UART1_init(SysClock,115200); //串口1初始化 NVIC_INIT(); //中断初始化 STMFLASH_Unlock(); //内部flash解锁 LedInit(); //IO初始化 delay_init(SysClock); //滴答延时初始化 BT_PowerInit(); //蓝牙电源初始化完成,默认关闭 MotorInit(); //马达初始化 BatteryCheckInit(); //电池电压监测初始化 IIC_Init(); //IIC初始化 MPU6050_DMP_Initialize(); //初始化DMP引擎 //HMC5883L_SetUp(); //初始化磁力计HMC5883L PID_INIT(); //PID参数初始化 ParameterRead(); //Flash参数读取 NRF24L01_INIT(); //NRF24L01初始化 SetRX_Mode(); //设无线模块为接收模式 PowerOn(); //开机等待 BT_on(); //蓝牙开 TIM3_Init(SysClock,10); //定时器3初始化,调试串口输出 TIM4_Init(SysClock,50); //定时器4初始化,定时采样传感器数据,更新PID输出 while (1); //等待数据更新中断到来 }
/** * @brief: 触摸屏初始化 * @retval:0 - 没有进行校准 * 1 - 进行过校准 * @note: * - T_SCK -> PB1 * - T_CS -> PB2 * - T_MISO -> PF8 * - T_MOSI -> PF9 * - T_PEN -> PF10 */ u8 TP_Init(void) { RCC->APB2ENR|=1<<3; //PB时钟使能 RCC->APB2ENR|=1<<7; //PF时钟使能 GPIOB->CRL&=0XFFFFF00F; //PB1 PB2 GPIOB->CRL|=0X00000330; GPIOB->ODR|=3<<1; //PB1 PB2 推挽输出 GPIOF->CRH&=0XFFFFF000; GPIOF->CRH|=0X00000838; //PF8-上拉输入 PF9-推挽输出 PF10-上拉输入 GPIOF->ODR|=7<<8; //PF8 PF9 PF10 全部上拉 TP_Read_XY(&tp_dev.x,&tp_dev.y);//第一次读取初始化 IIC_Init(); //初始化IIC while(AT24CXX_Check()) //检测不到24c02 { LCD_ShowString(16,150,200,16,16,"24C02 Check Failed!"); delay_ms(500); LCD_ShowString(16,150,200,16,16,"Please Check! "); delay_ms(500); } if(TP_Get_Adjdata())return 0; //已经校准 else //未校准 { LCD_Clear(WHITE); //清屏 TP_Adjust(); //屏幕校准 TP_Save_Adjdata(); } TP_Get_Adjdata(); return 1; }
void HAL_I2C_soft_Configuration(void) { #ifdef USE_I2C_OLD I2C_soft_Configuration(); #else IIC_Init(); #endif }
/* MainInit routine This routine performs the main initialisation. */ void MainInit(void) { PortInit(); Timer_Init(); IIC_Init(); UART_Init(); sei(); // Enable global interrupts WM8960_Init(); WM8960_Init();//again }
void CAT9555_Init(void) { IIC_Init(); CAT9555Mirror0=0xFFFF; CAT9555Mirror1=0xFFFF; CAT9555_WriteCFG(CAT9555_ADDR0,0x00,0x00); CAT9555_WriteCFG(CAT9555_ADDR1,0x00,0x00); CAT9555_Write_OneByte(CAT9555_ADDR0,0xff,0xff); CAT9555_Write_OneByte(CAT9555_ADDR1,0xff,0xff); }
void MPU_Init() { // delay_ms(500); //上电后需要有一定的延时 IIC_Init(); //IIC引脚初始化 MPU_WriteByte(PWR_MGMT_1, 0x00); //解除休眠状态 MPU_WriteByte(SMPLRT_DIV, 0x07); MPU_WriteByte(MPU_CONFIG, 0x06); MPU_WriteByte(GYRO_CONFIG, 0x18); MPU_WriteByte(ACCEL_CONFIG, 0x01); }
//初始化ADXL345. //返回值:0,初始化成功;1,初始化失败. u8 ADXL345_Init(void) { IIC_Init(); //初始化IIC总线 if(ADXL345_RD_Reg(DEVICE_ID)==0XE5) //读取器件ID { ADXL345_WR_Reg(DATA_FORMAT,0X2B); //低电平中断输出,13位全分辨率,输出数据右对齐,16g量程 ADXL345_WR_Reg(BW_RATE,0x0A); //数据输出速度为100Hz ADXL345_WR_Reg(POWER_CTL,0x28); //链接使能,测量模式 ADXL345_WR_Reg(INT_ENABLE,0x00); //不使用中断 ADXL345_WR_Reg(OFSX,0x00); ADXL345_WR_Reg(OFSY,0x00); ADXL345_WR_Reg(OFSZ,0x00); return 0; } return 1; }
//WM8978初始化 //返回值:0,初始化正常 // 其他,错误代码 u8 WM8978_Init(void) { u8 res; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB|RCC_AHB1Periph_GPIOC, ENABLE); //使能外设GPIOB,GPIOC时钟 //PB9/10/14/15 复用功能输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_14 | GPIO_Pin_15; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 //PC2/PC3/PC6复用功能输出 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能 GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化 GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_SPI2); //PB9,AF5 I2S_LRCK GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_SPI2); //PB10,AF5 I2S_SCLK GPIO_PinAFConfig(GPIOB,GPIO_PinSource15,GPIO_AF_SPI2); //PB15 ,AF5 I2S_DACDATA GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_SPI2); //PC6 ,AF5 I2S_MCK GPIO_PinAFConfig(GPIOB,GPIO_PinSource14,GPIO_AF6_SPI2); //PB14 ,AF6 I2S_ADCDATA I2S2ext_SD是AF6!!! IIC_Init();//初始化IIC接口 res=WM8978_Write_Reg(0,0); //软复位WM8978 if(res)return 1; //发送指令失败,WM8978异常 //以下为通用设置 WM8978_Write_Reg(1,0X1B); //R1,MICEN设置为1(MIC使能),BIASEN设置为1(模拟器工作),VMIDSEL[1:0]设置为:11(5K) WM8978_Write_Reg(2,0X1B0); //R2,ROUT1,LOUT1输出使能(耳机可以工作),BOOSTENR,BOOSTENL使能 WM8978_Write_Reg(3,0X6C); //R3,LOUT2,ROUT2输出使能(喇叭工作),RMIX,LMIX使能 WM8978_Write_Reg(6,0); //R6,MCLK由外部提供 WM8978_Write_Reg(43,1<<4); //R43,INVROUT2反向,驱动喇叭 WM8978_Write_Reg(47,1<<8); //R47设置,PGABOOSTL,左通道MIC获得20倍增益 WM8978_Write_Reg(48,1<<8); //R48设置,PGABOOSTR,右通道MIC获得20倍增益 WM8978_Write_Reg(49,1<<1); //R49,TSDEN,开启过热保护 WM8978_Write_Reg(10,1<<3); //R10,SOFTMUTE关闭,128x采样,最佳SNR WM8978_Write_Reg(14,1<<3); //R14,ADC 128x采样率 return 0; }
/******************************************** 飞控主函数入口 功能: 1.初始化各个硬件 2.初始化系统参数 3.开定时器4等待数据中断到来 4.开定时器3串口广播实时姿态以及相关信息 ********************************************/ int main(void) { SystemClock(9); //系统时钟初始化 UART1_init(SysClock,115200); //串口1初始化 NVIC_INIT(); //中断初始化 STMFLASH_Unlock(); //内部flash解锁 LedInit(); //IO初始化 delay_init(SysClock); //滴答延时初始化 BT_PowerInit(); //蓝牙电源初始化完成,默认关闭 MotorInit(); //马达初始化 BatteryCheckInit(); //电池电压监测初始化 IIC_Init(); //IIC初始化 MPU6050_DMP_Initialize(); //初始化DMP引擎 PID_INIT(); //PID参数初始化 ParameterRead(); //Flash参数读取 NRF24L01_INIT(); //NRF24L01初始化 SetRX_Mode(); //设无线模块为接收模式 PowerOn(); //开机等待 BT_on(); //蓝牙开 TIM3_Init(36,2000); //定时器3初始化,调试串口输出 TIM4_Init(36,1000); //定时器4初始化,定时采样传感器数据,更新PID输出 while (1) //等待数据更新中断到来 { // switch(UART1_Get_Char())//检测串口发送数据,做相应处理 // { // case 'w': // LedA_on;LedB_on;LedC_on;LedD_on; // //TxBuf[0]++; // //UART1_Put_Package(TxBuf); // LedA_on;LedB_on;LedC_on;LedD_on; // MotoPWM+=10; // UART1_Put_Char(0x5a); // break; // case 's': // LedA_off;LedB_off;LedC_off;LedD_off; // UART1_Put_Char(0xaa); // MotoPWM-=10; // break; // default :break; // } // MotorPwmFlash(MotoPWM,MotoPWM,MotoPWM,MotoPWM); // } }
int main(void) { delay_init(); //延时函数初始化 uart_init(115200); TLV5616_Init(); TLV5616_SetOutput_Voltage(500); IIC_Init(); //初始化ADS1115的I2C KEY_Init(); Uc1701_Init(); clear_screen(0x55); TIM2_Int_Init(14999,71);//15ms TIM3_Int_Init(19999,71);//1Mhz的计数频率,计数到19999为20ms TIM4_Int_Init(4999,71);//5ms while(1) { delay_ms(2); } }
//初始化MPU6050 //返回值:0,成功 // 其他,错误代码 u8 MPU_Init(void) { u8 res; IIC_Init();//初始化IIC总线 if(MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80)){ return 2; } //复位MPU6050 delay_ms(100); if(MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00)){ return 3; } //唤醒MPU6050 if(MPU_Set_Gyro_Fsr(3)){ return 4; } //陀螺仪传感器,±2000dps if(MPU_Set_Accel_Fsr(0)){ return 5; } //加速度传感器,±2g if(MPU_Set_Rate(50)){ return 6; } //设置采样率50Hz if(MPU_Write_Byte(MPU_INT_EN_REG,0X00)){ return 7; } //关闭所有中断 if(MPU_Write_Byte(MPU_USER_CTRL_REG,0X00)){ return 8; } //I2C主模式关闭 if(MPU_Write_Byte(MPU_FIFO_EN_REG,0X00)){ return 9; } //关闭FIFO if(MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80)){ return 10; } //INT引脚低电平有效 res=MPU_Read_Byte(MPU_DEVICE_ID_REG); if(res==MPU_ADDR)//器件ID正确 { MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); //设置CLKSEL,PLL X轴为参考 MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //加速度与陀螺仪都工作 MPU_Set_Rate(50); //设置采样率为50Hz }else { printf("address error: %X\n",res); return 1; }; return 0; }
void ALL_Init(void) { SOPT1 &= 0x3F; //disable cop MCG_Init(); //PTED_Init(); //LED_Init(); //KBI_Init(); RTC_Init(); //SPI_Init(); //ATD_Init(); //SCI_Init(); //PWM_Init(); IIC_Init(); //HCS08_EE_Init(); //初始化Flash控制寄存器 PCF8563_Init(); LCD_INIT(); }
UINT8 IIC_SendMsg (UINT8 SlaveAddress, UINT8 LenTx, UINT8 LenRx) { MasterRxFlag = FALSE; TxPacketpositionptr = &TxPacket[0]; // apuntar al primer elemento de TxPacket TxBufferemptyflag = FALSE; TxPacketendptr = &TxPacket[(LenTx-1)]; // apuntar al último elemento a transmitir CurrentSlave = SlaveAddress; TxMsgLen = LenTx; RxMsgLen = LenRx; IIC_TransferState = IIC_TRANSFERING; // Estado = Transfiriendo TxTimerID = TMR_SetTimer ( 500 , PROC_IIC, f_IICTimeout, FALSE); while(IBSR & IBB){ // mientras el bus IIC esté ocupado if (IIC_TransferState != IIC_TRANSFERING) return ERROR; } IBCR = (IBEN|IBIE|MSSL|TXRX); // grab bus - start condition initiated IBDR = SlaveAddress; // address the slave while (IIC_TransferState == IIC_TRANSFERING){ REFRESH_WATCHDOG; } if ( IIC_TransferState == IIC_TX_OK ){ TMR_FreeTimer (TxTimerID); // anular el timer de supervisión de la transmisión if (!RxMsgLen) return OK; else return (IIC_ReceiveMsg (CurrentSlave, RxMsgLen)); // #################### } IIC_Init(); return ERROR; }
void My_System_Init(void) { uint8_t flag, i=0; NVIC_Configuration(); delay_init(72); //延时初始化 for(i=0;i<5;i++) delay_Ms_Loop(1000); //上电短延时 确保供电稳定 LED_Init(); LEDALL_ON; timer2_init(); //PWM输出定时器初始化 timer3_init(); //PWM输出定时器初始化 IIC_Init(); uart_init(38400); //调试用串口初始化 My_usart2_init(38400); //蓝牙用串口初始化 printf("欢迎使用启天科技BUTTERFLY四旋翼\r\n"); printf("QQ群:471023785\r\n"); LEDALL_OFF; MPU6050_Init(); //6050初始化 SPI1_INIT(); //SPI初始化,用于nRF模块 flag = NRF_CHECK(); //检查NRF模块是否正常工作 if(flag != 1) { while(1) { LEDALL_OFF; delay_Ms_Loop(200); LEDALL_ON; delay_Ms_Loop(200); } } NRF24L01_INIT(); //nRF初始化 SetRX_Mode(); //设置为接收模式 NRF24L01_INIT(); //nRF初始化 NRF_GPIO_Interrupt_Init(); //nRF使用的外部中断的引脚初始化 tim4_init(); //定时中断,作为系统的控制频率 adcInit(); //ADC初始化,测量电池电压 }
//初始化MPU6050 //返回值:0,成功 // 其他,错误代码 u8 MPU_Init(void) { u8 res; IIC_Init();//初始化IIC总线 MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X80); //复位MPU6050 delay_ms(100); MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X00); //唤醒MPU6050 MPU_Set_Gyro_Fsr(3); //陀螺仪传感器,±2000dps MPU_Set_Accel_Fsr(0); //加速度传感器,±2g MPU_Set_Rate(50); //设置采样率50Hz MPU_Write_Byte(MPU_INT_EN_REG,0X00); //关闭所有中断 MPU_Write_Byte(MPU_USER_CTRL_REG,0X00); //I2C主模式关闭 MPU_Write_Byte(MPU_FIFO_EN_REG,0X00); //关闭FIFO MPU_Write_Byte(MPU_INTBP_CFG_REG,0X80); //INT引脚低电平有效 res=MPU_Read_Byte(MPU_DEVICE_ID_REG); if(res==MPU_ADDR)//器件ID正确 { MPU_Write_Byte(MPU_PWR_MGMT1_REG,0X01); //设置CLKSEL,PLL X轴为参考 MPU_Write_Byte(MPU_PWR_MGMT2_REG,0X00); //加速度与陀螺仪都工作 MPU_Set_Rate(50); //设置采样率为50Hz }else return 1; return 0; }
UINT8 IIC_ReceiveMsg (UINT8 SlaveAddress, UINT8 LenRx) { MasterRxFlag = TRUE; RxCompleteflag = FALSE; RxPacketpositionptr = &RxPacket[0]; // apuntar al primer elemento de RxPacket RxBufferfullflag = FALSE; RxPacketendptr = &RxPacket[(LenRx-1)]; // apuntar a la ubicación del último elemento a recibir IIC_TransferState = IIC_TRANSFERING; // Estado = Transfiriendo RxTimerID = TMR_SetTimer ( 500 , PROC_IIC, f_IICTimeout, FALSE); while(IBSR & IBB){ // mientras el bus IIC esté ocupado if (IIC_TransferState != IIC_TRANSFERING) return ERROR; } IBCR = (IBEN|IBIE|MSSL|TXRX); // grab bus - start condition initiated IBDR = (SlaveAddress | 0x01); // address the slave, insruct slave to transmit while (IIC_TransferState == IIC_TRANSFERING){ REFRESH_WATCHDOG; } if ( IIC_TransferState == IIC_RX_OK ){ TMR_FreeTimer (RxTimerID); return OK; } IIC_Init(); return ERROR; }
int main(void) { u8 i; u8 RecvBuf[32]; u8 SendBuf[32]; u8 offline = 0; u8 recv_flag = 0; u32 pd2ms=0,pd20ams=0,pd20bms=0,pd100ms=0; SystemInit(); RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); USART1_Configuration(); dbgPrintf(" Init Ticktack !\r\n"); cycleCounterInit(); SysTick_Config(SystemCoreClock / 1000); for(i=0;i<2;i++) { OP_LED1;OP_LED2;OP_LED3;OP_LED4; delay_ms(500); OP_LED1;OP_LED2;OP_LED3;OP_LED4; delay_ms(500); } FilterInit(); controllerInit(); dbgPrintf(" Init eeprom!\r\n"); FLASH_Unlock(); EE_Init(); EE_Read_ACC_GYRO_Offset(); /* 如果PID丢失或者错误,将下面两行注释去掉,重新编译烧写,运行一遍,可将PID还原,然后重新注释,再烧写一遍 */ //EE_Write_PID(); //EE_Write_Rate_PID(); EE_Read_PID(); EE_Read_Rate_PID(); dbgPrintf(" Init adc!\r\n"); ADC_DMA_Init(); dbgPrintf(" Init NRF24L01 !\r\n"); SPI_NRF_Init(); Nrf24l01_Init(); NRF24l01_SetRxMode(); while(Nrf24l01_Check()) { dbgPrintf("NRF24L01 Fault !\r\n"); delay_ms(500); } dbgPrintf("NRF24L01 Is Detected !\r\n"); dbgPrintf("Init MPU6050...\r\n"); IIC_Init(); MPU6050_initialize(); dbgPrintf("Init Motor...\r\n"); Motor_Init(); Motor_SetPwm(0,0,0,0); pd20bms = TIMIRQCNT + 10*ITS_PER_MS; while(1) { if(TIMIRQCNT>pd2ms + 2*ITS_PER_MS-1) // every 4ms { GetEulerAngle(); if(lock_flag==UNLOCK) AttitudeToMotors(angle.y,angle.x,angle.z); else { MOTOR1=0; MOTOR2=0; MOTOR3=0; MOTOR4=0; } Motor_SetPwm(MOTOR1,MOTOR2,MOTOR3,MOTOR4); pd2ms = TIMIRQCNT; } if(TIMIRQCNT>pd20ams + 20*ITS_PER_MS-1) // every 20ms { if(NRF24l01_Recv(RecvBuf)>10) { if((RecvBuf[RecvBuf[2]+3]==CheckSum(RecvBuf, RecvBuf[2]+3))&&(RecvBuf[0]==0xAA)) { if(RecvBuf[1]!=0xC0) OP_LED1; offline=0; switch(RecvBuf[1]) { case 0xC0: //control Getdesireddata(RecvBuf); OP_LED2; break; case 0x10: //W PID SetPID(RecvBuf); break; case 0x11: //W Attitude SetAccGyroOffset(RecvBuf); break; case 0x12: //W Control offset break; case 0x14: //W Rate PID SetRatePID(RecvBuf); break; case 0x20: //R PID recv_flag = RESEND; GetPID(SendBuf); break; case 0x21: //R Attitude recv_flag = RESEND; GetAccGyroOffset(SendBuf); break; case 0x22: //R Control offset break; case 0x24: //R Rate PID recv_flag = RESEND; GetRatePID(SendBuf); break; case 0x40: //校准姿态 EnableCalibration(); break; case 0x41: //校准遥控器零点 break; default: break; } } } pd20ams = TIMIRQCNT; } if(TIMIRQCNT>pd20bms + 20*ITS_PER_MS-1) // every 20ms { if(recv_flag==0) GetState(SendBuf); else recv_flag--; NRF_SendData(SendBuf); OP_LED3; pd20bms = TIMIRQCNT; } if(TIMIRQCNT>pd100ms + 100*ITS_PER_MS-1) // every 100ms { if(offline>20) lock_flag = LOCK; offline++; // OP_LED4; pd100ms = TIMIRQCNT; } } }
int main(void) { u16 sta , i=0; int t = 200; delay_init(); //延时函数初始化 LED_Init(); LED0 = 0;delay_ms(200); LED0 = 1; LED1 = 0;delay_ms(200); LED1 = 1; LED2 = 0;delay_ms(200); LED2 = 1; LED3 = 0;delay_ms(200); LED3 = 1; NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 uart_init(9600); //串口初始化为9600 EXTIX_Init( ); NRF24L01_Init(); //初始化NRF24L01 NRF24L01_RX_Mode(); while(NRF24L01_Check() == 1)//检查NRF24L01是否在位. { LED3=!LED3; printf("NRF ERROR!!\r\n"); delay_ms(100 ); } printf("NRF okk!!\r\n"); sta=NRF24L01_Read_Reg(STATUS); //读取状态寄存器的值 NRF24L01_Write_Reg(WRITE_REG_NRF+STATUS,sta); //清除TX_DS或MAX_RT中断标志 NRF24L01_Write_Reg(FLUSH_RX,0xff);//清除RX FIFO寄存器 TIM2_PWM_Init(999,9); //PWM OUT TIM3_Int_Init(0xffff,71); //做计时器用 IIC_Init(); InitMPU6050(); Init_HMC5883(); suanfa_GetOrigin(); //初始欧拉角 TIM4_Int_Init(49,7199); //PID调速中断 放在最后初始化,防止打断角度校准 LOCK = 1; UN_LOCK = 0; while(1){ if(LOCK) { lock(); LED0 = 0;delay_ms(500);LED0 = 1; Power = 0; Target_x = 0; Target_y = 0; } if(UN_LOCK) { suanfa(); printf("%.2lf %.2lf %.2lf\r\n",EA.Roll,EA.Pitch,EA.Yaw); // TIM_Cmd(TIM4, ENABLE); Data_Receive_Anl(); } // printf("--p\r\n"); } }
int main(void) { // u8 key; // u16 i=0; u16 adc_91000,adc_91200,reg_read_data=0,reg_write_data=0; float LMP91000_VOUT,LMP91200_VOUT,Temp_Integer,Temp_Decimal; short temp; u8 read_val[5] = {0xff}; u8 status = TI_LMP91000_NOT_READY; SystemInit(); delay_init(72); //延时初始化 NVIC_Configuration(); uart_init(9600); LED_Init(); KEY_Init(); Adc_Init(); IIC_Init(); //IIC初始化 LMP91200_Init(); //初始化PH delay_ms(500); printf("\nHello XXL"); /************************************************************ * 溶氧电极初始化及数据采集 ************************************************************/ LMP91000_ENABLE(); // enable LM while (status == TI_LMP91000_NOT_READY) // wait while device is not ready status = LMP91000_ReadOneByte(TI_LMP91000_STATUS_REG); // Read device ready status read_val[0] = LMP91000_ReadOneByte(TI_LMP91000_LOCK_REG); // Read from lock register default value 0x01 read_val[1] = LMP91000_ReadOneByte(TI_LMP91000_TIACN_REG); // Read TIA control register default value 0x03 read_val[2] = LMP91000_ReadOneByte(TI_LMP91000_REFCN_REG); // Read Reference control register default value 0x20 read_val[3] = LMP91000_ReadOneByte(TI_LMP91000_MODECN_REG); // Read Mode control register default value 0x00 LMP91000_WriteOneByte(TI_LMP91000_LOCK_REG, TI_LMP91000_WRITE_UNLOCK); // unlock the registers for write LMP91000_WriteOneByte(TI_LMP91000_TIACN_REG, TIACN_NEW_VALUE); // Modify TIA control register LMP91000_WriteOneByte(TI_LMP91000_REFCN_REG, REFCN_NEW_VALUE); // Modify REF control register LMP91000_WriteOneByte(TI_LMP91000_MODECN_REG, MODECN_NEW_VALUE); // Modify MODE control register read_val[1] = LMP91000_ReadOneByte(TI_LMP91000_TIACN_REG); // Read to confirm register is modified read_val[2] = LMP91000_ReadOneByte(TI_LMP91000_REFCN_REG); // Read to confirm register is modified read_val[3] = LMP91000_ReadOneByte(TI_LMP91000_MODECN_REG); // Read to confirm register is modified read_val[4] = LMP91000_ReadOneByte(TI_LMP91000_STATUS_REG); // test if write/read values match if (read_val[1] == TIACN_NEW_VALUE) { // while (1) // no error: blink LED continuously // { delay_ms(500); LED1=!LED1;//绿灯闪烁 // } } else { delay_ms(500); LED0=!LED0;//红灯闪烁 // error } // 获取LMP91000输出电压 // while(1) // { adc_91000=Get_Adc(ADC_Channel_8); LMP91000_VOUT=(float)adc_91000*(3.3/4096); LMP91000_VOUT=LMP91000_VOUT; printf("\n\nDO=%fv ",LMP91000_VOUT); temp=DS18B20_Get_Temp(); if(temp<0) { temp=-temp; } Temp_Integer=temp/10; Temp_Integer=Temp_Integer; //整数 Temp_Decimal=temp%10; Temp_Decimal=Temp_Decimal; //小数 printf(" Temp=%f.%fc " __TIME__ "",Temp_Integer,Temp_Decimal); delay_ms(1000); // } LMP91000_WriteOneByte(TI_LMP91000_LOCK_REG, TI_LMP91000_WRITE_LOCK); // lock the registers LMP91000_DISABLE(); /************************************************************ * PH电极初始化及数据采集,无论在空气,碱水,酸水中vout总是1.420~1.429,可能电极坏掉 ************************************************************/ Clr_LMP91200_CSN; reg_write_data = TI_LMP91200_CONFIG_REG_TEST_VALUE; // value to write reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write to config register, read old value reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write again to read previous update reg_read_data = SPIx_ReadWriteByte(reg_write_data); // Write again to read previous update,if not read this time,bit-15 will be wrong // Set_LMP91200_CSN; while(1) // test if write/read values match { if (reg_write_data == reg_read_data) { adc_91200=Get_Adc(ADC_Channel_9); LMP91200_VOUT=(float)adc_91200*(3.3/4096); LMP91200_VOUT=LMP91200_VOUT; LED1=!LED1; //正确则绿灯亮 delay_ms(500); } else { LED0=!LED0; //错误则黄灯亮 delay_ms(300); } } /************************************************************ * 温度传感器初始化及数据采集 ************************************************************/ while(DS18B20_Init())//初始化DS18B20,兼检测18B20 { delay_ms(600); // error_report();//DS18B20 未连接 } while(1) { temp=DS18B20_Get_Temp(); if(temp<0) { temp=-temp; } Temp_Integer=temp/10; Temp_Integer=Temp_Integer; //整数 Temp_Decimal=temp%10; Temp_Decimal=Temp_Decimal; //小数 delay_ms(500); LED1=!LED1;//DS1闪烁 } /************************************************************ * 按键扫描 ************************************************************/ #if 0 while(1) { key=KEY_Scan(); if(key==1)//KEY0按下,写入24C02 { ; } if(key==3)//KEY_UP按下,读取字符串并显示 { ; } i++; delay_ms(10); } #endif }
//初始化IIC接口 void AT24CXX_Init(void) { IIC_Init(); }
//初始化IIC接口 void AT24CXX_Init(void) { IIC_Init();//IIC初始化 }
/*............................................................................. @Name : AT24Cxx_Init @Param : void @return : void @Info : ��ʼ�� EEPROMоƬ AT24C02 .............................................................................*/ void AT24Cxx_Init(void) { IIC_Init(); }
uint8_t MPU_Init(void) { GPIO_InitTypeDef gpio; NVIC_InitTypeDef nvic; EXTI_InitTypeDef exti; int res=0; IIC_Init(); RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); gpio.GPIO_Pin = GPIO_Pin_5; gpio.GPIO_Mode = GPIO_Mode_IN; gpio.GPIO_OType = GPIO_OType_PP; gpio.GPIO_PuPd = GPIO_PuPd_UP; gpio.GPIO_Speed = GPIO_Speed_100MHz; GPIO_Init(GPIOB, &gpio); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOB,GPIO_PinSource5); exti.EXTI_Line = EXTI_Line5; exti.EXTI_Mode = EXTI_Mode_Interrupt; exti.EXTI_Trigger = EXTI_Trigger_Falling;//下降沿中断 exti.EXTI_LineCmd = ENABLE; EXTI_Init(&exti); nvic.NVIC_IRQChannel = EXTI9_5_IRQn; nvic.NVIC_IRQChannelPreemptionPriority = ITP_MPU_EXTI9_5_PREEMPTION; nvic.NVIC_IRQChannelSubPriority = ITP_MPU_EXTI9_5_SUB; nvic.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&nvic); if(mpu_init()==0) //初始化MPU6050 { res=mpu_set_sensors(INV_XYZ_GYRO|INV_XYZ_ACCEL);//设置所需要的传感器 if(res)return 1; res=mpu_configure_fifo(INV_XYZ_GYRO | INV_XYZ_ACCEL);//设置FIFO if(res)return 2; res=mpu_set_sample_rate(DEFAULT_MPU_HZ); //设置采样率 if(res)return 3; res=dmp_load_motion_driver_firmware(); //加载dmp固件 if(res)return 4; res=dmp_set_orientation(inv_orientation_matrix_to_scalar(gyro_orientation));//设置陀螺仪方向 if(res)return 5; res=dmp_enable_feature(DMP_FEATURE_6X_LP_QUAT|DMP_FEATURE_TAP| //设置dmp功能 DMP_FEATURE_ANDROID_ORIENT|DMP_FEATURE_SEND_RAW_ACCEL|DMP_FEATURE_SEND_CAL_GYRO| DMP_FEATURE_GYRO_CAL); if(res)return 6; res=dmp_set_fifo_rate(DEFAULT_MPU_HZ); //设置DMP输出速率(最大不超过200Hz) if(res)return 7; res=mpu_set_int_level(1); if(res)return 8; res=dmp_set_interrupt_mode(DMP_INT_CONTINUOUS); if(res)return 9; res=run_self_test(); //自检 if(res)return 10; res=mpu_set_dmp_state(1); //使能DMP if(res)return 11; } else return 12; return 0; }
int main(void) { u8 t; u8 len; u8 acc_flag = 0; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2 delay_init(168); //延时初始化 uart_init(115200); //串口初始化波特率为115200 LED_Init(); //初始化与LED连接的硬件接口 IIC_Init(); while(1) { if(USART_RX_STA&0x8000) { printf("\n"); len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度 for(t=0;t<len;t++) { USART_SendData(USART2, USART_RX_BUF[t]); //向串口2发送数据 while(USART_GetFlagStatus(USART2,USART_FLAG_TC)!=SET);//等待发送结束 } string_to_value_new(); if(COMD_FLAG != 1) { if(COMMAND_BUF[0]-'l' == 0 && COMMAND_BUF[1]-'e' == 0 && COMMAND_BUF[2]-'d' == 0) { stop_timer(); stop_pwm(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] != 3 && FACTOR[0] != 4 && FACTOR[0] != 5 && FACTOR[0] != 6) { printf("\n命令错误!请重新输入!参数为3|4|5|6"); } else { printf("\nLED控制: %d",FACTOR[0]); turn_on_led(FACTOR[0]); } } else if(COMMAND_BUF[0]-'p' == 0 && COMMAND_BUF[1]-'w' == 0 && COMMAND_BUF[2]-'m' == 0) { stop_timer(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] - 100 > 0 || FACTOR[0] - 0 < 0) { printf("\n命令错误!请重新输入!参数范围在0到100"); } else { printf("\nPWM占空比:%d",FACTOR[0]); setPWMvalue(FACTOR[0]); } } else if(COMMAND_BUF[0]-'t' == 0 && COMMAND_BUF[1]-'i' == 0 && COMMAND_BUF[2]-'m' == 0) { stop_pwm(); acc_flag = 0; turn_off_led(0); if(FACTOR[0] > 10000 || FACTOR[0] < 20) { printf("\n命令错误!请重新输入!参数范围在20到10000"); } else { printf("\nTIMER定时时间:%d ",FACTOR[0]); send_str("ms"); set_TIM3_time(FACTOR[0]); } } else if(COMMAND_BUF[0]-'a' == 0 && COMMAND_BUF[1]-'c' == 0 && COMMAND_BUF[2]-'c' == 0) { acc_flag = 1; if(FACTOR[0] != 0 && FACTOR[0] != 1 && FACTOR[0] != 2) { printf("\n参数输入有误;默认输出Z轴!"); } } else { acc_flag = 0; turn_off_led(0); printf("\n命令错误!请重新输入!"); } } } if(acc_flag) { LSM303_ACC_Init(); LSM_ACC_Read(); ACC_Result_XYZ(); switch(FACTOR[0]) { case 0: printf("\nX轴:%f", ACC_XYZ[0]); break; case 1: printf("\nY轴:%f", ACC_XYZ[1]); break; case 2: printf("\nZ轴:%f.3", ACC_XYZ[2]); break; default:printf("\nZ轴:%f", ACC_XYZ[2]); } delay_ms(100); } }; }