初級會員
Joined
: 2008/4/10 22:06 最後登入時間
: 2009/5/30 21:28
Group:
註冊會員
Level : 4 HP : 0 / 78 MP : 9 / 3766 EXP : 12
|
那他的範例程式該如何修改?
我有試著去改,但一直都改不出來 ==
define __dsPIC30F4011__
#include <p30F4011.h> #include "C30EVM_LCD.h" // 將函式的原型宣告檔案含入 #include <timer.h> #include <InCap.h>
#define FCY 7372800 * 2 // 因為使用頻率為將外部 7.3728 MHz * 8 的模式 , 每一指令週期需 4 個 clock // 所以 FCY = (7.3728 * 8 / 4 ) MHz = 7372800* 2
_FOSC(CSW_FSCM_OFF & XT_PLL8); // XT with 8xPLL oscillator, Failsafe clock off _FWDT(WDT_OFF); // Watchdog timer disabled _FBORPOR(PBOR_OFF & MCLR_EN); // Brown-out reset disabled, MCLR reset enabled _FGS(CODE_PROT_OFF); // Code protect disabled
const char My_String1[]=" Capture" ; // 宣告字串於 Program Memory const char My_String2[]="Duty Cycle: %" ; // 宣告字串於 Data Memory
void uitoa( unsigned char * , unsigned int ) ;
int Interrupt_Count = 0 , Int_flag; unsigned int timer_edge[4]; unsigned char phase, phase2, phase3, Convert_Buf[10];
void _ISR _IC7Interrupt(void) { Interrupt_Count++; switch(Interrupt_Count) // 根據輸入捕捉的次數決定執行的動作 { case(1): // 第一次,儲存訊號的電位狀態 phase = PORTBbits.RB4 ; break; case(2): // 第二次,儲存訊號的電位狀態 phase2 = PORTBbits.RB4 ; break; case(3): ReadCapture7(timer_edge); // 第三次,將所有的輸入捕捉暫存器數值讀出 phase3 = PORTBbits.RB4 ; // 並儲存訊號的電位狀態 break; } Int_flag = 1; IFS1bits.IC7IF = 0; }
int main(void) { unsigned int period1, period2; unsigned int duty;
ADPCFG = 0xffff; // Set All Analog Pins as Digital IO
OpenLCD( ) ; // 使用 OpenLCD( )對 LCD 模組作初始化設定 // 4 bits Data mode // 5 * 7 Character
setcurLCD(0,0) ; // 使用 setcurLCD( ) 設定游標於 (0,0) putrsLCD( My_String1 ) ; // 將存在 Program Memory 的字串使用 // putrsLCD( ) 印出至 LCD setcurLCD(0,1) ; putrsLCD( My_String2 ) ; // 使用 setcurLCD( ) 設定游標於 (0,1) // 將存在 Data Memory 的字串使用 // putsLCD( ) 印出至 LCD
T3CON = 0x8000; /* Timer 3 On */ /* Configure the InputCapture in stop in idle mode , Timer 3 as source , interrupt on capture 1, I/C on every edge */ OpenCapture7(IC_IDLE_STOP & IC_TIMER3_SRC & IC_INT_1CAPTURE & IC_EVERY_EDGE); /* Enable Timer1 Interrupt and Priority to '1' */ ConfigIntCapture7(IC_INT_PRIOR_7 & IC_INT_ON);
Int_flag = 0;
while(1) { while(!Int_flag); /* wait here till first capture event */ Int_flag = 0; while(!Int_flag); /* wait here till next capture event */ Int_flag = 0; while(!Int_flag); /* wait here till last capture event */ DisableIntIC7; Int_flag = 0;
/* calculate time count between two capture events */ if( timer_edge[1] >= timer_edge[0]) period1 = timer_edge[1] - timer_edge[0]; else period1 = 65536 + timer_edge[1] - timer_edge[0];
/* calculate time count between two capture events */ if( timer_edge[2] >= timer_edge[1]) period2 = timer_edge[2] - timer_edge[1]; else period2 = 65536 + timer_edge[2] - timer_edge[1];
if (phase) duty = 100*((float)period1/(period1+period2)); else duty = 100*((float)period2/(period1+period2));
uitoa ( Convert_Buf , duty ) ; setcurLCD(12,1) ; putrsLCD( " " ) ; setcurLCD(12,1) ; putsLCD( Convert_Buf ) ;
Interrupt_Count = 0; IFS1bits.IC7IF = 0; ReadCapture7(timer_edge); EnableIntIC7; } CloseCapture7(); }
void uitoa( unsigned char *ASCII_Buf , unsigned int IntegerValue ) { unsigned int TempValue ; unsigned char ZeroDisable ; unsigned int BaseQty ; unsigned int Loop ;
ZeroDisable = 1 ; BaseQty = 10000 ;
for ( Loop = 0 ; Loop < 4 ; Loop ++) { TempValue = IntegerValue / BaseQty ;
if ( TempValue > 0) { *ASCII_Buf++ = (unsigned char)TempValue + '0' ; ZeroDisable = 0 ; } else if ( ZeroDisable == 0 ) *ASCII_Buf++ = '0' ; IntegerValue = IntegerValue - ( TempValue * BaseQty ) ; BaseQty = BaseQty / 10 ;
} *ASCII_Buf++ = (unsigned char)IntegerValue + '0' ; *ASCII_Buf = (unsigned char) 0x00 ; }
發表於: 2009/3/30 18:59
|