PWM
This commit is contained in:
parent
7717393c2c
commit
48f5e8f397
@ -1,7 +1,9 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
|
||||
#include "BLEh.h"
|
||||
#include "driver/ledc.h"
|
||||
#include "esp_err.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
@ -9,6 +11,7 @@
|
||||
#include "freertos/projdefs.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/portmacro.h"
|
||||
#include "hal/ledc_types.h"
|
||||
#include "host/ble_uuid.h"
|
||||
|
||||
#include "nvs_flash.h"
|
||||
@ -17,11 +20,12 @@
|
||||
|
||||
#include "BLE.h"
|
||||
#include "measure.h"
|
||||
#include "PWM.h"
|
||||
|
||||
#define TAG "Main"
|
||||
|
||||
static uint32_t switching_frequency = CONFIG_SW_FREQ;
|
||||
static uint32_t duty_cycle = 50000000;
|
||||
static uint32_t duty_cycle = 0;
|
||||
|
||||
static uint8_t adc_pins[] = {CONFIG_OUT_VOLT_PIN, CONFIG_OUT_CURR_PIN, CONFIG_PHASE1_CURR_PIN, CONFIG_PHASE2_CURR_PIN, CONFIG_PHASE3_CURR_PIN};
|
||||
|
||||
@ -54,12 +58,17 @@ int on_char_write_handler(int svc_ind, int chr_ind, struct os_mbuf* os){
|
||||
;
|
||||
rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL);
|
||||
switching_frequency = val;
|
||||
// TODO update running val
|
||||
ESP_ERROR_CHECK(ledc_set_freq(LEDC_LOW_SPEED_MODE, LEDC_TIMER_0, switching_frequency));
|
||||
break;
|
||||
case DUTY_CYCLE_CHR_ID:
|
||||
rc = gatt_svr_chr_write_get_data(os, sizeof(val), sizeof(val), &val, NULL);
|
||||
duty_cycle = val;
|
||||
// TODO update running val
|
||||
|
||||
uint32_t max_value = 1 << CONFIG_SW_RES;
|
||||
uint32_t raw_value = max_value * duty_cycle / 100000;
|
||||
ESP_LOGI(TAG, "setting duty cycle %lu/%lu", raw_value, max_value);
|
||||
ESP_ERROR_CHECK(ledc_set_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0, raw_value));
|
||||
ESP_ERROR_CHECK(ledc_update_duty(LEDC_LOW_SPEED_MODE, LEDC_CHANNEL_0));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
@ -72,6 +81,8 @@ void app_main(void){
|
||||
init_adc(adc_pins);
|
||||
init_adc_cali();
|
||||
adc_start();
|
||||
|
||||
init_pwm();
|
||||
|
||||
nvs_flash_init();
|
||||
|
||||
|
@ -32,7 +32,7 @@ struct dsc_content switching_frequency_dsc = {
|
||||
struct char_pres_format duty_cycle_char_pres_format = {
|
||||
.format = FORMAT_UINT32,
|
||||
.unit = UNITLESS_UNIT_UUID,
|
||||
.exponent = -6,
|
||||
.exponent = -3,
|
||||
.namespc = 1,
|
||||
.descrH = NSP_DESC_MAIN & 0xff,
|
||||
.descrL = (NSP_DESC_MAIN>>8) & 0xff,
|
||||
|
@ -1,4 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "Alim1000W.c" "BLE.c" "measure.c"
|
||||
SRCS "Alim1000W.c" "BLE.c" "measure.c" "PWM.c"
|
||||
INCLUDE_DIRS "."
|
||||
)
|
||||
|
27
main/PWM.c
Normal file
27
main/PWM.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include "driver/ledc.h"
|
||||
#include "hal/ledc_types.h"
|
||||
|
||||
#include "sdkconfig.h"
|
||||
|
||||
void init_pwm(){
|
||||
ledc_timer_config_t ledc_timer = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.timer_num = LEDC_TIMER_0,
|
||||
.duty_resolution = CONFIG_SW_RES,
|
||||
.freq_hz = CONFIG_SW_FREQ,
|
||||
.clk_cfg = LEDC_AUTO_CLK,
|
||||
};
|
||||
|
||||
ESP_ERROR_CHECK(ledc_timer_config(&ledc_timer));
|
||||
|
||||
ledc_channel_config_t ledc_channel = {
|
||||
.speed_mode = LEDC_LOW_SPEED_MODE,
|
||||
.channel = 0,
|
||||
.timer_sel = LEDC_TIMER_0,
|
||||
.intr_type = LEDC_INTR_DISABLE,
|
||||
.gpio_num = CONFIG_PHASE1_CON_PIN,
|
||||
.duty = 0,
|
||||
.hpoint = 0,
|
||||
};
|
||||
ESP_ERROR_CHECK(ledc_channel_config(&ledc_channel));
|
||||
}
|
1
main/PWM.h
Normal file
1
main/PWM.h
Normal file
@ -0,0 +1 @@
|
||||
void init_pwm();
|
@ -1659,8 +1659,8 @@ CONFIG_PHASE3_CURR_PIN=4
|
||||
#
|
||||
# Phases control signal pins
|
||||
#
|
||||
CONFIG_PHASE1_CON_PIN=6
|
||||
CONFIG_PHASE2_CON_PIN=7
|
||||
CONFIG_PHASE1_CON_PIN=7
|
||||
CONFIG_PHASE2_CON_PIN=6
|
||||
CONFIG_PHASE3_CON_PIN=8
|
||||
# end of Phases control signal pins
|
||||
# end of Pins definition
|
||||
|
@ -1641,6 +1641,7 @@ CONFIG_WPA_MBEDTLS_TLS_CLIENT=y
|
||||
#
|
||||
# Pins definition
|
||||
#
|
||||
CONFIG_ADC_IN_PIN_NUM=5
|
||||
CONFIG_OUT_VOLT_PIN=2
|
||||
CONFIG_OUT_OVERVOLT_PIN=5
|
||||
CONFIG_OUT_EN_PIN=9
|
||||
@ -1669,7 +1670,7 @@ CONFIG_PHASE3_CON_PIN=8
|
||||
#
|
||||
CONFIG_SW_FREQ=800000
|
||||
CONFIG_SW_RES=6
|
||||
CONFIG_UPD_DELAY=100
|
||||
CONFIG_UPD_DELAY=1000
|
||||
# end of Controller parameters
|
||||
# end of This project config
|
||||
# end of Component config
|
||||
|
Loading…
x
Reference in New Issue
Block a user