K210 Getting Started-Bare Metal Development (6) Timer pwm

Development board: K210 AIRV R3 version widora

Development environment: kendryte IDE official

Required reference documents: Standalone SDK Programming Guide v0.5.0, and Widora schematic

(1) Create a new folder 06my_code_pwm

Gift + earth

Example -> enter pwm -> select the develop version -> then download to the folder just now

Installation dependencies

Clean + build

The environment configuration is complete, let's start to modify the code

(2) Modify the code

PWM is in Chapter 20

There are also examples

Two macro definitions are defined to facilitate future transplantation

The code is modified as follows

Let's talk about PWM

The parameter types are as follows

Similarly, to facilitate transplantation, first make a macro definition

The initialization code is as follows

Configure PWM set duty cycle

Enable PWM output, and finally turn on the master interrupt switch

Timer interrupt service function, the routine here is the breathing light (dark->bright->dark->bright), good, no need to change

(3) Where is the lamp? Find the lamp

Click on the device management and found that he actually configured TIMER1

The timer device we used 0, change it to LED to correspond to IO17,18

But we only configured channel 0 so there is only 1 light output, then configure one light, save

One-stop clean + build + download

(4) Effect 

The light is on, but there seems to be no breathing effect?

 

(5) Problems found

I changed the PWM to channel 1 for more than half an hour before I remembered the content written in the data sheet.

 

Change the configuration to channel 2

Go one-stop

effect 

There is a breathing light

 

Timer PWM learning is complete, see you next time

 

Code

#include <fpioa.h>
#include <plic.h>
#include <pwm.h>
#include <stdio.h>
#include <sysctl.h>
#include <syslog.h>
#include <timer.h>

#define TIMER_DEVICE TIMER_DEVICE_0
#define TIMER_DEVICE_CH TIMER_CHANNEL_0

#define TIMER_PWM PWM_DEVICE_0
#define TIMER_PWM_CHN PWM_CHANNEL_1

int timer_callback(void *ctx) {
  static double cnt = 0.1;
  static int flag = 0;

  // 重新赋值PWM输出频率以及占空比
  pwm_set_frequency(TIMER_PWM, TIMER_PWM_CHN, 200000, cnt);

  // 三目运算,构成呼吸灯
  flag ? (cnt -= 0.01) : (cnt += 0.01);

  //越界判断
  if (cnt > 1.0) {
    cnt = 1.0;
    flag = 1;
  } else if (cnt < 0.0) {
    cnt = 0.0;
    flag = 0;
  }
}

int main(void) {
  /* Init Platform-Level Interrupt Controller(PLIC) */
  plic_init();  // 初始化中断
  /* Init timer */
  timer_init(TIMER_DEVICE);  //初始化定时器

  /* Set timer interval to 10ms (1e7ns) */
  // 配置定时器时间
  timer_set_interval(TIMER_DEVICE, TIMER_DEVICE_CH, 1e7);

  /* Set timer callback function with repeat method */
  // 绑定下中断回调函数(不带形参)
  timer_irq_register(TIMER_DEVICE, TIMER_DEVICE_CH, 0, 1, timer_callback, NULL);

  // 定时器0, 通道0,定时器回调函数,优先级
  // //上一节学的定时器,可以替代上面的注册函数吧 timer_set_irq(TIMER_DEVICE,
  // TIMER_DEVICE_CH, timer_callback, 1);

  /* Enable timer */
  //使能定时器
  timer_set_enable(TIMER_DEVICE, TIMER_DEVICE_CH, 1);

  /* Init PWM */
  // 初始化PWM
  pwm_init(TIMER_PWM);
  /* Set PWM to 200000Hz */
  // 设置PWM的频率 以及占空比
  pwm_set_frequency(TIMER_PWM, TIMER_PWM_CHN, 200000, 0.5);
  /* Enable PWM */
  // 启动产生PWM
  pwm_set_enable(TIMER_PWM, TIMER_PWM_CHN, 1);

  /* Enable global interrupt for machine mode of RISC-V */
  sysctl_enable_irq();  // 开中断
  while (1)
    ;
}

 

Guess you like

Origin blog.csdn.net/jwdeng1995/article/details/108033986