[菜农助学交流] 用半主机方式调试---PWM捕捉定时器小练。

[复制链接]
 楼主| plc_avr 发表于 2011-8-27 18:08 | 显示全部楼层 |阅读模式
本帖最后由 plc_avr 于 2011-10-2 15:04 编辑

操作说明:用半主机方式调试---PWM捕捉定时器小练。PWM1输出PWM波形,PWM0用捕获方式读取高电平时间和低电平时间,并输出显示。试验时短接助学板上的PWM0和PWM1引脚即可。

  1. #include <stdio.h>
  2. #include "NUC1xx.h"
  3. #include "Driver\DrvSYS.h"
  4. #include "Driver\DrvGPIO.h"
  5. #include "Driver\DrvUART.h"
  6. #define PWM_CNR 0xFFFF
  7. uint16_t CaptureCounter=0;
  8. uint32_t CaptureValue[2];
  9. void InitCapture(void)
  10. {
  11. /* Step 1. GPIO initial */
  12. SYS->GPAMFP.PWM0_AD13=1;
  13. /* Step 2. Enable and Select PWM clock source*/
  14. SYSCLK->APBCLK.PWM01_EN = 1;//Enable PWM clock
  15. SYSCLK->CLKSEL1.PWM01_S = 0;//Select 12Mhz for PWM clock source
  16. PWMA->;PPR.CP01=11; //Prescaler 0~255, Setting 0 to stop output clock
  17. PWMA->CSR.CSR0=100; //clock divider->0:/2, 1:/4, 2:/8, 3:/16, 4:/1
  18. //计数时基1us
  19. /* Step 3. Select PWM Operation mode */
  20. PWMA->;PCR.CH0MOD=1; //0:One-shot mode, 1:Auto-load mode
  21. //CNR and CMR will be auto-cleared after setting CH0MOD form
  22. 0 to 1.
  23. PWMA->CNR0=PWM_CNR; //Set Reload register
  24. PWMA->CAPENR=1; //Enable Capture function pin
  25. PWMA->CCR0.CAPCH0EN=1; //Enable Capture function
  26. /* Step 4. Set PWM Interrupt */
  27. PWMA->CCR0.CRL_IE0=1; //Enable Capture rising edge interrupt
  28. PWMA->CCR0.CFL_IE0=1; //Enable Capture falling edge interrupt
  29. PWMA->;PIER.PWMIE0=1; //Enable PWM interrupt for down-counter equal zero.
  30. NVIC_EnableIRQ(PWMA_IRQn); //enable PWM inturrupt
  31. /* Step 5. Enable PWM down counter*/
  32. PWMA->;PCR.CH0EN=1; //Enable PWM down counter
  33. }
  34. //--------------------------------------------
  35. void InitPWM1(void)
  36. {
  37. /* Step 1. GPIO initial */
  38. SYS->GPAMFP.PWM1_AD14=1;
  39. /* Step 2. Enable and Select PWM clock source*/
  40. SYSCLK->APBCLK.PWM01_EN = 1;//Enable PWM clock
  41. SYSCLK->CLKSEL1.PWM01_S = 0;//Select 12Mhz for PWM clock source
  42. //主时钟在进入PWM定时器之前被(CP01+1)分频 
  43. PWMA->;PPR.CP01=11; //Prescaler 0~255, Setting 0 to stop output clock
  44. //PWM定时器16分频
  45. PWMA->CSR.CSR1=3; // PWM clock = clock source/(Prescaler + 1)/divider
  46. //clock divider->0:/2, 1:/4, 2:/8, 3:/16, 4:/1
  47. //PWM定时器每计数一个时间:12M/(11+1)*16=16us
  48. /* Step 3. Select PWM Operation mode */
  49. //PWM0 自动重装载模式
  50. PWMA->;PCR.CH1MOD=1; //0:One-shot mode, 1:Auto-load mode
  51. //CNR and CMR will be auto-cleared after setting CH0MOD form
  52. 0 to 1.
  53. PWMA->CNR1=0xFFFF; //周期 65535*16=1.048560S
  54. PWMA->CMR1=0x3FFF; //占空比=(CMR+1)/(CNR+1)
  55. PWMA->;PCR.CH1INV=0; //Inverter->0:off, 1:on PWM输出极性反转
  56. PWMA->;PCR.CH1EN=1; //PWM function->0:Disable, 1:Enable
  57. PWMA->;POE.PWM1=1; //Output to pin->0:Diasble, 1:Enable
  58. }
  59. void PWMA_IRQHandler(void) // PWM interrupt subroutine
  60. {
  61. if(PWMA->;PIIR.PWMIF0)
  62. {
  63. CaptureCounter++;//Delay (PWM_CNR+1) usec
  64. if(CaptureCounter==0)
  65. {
  66. //Overflow
  67. }
  68. PWMA->;PIIR.PWMIF0=1;
  69. }
  70. if(PWMA->CCR0.CAPIF0)
  71. {
  72. if(PWMA->CCR0.CFLRI0)//Calculate High Level
  73. {
  74. CaptureValue[0]=CaptureCounter*(PWM_CNR+1)+(PWM_CNR-PWMA->CFLR0);//usec
  75. CaptureCounter=0;
  76. PWMA->CCR0.CFLRI0=0;
  77. }
  78. if(PWMA->CCR0.CRLRI0)//Calculate Low Level
  79. {
  80. CaptureValue[1]=CaptureCounter*(PWM_CNR+1)+(PWM_CNR-PWMA->CRLR0);//usec
  81. CaptureCounter=0;
  82. PWMA->CCR0.CRLRI0=0;
  83. }
  84. PWMA->CCR0.CAPIF0=1;
  85. }
  86. }

  87. void Delay(int count)
  88. {
  89. while(count--)
  90. {
  91. //__NOP;
  92. }
  93. }
  94. /*----------------------------------------------------------------------------
  95. MAIN function
  96. ----------------------------------------------------------------------------*/
  97. int32_t main (void)
  98. {
  99. STR_UART_T sParam;
  100. /* Unlock the protected registers */
  101. UNLOCKREG();
  102. /* Enable the 12MHz oscillator oscillation */
  103. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);

  104. /* Waiting for 12M Xtal stalble */
  105. DrvSYS_Delay(5000);

  106. /* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
  107. DrvSYS_SelectHCLKSource(0);
  108. /*lock the protected registers */
  109. LOCKREG();
  110. DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */
  111. /* Set UART Pin */
  112. DrvGPIO_InitFunction(E_FUNC_UART0);
  113. /* UART Setting */
  114. sParam.u32BaudRate = 115200;
  115. sParam.u8cDataBits = DRVUART_DATABITS_8;
  116. sParam.u8cStopBits = DRVUART_STOPBITS_1;
  117. sParam.u8cParity = DRVUART_PARITY_NONE;
  118. sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
  119. /* Set UART Configuration */
  120. if(DrvUART_Open(UART_PORT0,&sParam) != E_SUCCESS)
  121. {
  122. printf("UART0 open failed\n");
  123. return FALSE;
  124. }

  125. InitPWM1();
  126. InitCapture();
  127. printf("================================================\n\r");
  128. printf(" 菜农新唐M0助学板范例 \n\r");
  129. printf(" 实验八: 捕捉定时器试验  \n\r");
  130. printf(" 烈火狂龙 论坛ID: plc_avr \n\r");
  131. printf("================================================\n\r");
  132. while(1)
  133. {

  134. if(CaptureValue[0]>=1000000)
  135. {
  136. printf("高电平时间:%dsec\n",CaptureValue[0]/1000000);
  137. }
  138. else if(CaptureValue[0]>=1000)
  139. {
  140. printf("高电平时间:%dmsec\n",CaptureValue[0]/1000);
  141. }
  142. else
  143. printf("高电平时间:%dusec\n",CaptureValue[0]);

  144. if(CaptureValue[1]>=1000000)
  145. {
  146. printf("低电平时间:%dsec\n",CaptureValue[1]/1000000);
  147. }
  148. else if(CaptureValue[1]>=1000)
  149. {
  150. printf("低电平时间:%dmsec\n",CaptureValue[1]/1000);
  151. }
  152. else
  153. printf("低电平时间:%dusec\n",CaptureValue[1]);

  154. DrvSYS_Delay(500000);
  155. }
  156. }


