Browsing this Thread:
1 Anonymous Users
Topic options
View mode
舊的在前
Re: PIC24FJ128GB106 PWM頻率問題
版主
Joined
: 2004/4/30 10:53最後登入時間
: 2021/11/30 18:11
From CAE, Microchip
Group:
站務管理者 註冊會員
Level : 75
HP : 744 / 1861
MP : 5367 / 108851
EXP : 45
PWM 設定後也要完成 PWM 輸出腳位的規劃。 你用的是 Software Trigger 方式,要注意如沒有軟體的觸發,PWM 是不會有動作的,且軟體觸發一次則 PWM 動作一次。
發表於: 2020/11/30 12:26
Re: PIC24FJ128GB106 PWM頻率問題
新會員
Joined
: 2020/11/20 13:50最後登入時間
: 2021/6/8 9:04
Group:
註冊會員
Level : 1
HP : 0 / 15
MP : 2 / 152
EXP : 60
非常感謝您的回覆 當初在看到教育光碟的時候想說dspic30不適用於我這顆pic24的晶片,所以沒有讀到這部分。 我目前是照著mcc設定為Edge-Aligned PWM mode設定如附圖 您提到的方式我會再研究的,謝謝。
Attach file :
oc1.jpg (71.42 KB)
發表於: 2020/11/30 12:14
Re: PIC24FJ128GB106 PWM頻率問題
版主
Joined
: 2004/4/30 10:53最後登入時間
: 2021/11/30 18:11
From CAE, Microchip
Group:
站務管理者 註冊會員
Level : 75
HP : 744 / 1861
MP : 5367 / 108851
EXP : 45
看起來你的 PWM 應該是使用 Compare Match Mode 來產生的。在此模式下,有些暫存器的設定值是有限制的,這需要遵守一個設定的原則: PR2 >= OCxRS>OCxR 。 PR2 是 Timer2 的計時器提供了 PWM 的週期,改變 PR2 的值就可以改變 PWM 的頻率。 一般常使用的 PWM Mode 是設定成 "Continuous Output Pulse mode ";在此模式下產生的是連續的 PWM 波形,PR2 從 0 往上計數,PR2 == OCxR 時期 PWM 的輸出為 Hi;當 PR2 在往上計數到 == OCxRS 時,PWM 輸出便 Low ;此動作會一直循環而產生 PWM 的輸出。調整 PR2 可以改變 PWM 週期,變更 OCxR 及 OCxRS 則可改變 Duty。 關於此動作說明可以到教育訓練光碟下載 "dsPICMOD dsPIC30F Module Training" 裡面有一 "9. Output Comparator" 的教材面就有說明。 Compare Match Mode 的輸出參考一下貼圖。
Attach file :
擷取.JPG (56.67 KB)
發表於: 2020/11/30 11:29
Re: PIC24FJ128GB106 PWM頻率問題
新會員
Joined
: 2020/11/20 13:50最後登入時間
: 2021/6/8 9:04
Group:
註冊會員
Level : 1
HP : 0 / 15
MP : 2 / 152
EXP : 60
您好,感謝回覆
我使用的是mcc的程式生成,以下是相關code
oc1的設定。
參照:
#include "oc1.h"
/** OC Mode.
@Summary
Defines the OC Mode.
@Description
This data type defines the OC Mode of operation.
*/
static uint16_t gOC1Mode ;
/**
Section: Driver Interface
*/
void OC1_Initialize ( void )
{
// ENFLT0 disabled; OCSIDL disabled; OCM Edge-Aligned PWM mode; OCFLT0 disabled; OCTSEL TMR2; TRIGMODE Only Software;
OC1CON1 = 0x06 ;
// SYNCSEL TMR2; TRIGSTAT disabled; OCINV disabled; OCTRIG Trigger; OC32 disabled; FLTOUT disabled; OCTRIS disabled; FLTMD Cycle; FLTTRIEN disabled;
OC1CON2 = 0xCC ;
// CMP2B 100;
OC1RS = 1000 ;
// CMP1B 30;
OC1R = 300 ;
// OC1TMR 0;
OC1TMR = 0x00 ;
gOC1Mode = OC1CON1bits . OCM ;
IFS0bits . OC1IF = false ;
IEC0bits . OC1IE = true ;
}
void __attribute__ (( weak )) OC1_CallBack ( void )
{
// Add your custom callback code here
}
void __attribute__ ( ( interrupt , no_auto_psv ) ) _ISR _OC1Interrupt ( void )
{
if( IFS0bits . OC1IF )
{
// OC1 callback function
OC1_CallBack ();
IFS0bits . OC1IF = 0 ;
}
}
void OC1_Start ( void )
{
OC1CON1bits . OCM = gOC1Mode ;
}
void OC1_Stop ( void )
{
OC1CON1bits . OCM = 0 ;
}
void OC1_SecondaryValueSet ( uint16_t secVal )
{
OC1RS = secVal ;
}
void OC1_PrimaryValueSet ( uint16_t priVal )
{
OC1R = priVal ;
}
bool OC1_IsCompareCycleComplete ( void )
{
return( IFS0bits . OC1IF );
}
bool OC1_FaultStatusGet ( OC1_FAULTS faultNum )
{
bool status ;
/* Return the status of the fault condition */
switch( faultNum )
{
case OC1_FAULT0 :
status = OC1CON1bits . OCFLT0 ;
break;
default :
break;
}
return( status );
}
void OC1_FaultStatusClear ( OC1_FAULTS faultNum )
{
switch( faultNum )
{
case OC1_FAULT0 :
OC1CON1bits . OCFLT0 = 0 ;
break;
default :
break;
}
}
void OC1_ManualTriggerSet ( void )
{
OC1CON2bits . TRIGSTAT = true ;
}
bool OC1_TriggerStatusGet ( void )
{
return( OC1CON2bits . TRIGSTAT );
}
void OC1_TriggerStatusClear ( void )
{
/* Clears the trigger status */
OC1CON2bits . TRIGSTAT = 0 ;
}
/**
End of File
*/
timer2的設定。
參照:
#include <stdio.h>
#include "tmr2.h"
/**
Section: File specific functions
*/
/**
Section: Data Type Definitions
*/
/** TMR Driver Hardware Instance Object
@Summary
Defines the object required for the maintenance of the hardware instance.
@Description
This defines the object required for the maintenance of the hardware
instance. This object exists once per hardware instance of the peripheral.
Remarks:
None.
*/
typedef struct _TMR_OBJ_STRUCT
{
/* Timer Elapsed */
volatile bool timerElapsed ;
/*Software Counter value*/
volatile uint8_t count ;
} TMR_OBJ ;
static TMR_OBJ tmr2_obj ;
/**
Section: Driver Interface
*/
void TMR2_Initialize ( void )
{
//TMR2 0;
TMR2 = 0x00 ;
//Period = 0.01 s; Frequency = 8000000 Hz; PR2 9999;
PR2 = 0x270F ;
//TCKPS 1:8; T32 16 Bit; TON enabled; TSIDL disabled; TCS FOSC/2; TGATE disabled;
T2CON = 0x8010 ;
tmr2_obj . timerElapsed = false ;
}
void TMR2_Tasks_16BitOperation ( void )
{
/* Check if the Timer Interrupt/Status is set */
if( IFS0bits . T2IF )
{
tmr2_obj . count ++;
tmr2_obj . timerElapsed = true ;
IFS0bits . T2IF = false ;
}
}
void TMR2_Period16BitSet ( uint16_t value )
{
/* Update the counter values */
PR2 = value ;
/* Reset the status information */
tmr2_obj . timerElapsed = false ;
}
uint16_t TMR2_Period16BitGet ( void )
{
return( PR2 );
}
void TMR2_Counter16BitSet ( uint16_t value )
{
/* Update the counter values */
TMR2 = value ;
/* Reset the status information */
tmr2_obj . timerElapsed = false ;
}
uint16_t TMR2_Counter16BitGet ( void )
{
return( TMR2 );
}
void TMR2_Start ( void )
{
/* Reset the status information */
tmr2_obj . timerElapsed = false ;
/* Start the Timer */
T2CONbits . TON = 1 ;
}
void TMR2_Stop ( void )
{
/* Stop the Timer */
T2CONbits . TON = false ;
}
bool TMR2_GetElapsedThenClear ( void )
{
bool status ;
status = tmr2_obj . timerElapsed ;
if( status == true )
{
tmr2_obj . timerElapsed = false ;
}
return status ;
}
int TMR2_SoftwareCounterGet ( void )
{
return tmr2_obj . count ;
}
void TMR2_SoftwareCounterClear ( void )
{
tmr2_obj . count = 0 ;
}
/**
End of File
*/
原本想放上截圖但圖片無法插入,以上是mcc生成的code
謝謝。
發表於: 2020/11/30 11:07
Re: PIC24FJ128GB106 PWM頻率問題
管理員
Joined
: 2010/6/30 16:18最後登入時間
: 5/3 15:24
From CAE, Microchip Technology Inc.
Group:
站務管理者 註冊會員
Level : 36
HP : 0 / 891
MP : 652 / 36076
EXP : 67
OCxRS/OCxR 在PWM模式下,是Dual Buffer的架構,主要是避免突然改變數值造成當前周期的PWM工作週期異常的狀態。 以上是多說了。 簡單的說,OCxRS/OCxR就是改變周期的,PRx則是改變頻率的。 如果動作不正常就是操作上有錯誤,是否可以把你的程式碼上傳來看看,或許能找到問題點。
發表於: 2020/11/30 9:45
PIC24FJ128GB106 PWM頻率問題
新會員
Joined
: 2020/11/20 13:50最後登入時間
: 2021/6/8 9:04
Group:
註冊會員
Level : 1
HP : 0 / 15
MP : 2 / 152
EXP : 60
各位前輩好,小弟目前剛碰PIC24的晶片,以前也沒有過相關經驗,還請各位前輩多多賜教 我目前正在製作PWM,用的是PIC24FJ128GB106這塊晶片,使用MPLAB X IDE內的MCC撰寫程式 我上網查資料以及看了datasheet後有些地方一直不了解。 根據datasheet中pwm的頻率公式是:PWM Period = [(PRy) + 1] • TCY • (Timer Prescale Value) 裡面說明與PRy有關係。 但是測試後發現幾個問題: 1、我在調整PWM的DUTY時更動OCxRS OCxR 這兩個暫存器時竟然會連帶影響到PWM的頻率 2、根據原理,我調整PR2(我使用TIMER2)時並不會改變PWM的頻率 3、根據理論,當我的PR2+1我想請問要如何才能正確的調整PWM的頻率? 感謝
發表於: 2020/11/29 20:56
You can view topic.
你 不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.