[AT32F405]

【AT-START-F405测评】+上位机串口控制PWM调节LED亮度

[复制链接]
754|1
手机看帖
扫描二维码
随时随地手机跟帖
ysuer|  楼主 | 2024-6-11 17:00 | 显示全部楼层 |阅读模式
[AT32F405] 【AT-START-F405测评】+上位机串口控制PWM调节LED亮度
本次测试使用IDE为MDK5.23,直接在at_start_f405项目模板templates上添加自己的软件部分实现
dfdf33f7241ae5c942011d70bb913d7a
我在工程下增加了两个c文件:bsp_uart1.c和pwm.c.bsp_uart1.c这个文件主要是实现uart1 PA9和PA10的uart通讯;pwm.c主要是实现PWM的输出控制PF4输出的PWM占空比,进而调节LED2红灯的亮度
29438dfc98290fc3955ffceb45233b0d
bsp_uart1.c代码部分
#include "includes.h"
uint8_t uart1_recbuf[USART1_RXBUF_NUM]={0};
uint8_t uart1_rec_cnt=0;
static uint8_t uart1_flag=0;
uint8_t get_uart1_flag(void)
{
return uart1_flag;
}
void set_uart1_flag(uint8_t flag)
{
uart1_flag=flag;
}
uint8_t get_uart1_len(void)
{
return uart1_rec_cnt;
}
void set_uart1_index(uint8_t val)
{
uart1_rec_cnt=val;
}
void USART1_IRQHandler(void)
{
volatile unsigned int IT_temp=0;
if(usart_flag_get(USART1, USART_RDBF_FLAG)==SET)
{
usart_flag_clear(USART1, USART_RDBF_FLAG);
IT_temp = USART1->sts;
uart1_recbuf[uart1_rec_cnt++] = USART1->dt; //?ú?aà?2?òaó??a'?µ?o¯êy??òò?a IDLE?D??2??§3?D'0à'??3y
}
if(usart_flag_get(USART1, USART_IDLEF_FLAG)==SET)
  {
IT_temp = USART1->sts;
IT_temp = USART1->dt; //
set_uart1_flag(1);
  }
if(usart_flag_get(USART1, USART_TDC_FLAG)==SET)
  {
usart_flag_clear(USART1, USART_TDC_FLAG);
  }
}
void uart1_init(uint32_t baudrate)
{
usart_parity_selection_type parity;
gpio_init_type gpio_init_struct;
  /* enable the gpioa clock */
  crm_periph_clock_enable(CRM_GPIOA_PERIPH_CLOCK, TRUE);
  /* enable the usart2 and gpio clock */
  crm_periph_clock_enable(CRM_USART1_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_DMA1_PERIPH_CLOCK, TRUE);
gpio_default_para_init(&gpio_init_struct);
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_pins = GPIO_PINS_10 | GPIO_PINS_9;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init(GPIOA, &gpio_init_struct);
RX_EN1();
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_MODERATE;//RESE
  gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_mode = GPIO_MODE_OUTPUT;
  gpio_init_struct.gpio_pins = GPIO_PINS_8;
  gpio_init_struct.gpio_pull = GPIO_PULL_UP;
  gpio_init(GPIOA, &gpio_init_struct);
  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE9, GPIO_MUX_7);
  gpio_pin_mux_config(GPIOA, GPIO_PINS_SOURCE10, GPIO_MUX_7);
