You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
211 lines
6.7 KiB
211 lines
6.7 KiB
#include "main.h"
|
|
|
|
#include <stdbool.h> //定义布尔
|
|
#include <string.h>
|
|
|
|
#include "adc.h"
|
|
#include "board.h"
|
|
#if 0
|
|
|
|
#define PWM_FREQHZ 25000 //硬件pwm频率
|
|
/***********************************************************************************************************************
|
|
* =====================================================函数声明====================================================== *
|
|
***********************************************************************************************************************/
|
|
void onkey(zkey_t* key, zkey_state_t key_state);
|
|
/***********************************************************************************************************************
|
|
* =====================================================全局变量====================================================== *
|
|
***********************************************************************************************************************/
|
|
|
|
static zkey_t s_keys[] = {
|
|
ZKEY_INIT("timerkey", port_gpio_get_timer_key_state), //左2
|
|
ZKEY_INIT("levelkey", port_gpio_get_level_key_state), //左1
|
|
ZKEY_INIT("intervalkey", port_gpio_get_interval_key_state), //左3
|
|
ZKEY_INIT("powerkey", port_gpio_get_power_key_state),
|
|
};
|
|
zkey_module_t key_module = ZMODULE_INIT(s_keys, onkey);
|
|
|
|
void onkey(zkey_t* key, zkey_state_t key_state) {
|
|
if (key == &s_keys[2] /*左3*/ && zks_falling_edge == key_state) {
|
|
ozone_pwm_control_update();
|
|
printf("onkey2\n");
|
|
}
|
|
if (key == &s_keys[0] /*左2*/ && zks_falling_edge == key_state) {
|
|
ozone_pwm_control_update();
|
|
printf("onkey0\n");
|
|
}
|
|
if (key == &s_keys[1] /*左2*/ && zks_falling_edge == key_state) {
|
|
}
|
|
if (key == &s_keys[3] /*左2*/ && zks_falling_edge == key_state) {
|
|
printf("onkey3\n");
|
|
ozone_pwm_control_update();
|
|
}
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* =================================================INTERNAL_FUNCTION================================================= *
|
|
***********************************************************************************************************************/
|
|
void UART0_IRQHandler(void) {
|
|
/**
|
|
* @brief 接收来自串口的数据并做回显
|
|
*
|
|
*/
|
|
// rx_data = UART_RecByte(UART0);
|
|
// if (!s_rxbuf_is_ready) {
|
|
// if (rx_count < sizeof(rx_buffer) - 1) {
|
|
// rx_buffer[rx_count++] = rx_data;
|
|
// uart0_lastreceive_ticket = get_sys_ticket();
|
|
// }
|
|
// }
|
|
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_TB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_RB);
|
|
UART_ClearITPendingBit(UART0, UART_FLAG_FE);
|
|
}
|
|
|
|
/**
|
|
* @brief
|
|
*
|
|
* @param freq
|
|
* @param us
|
|
*/
|
|
|
|
float get_duty_by_freq_and_valid_time(uint32_t freq, uint32_t us) {
|
|
float period = 1000000.0 / freq;
|
|
float duty = us * 100 / period;
|
|
return duty;
|
|
}
|
|
|
|
void ozone_pwm_control_update() {
|
|
printf("freqhz:%d duty:%d us\n", freqhz, dutyus);
|
|
set_pwm_modbul_freq_duty2(freqhz, get_duty_by_freq_and_valid_time(freqhz, dutyus));
|
|
}
|
|
|
|
#define DO_IT_EACH_MS(time) \
|
|
{ \
|
|
static uint32_t __ticket = 0; \
|
|
if (port_haspassedms(__ticket) > (time)) { \
|
|
__ticket = get_sys_ticket();
|
|
|
|
#define END() \
|
|
} \
|
|
}
|
|
|
|
/***********************************************************************************************************************
|
|
* ======================================================主函数=======================================================
|
|
***********************************************************************************************************************/
|
|
|
|
bool strcontains(const char* sonstr, const char* fatherstr) { return (strncmp(sonstr, fatherstr, strlen(sonstr)) == 0); }
|
|
|
|
void process_rx_order() {
|
|
if (strcontains("AT+PWM_FREQ_UP_FINE_TUNING", rx_buffer)) {
|
|
freqhz += 50;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_DOWN_FINE_TUNING", rx_buffer)) {
|
|
freqhz -= 50;
|
|
if (freqhz <= 0) freqhz = 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_UP", rx_buffer)) {
|
|
freqhz += 1000;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_FREQ_DOWN", rx_buffer)) {
|
|
freqhz -= 1000;
|
|
if (freqhz <= 0) freqhz = 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_DUTY_UP", rx_buffer)) {
|
|
dutyus += 1;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM_DUTY_DOWN", rx_buffer)) {
|
|
dutyus -= 1;
|
|
if (dutyus < 0) dutyus = 0;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+PWM=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int _dutyus = 0;
|
|
int _freq = 0;
|
|
sscanf(rx_buffer, "AT+PWM=%d,%d", &_freq, &_dutyus);
|
|
if (_dutyus < 0 || _freq < 10000) {
|
|
printf("Error\n");
|
|
return;
|
|
}
|
|
dutyus = _dutyus;
|
|
freqhz = _freq;
|
|
ozone_pwm_control_update();
|
|
} else if (strcontains("AT+MAT2=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int mat2 = 0;
|
|
sscanf(rx_buffer, "AT+MAT2=%d", &mat2);
|
|
set_pwm_mat2(mat2);
|
|
} else if (strcontains("AT+FAN=", rx_buffer)) {
|
|
/**
|
|
* AT+PWM=25000,8
|
|
*/
|
|
int mat2 = 0;
|
|
sscanf(rx_buffer, "AT+FAN=%d", &mat2);
|
|
if (mat2 > 0) {
|
|
port_fan_set(true);
|
|
|
|
} else {
|
|
port_fan_set(false);
|
|
}
|
|
}
|
|
}
|
|
|
|
void try_process_rx_order() {
|
|
if (port_haspassedms(uart0_lastreceive_ticket) > 100) {
|
|
if (rx_count != 0) {
|
|
s_rxbuf_is_ready = true;
|
|
process_rx_order();
|
|
rx_count = 0;
|
|
memset(rx_buffer, 0, sizeof(rx_buffer));
|
|
s_rxbuf_is_ready = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
float power_filter(float nowpower, float factor) {
|
|
static float last = 0;
|
|
float now_raw = nowpower;
|
|
float now = ((float)last * (1.0f - factor)) + ((float)now_raw * factor);
|
|
last = now;
|
|
return now;
|
|
}
|
|
|
|
uint16_t capture(int timeoff);
|
|
#endif
|
|
int main(void) {
|
|
HRC_Config(Enable, SCU_HRC_48M, Enable); //时钟源SCU_CLK_HRC
|
|
SystemInit(); //
|
|
DeviceClockAllEnable(); //打开所有外设时钟
|
|
|
|
/**
|
|
* @brief 初始化系统串口
|
|
*/
|
|
|
|
// keil勾选Use MicroLIB 2、IAR/keil #define __PRINTF_USE_UART0__
|
|
|
|
|
|
#if 0
|
|
//硬件初始化
|
|
unused_gpio_init();
|
|
gpio_init();
|
|
uart0_init();
|
|
t16_pa4_init();
|
|
//模块初始化
|
|
zkey_init(&key_module);
|
|
//上电默认开机并高档工作
|
|
port_fan_set(true);
|
|
//启动结束
|
|
printf("Initialization completed \r\n");
|
|
printf("version:%s\r\n", VERSION);
|
|
ozone_pwm_control_update();
|
|
// stop_pwm_output();
|
|
|
|
while (true) {
|
|
// End..................................
|
|
}
|
|
#endif
|
|
}
|