找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2637|回复: 1
收起左侧

C8051F410单片机利用pca捕获外部脉冲

[复制链接]
ID:511588 发表于 2019-7-31 14:16 | 显示全部楼层 |阅读模式
//-----------------------------------------------------------------------------
// 声明:外部引脚的脉冲输入CEX0为P0^0,
//-----------------------------------------------------------------------------

#include <c8051f410.h>                 // SFR declarations
#include"UART.h"
#include <stdio.h>

#define SYSCLK      24500000           // SYSCLK frequency in Hz
#define BAUDRATE        9600           // Baud rate of UART in bps

sfr16 PCA0CP0 = 0xFB;                  // PCA0 Compare Register Definition
unsigned int CLKNUM = 0;
float period;

void OSCILLATOR_Init (void);
void PORT_Init (void);
void PCA0_Init (void);
static unsigned int capture_period;
unsigned int counter;
void main (void)
{
   PCA0MD = 0x00;                      // Disable watchdog timer

   PORT_Init ();                       // Initialize crossbar and GPIO
   OSCILLATOR_Init ();                 // Initialize oscillator
   PCA0_Init ();                       // Initialize PCA0
   UART0_Init ();

   EA = 1;

   while (1)
   {
      printf("CLKNUM= %d us\n",CLKNUM);
         printf("period= %f us\n",period);
   }
}


void OSCILLATOR_Init (void)
{
   OSCICN = 0x87;                      // Set internal oscillator to run
                                       // at its maximum frequency

   CLKSEL = 0x00;                      // Select the internal osc. as
                                       // the SYSCLK source
     RSTSRC = 0x04;                      // enable missing clock detector(串口需要)
}

void PORT_Init (void)
{
   XBR0    = 0x01;                     //串口通信使能
   XBR1    = 0x41;                     // Route CEX0 to P0.0,
                                       // Enable crossbar and weak pull-ups
}

void PCA0_Init (void)
{
   PCA0CN = 0x00;                      // Stop counter; clear all flags
   PCA0MD = 0x08;                      // Use 系统时钟 as time base
   PCA0CPM0 = 0x21;                    // Module 0 = Rising Edge Capture Mode
                                       // enable CCF flag.
   EIE1 = 0x10;                       // Enable PCA interrupts  
   CR = 1;                             // Start PCA counter
}
void PCA0_ISR (void) interrupt 11
{
   static unsigned int current_capture_value, previous_capture_value;

   if (CCF0)                                                                         // If Module 0 caused the interrupt
   {
//          CLKNUM++;
         
      CCF0 = 0;                                                                      // Clear module 0 interrupt flag.   
      current_capture_value = PCA0CP0;                                               // Store most recent capture value     
      capture_period = current_capture_value - previous_capture_value;               // Calculate capture period from last two values.
      previous_capture_value = current_capture_value;                                // Update previous capture value with most recent info.
            period = capture_period *4.0816326530612244897959183673469e-8;
         
         
         
         
     }
   else                                // Interrupt was caused by other bits.
   {
      PCA0CN &= ~0x86;                 // Clear other interrupt flags for PCA
   }
}

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:64765 发表于 2019-11-16 17:01 | 显示全部楼层
好资料,学习了,谢谢分享。
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表