2013年1月7日 星期一

DIY - PIC:PIC18F4550 CCP 的 Capture 輸入捕捉函數庫程式 (一百六十六)


DIY - PICPIC18F4550 CCP Capture 輸入捕捉函數庫程式 (一百六十六)  

在讀取超聲波模組返回的時間,筆者會利用 PIC18F4550 內的 CCP Capture 功能來計算總時間,所以要使用 CCP輸入捕捉函數 (Capture)Microchip C18 內置支持 CCP輸入捕捉函數庫模塊 (capture.h),衹需要用包括 capture 頭文件 ()。然後再呼叫不同的功能,便可控制 CCP輸入捕捉功能。

函數       
Description
說明
CloseCapturex
Disable capture peripheral x
禁止捕捉外設x
OpenCapturex
Configure capture peripheral x
配置捕捉外設 x
ReadCapturex
Read a value from capture peripheral x
從捕捉外設x 讀取值
CloseECapturex (1)
Disable enhanced capture peripheral x
禁止增強型捕捉外設 x
OpenECapturex (1)
Configure enhanced capture peripheral x
配置增強型捕捉外設x
ReadECapturex (1)
Read a value from enhanced capture peripheral x
從增強型捕捉外設x 讀取值
1 僅帶有ECCPxCONT 寄存器的器件具有增強捕捉功能。

功能 CloseCapture1 / CloseCapture2 / CloseCapture3 / CloseCapture4 / CloseCapture5 /CloseECapture1 (禁止輸入捕捉x)
頭文件: capture.h
原型: void CloseCapture2( void )
void CloseCapture3( void )
void CloseCapture4( void )
void CloseCapture5( void )
void CloseECapture1( void )
說明:該函數禁止與指定輸入捕捉相對應的中斷。
檔案名:     cp1close.c
cp2close.c
cp3close.c
cp4close.c
cp5close.c
ep1close.c

功能 OpenCapture1 / OpenCapture2 / OpenCapture3 / OpenCapture4 / OpenCapture5 / OpenECapture1
(配置並使能輸入捕捉x)
頭文件: capture.h
函數原型    void OpenCapture1( unsigned char config )
void OpenCapture2( unsigned char config )
void OpenCapture3( unsigned char config )
void OpenCapture4( unsigned char config )
void OpenCapture5( unsigned char config )
void OpenECapture1( unsigned char config )
參數:config
從下面所列出各類型中分別取一個值並相與(’&’)所得的值。這些值在capture.h 檔中定義。
使能CCP 中斷:
CAPTURE_INT_ON                  使能中斷
CAPTURE_INT_OFF                 禁止中斷

中斷觸發 (用CCP 模組號代替 x ):
Cx_EVERY_FALL_EDGE          每個下降沿產生中斷
Cx_EVERY_RISE_EDGE           每個上升沿產生中斷
Cx_EVERY_4_RISE_EDGE       4 個上升沿產生1 個中斷
Cx_EVERY_16_RISE_EDGE     16 個上升沿產生1 個中斷
EC1_EVERY_FALL_EDGE        每個下降沿產生中斷(增強型)
EC1_EVERY_RISE_EDGE         每個上升沿產生中斷(增強型)
EC1_EVERY_4_RISE_EDGE     4 個上升沿產生1個中斷 (增強型)
EC1_EVERY_16_RISE_EDGE   16 個上升沿產生1 個中斷(增強型)
說明:該函數首先把捕捉模組重定到POR 狀態,然後將輸入捕捉配置為指定的邊沿檢測。
捕捉函數使用在capture.h 中定義的一個結構,來指示每個捕捉模組的溢出狀態。此結構名為CapStatus,包含如下位域:
Cap1OVF
Cap2OVF
Cap3OVF
Cap4OVF
Cap5OVF
ECap1OVF
進行任何捕捉操作之前,不僅要配置和使能捕捉模組,還要使能相應的計時器模組。關於CCP 和計時器連接配置的資訊,請參見相應的資料手冊;關於函數OpenTimer3 中使用的參數的資訊,請參見相關網址 (DIY - PIC:C18 Timers 函數庫程式 (廿十六) )
檔案名:     cp1open.c
cp2open.c
cp3open.c
cp4open.c
cp5open.c
ep1open.c
代碼示例: OpenCapture1( CAPTURE_INT_ON & C1_EVERY_4_RISE_EDGE );

功能 ReadCapture1 / ReadCapture2 / ReadCapture3 / ReadCapture4 / ReadCapture5 / ReadECapture1 (從指定的輸入捕捉中讀取捕捉事件的結果)
頭文件: capture.h
函數原型:    unsigned int ReadCapture1( void );
unsigned int ReadCapture2( void );
unsigned int ReadCapture3( void );
unsigned int ReadCapture4( void );
unsigned int ReadCapture5( void );
unsigned int ReadECapture1( void );
說明: 該函數讀取各個輸入捕捉特殊功能寄存器的值。
返回值: 該函數返回捕捉事件的結果。
檔案名:     cp1read.c
cp2read.c
cp3read.c
cp4read.c
cp5read.c
ep1read.c

使用輸入捕捉函數的例子:
#include
#include
#include
#include
#include

void main(void)
{
unsigned int result;
char str[7];

// Configure Capture1
OpenCapture1( C1_EVERY_4_RISE_EDGE &
CAPTURE_INT_OFF );

// Configure Timer3
OpenTimer3( TIMER_INT_OFF &
T3_SOURCE_INT );

// Configure USART
OpenUSART( USART_TX_INT_OFF &
USART_RX_INT_OFF &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX,
25 );

while(!PIR1bits.CCP1IF);              // Wait for event
result = ReadCapture1();                // read result
ultoa(result,str);                             // convert to string

// Write the string out to the USART if
// an overflow condition has not occurred.
if(!CapStatus.Cap1OVF)
{
putsUSART(str);
}

// Clean up
CloseCapture1();
CloseTimer3();
CloseUSART();
}
資料來源:Microchip MPLAB C18 C COMPILER LIBRARIES

相關網址

201317 天氣報告
氣溫:15.9 @ 22:20
相對濕度:百分之66%
天氣:大致多雲

沒有留言:

張貼留言