/** * @brief 串口初始化 * @param ucPORT 串口号 * ulBaudRate 波特率 * ucDataBits 数据位 * eParity 校验位 * @retval None */ BOOL xMBPortSerialInit( UCHAR ucPORT, ULONG ulBaudRate, UCHAR ucDataBits, eMBParity eParity ) { // GPIO_InitTypeDef GPIO_InitStructure; // USART_InitTypeDef USART_InitStructure; // NVIC_InitTypeDef NVIC_InitStructure; (void)ucPORT; //不修改串口 (void)ucDataBits; //不修改数据位长度 (void)eParity; //不修改校验格式 RS485_Init(ulBaudRate); // //使能USART1,GPIOA // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | // RCC_APB2Periph_USART1, ENABLE); // //GPIOA9 USART1_Tx // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //推挽输出 // GPIO_Init(GPIOA, &GPIO_InitStructure); // //GPIOA.10 USART1_Rx // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮动输入 // GPIO_Init(GPIOA, &GPIO_InitStructure); // USART_InitStructure.USART_BaudRate = ulBaudRate; //只修改波特率 // USART_InitStructure.USART_WordLength = USART_WordLength_8b; // USART_InitStructure.USART_StopBits = USART_StopBits_1; // USART_InitStructure.USART_Parity = USART_Parity_No; // USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // //串口初始化 // USART_Init(USART1, &USART_InitStructure); // //使能USART1 // USART_Cmd(USART1, ENABLE); // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); // //设定USART1 中断优先级 // NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; // NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // NVIC_Init(&NVIC_InitStructure); // //最后配置485发送和接收模式 // RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); // //GPIOD.8 // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // GPIO_Init(GPIOD, &GPIO_InitStructure); return TRUE; }
//extern void flash_write_time_volitage(u8 addr_index,uint16_t volitage,u8 year_H,u8 year_L,u8 month_H,u8 month_L,u8 day_H,u8 day_L,u8 hh_H,u8 hh_L,u8 mm_H,u8 mm_L,u8 ss_H,u8 ss_L ) ; void operating_12864_system (void) //按键操作和界面切换 { u16 u16_WaitForOscSource,startv=0; if(rcc_flag==0) { if(k1==key_ok) { k1=key_none; rcc_flag=1; DebugMenu(1); } if(k1==key_cancel) { k1=key_none; flag_i=1; voltage=0; Alarm_flag_1=0; BUZZER_off; Fault_led_off; Relay_off; // buzzer_tick_flag=1; } } if(rcc_flag==1) { // Delay_ms(1000); // flag_system=1; if(flag_debug==0) { flag_debug=1; } switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>4) { flag_debug=1; } DebugMenu(flag_debug); // Delay_ms(20); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=4; } DebugMenu(flag_debug); // Delay_ms(20); break; case key_cancel: rcc_flag=0; flag_debug=1; // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: k1=key_none; if(flag_debug==1) { rcc_flag=21; flag_debug=1; SecondSet(flag_debug); } if(flag_debug==2) { rcc_flag=22; flag_debug=1; SecondSystemDebug(flag_debug); } if(flag_debug==3) { rcc_flag=23; fault_num=flash_num<0?0:flash_num; // if(flash_num<0) // { // fault_num=0; // } // else{ // fault_num=flash_num; // } flash_read_time_volitage(start_addr+fault_num*14); Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L); // flash_read_time_volitage(start_addr+14*fault_num); // Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L); } if(flag_debug==4) { rcc_flag=24; Second_clear_fault_records(); } break; } } if(rcc_flag==21) { //flag_debug=1; switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>2) { flag_debug=1; } SecondSet(flag_debug); // Delay_ms(20); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=2; } SecondSet(flag_debug); // Delay_ms(20); break; case key_cancel: flag_debug=0; rcc_flag=1; DebugMenu(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: k1=key_none; if(flag_debug==1) { rcc_flag=31; ThirdSystemSetting(1); } if(flag_debug==2) { flag_debug=1; rcc_flag=32; ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits); } break; } } if(rcc_flag==22) { //flag_debug=1; switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>4) { flag_debug=1; } SecondSystemDebug(flag_debug); // Delay_ms(20); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=4; } SecondSystemDebug(flag_debug); // Delay_ms(20); break; case key_cancel: flag_debug=1; rcc_flag=1; // Delay_ms(20); k1=key_none; DebugMenu(1); //DebugMenu(flag_debug); break; case key_ok: k1=key_none; if(flag_debug==1) { flag_debug=1; rcc_flag=33; ThirdChange(); } if(flag_debug==2) { flag_debug=1; rcc_flag=34; ThirdCorrect(); } if(flag_debug==3) { flag_debug=1; rcc_flag=35; ThirdCheck(); } if(flag_debug==4) { flag_debug=1; rcc_flag=36; ThirdReguration(1,1,1,1); } break; } } if(rcc_flag==23) { //flag_debug=1; switch (k1) { case key_down: fault_num++; if(fault_num>=50) { fault_num=0; } flash_read_time_volitage(start_addr+14*fault_num); Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L); // fault_num=flash_num; k1=key_none; break; case key_up: fault_num--; if(fault_num<0) { fault_num=49; } flash_read_time_volitage(start_addr+14*fault_num); Second_Fault_record(frecord.voltage,fault_num+1,frecord.year_H,frecord.year_L,frecord.month_H,frecord.month_L,frecord.day_H,frecord.day_L,frecord.hh_H,frecord.hh_L,frecord.mm_H,frecord.mm_L,frecord.ss_H,frecord.ss_L); // fault_num=flash_num; k1=key_none; break; case key_cancel: rcc_flag=1; // Delay_ms(20); flag_debug=1; k1=key_none; DebugMenu(1); //DebugMenu(flag_debug); break; case key_ok: k1=key_none; // rcc_flag=2; break; } } if(rcc_flag==24) { //flag_debug=1; switch (k1) { case key_cancel: rcc_flag=1; flag_debug=1; // Delay_ms(20); k1=key_none; DebugMenu(1); //DebugMenu(flag_debug); break; case key_ok: k1=key_none; // fi++; rcc_flag=2; //flash_cache[300]=0; // for(fi=0;fi<255;fi++) // { // flash_cache[fi]=0; // } // Delay_ms(100); // flash_write_time_volitage(0,0,0,0,0,0,0,0,0,0,0,0,0,0 ) ; // Delay_ms(500); // flash_write(0xffff,flash_num_addr); break; } } if(rcc_flag==31) { //flag_debug=1; switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>2) { flag_debug=1; } ThirdSystemSetting(flag_debug); // Delay_ms(20); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=2; } ThirdSystemSetting(flag_debug); // Delay_ms(20); break; case key_cancel: flag_debug=1; rcc_flag=21; SecondSet(1); // Delay_ms(20); k1=key_none; timer.sec=ss_H*10+ss_L; timer.min=mm_H*10+mm_L; timer.hour=hh_H*10+hh_L; timer.w_date=day_H*10+day_L; timer.w_month=month_H*10+month_L; timer.w_year=(year_H*10+year_L)+2000; RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE); /* Allow access to BKP Domain */ PWR_BackupAccessCmd(ENABLE); /* Reset Backup Domain */ BKP_DeInit(); /* Enable LSE */ RCC_LSEConfig(RCC_LSE_ON); for(u16_WaitForOscSource=0;u16_WaitForOscSource<5000;u16_WaitForOscSource++) { } /* Wait till LSE is ready */ while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET); /* Select LSE as RTC Clock Source */ RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); /* Enable RTC Clock */ RCC_RTCCLKCmd(ENABLE); /* Wait for RTC registers synchronization */ RTC_WaitForSynchro(); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Enable the RTC Second */ RTC_ITConfig(RTC_IT_SEC, ENABLE); /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); /* Set RTC prescaler: set RTC period to 1sec */ RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */ /* Wait until last write operation on RTC registers has finished */ RTC_WaitForLastTask(); BKP_WriteBackupRegister(BKP_DR1, 0x5A5A); RTC_Set(timer.w_year,timer.w_month,timer.w_date,timer.hour,timer.min,timer.sec); break; case key_ok: k1=key_none; if(flag_debug==1) { flag_debug=1; rcc_flag=41; FourStart(start_v); } if(flag_debug==2) { flag_debug=1; rcc_flag=42; FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); } break; } } if(rcc_flag==32) { //flag_debug=1; switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>4) { flag_debug=1; } ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits); // Delay_ms(20); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=4; } ThirdBaudRateSetting(flag_debug,Device_Add,Band_Rate,Data_bits,Stop_bits); // Delay_ms(20); break; case key_cancel: flag_debug=1; rcc_flag=21; SecondSet(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: k1=key_none; if(flag_debug==1) { // flag_debug=1; rcc_flag=43; } else if(flag_debug==2) { // flag_debug=1; rcc_flag=44; } else if(flag_debug==3) { // flag_debug=1; rcc_flag=45; } else if(flag_debug==4) { // flag_debug=1; rcc_flag=46; } break; } } if(rcc_flag==33) { //flag_debug=1; switch (k1) { case key_cancel: flag_debug=1; rcc_flag=22; SecondSystemDebug(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: break; } } if(rcc_flag==34) { //flag_debug=1; switch (k1) { case key_down: break; case key_up: // Delay_ms(20); break; case key_cancel: flag_debug=1; rcc_flag=22; SecondSystemDebug(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: break; } } if(rcc_flag==35) { //flag_debug=1; switch (k1) { case key_down: break; case key_up: // Delay_ms(20); break; case key_cancel: flag_debug=1; rcc_flag=22; SecondSystemDebug(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: break; } } if(rcc_flag==36) { //flag_debug=1; switch (k1) { case key_down: k1=key_none; flag_debug++; if(flag_debug>3) { flag_debug=1; } ThirdReguration(flag_debug,flag_Led,flag_Relay,flag_Buzzer); break; case key_up: k1=key_none; flag_debug--; if(flag_debug==0) { flag_debug=3; } ThirdReguration(flag_debug,flag_Led,flag_Relay,flag_Buzzer); break; case key_cancel: flag_debug=1; rcc_flag=22; SecondSystemDebug(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: k1=key_none; if(flag_debug==1) { ++flag_Led; if(flag_Led<1) { flag_Led=2; } else if(flag_Led>2) { flag_Led=1; } ThirdReguration(1,flag_Led,flag_Relay,flag_Buzzer); } if(flag_debug==2) { ++flag_Relay; if(flag_Relay<1) { flag_Relay=2; } else if(flag_Relay>2) { flag_Relay=1; } ThirdReguration(2,flag_Led,flag_Relay,flag_Buzzer); } if(flag_debug==3) { ++flag_Buzzer; if(flag_Buzzer<1) { flag_Buzzer=2; } else if(flag_Buzzer>2) { flag_Buzzer=1; } ThirdReguration(3,flag_Led,flag_Relay,flag_Buzzer); } break; } } if(rcc_flag==41) { //flag_debug=1; switch (k1) { case key_down: if(start_v<=0) { start_v=0; } start_v-=5; FourStart(start_v); // Delay_ms(20); k1=key_none; break; case key_up: start_v+=5; FourStart(start_v); // Delay_ms(20); k1=key_none; break; case key_cancel: startv=start_v; flag_debug=1; rcc_flag=31; ThirdSystemSetting(1); k1=key_none; // flash_write_start_v(start_v); flash_write(startv,start_v_addr); break; case key_ok: break; } } if(rcc_flag==42) { //flag_debug=1; switch (k1) { case key_down: break; case key_up: break; case key_cancel: flag_debug=1; rcc_flag=31; ThirdSystemSetting(1); // Delay_ms(20); k1=key_none; //DebugMenu(flag_debug); break; case key_ok: ++flagTime; if(flagTime > 12) { flagTime = 1; } k1=key_none; break; } } /*设置时间*/ if(flagTime==1) { switch (k1) { case key_down: --ss_L; if(ss_L<0) { ss_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++ss_L; if(ss_L>9) { ss_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==2) { switch (k1) { case key_down: --ss_H; if(ss_H<0) { ss_H = 6; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++ss_H; if(ss_H>6) { ss_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==3) { switch (k1) { case key_down: --mm_L; if(mm_L<0) { mm_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++mm_L; if(mm_L>9) { mm_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==4) { switch (k1) { case key_down: --mm_H; if(mm_H<0) { mm_H = 6; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++mm_H; if(mm_H>6) { mm_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==5) { switch (k1) { case key_down: --hh_L; if(hh_L<0) { hh_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++hh_L; if(hh_L>9) { hh_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==6) { switch (k1) { case key_down: --hh_H; if(hh_H<0) { hh_H = 2; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++hh_H; if(hh_H>2) { hh_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==7) { switch (k1) { case key_down: --day_L; if(day_L<0) { day_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++day_L; if(day_L>9) { day_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==8) { switch (k1) { case key_down: --day_H; if(day_H<0) { day_H = 3; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++day_H; if(day_H>3) { day_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==9) { switch (k1) { case key_down: --month_L; if(month_L<0) { month_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++month_L; if(month_L>9) { month_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==10) { switch (k1) { case key_down: --month_H; if(month_H<0) { month_H = 1; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++month_H; if(month_H>1) { month_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==11) { switch (k1) { case key_down: --year_L; if(year_L<0) { year_L = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++year_L; if(year_L>9) { year_L = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } else if(flagTime==12) { switch (k1) { case key_down: --year_H; if(year_H<0) { year_H = 9; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; case key_up: ++year_H; if(year_H>9) { year_H = 0; } FourTime(year_H, year_L, month_H, month_L, day_H, day_L, hh_H, hh_L, mm_H, mm_L, ss_H, ss_L); k1=key_none; break; } } if(rcc_flag==43) //设置装置号 { switch (k1) { case key_down: --Device_Add; if(Device_Add<1) { Device_Add = 255; } ThirdBaudRateSetting(1,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_up: ++Device_Add; if(Device_Add>255) { Device_Add = 1; } ThirdBaudRateSetting(1,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_cancel: //设置装置后返回 flag_debug=1; rcc_flag=21; SecondSet(1); // flash_write_device_b(Device_Add); flash_write(Device_Add,device_id_addr); k1=key_none; break; } } if(rcc_flag==44) //设置波特率 { switch (k1) { case key_down: k1=key_none; --flag_debug; if(flag_debug<1) { flag_debug = 4; } switch(flag_debug) { case 1: Band_Rate=4800; break; case 2: Band_Rate=9600; break; case 3: Band_Rate=19200; break; case 4: Band_Rate=115200; break; } ThirdBaudRateSetting(2,Device_Add,Band_Rate,Data_bits,Stop_bits); break; case key_up: k1=key_none; k1=key_none; ++flag_debug; if(flag_debug>4) { flag_debug = 0; } switch(flag_debug) { case 1: Band_Rate=4800; break; case 2: Band_Rate=9600; break; case 3: Band_Rate=19200; break; case 4: Band_Rate=115200; break; } ThirdBaudRateSetting(2,Device_Add,Band_Rate,Data_bits,Stop_bits); break; case key_cancel: //设置装置后返回 flag_debug=1; rcc_flag=21; SecondSet(1); k1=key_none; uart_init(Band_Rate); RS485_Init(Band_Rate); //波特率设置 485 // flash_write_Band_Rate(Band_Rate); flash_write(Band_Rate,Band_Rate_addr); break; } } if(rcc_flag==45) //设置数据位 { switch (k1) { case key_down: --Data_bits; if(Data_bits<5) { Data_bits = 8; } ThirdBaudRateSetting(3,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_up: ++Data_bits; if(Data_bits>9) { Data_bits = 5; } ThirdBaudRateSetting(3,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_cancel: //设置停止位 flag_debug=1; rcc_flag=21; SecondSet(1); // flash_write_device_b(Device_Add); // flash_write_databits(Data_bits); flash_write(Data_bits,databits_addr); k1=key_none; break; } } if(rcc_flag==46) //设置停止位 { switch (k1) { case key_down: --Stop_bits; if(Stop_bits<1) { Stop_bits = 9; } ThirdBaudRateSetting(4,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_up: ++Stop_bits; if(Stop_bits>10) { Stop_bits = 1; } ThirdBaudRateSetting(4,Device_Add,Band_Rate,Data_bits,Stop_bits); k1=key_none; break; case key_cancel: //设置停止位 flag_debug=1; rcc_flag=21; SecondSet(1); // flash_write_stopbits(Stop_bits); flash_write(Stop_bits,stopbits_addr); k1=key_none; break; } } }
int main() { RCC_ClocksTypeDef RCC_Clocks; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); int rf_len = 0; int rs485_len = 0; #if DEBUG // int usart_len = 0; // char buff_usart[BUFFER_SIZE]; #endif char buff_rf[BUFFER_SIZE]; char buff_rs485[BUFFER_SIZE]; unsigned int sensors_time_poll = 0; // int temp_time_poll = 0; // int sms_test_time = 0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_WriteBit(GPIOB, GPIO_Pin_9, Bit_SET); // off Delay_Init(); Enrf24_init(CE_PIN, CSN_PIN, IRQ_PIN); Enrf24_begin(1000000, 0); // Defaults 1Mbps, channel 0, max TX power Enrf24_setTXaddress((void*)enrf24_addr); Enrf24_setRXaddress((void*)enrf24_addr); Enrf24_enableRX(); // Start listening #if DEBUG USART1_Init(115200); #endif DS1307_Init(); Sensors_Init(); RS485_Init(115200); sim_hal_init(115200); OutputInit(); if (ThesisInit() == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\nFlash write error.\n"); #endif TurnBuzzerOn(); Delay(1000); } #if DEBUG RCC_GetClocksFreq(&RCC_Clocks); USART1_SendStr("System Clock: "); USART1_SendNum(RCC_Clocks.SYSCLK_Frequency); USART1_SendStr("\r\n"); USART1_SendStr("Device ID: "); USART1_SendByte(__flash_data.id, HEX); USART1_SendStr("\r\n"); USART1_SendStr("Device Unique Number: "); USART1_SendByte(__flash_data.unique_number[0], HEX); USART1_SendByte(__flash_data.unique_number[1], HEX); USART1_SendByte(__flash_data.unique_number[2], HEX); USART1_SendByte(__flash_data.unique_number[3], HEX); USART1_SendStr("\r\n"); #endif OneWire_Init(); for EVER { if (millis() - sensors_time_poll > 100) { led_toggle(); Sensors_Poll(); sensors_time_poll = millis(); // buzzer_toggle(); // output_toggle(); if (millis() > 10000) { if (sensors.Gas >= GAS_LIMIT) { #if DEBUG USART1_SendStr("Gas detected.\r\n"); USART1_SendStr("Current Gas: "); USART1_SendFloat(sensors.Gas); USART1_SendStr("Limited Gas: "); USART1_SendFloat(GAS_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else if (sensors.Lighting >= LIGHT_LIMIT) { #if DEBUG USART1_SendStr("Light detected.\r\n"); USART1_SendStr("Current Light: "); USART1_SendFloat(sensors.Lighting); USART1_SendStr("Limited Light: "); USART1_SendFloat(LIGHT_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else if (sensors.TempC >= TEMPC_LIMIT) { #if DEBUG USART1_SendStr("Tempc detected.\r\n"); USART1_SendStr("Current Tempc: "); USART1_SendFloat(sensors.TempC); USART1_SendStr("Limited Tempc: "); USART1_SendFloat(TEMPC_LIMIT); USART1_SendStr("\r\n"); #endif TurnBuzzerOn(); TurnSpeakerOn(); TurnRelayOn(); } else { TurnBuzzerOff(); TurnSpeakerOff(); TurnRelayOff(); } } // Sim900_Process(); } // if (millis() - sms_test_time > 10000) // { // Sim900_SendSMS("Hi kieu", "01677880531"); // sms_test_time = millis(); // } // usart_len = USART1_Available(); // // // usart: for test // if (usart_len > 4) // { // int i; // USART1_SendStr("\nUSART1 received packet: \n"); // USART1_GetData(buff_usart, usart_len); // for (i = 0; i < usart_len; i++) // USART1_SendByte(buff_usart[i], HEX); // USART1_SendChar('\n'); // if (ThesisProcess(buff_usart, usart_len) == THESIS_OK) // { // memset(buff_usart, 0, usart_len); // USART1_Flush(); // if (thesis_need_to_send) // { // int i; // USART1_SendStr("\nNeed to send packet: "); // for (i = 0; i < thesis_msg_len; i++) // { // USART1_SendByte(thesis_sent_msg[i], HEX); // } // USART1_SendStr("\nNeed to send packet length: "); // USART1_SendNum(thesis_msg_len); // USART1_SendStr("\n"); // thesis_msg_len = 0; // thesis_need_to_send = 0; // } // USART1_SendStr("\nPacket processed.\n"); // } // else if (thesis_errn == THESIS_FLASH_ERROR) // { // USART1_SendStr("\n"); // USART1_SendStr(thesis_err_msg); // USART1_SendStr("\n"); // led_toggle(); // for(;;); // } // else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) // { // memset(buff_usart, 0, usart_len); // USART1_Flush(); // USART1_SendStr("Packet processing fail.\n"); // } // // USART1_SendStr("\n"); // USART1_SendStr(thesis_err_msg); // USART1_SendStr("\n"); // } // rf if (Enrf24_available(1)) { int i; rf_len = Enrf24_read(buff_rf + rf_len, BUFFER_SIZE - 1 - rf_len); #if DEBUG USART1_SendStr("\nRF received packet.\n"); for (i = 0; i < rf_len; i++) USART1_SendByte(buff_rf[i], HEX); USART1_SendChar('\n'); #endif if (ThesisProcess(buff_rf, rf_len) == THESIS_OK) { memset(buff_rf, 0, rf_len); rf_len = 0; if (thesis_need_to_send) { int i; #if DEBUG USART1_SendStr("\nNeed to send packet: "); for (i = 0; i < thesis_msg_len; i++) { // Enrf24_write(thesis_sent_msg[i]); USART1_SendByte(thesis_sent_msg[i], HEX); } #endif Enrf24_write_buff(thesis_sent_msg, thesis_msg_len); Enrf24_flush(); #if DEBUG USART1_SendStr("\nNeed to send packet length: "); USART1_SendNum(thesis_msg_len); USART1_SendStr("\n"); #endif thesis_msg_len = 0; thesis_need_to_send = 0; } #if DEBUG USART1_SendStr("\nPacket processed.\n"); #endif } else if (thesis_errn == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif led_toggle(); for(;;); } else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) { memset(buff_rf, 0, rf_len); // RF_Flush(); rf_len = 0; #if DEBUG USART1_SendStr("Packet processing fail.\n"); #endif } #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif } // rs485 rs485_len = RS485_Available(); if (rs485_len > 4) { int i; RS485_GetData(buff_rs485, rs485_len); #if DEBUG USART1_SendStr("\nUSART1 received packet: \n"); for (i = 0; i < rs485_len; i++) USART1_SendByte(buff_rs485[i], HEX); USART1_SendChar('\n'); #endif if (ThesisProcess(buff_rs485, rs485_len) == THESIS_OK) { memset(buff_rs485, 0, rs485_len); RS485_Flush(); if (thesis_need_to_send) { int i; #if DEBUG USART1_SendStr("\nNeed to send packet: "); #endif RS485_DIR_Output(); for (i = 0; i < thesis_msg_len; i++) { RS485_SendChar(thesis_sent_msg[i]); #if DEBUG USART1_SendByte(thesis_sent_msg[i], HEX); #endif } RS485_DIR_Input(); #if DEBUG USART1_SendStr("\nNeed to send packet length: "); USART1_SendNum(thesis_msg_len); USART1_SendStr("\n"); #endif thesis_msg_len = 0; thesis_need_to_send = 0; } #if DEBUG USART1_SendStr("\nPacket processed.\n"); #endif } else if (thesis_errn == THESIS_FLASH_ERROR) { #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif led_toggle(); for(;;); } else if (thesis_errn != THESIS_PACKET_NOT_ENOUGH_LENGTH) { memset(buff_rs485, 0, rs485_len); RS485_Flush(); #if DEBUG USART1_SendStr("Packet processing fail.\n"); #endif } #if DEBUG USART1_SendStr("\n"); USART1_SendStr(thesis_err_msg); USART1_SendStr("\n"); #endif } } }
void main(void) { CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1); Delay_Init(); RS485_Init(115200); #if DEBUG // UART_Init(115200); #endif SRF05_Init(); SRF05_AutoPoolEnable(); //flash_read_buffer((char *)&my_data, sizeof (struct flash_data)); my_data.id = 0x21; // UART_SendByte(0x21, HEX); // RS485_SendStr("Hello world.\n"); // LED run GPIO_Init(LED_RUN_PORT, LED_RUN_PIN, GPIO_MODE_OUT_PP_HIGH_FAST); GPIO_WriteHigh(LED_RUN_PORT, LED_RUN_PIN); while (1) { if (Millis() - tmp_time > 500) { LED_RUN_TOGGLE; tmp_time = Millis(); #if DEBUG RS485_DIR_OUTPUT; RS485_SendStr("\n"); RS485_SendFloat(SRF05_GetDistance()); RS485_DIR_INPUT; #endif } if (GPIO_ReadInputPin(RS485_SEL_PORT, RS485_SEL_PIN) == RESET) { if (RS485_Available() >= 8) { // memset(packet_buff, 0, PACKET_BUFFER_SIZE); packet_len = RS485_GetData(packet_buff); packet = (struct Packet *)packet_buff; if (packet_len < 4 + getTypeLength(packet->data_type)) { // not enough length } else { switch (packet->cmd) { case CMD_QUERY: if (packet->id == my_data.id) { LED_RUN_TOGGLE; tmp_distance = SRF05_GetDistance(); packet->data_type = TYPE_FLOAT | BIG_ENDIAN_BYTE_ORDER; memcpy(packet->data, &tmp_distance, getTypeLength(packet->data_type)); packet->data[getTypeLength(packet->data_type)] = checksum((char *)packet); RS485_DIR_OUTPUT; RS485_SendData(packet_buff, 4 + getTypeLength(packet->data_type)); RS485_DIR_INPUT; } else if (IS_BROADCAST_ID(packet->id)) { // if (GPIO_ReadInputPin(RS485_SEL_PORT, RS485_SEL_PIN) == RESET) // { LED_RUN_TOGGLE; packet->id = my_data.id; tmp_distance = SRF05_GetDistance(); packet->data_type = TYPE_FLOAT | BIG_ENDIAN_BYTE_ORDER; memcpy(packet->data, &tmp_distance, getTypeLength(packet->data_type)); packet->data[getTypeLength(packet->data_type)] = checksum((char *)packet); RS485_DIR_OUTPUT; RS485_SendData(packet_buff, 4 + getTypeLength(packet->data_type)); RS485_DIR_INPUT; // } } else { // not own id } break; case CMD_CONTROL: // by default, this mode used only for setting id // the id stored on the first bytes of data bytes if (IS_BROADCAST_ID(packet->id)) { LED_RUN_TOGGLE; if (IS_SENSOR_ULTRA_SONIC(packet->data[0])) { my_data.id = packet->data[0]; } } break; default: break; } RS485_Flush(); } } } else { RS485_Flush(); } SRF05_ProcessTrigger(); } }