/* ********************************************************************************************************* * 函 数 名: bsp_InitLed * 功能说明: 配置LED指示灯相关的GPIO, 该函数被 bsp_Init() 调用。 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ void bsp_InitLed(void) { GPIO_InitTypeDef GPIO_InitStructure; /* 打开GPIO时钟 */ RCC_AHB1PeriphClockCmd(RCC_ALL_LED, ENABLE); /* 配置所有的LED指示灯GPIO为推挽输出模式 由于将GPIO设置为输出时,GPIO输出寄存器的值缺省是0,因此会驱动LED点亮. 这是我不希望的,因此在改变GPIO为输出前,先关闭LED指示灯 */ bsp_LedOff(1); bsp_LedOff(2); bsp_LedOff(3); bsp_LedOff(4); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; /* 设为输出口 */ GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; /* 设为推挽模式 */ GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; /* 上下拉电阻不使能 */ GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; /* IO口最大速度 */ GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED1; GPIO_Init(GPIO_PORT_LED1, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED2; GPIO_Init(GPIO_PORT_LED2, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED3; GPIO_Init(GPIO_PORT_LED3, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_PIN_LED4; GPIO_Init(GPIO_PORT_LED4, &GPIO_InitStructure); }
/************************************************************************************************************** * Function Name : bsp_LedInit * Description : Configures LED on GPIO * Input Variable : None * Return Variable : None * Author : Bryant * Create Date : Sep-29-2015 * Call : Outside ****************************************************************************************************************/ void bsp_LedInit(void) { GPIO_InitTypeDef GPIO_InitStruct; Enum_LedName ledName; for(ledName = 0;ledName < g_ledNumber;ledName++) { bsp_GPIOxClockEnable(LED[ledName].LED_PORT); /* Enable GPIO Port clock */ /* Config as push-pull */ GPIO_InitStruct.Pin = LED[ledName].LED_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(LED[ledName].LED_PORT,&GPIO_InitStruct); if (LED[ledName].LED_INIT == LED_ON) { bsp_LedOn(ledName); } else { bsp_LedOff(ledName); } } }
/**************************************************************************************************************** * Function Name : bsp_LedInit * Description : Configures LED on GPIO * Input Variable : None * Return Variable : None * Author : Bryant * Create Date : Feb-27-2016 * Call : Outside *****************************************************************************************************************/ void bsp_LedInit(void) { GPIO_InitTypeDef GPIO_InitStruct; LED_NameTypeDef ledName; for(ledName = g_ledInit[0].LedName;ledName < (LED_NameTypeDef)g_ledNumber;ledName++) { bsp_GPIOxClockEnable(g_ledInit[ledName].LedPort); /* Enable GPIO Port clock */ /* Config as push-pull */ GPIO_InitStruct.Pin = g_ledInit[ledName].LedPin; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(g_ledInit[ledName].LedPort,&GPIO_InitStruct); /* Set the led to its initial status */ if (g_ledInit[ledName].LedInitStatus == LED_ON) { bsp_LedOn(ledName); } else { bsp_LedOff(ledName); } } }
void SolarArraySim(void) { uint16_t i,j; static uint16_t i_SAS; // float v_set[chan_num]; float pwm_out[chan_num]; //pid参数初始化; SAS_PID_Init(); for (j=0;j<chan_num;j++) { SAS_EN(j,DISABLE); } #if USER_SAS_SIM_EN > 0u V_value[0] =7.0f; // V_value[1] =9.0f,V_value[2] =12.0f; C_value[0] =0.6f; // C_value[1] =0.3f,C_value[2] =0.2f; R_value[0] = V_value[0]/C_value[0]; // R_value[1] = V_value[1]/C_value[1]; // R_value[2] = V_value[2]/C_value[2]; #endif while(1) { #if USER_SAS_EN > 0u //对没一个PV模型,若其参数有改动,则进行重新配置,生成新的IV曲线 for (i=0;i<model_num;i++) { switch (PV_model[i].PVpara.flag) { //若参数变化则跳转到参数变化分支更改参数, //参数修改后顺序执行,修改曲线列表,执行修改后程序 //若无参数修改出现则只执行程序执行部分 case 1: //电池片类型参数变化 PV_ChangeType(&PV_model[i],&PV_para[i]); //IV_generate(&PV_model[i]); //break; case 2: //电池片输入条件变化 PV_ChangeCondition(&PV_model[i],&PV_para[i]); IV_generate(&PV_model[i]); #if USER_DEBUG_EN >0u bsp_LedOff(2); for (j=0;j<PVdiv_num;j++) { printf("IV_I[%d] %f2\r\n",i,PV_model[i].IV[0][j]); } printf("PV[%d]->position_prev %d \r\n",i,PV_model[i].position_prev); printf("PV[%d]->position_mppt %d \r\n",i,PV_model[i].position_mppt); printf("PV[%d]->position_nxt %d \r\n",i,PV_model[i].position_nxt); printf("PV[%d]->position_current %d \r\n",i,PV_model[i].position_current); bsp_LedOn(2); #endif if (i == model_num-1) { for (j=0;j<model_num;j++) { SAS_EN(j,ENABLE); } } //break; case 0: //无条件变化,执行输出控制程序 break; default: break; } } if (i_SAS++ >= 100) { i_SAS = 0; SolarArraySim_Algorithm(&PV_model[0],&V_value[0], &C_value[0], &PV_num[0]); } for (i=0; i<model_num;i++) { PV_para[i].flag = 0; PV_model[i].PVpara.flag = 0; } #endif //ADC_Sample(); #if USER_SAS_SIM_EN == 0u for (i=0;i<chan_num;i++) { V_value[i] = Value_Real[i]*V_Div; C_value[i] = Value_Real[i+3]/C_Multi; #if USER_DEBUG_EN >0u printf("\r\nV_value[%d] = %f \r\n",i,V_value[i]); printf("\r\nC_value[%d] = %f \r\n",i,C_value[i]); #endif } #endif // V_value[0] = Value_Real[0]*V_Div; // v_set[0] = 12.0f; printf("v_value = %2.2f\r\n",V_value[0]); PID_BUCK_SAS_var_V.SetValue = v_set[0]; printf("v_set = %2.2f\r\n",v_set[0]); pwm_out[0] = Cal_IncPID(&PID_BUCK_SAS_V, &PID_BUCK_SAS_var_V,V_value[0]); printf("pwm_out[0] = %4.2f\r\n",pwm_out[0]); TIM4_CCR_Val[2] = (uint16_t)pwm_out[0]; #if USER_DEBUG_EN >0u printf("TIM4 CH3 OUT %4d\r\n",TIM4_CCR_Val[2]); #endif PWM_Updata(TIM4,TIM4_CCR_Val[0],TIM4_CCR_Val[1],TIM4_CCR_Val[2],TIM4_CCR_Val[3]); // // PWM_Updata(TIM4,400,200,0,0); OSTimeDlyHMSM(0, 0, 0, SAS_DELAY_TIME ); } }
/* 充放电结束后放置一小时 */ int delay_hour(void) { FRESULT result; FATFS fs; FIL file; DIR DirInf; uint32_t bw; char time_buf[TIME_MAX]; char write_buf[WR_MAX]; char tmp_buf[8]; int sec = 0; int i = 0; int min_count = 0; double vol; result = f_mount(0, &fs); if (result != FR_OK){ printf("FileSystem Mounted Failed (%d)\r\n", result); goto ERROR; } result = f_opendir(&DirInf, "/"); if (result != FR_OK){ printf("Open Root Directory Error (%d)\r\n", result); goto ERROR; } result = f_open(&file, conf->file_name, FA_OPEN_ALWAYS | FA_READ | FA_WRITE); if(result !=FR_OK){ printf("open error, errornu:%d\n\r",result); goto ERROR; } if(f_lseek(&file,file.fsize) != FR_OK){ printf("f_lseek error, errornu:%d\n\r",result); goto ERROR; } beeper_on(); delay_ms(200); beeper_off(); memset(time_buf,0,TIME_MAX); memset(write_buf,0,WR_MAX); printf("\n\r静置开始"); Time_Display(RTC_GetCounter()); printf("\n\r"); bsp_LedOn(4); StartTimer(0, 1000); // Time_Display(RTC_GetCounter()); do{ if(jump) goto JMP; if(CheckTimer(0) ) { StartTimer(0, 1000); sec++; min_count ++; vol = ADC_SAMPLING(); if(min_count >= 60){ min_count = 0; record_time(time_buf); strcat(time_buf,","); strcat(write_buf, time_buf); memset(time_buf,0,TIME_MAX); sprintf(tmp_buf, "%.2f", vol); strcat(write_buf, tmp_buf); strcat(write_buf,",静置\n"); if(i >= 9){ result = f_write(&file,write_buf,sizeof(write_buf),&bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } memset(write_buf,0,WR_MAX); i = 0; } i++; } } }while(sec < 3600); /* 静置时间(单位:秒) */ JMP: jump = 0; if(i != 0){ result = f_write(&file,write_buf,(15 + TIME_MAX)*i, &bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } i = 0; } memset(write_buf,0,WR_MAX); printf("静置结束"); Time_Display(RTC_GetCounter()); printf("\n\r"); bsp_LedOff(4); f_close(&file); f_mount(0, NULL); return 0; ERROR: bsp_LedOff(4); f_close(&file); f_mount(0, NULL); return 1; }
/*********************************************** 电池放电 ***********************************************/ int dis_charge(void) { FRESULT result; FATFS fs; FIL file; DIR DirInf; uint32_t bw; char time_buf[TIME_MAX]; char write_buf[WR_MAX]; char tmp_buf[8]; int dis_time = 0; int i = 0; int min_count = 0; double vol; result = f_mount(0, &fs); if (result != FR_OK){ printf("FileSystem Mounted Failed (%d)\r\n", result); goto ERROR; } result = f_opendir(&DirInf, "/"); if (result != FR_OK){ printf("Open Root Directory Error (%d)\r\n", result); goto ERROR; } result = f_open(&file, conf->file_name, FA_OPEN_ALWAYS | FA_READ | FA_WRITE); if(result !=FR_OK){ printf("open error, errornu:%d\n\r",result); goto ERROR; } if(f_lseek(&file,file.fsize) != FR_OK){ printf("f_lseek error, errornu:%d\n\r",result); goto ERROR; } load_con_on(); //打开负载通道LOAD_CON bsp_LedOn(3); beeper_on(); delay_ms(200); beeper_off(); memset(time_buf,0,TIME_MAX); memset(write_buf,0,WR_MAX); StartTimer(0, 1000); // Time_Display(RTC_GetCounter()); // vol = ADC_SAMPLING(); do{ if(jump) goto JMP; if(CheckTimer(0)) { dis_time ++; StartTimer(0, 1000); min_count ++; vol = ADC_SAMPLING(); if(min_count >=60){ min_count = 0; memset(time_buf,0,TIME_MAX); record_time(time_buf); strcat(time_buf,","); strcat(write_buf, time_buf); memset(time_buf,0,TIME_MAX); sprintf(tmp_buf, "%.2f", vol); strcat(write_buf, tmp_buf); strcat(write_buf,",放电\n"); if(i > 9){ result = f_write(&file,write_buf,sizeof(write_buf),&bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } memset(write_buf,0,WR_MAX); i = 0; } i++; } } }while(dis_charge_timeout(vol)); JMP: record[rec]= dis_time/60; rec++; jump = 0; if(i != 0){ result = f_write(&file,write_buf,35*i, &bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } i = 0; } memset(write_buf,0,WR_MAX); bsp_LedOff(3); //放电结束 load_con_off(); f_close(&file); f_mount(0, NULL); return 0; //关闭负载通道LOAD_CON ERROR: bsp_LedOff(3); f_close(&file); f_mount(0, NULL); return 1; }
/*********************************************************** 电池充电 **************************************************************/ int charge(void) { FRESULT result; FATFS fs; FIL file; DIR DirInf; uint32_t bw; char time_buf[TIME_MAX]; char write_buf[WR_MAX]; char tmp_buf[8]; int i = 0; int min_count = 0; int adc_value = 0; int adc_tmp = 0; int flag = 0; int times = 0; double charge_time = 0; double vol; /* 挂载文件系统,打开文件,定位文件指针 */ result = f_mount(0, &fs); if (result != FR_OK){ printf("FileSystem Mounted Failed (%d)\r\n", result); goto ERROR; } result = f_opendir(&DirInf, "/"); if (result != FR_OK){ printf("Open Root Directory Error (%d)\r\n", result); goto ERROR; } result = f_open(&file, conf->file_name, FA_OPEN_ALWAYS | FA_READ | FA_WRITE); if(result !=FR_OK){ printf("open error, errornu:%d\n\r",result); goto ERROR; } if(f_lseek(&file,file.fsize) != FR_OK){ printf("f_lseek error, errornu:%d\n\r",result); goto ERROR; } beeper_on(); /* 蜂鸣器提示 */ delay_ms(200); beeper_off(); load_con_off(); /*负载通道关闭 */ charge_con_on(); /*打开CHAR_CON充电通道 */ bsp_LedOn(2); memset(time_buf,0,TIME_MAX); /*清空数组,防止乱码*/ memset(write_buf,0,WR_MAX); StartTimer(0, 100); adc_value = GetADC(); do{ if(jump) goto JMP; if(CheckTimer(0)) { vol = ADC_SAMPLING(); charge_time ++; min_count ++; StartTimer(0, 1000); adc_tmp = GetADC(); if((abs(adc_value - adc_tmp) > 3) || (times != 0)){ if(abs(adc_value - adc_tmp) <= 3){ times = 0; } else{ times++; if(times >= 10){ times = 0; flag = 0; adc_value = adc_tmp; } } } else if((abs(adc_value - adc_tmp) <= 3) && (times == 0)){ flag ++; } if(min_count >= 60){ /* 每隔一分钟采样一次 */ min_count = 0; record_time(time_buf); strcat(time_buf,","); strcat(write_buf, time_buf); memset(time_buf,0,TIME_MAX); sprintf(tmp_buf, "%.2f", vol); strcat(write_buf, tmp_buf); strcat(write_buf,",充电\n"); if(i > 9){ /* 10次采样将数据记录到flash中 */ result = f_write(&file,write_buf,sizeof(write_buf),&bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } memset(write_buf,0,WR_MAX); i = 0; } i++; } } }while(charge_timeout(charge_time/3600, vol, flag)); /* 充电时间 */ JMP: jump = 0; if(i != 0){ /* 最后将不足10次的数据记录到flash中 */ result = f_write(&file,write_buf,(15 + TIME_MAX)*i, &bw); if(result != FR_OK){ printf("file write faild, errornu:%d\n\r",result); goto ERROR; } i = 0; } memset(write_buf,0,WR_MAX); charge_con_off(); /* 关闭CHAR_CON充电通道 */ bsp_LedOff(2); /* 关闭文件,卸载文件系统 */ f_close(&file); f_mount(0, NULL); return 0; ERROR: /* 错误处理 */ bsp_LedOff(2); f_close(&file); f_mount(0, NULL); return 1; }