[Atmel] 用SAM-BA或JLINK跑ATSAM4E16的程序(9)寄存器PWM

[复制链接]
 楼主| ddllxxrr 发表于 2015-11-6 21:47 | 显示全部楼层 |阅读模式
在这个程序里,利用PWM点亮两个LED。

而这两个LED的PWM定义及GPIO的第几功能在

以下宏中定义:


/** PWM LED1 pin definitions. */
#define PIN_PWM_LED1_GPIO     PIO_PC20_IDX
#define PIN_PWM_LED1_FLAGS    (IOPORT_MODE_MUX_B)
#define PIN_PWM_LED1_CHANNEL  PWM_CHANNEL_2

/** PWM LED0 pin definitions. */
#define PIN_PWM_LED0_GPIO     PIO_PA0_IDX
#define PIN_PWM_LED0_FLAGS    (IOPORT_MODE_MUX_A)
#define PIN_PWM_LED0_CHANNEL  PWM_CHANNEL_0

以LED1为例,其数据手册,通道B正好是PWM通道2




这样PWM就同LED连接上了。

以下是程序:

  1. #include "asf.h"
  2. #include "stdio_serial.h"
  3. #include "conf_board.h"
  4. #include "conf_clock.h"

  5. /** PWM frequency in Hz */
  6. #define PWM_FREQUENCY      1000
  7. /** Period value of PWM output waveform */
  8. #define PERIOD_VALUE       100
  9. /** Initial duty cycle value */
  10. #define INIT_DUTY_VALUE    0

  11. #define STRING_EOL    "\r"
  12. #define STRING_HEADER "-- PWM LED Example --\r\n" \
  13.                 "-- "BOARD_NAME" --\r\n" \
  14.                 "-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL

  15. /** PWM channel instance for LEDs */
  16. pwm_channel_t g_pwm_channel_led;

  17. /**
  18. * \brief Interrupt handler for the PWM controller.
  19. */
  20. void PWM_Handler(void)
  21. {
  22.         static uint32_t ul_count = 0;  /* PWM counter value */
  23.         static uint32_t ul_duty = INIT_DUTY_VALUE;  /* PWM duty cycle rate */
  24.         static uint8_t fade_in = 1;  /* LED fade in flag */
  25.         uint32_t events = pwm_channel_get_interrupt_status(PWM);

  26.         /* Interrupt on PIN_PWM_LED0_CHANNEL */
  27.         if ((events & (1 << PIN_PWM_LED0_CHANNEL)) ==
  28.                         (1 << PIN_PWM_LED0_CHANNEL)) {
  29.                 ul_count++;

  30.                 /* Fade in/out */
  31.                 if (ul_count == (PWM_FREQUENCY / (PERIOD_VALUE - INIT_DUTY_VALUE))) {
  32.                         /* Fade in */
  33.                         if (fade_in) {
  34.                                 ul_duty++;
  35.                                 if (ul_duty == PERIOD_VALUE) {
  36.                                         fade_in = 0;
  37.                                 }
  38.                         } else {
  39.                                 /* Fade out */
  40.                                 ul_duty--;
  41.                                 if (ul_duty == INIT_DUTY_VALUE) {
  42.                                         fade_in = 1;
  43.                                 }
  44.                         }

  45.                         /* Set new duty cycle */
  46.                         ul_count = 0;
  47.                         g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
  48.                         pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
  49.                         g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
  50.                         pwm_channel_update_duty(PWM, &g_pwm_channel_led, ul_duty);
  51.                 }
  52.         }
  53. }

  54. /**
  55. *  \brief Configure the Console UART.
  56. */
  57. static void configure_console(void)
  58. {
  59.         const usart_serial_options_t uart_serial_options = {
  60.                 .baudrate = CONF_UART_BAUDRATE,
  61.                 .paritytype = CONF_UART_PARITY
  62.         };

  63.         /* Configure console UART. */
  64.         sysclk_enable_peripheral_clock(CONSOLE_UART_ID);
  65.         stdio_serial_init(CONF_UART, &uart_serial_options);
  66. }

  67. /**
  68. * \brief Application entry point for PWM with LED example.
  69. * Output PWM waves on LEDs to make them fade in and out.
  70. *
  71. * \return Unused (ANSI-C compatibility).
  72. */
  73. int main(void)
  74. {
  75.         /* Initialize the SAM system */
  76.         sysclk_init();
  77.         board_init();

  78.         /* Configure the console uart for debug information */
  79.         configure_console();

  80.         /* Output example information */
  81.         puts(STRING_HEADER);

  82.         /* Enable PWM peripheral clock */
  83.         pmc_enable_periph_clk(ID_PWM);

  84.         /* Disable PWM channels for LEDs */
  85.         pwm_channel_disable(PWM, PIN_PWM_LED0_CHANNEL);
  86.         pwm_channel_disable(PWM, PIN_PWM_LED1_CHANNEL);

  87.         /* Set PWM clock A as PWM_FREQUENCY*PERIOD_VALUE (clock B is not used) */
  88.         pwm_clock_t clock_setting = {
  89.                 .ul_clka = PWM_FREQUENCY * PERIOD_VALUE,
  90.                 .ul_clkb = 0,
  91.                 .ul_mck = sysclk_get_cpu_hz()
  92.         };
  93.         pwm_init(PWM, &clock_setting);

  94.         /* Initialize PWM channel for LED0 */
  95.         /* Period is left-aligned */
  96.         g_pwm_channel_led.alignment = PWM_ALIGN_LEFT;
  97.         /* Output waveform starts at a low level */
  98.         g_pwm_channel_led.polarity = PWM_LOW;
  99.         /* Use PWM clock A as source clock */
  100.         g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
  101.         /* Period value of output waveform */
  102.         g_pwm_channel_led.ul_period = PERIOD_VALUE;
  103.         /* Duty cycle value of output waveform */
  104.         g_pwm_channel_led.ul_duty = INIT_DUTY_VALUE;
  105.         g_pwm_channel_led.channel = PIN_PWM_LED0_CHANNEL;
  106.         pwm_channel_init(PWM, &g_pwm_channel_led);

  107.         /* Enable channel counter event interrupt */
  108.         pwm_channel_enable_interrupt(PWM, PIN_PWM_LED0_CHANNEL, 0);

  109.         /* Initialize PWM channel for LED1 */
  110.         /* Period is center-aligned */
  111.         g_pwm_channel_led.alignment = PWM_ALIGN_CENTER;
  112.         /* Output waveform starts at a high level */
  113.         g_pwm_channel_led.polarity = PWM_HIGH;       
  114.         /* Period is left-aligned */
  115.         g_pwm_channel_led.ul_prescaler = PWM_CMR_CPRE_CLKA;
  116.         /* Period value of output waveform */
  117.         g_pwm_channel_led.ul_period = PERIOD_VALUE;
  118.         /* Duty cycle value of output waveform */
  119.         g_pwm_channel_led.ul_duty = INIT_DUTY_VALUE;
  120.         g_pwm_channel_led.channel = PIN_PWM_LED1_CHANNEL;
  121.         pwm_channel_init(PWM, &g_pwm_channel_led);

  122.         /* Disable channel counter event interrupt */
  123.         pwm_channel_disable_interrupt(PWM, PIN_PWM_LED1_CHANNEL, 0);




  124.    
  125.        
  126.         /* Configure interrupt and enable PWM interrupt */
  127.         NVIC_DisableIRQ(PWM_IRQn);
  128.         NVIC_ClearPendingIRQ(PWM_IRQn);
  129.         NVIC_SetPriority(PWM_IRQn, 0);
  130.         NVIC_EnableIRQ(PWM_IRQn);

  131.         /* Enable PWM channels for LEDs */
  132.         pwm_channel_enable(PWM, PIN_PWM_LED0_CHANNEL);
  133.         pwm_channel_enable(PWM, PIN_PWM_LED1_CHANNEL);


  134.   
  135.     ioport_set_pin_level(LED2_GPIO, IOPORT_PIN_LEVEL_LOW);
  136.         ioport_set_pin_level(LED3_GPIO, IOPORT_PIN_LEVEL_LOW);

  137.         /* Infinite loop */
  138.         while (1) {
  139.                
  140.                
  141.                
  142.                
  143.                
  144.         }
  145. }


经测试,两个LED,渐明渐暗。




本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2403

主题

6994

帖子

68

粉丝
快速回复 在线客服 返回列表 返回顶部