相关图片:

相关源码:






本帖子中包含更多资源

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

×
 楼主| plc_avr 发表于 2011-8-27 18:25 | 显示全部楼层
明天要出差,把今天学的发上来,哈哈。晕,发代码不会。。。。。
hotpower 发表于 2011-8-27 18:30 | 显示全部楼层
用<>把代码扩起来不会出现怪脸的~~~
 楼主| plc_avr 发表于 2011-8-27 19:41 | 显示全部楼层
怪脸总算搞好了。:D
weshiluwei6 发表于 2011-8-27 21:28 | 显示全部楼层
哈哈 火哥 俺来教你啊 就是 上面<> 代码放这个里面就可以了 火哥
Cortex-M0 发表于 2011-8-28 06:17 | 显示全部楼层
  1. 支持~~~
电子write_cai 发表于 2011-8-28 13:09 | 显示全部楼层
支持。
hotpower 发表于 2011-8-28 14:54 | 显示全部楼层
晕,烈火,不用如此复杂,有专门的代码段。
电子write_cai 发表于 2011-8-28 16:46 | 显示全部楼层
晕,烈火,不用如此复杂,有专门的代码段。
hotpower 发表于 2011-8-28 14:54
可能烈火不知道,呵呵
mbydyjj 发表于 2011-9-30 13:17 | 显示全部楼层
零度888 发表于 2014-4-25 17:14 | 显示全部楼层
我也不知道  在弄个定时器timer0的捕获功能,但是进不了中断,谁能指教指教吗
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:烈火DIY Mini四轴飞行器群:234879071  http://fire-dragon.taobao.com/

42

主题

1633

帖子

23

粉丝
快速回复 在线客服 返回列表 返回顶部
个人签名:烈火DIY Mini四轴飞行器群:234879071  http://fire-dragon.taobao.com/

42

主题

1633

帖子

23

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