parity=USART_PARITY_NONE;
usart_parity_selection_config(USART1,parity);
usart_init(USART1, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
  usart_transmitter_enable(USART1, TRUE);
  usart_receiver_enable(USART1, TRUE);
  usart_dma_transmitter_enable(USART1, TRUE);
  usart_dma_receiver_enable(USART1, TRUE);
usart_flag_clear(USART1, USART_IDLEF_FLAG|USART_RDBF_INT);
usart_interrupt_enable(USART1, USART_IDLE_INT, TRUE);
usart_interrupt_enable(USART1, USART_RDBF_INT, TRUE);
//usart_interrupt_enable(USART1, USART_TDC_INT, TRUE);USART_RDBF_INT
usart_flag_clear(USART1, USART_IDLEF_FLAG|USART_TDC_FLAG);
usart_enable(USART1,TRUE);
nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
nvic_irq_enable(USART1_IRQn, 0, 2);
}
void bsp_uart1_init(uint32_t baudrate)
{
uart1_init(baudrate);
//dma1_ch5_uart1_txd_init();
//dma1_ch6_uart1_rxd_init();
}
void send_string1(unsigned char *pd,unsigned int len)
{
uint8_t i=0;
while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
for(i=0;i<len;i++)
    {
//usart_data_transmit(USART1, usart2_tx_buffer[usart2_tx_counter++]);
USART1->dt=*(pd+i);
while(usart_flag_get(USART1, USART_TDBE_FLAG) == RESET);
}
//TX_EN1();
//DMA1_CHANNEL5->ctrl_bit.chen=0;
//DMA1->clr = DMA1_GL5_FLAG|DMA1_FDT5_FLAG|DMA1_HDT5_FLAG|DMA1_DTERR5_FLAG;
//DMA1_CHANNEL5->maddr=(unsigned long)pd;
//DMA1_CHANNEL5->dtcnt=len;
//DMA1_CHANNEL5->ctrl_bit.chen=1;
}
pwm.c.代码部分
#include "includes.h"
crm_clocks_freq_type crm_clocks_freq_struct = {0};
void pwm_init(void)
{
uint16_t CCR1Val = 136;
uint16_t PrescalerValue = 0;
uint16_t ch1_val = 1500;//325-1605
uint16_t div_value = 0;
tmr_output_config_type tmr_oc_init_structure;
gpio_init_type gpio_init_struct;
  crm_periph_clock_enable(CRM_GPIOF_PERIPH_CLOCK, TRUE);
crm_periph_clock_enable(CRM_TMR2_PERIPH_CLOCK , TRUE);
crm_clocks_freq_get(&crm_clocks_freq_struct);
  gpio_init_struct.gpio_pins = GPIO_PINS_4;
  gpio_init_struct.gpio_out_type = GPIO_OUTPUT_PUSH_PULL;
  gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
  gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
  gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
  gpio_init(GPIOF, &gpio_init_struct);
gpio_pin_mux_config(GPIOF, GPIO_PINS_SOURCE4, GPIO_MUX_1);
  div_value =  (uint16_t) ((crm_clocks_freq_struct.apb1_freq) / 36000000) - 1;
  /* tmr10 time base configuration */
  tmr_base_init(TMR2, 1999, div_value);
  tmr_cnt_dir_set(TMR2, TMR_COUNT_UP);
  tmr_clock_source_div_set(TMR2, TMR_CLOCK_DIV1);
  tmr_output_default_para_init(&tmr_oc_init_structure);
  tmr_oc_init_structure.oc_mode = TMR_OUTPUT_CONTROL_PWM_MODE_A;
  tmr_oc_init_structure.oc_idle_state = FALSE;
  tmr_oc_init_structure.oc_polarity = TMR_OUTPUT_ACTIVE_HIGH;
  tmr_oc_init_structure.oc_output_state = TRUE;
  tmr_output_channel_config(TMR2, TMR_SELECT_CHANNEL_1, &tmr_oc_init_structure);
  tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_1, ch1_val);
  tmr_output_channel_buffer_enable(TMR2, TMR_SELECT_CHANNEL_1, TRUE);
  tmr_period_buffer_enable(TMR2, TRUE);
  /* tmr enable counter */
  tmr_counter_enable(TMR2, TRUE);
}
同时修改主函数main.c部分功能如下
int main(void)
{
uint16_t pwm_ratio=0;
data_un dat_un1={0};
uint8_t len=0;
  system_clock_config();
  at32_board_init();
bsp_uart1_init(115200);
pwm_init();
  button_exint_init();
  while(1)
  {
//    at32_led_toggle(LED2);
//    delay_ms(g_speed * DELAY);
//    at32_led_toggle(LED3);
//    delay_ms(g_speed * DELAY);
//    at32_led_toggle(LED4);
//    delay_ms(g_speed * DELAY);
if(get_uart1_flag()==1)
{
set_uart1_flag(0);
//len=USART1_RXBUF_NUM-DMA1_CHANNEL6->dtcnt;
len=get_uart1_len();
if(len<=36)
{
memcpy(dat_un1.data_buf,uart1_recbuf,len);
set_uart1_index(0);
if(len!=0)
{
if(dat_un1.com_struct.addr==1)
{
if(MBCRC16(dat_un1.data_buf,len)==0)
{
if(dat_un1.com_struct.func==6)
{
if(dat_un1.com_struct.reg_addr==0x00)
{
pwm_ratio=dat_un1.com_struct.reg_val;
if(pwm_ratio<=1000)
tmr_channel_value_set(TMR2, TMR_SELECT_CHANNEL_1, pwm_ratio*2);
}
}
}
}
send_string1(uart1_recbuf,len);
}
}
}
  }
}
为了增加显示效果,我基于C#修改了一个简单的上位机界面来实现亮度连续的调节
2d1e38a00ebd7aae99ac59a07984d902
通过拖动滚动条,串口连续输出PWM的占空比0-1000,开发板LED2同步实现亮度的调节。有感兴趣的网友可以找我私信MDK工程文件源码。

使用特权

评论回复
这个上位机很方便

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

2

主题

33

帖子

2

粉丝