[Atmel] 跑一下SAM4N例程(二十五):TC捕捉功能

[复制链接]
1881|0
 楼主| ddllxxrr 发表于 2015-5-12 20:32 | 显示全部楼层 |阅读模式
这个例程展示了怎么样配置TC,一个从T1OA1发出波形一个从T1OA2输入波形。当TC中断发生,我们可以读RA和RB的值来计算脉冲频率和脉冲数量。


以下是发波的配置:

  1. /** TC waveform configurations */
  2. static const struct waveconfig_t gc_waveconfig[] = {
  3.         {TC_CMR_TCCLKS_TIMER_CLOCK4, 178, 30},
  4.         {TC_CMR_TCCLKS_TIMER_CLOCK3, 375, 50},
  5.         {TC_CMR_TCCLKS_TIMER_CLOCK3, 800, 75},
  6.         {TC_CMR_TCCLKS_TIMER_CLOCK2, 1000, 80},
  7.         {TC_CMR_TCCLKS_TIMER_CLOCK2, 4000, 55}
  8. };


以下是发波初始化:

  1. static void tc_waveform_initialize(void)
  2. {
  3.         uint32_t ra, rc;

  4.         /* Configure the PMC to enable the TC module. */
  5.         sysclk_enable_peripheral_clock(ID_TC_WAVEFORM);
  6. #if SAMG55
  7.         /* Enable PCK output */
  8.         pmc_disable_pck(PMC_PCK_3);
  9.         pmc_switch_pck_to_mck(PMC_PCK_3, PMC_PCK_PRES_CLK_1);
  10.         pmc_enable_pck(PMC_PCK_3);
  11. #endif

  12.         /* Init TC to waveform mode. */
  13.         tc_init(TC, TC_CHANNEL_WAVEFORM,
  14.                         /* Waveform Clock Selection */
  15.                         gc_waveconfig[gs_uc_configuration].ul_intclock
  16.                         | TC_CMR_WAVE /* Waveform mode is enabled */
  17.                         | TC_CMR_ACPA_SET /* RA Compare Effect: set */
  18.                         | TC_CMR_ACPC_CLEAR /* RC Compare Effect: clear */
  19.                         | TC_CMR_CPCTRG /* UP mode with automatic trigger on RC Compare */
  20.         );

  21.         /* Configure waveform frequency and duty cycle. */
  22.         rc = (sysclk_get_peripheral_bus_hz(TC) /
  23.                         divisors[gc_waveconfig[gs_uc_configuration].ul_intclock]) /
  24.                         gc_waveconfig[gs_uc_configuration].us_frequency;
  25.         tc_write_rc(TC, TC_CHANNEL_WAVEFORM, rc);
  26.         ra = (100 - gc_waveconfig[gs_uc_configuration].us_dutycycle) * rc / 100;
  27.         tc_write_ra(TC, TC_CHANNEL_WAVEFORM, ra);

  28.         /* Enable TC TC_CHANNEL_WAVEFORM. */
  29.         tc_start(TC, TC_CHANNEL_WAVEFORM);
  30.         printf("Start waveform: Frequency = %d Hz,Duty Cycle = %2d%%\n\r",
  31.                         gc_waveconfig[gs_uc_configuration].us_frequency,
  32.                         gc_waveconfig[gs_uc_configuration].us_dutycycle);
  33. }
以下是捕捉初始化:


  1. static void tc_capture_initialize(void)
  2. {
  3.         /* Configure the PMC to enable the TC module */
  4.         sysclk_enable_peripheral_clock(ID_TC_CAPTURE);
  5. #if SAMG55
  6.         /* Enable PCK output */
  7.         pmc_disable_pck(PMC_PCK_3);
  8.         pmc_switch_pck_to_mck(PMC_PCK_3, PMC_PCK_PRES_CLK_1);
  9.         pmc_enable_pck(PMC_PCK_3);
  10. #endif

  11.         /* Init TC to capture mode. */
  12.         tc_init(TC, TC_CHANNEL_CAPTURE,
  13.                         TC_CAPTURE_TIMER_SELECTION /* Clock Selection */
  14.                         | TC_CMR_LDRA_RISING /* RA Loading: rising edge of TIOA */
  15.                         | TC_CMR_LDRB_FALLING /* RB Loading: falling edge of TIOA */
  16.                         | TC_CMR_ABETRG /* External Trigger: TIOA */
  17.                         | TC_CMR_ETRGEDG_FALLING /* External Trigger Edge: Falling edge */
  18.         );
  19. }
