1簡介
本文主要介紹基于HPM6750 EVK Mini板驅動點亮LED三色燈,從而讓用戶能快速熟悉先楫MCU PWM定時器原理和HPM_SDK Driver API的使用方法。
本文檔中有關HPM6750 EVK mini板的使用說明可以參考位于SDK軟件包中\Doc目錄下的HPM6750EVKMINI_UG.pdf。
本文檔中有關HPM6750 EVK mini 硬件原理圖,請查閱HPM6750EVKMINI硬件設計資料\原理圖\HPM6750EVKMINI Rev.B.pdf。
本文檔中有關HPM6750 MCU datasheet,請查閱HPM6750_DS.pdf;有關HPM6750 MCU user manual請查閱HPM6750_UM.pdf。
本文將基于HPM SDK中pwm driver api接口抽象封裝針對LED燈的adapte接口,并通過adapte接口實現LED三色燈的開關和呼吸效果。
2 環境準備
2.1PWM定時器原理
查閱HPM6750 MCU user manual文檔中章節:電機系統->45 PWM定時器PWM內容。
PWM的框圖
大致介紹:
l 先楫PWM定時器由4個模塊組成:定時器時間基準模塊、比較器模塊、輸出通道模塊、PWM輸出控制模塊。
l 定時器時間基準模塊決定PWM定時器運行的時間和周期。通過讀寫計數器、起始寄存器、重載寄存器、計數器使能來設置定時器時間基準模塊。
l PWM生成通過定時器時間基準模塊配合比較器以及輸出通道來生成輸出參考信號。一個輸出通道可匹配多個比較器(注意:匹配多個比較器必須連續),當計數器的值等于比較器的值時,產生匹配事件邏輯1,當計數器到達重載數值時,產生重載事件邏輯0。如果比較器的值和重載值相同,則會保持邏輯1產生100%占空比輸出,如果比較器的值>重載值,則會保持邏輯0產生0%占空比輸出。
l 輸出通道輸出的參考信號經過PWM輸出控制模塊處理互補控制、死區插入、取反控制、強制輸出、故障保護后,形成輸出信號到IO口。如果不對輸出控制模塊操作則透傳輸出通道參考信號。由于LED RGB三色燈使用不到以上,本文檔不介紹PWM輸出控制模塊。
l 針對LED 簡單的PWM波形,只需要一個比較器即可實現。波形實現如圖:
波形實現如圖
PWM頻率:總線時鐘頻率/重載值。
占空比:比較器值/總步長 * 100。
2.2PWM Driver API梳理
查閱HPM SDK軟件API手冊中PWM driver APIs章節。
針對LED 簡單波形使用到的接口有:
//重載值設置pwm_set_reload//計數器起始值設置pwm_set_start_count//比較器配置pwm_config_cmp//PWM波形設置(輸出通道和比較器匹配設置)pwm_setup_waveform//加載比較器影子寄存器匹配設置
pwm_load_cmp_shadow_on_match//計時器使能計數pwm_start_counter//計數器禁止計數pwm_stop_counter//更新比較器邊沿觸發值(調整占空比)pwm_update_raw_cmp_edge_aligned//使能PWM通道輸出pwm_enable_output
//禁止PWM通道輸出pwm_disable_output
2.3確認LED 硬件PIN腳
查閱HPM6750 EVK mini 硬件原理圖,確認LED三色燈三路PWM通道和管腳,分別為:
RED: PWM0.P7 —— PB20
GREEN: PWM1.P1 —— PB18
BLUE: PWM1.P0 —— PB19
3 接口封裝
3.1PWM初始化
pwm_port_handle_t hpm_pwm_init(PWM_Type* pwm_id, uint32_t pwm_out, uint32_t pwm_cmp, bool init_zero, clock_name_t clock_name){ led_pwm_t *handle = malloc(sizeof(led_pwm_t)); if(NULL == handle) return NULL; memset(handle, 0, sizeof(led_pwm_t)); handle->pwm = pwm_id; handle->reload = 0; handle->step = 0; handle->pwm_irq = 0; handle->pwm_cmp = pwm_cmp; handle->pwm_ch = pwm_out; handle->pwm_cmp_initial_zero = init_zero; handle->bus_freq = clock_get_frequency(clock_name); return (pwm_port_handle_t)handle;}
handle->pwm_irq = 0; handle->pwm_cmp = pwm_cmp; handle->pwm_ch = pwm_out; handle->pwm_cmp_initial_zero = init_zero; handle->bus_freq = clock_get_frequency(clock_name); return (pwm_port_handle_t)handle;
3.2PWM釋放
void hpm_pwm_deinit(pwm_port_handle_t pwm_handle){ led_pwm_t *handle = (led_pwm_t *)pwm_handle; if(NULL == handle) return; pwm_disable_output(handle->pwm, handle->pwm_ch); free(handle); handle = NULL; return;
3.3PWM運行
#define PWM_DUTY_STEP_COUNT (100U)/*duty: 0~100%, freq: 100~200000000*/int hpm_pwm_start(pwm_port_handle_t pwm_handle, uint32_t duty, uint32_t freq){ led_pwm_t *handle = (led_pwm_t *)pwm_handle; uint32_t reload, now_cmp; pwm_cmp_config_t cmp_config = {0}; pwm_config_t pwm_config = {0}; if(NULL == handle) { return -1; } if(freq > handle->bus_freq) { return -2; } reload = handle->bus_freq / freq - 1; //start 0 index if(reload == 0) return -2; if(reload != handle->reload || handle->reload == 0) { handle->reload = reload; pwm_stop_counter(handle->pwm); pwm_get_default_pwm_config(handle->pwm, &pwm_config); pwm_get_default_cmp_config(handle->pwm, &cmp_config); pwm_config.enable_output = false; pwm_config.dead_zone_in_half_cycle = 0; pwm_config.invert_output = false; /* * reload and start counter */ pwm_set_reload(handle->pwm, 0, handle->reload); pwm_set_start_count(handle->pwm, 0, 0); cmp_config.mode = pwm_cmp_mode_output_compare; cmp_config.cmp = handle->pwm_cmp_initial_zero ? 0 : handle->reload + 1; cmp_config.update_trigger = pwm_shadow_register_update_on_modify; /* config initial compare value which should take affect immediately */ pwm_config_cmp(handle->pwm, handle->pwm_cmp, &cmp_config); /* * config pwm as output driven by cmp */ if (status_success != pwm_setup_waveform(handle->pwm, handle->pwm_ch, &pwm_config, handle->pwm_cmp, &cmp_config, 1)) { printf("failed to setup waveform\n"); return -3; } pwm_load_cmp_shadow_on_match(handle->pwm, handle->pwm_cmp, &cmp_config); handle->step = (handle->reload + 1)/PWM_DUTY_STEP_COUNT; pwm_update_raw_cmp_edge_aligned(handle->pwm, handle->pwm_cmp, handle->pwm_cmp_initial_zero ? 0 : handle->reload + 1); pwm_start_counter(handle->pwm); } if(duty == 100) { now_cmp = handle->reload + 1; } else { now_cmp = handle->step * PWM_DUTY_STEP_COUNT/100 * duty; } pwm_update_raw_cmp_edge_aligned(handle->pwm, handle->pwm_cmp, now_cmp); pwm_enable_output(handle->pwm, handle->pwm_ch); return 0;
3.4PWM停止
nt hpm_pwm_stop(pwm_port_handle_t pwm_handle){ led_pwm_t *handle = (led_pwm_t *)pwm_handle; if(NULL == handle) return -1; pwm_disable_output(handle->pwm, handle->pwm_ch); return 0;
4 功能實現
4.1呼吸燈功能
#define LED_PWM_FREQ 200000 //200khz
int sample_breath_lamp(pwm_port_handle_t handle){ if(NULL == handle) { printf("handle is null\n"); return -1; } for(uint32_t i = 0; i <= 100; i++) { hpm_pwm_start(handle, i, LED_PWM_FREQ); board_delay_ms(10); } for(uint32_t i = 100; i > 0; i--) { hpm_pwm_start(handle, i, LED_PWM_FREQ); board_delay_ms(10); } hpm_pwm_stop(handle); return 0;}
4.2LED點亮呼吸
board_init(); board_init_rgb_pwm_pins(); printf("rgb led example\n"); pwm_port_handle_t red_handle = hpm_pwm_init(RED_PWM, RED_PWM_OUT, RED_PWM_CMP, RED_PWM_CMP_INITIAL_ZERO, RED_PWM_CLOCK_NAME); if(red_handle == NULL) { printf("red pwm init fail!\n"); while(1); } pwm_port_handle_t green_handle = hpm_pwm_init(GREEN_PWM, GREEN_PWM_OUT, GREEN_PWM_CMP, GREEN_PWM_CMP_INITIAL_ZERO, GREEN_PWM_CLOCK_NAME); if(green_handle == NULL) { printf("green pwm init fail!\n"); while(1); } pwm_port_handle_t blue_handle = hpm_pwm_init(BLUE_PWM, BLUE_PWM_OUT, BLUE_PWM_CMP, BLUE_PWM_CMP_INITIAL_ZERO, BLUE_PWM_CLOCK_NAME); if(blue_handle == NULL) { printf("blue pwm init fail!\n"); while(1); } //on led and off led hpm_pwm_start(red_handle, 100, LED_PWM_FREQ); board_delay_ms(1000); hpm_pwm_start(red_handle, 0, LED_PWM_FREQ); board_delay_ms(1000); hpm_pwm_start(green_handle, 100, LED_PWM_FREQ); board_delay_ms(1000); hpm_pwm_start(green_handle, 0, LED_PWM_FREQ); board_delay_ms(1000); hpm_pwm_start(blue_handle, 100, LED_PWM_FREQ); board_delay_ms(1000); hpm_pwm_start(blue_handle, 0, LED_PWM_FREQ); board_delay_ms(1000); while(1) { printf("now red breath...\n"); sample_breath_lamp(red_handle); printf("now green breath...\n"); sample_breath_lamp(green_handle); printf("now bule breath...\n"); sample_breath_lamp(blue_handle); } hpm_pwm_deinit(red_handle); hpm_pwm_deinit(green_handle); hpm_pwm_deinit(blue_handle); return 0;}
5 功能測試
5.1 生成rgb_led工程
工程路徑在HPM_SDK 文件夾:“hpm_sdk\samples\rgb_led”
根據SDK 開發指南文檔《HPM6750EVKMINI_UG》中的工程生成步驟,在rgb_led目錄下來生成segger embedded studio的工程文件。
通過雙擊工程文件multcore_core0.emProject可以直接進入SES(segger embedded studio) 。
雙擊“multcore_core0.emProject”打開該工程,如下圖。
5.2 運行rgb_led
運行rgb_led例程,可以查看到Mini板三色點亮熄滅以及循環呼吸效果。
5.3PWM波形查看
通過示波器查看rgb pwm波形,確認頻率和占空比是否正確。
A.200KHz,50%占空比
實測:一個周期5us,波峰2.495us。
B.200KHz, 20%占空比
實測:一個周期5us,波峰:994.7ns,驗證通過。
C.100KHz, 50%占空比
實測:一個周期10us,波峰4.995us,驗證通過。
D.100KHz, 10%占空比
實測:一個周期10us,波峰994.8ns,驗證通過。
-
PWM
+關注
關注
114文章
5153瀏覽量
213456
發布評論請先 登錄
相關推薦
評論