打印

我使用带有pca功能的51cpu

[复制链接]
2595|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
chuandaoxy|  楼主 | 2009-10-15 22:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用捕获功能测的脉宽后,如何计算她的频率呢?因为pca的时基是分频得来的,所以有些搞不清初如何计算
51cpu的捕获功能例程
void PCA0_ISR (void) interrupt 11
{
   static unsigned int current_capture_value, previous_capture_value;
   static unsigned int capture_period;


   if (CCF0)                           // If Module 0 caused the interrupt
   {
      CCF0 = 0;                        // Clear module 0 interrupt flag.

      // Store most recent capture value
      current_capture_value = PCA0CP0;

      // Calculate capture period from last two values.
      capture_period = current_capture_value - previous_capture_value;

      // Update previous capture value with most recent info.
      previous_capture_value = current_capture_value;

   }
   else                                // Interrupt was caused by other bits.
   {
      PCA0CN &= ~0x86;                 // Clear other interrupt flags for PCA
   }
}

还有下面一段pic的单片机测频的代码,我想转成51的
while( 1 )
{
setup_timer_1(T1_DISABLED);                            /* desabilita el timer 1 */
setup_ccp1(CCP_OFF);                                       /* desabilita el modo capture */
set_timer1(0);                                                     / * timer 1 cero */
CCP1IF = 0;                                                          /* capture flag cero */
setup_timer_1(T1_INTERNAL);                             /* activa timer 1 */
setup_ccp1(CCP_CAPTURE_DIV_16);                  /* habilita modo capture */
while(!CCP1IF);                                                   /* Verifica si ocurrio captura */
temp1 = get_timer1();                                         /* Valor del TMR1 en temp1 */
CCP1IF = 0;                                                           / * capture flag = 0 */
while(!CCP1IF);                                                     /* Verifica si ocurrio captura */
temp2 = get_timer1();                                             /* Valor TMR1 en temp2 */
temp = temp2 - temp1;                                           /* valor TRM1 entre dos capturas */
temp1 = (16*1.0e6)/temp;                                      /* Temp1 = Frecuencia */

上面的pic代码十分简洁,大概意思和51cpu是一至的,关键在最后1行,频率是如何换算的?

相关帖子

沙发
duojinian| | 2009-10-16 08:00 | 只看该作者
如果是规整矩形波,测量一个高电平的时间乘以2即可得到周期。
如果不规整,可以连续测量一个高电平和一个低电平,求和即可。为了准确期间,建议连续测量100个高电平和连续100个低电平,求均值。

使用特权

评论回复
板凳
lyjian| | 2009-10-16 08:04 | 只看该作者
capture_period = current_capture_value - previous_capture_value(两次上升沿或下降沿捕获)
频率=PCA定时器时钟频率/capture_period =capture_period /PCA定时器时钟周i期

使用特权

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

本版积分规则

851

主题

1039

帖子

4

粉丝