以下是中断函数:

  1. void TC_Handler(void)
  2. {
  3.         //! [tc_capture_irq_handler_start]
  4.         //! [tc_capture_irq_handler_status]
  5.         if ((tc_get_status(TC, TC_CHANNEL_CAPTURE) & TC_SR_LDRBS) == TC_SR_LDRBS) {
  6.                 //! [tc_capture_irq_handler_status]
  7.                 gs_ul_captured_pulses++;
  8.                 //! [tc_capture_irq_handler_read_ra]
  9.                 gs_ul_captured_ra = tc_read_ra(TC, TC_CHANNEL_CAPTURE);
  10.                 //! [tc_capture_irq_handler_read_ra]
  11.                 //! [tc_capture_irq_handler_read_rb]
  12.                 gs_ul_captured_rb = tc_read_rb(TC, TC_CHANNEL_CAPTURE);
  13.                 //! [tc_capture_irq_handler_read_rb]
  14.         }
  15. //! [tc_capture_irq_handler_end]
  16. }
主循环如下:
  1. while (1) {
  2.                 scanf("%c", (char *)&key);

  3.                 switch (key) {
  4.                 case 'h':
  5.                         display_menu();
  6.                         break;

  7.                 case 's':
  8.                         if (gs_ul_captured_pulses) {
  9.                                 tc_disable_interrupt(TC, TC_CHANNEL_CAPTURE, TC_IDR_LDRBS);
  10.                                 printf("Captured %u pulses from TC%d channel %d, RA = %u, RB = %u \n\r",
  11.                                                 (unsigned)gs_ul_captured_pulses, TC_PERIPHERAL,
  12.                                                 TC_CHANNEL_CAPTURE,        (unsigned)gs_ul_captured_ra,
  13.                                                 (unsigned)gs_ul_captured_rb);
  14.                                 frequence = (sysclk_get_peripheral_bus_hz(TC) /
  15.                                                 divisors[TC_CAPTURE_TIMER_SELECTION]) /
  16.                                                 gs_ul_captured_rb;
  17.                                 dutycycle
  18.                                         = (gs_ul_captured_rb - gs_ul_captured_ra) * 100 /
  19.                                                 gs_ul_captured_rb;
  20.                                 printf("Captured wave frequency = %d Hz, Duty cycle = %d%% \n\r",
  21.                                                 frequence, dutycycle);

  22.                                 gs_ul_captured_pulses = 0;
  23.                                 gs_ul_captured_ra = 0;
  24.                                 gs_ul_captured_rb = 0;
  25.                         } else {
  26.                                 puts("No waveform has been captured\r");
  27.                         }

  28.                         puts("\n\rPress 'h' to display menu\r");
  29.                         break;

  30.                 case 'c':
  31.                         puts("Start capture, press 's' to stop \r");
  32.                         //! [tc_capture_init_module_irq]
  33.                         tc_enable_interrupt(TC, TC_CHANNEL_CAPTURE, TC_IER_LDRBS);
  34.                         //! [tc_capture_init_module_irq]
  35.                         /* Start the timer counter on TC TC_CHANNEL_CAPTURE */
  36.                         //! [tc_capture_start_now]
  37.                         tc_start(TC, TC_CHANNEL_CAPTURE);
  38.                         //! [tc_capture_start_now]
  39.                         break;

  40.                 default:
  41.                         /* Set waveform configuration #n */
  42.                         if ((key >= '0') && (key <= ('0' + gc_uc_nbconfig - 1))) {
  43.                                 if (!gs_ul_captured_pulses) {
  44.                                         gs_uc_configuration = key - '0';
  45.                                         tc_waveform_initialize();
  46.                                 } else {
  47.                                         puts("Capturing ... , press 's' to stop capture first \r");
  48.                                 }
  49.                         }

  50.                         break;
  51.                 }
最后的运行结果:显示了准确的捕捉。




本帖子中包含更多资源

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

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

本版积分规则

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

2404

主题

7001

帖子

68

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