• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






關於輸出PWM訊號
#1
新會員
新會員


查看用戶資訊
假如我有一個程式如下:
那麼我要修改哪裏讓它可以單純的輸出PWM訊號
且我想要用可變電組來調變DUTY CYCLE
程式如下
#include "p30F4011.h"
#define FCY 29491200 // // Fosc = 7.3728 MHz , XT_PLL16
#define PTPERvalue 920 // FPWM = 16 KHz ,[(Fcy/16K)/2]

_FOSC(CSW_FSCM_OFF & XT_PLL16); // XT with 4xPLL oscillator , Failsafe clock off
_FWDT(WDT_OFF); // Watchdog timer disabled
_FBORPOR(PBOR_OFF & MCLR_EN & PWMxH_ACT_HI & PWMxL_ACT_HI); //Brown-out reset disabled , MCLR reset enabled
// _FBORPOR(PBOR_OFF & MCLR_EN & PWMxH_ACT_LO & PWMxL_ACT_LO); //Brown-out reset disabled , MCLR reset enabled , PWM Active Low
// _FBORPOR(PBOR_OFF & MCLR_EN);
_FGS(CODE_PROT_OFF); //Code protect disabled


void InitMCPWM(void); // Init PWM Module to drive the Inverter
void InitADC10(void); // A/D Init Subroutine to Read POT.
void InitCN(void); // Change Notification for Hall Effect Sensors

unsigned int HallValue; // Variable containing the Hall Value from PORTB

/*************************************************************
Low side driver table is as below. In the StateLoTableClk
and the StateLoTableAntiClk tables, the Low side driver is
PWM while the high side driver is either on or off.
*************************************************************/

unsigned int StateLoTable[] = {0x0300, /* PWM1L -> PWM, PWM1H -> PWM*/
0x0900, /* PWM1L -> PWM, PWM2H -> PWM*/
0x0600, /* PWM2L -> PWM, PWM1H -> PWM*/
0x0C00, /* PWM2L -> PWM, PWM2H -> PWM*/
0x0600, /* PWM2L -> PWM, PWM1H -> PWM*/
0x0300, /* PWM1L -> PWM, PWM1H -> PWM*/
0x0C00, /* PWM2L -> PWM, PWM2H -> PWM*/
0x0900}; /* PWM1L -> PWM, PWM2H -> PWM*/

/****************************************************************
Interrupt vector for Change Notification CN5, 6 and 7 is as below. When a
Hall sensor changes states, an interrupt will be caused which will vector
to the routine below. The program then reads PORTB, mask bits 3, 4 and 5,
shift and adjust the value to read as 1, 2 ... 6. This value is then used
as an offset in the lookup table StateLoTableClk or StateLoTableAntiClk to
determine the value loaded in the OCDCON register.
*****************************************************************/

void __attribute__((__interrupt__)) _CNInterrupt (void)
{
TRISDbits.TRISD2=0;
LATDbits.LATD2=0;
IFS0bits.CNIF = 0; // clear flag
HallValue = PORTB & 0x0038; // mask RB3,4 & 5
HallValue = HallValue >> 3; // shift right 3 times
OVDCON = StateLoTable[HallValue]; // Energize Transistors from Table
LATDbits.LATD2=1;
}

/*********************************************************************
The ADC interrupt loads the PDCx registers with the demand pot
value. This is only done when the motor is running.
*********************************************************************/

void __attribute__((__interrupt__)) _ADCInterrupt (void)
{
TRISDbits.TRISD3=0;
LATDbits.LATD3=0;
//TRISD = 0xFFF7 ;
//LATD = 0x0008 ;
IFS0bits.ADIF = 0; // Clear Interrupt Flag
PDC1 = (ADCBUF0*0.00097)*1840 ; // get value ...
PDC2 = PDC1; // and load all three PWMs ...
PDC3 = 0; // duty cycles
//LATD = 0x0000 ;
LATDbits.LATD3=1;

}

/*********************************************************************
main subroutine. It initialized the peripherals used in the application,
* Input Change Notifications for Hall effect sensores,
* MCPWM module for driving the inverter,
* A/D Converter for reading the POT value.
When S2 is pressed, the hall effect sensors are read the the corresponding
transistors are turned on depending on a look up table. After energizing the
first pair of transistors everything else is done in the Change Notification
interrupts.
*********************************************************************/

int main(void)
{

InitCN(); // Init CN for Hall effect sensor inputs
InitMCPWM(); // Init PWM for 16 kHz operation
InitADC10(); // Init ADC for POT measurement
while(1);

}

/*******************************************************************
Below is the code required to setup the ADC registers for :
1. 1 channel conversion (in this case RB2/AN2)
2. PWM trigger starts conversion
3. Pot is connected to CH0 and RB2
4. Manual Stop Sampling and start converting
5. Manual check of Conversion complete
*********************************************************************/

void InitADC10(void)
{
ADPCFG = 0xFFFD; // all PORTB = Digital ; RB1 = analog
ADCON1 = 0x0064; // PWM starts conversion
ADCON2 = 0x0000; // simulataneous sample 1 channels
ADCHS = 0x0001; // Connect RB1/AN1 as CH0 = pot ..
// ch1 = Vbus, Ch2 = Motor, Ch3 = pot
ADCON3 = 0x0013; // Tad = 10Tcy Tad>=300ns
IFS0bits.ADIF = 0;
IEC0bits.ADIE = 1;

ADCON1bits.ADON = 1;// turn ADC ON

return;
}

/********************************************************************
InitMCPWM, intializes the PWM as follows:
1. FPWM = 19531 hz
2. Independant PWMs
3. Control outputs using OVDCON
4. Set Duty Cycle with the ADC value read from pot
5. Set ADC to be triggered by PWM special trigger
*********************************************************************/

void InitMCPWM(void)
{
PTPER = PTPERvalue ;
PWMCON1 = 0x0733; // disable PWMs
OVDCON = 0x0000; // allow control using OVD
PDC1 = 0; // init PWM 1, 2 and 3 to 0%(Duty_cycle)
PDC2 = 0;
PDC3 = 0;
SEVTCMP = 0x0000;
PWMCON2 = 0x0F00; // 16 postscale values
PTCON = 0x8002 ; // start PWM ; center-mode
return;
}

/********************************************************************
InitCN, Initializes the Input Change Notification for the Hall Effect
Sensors
*********************************************************************/

void InitCN(void)
{
CNEN1 = 0x00E0; // Enable CN5(RB3), CN6(RB4) and CN7(RB5)
IFS0bits.CNIF = 0; // clear CNIF
IEC0bits.CNIE = 1; // enable CN interrupt
return;
}



謝謝

發表於: 2007/7/25 10:01
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於輸出PWM訊號
#2
版主
版主


查看用戶資訊
只是單存要做 PWM 輸出可以考慮用 Output Compare Module 就可以了,可以參考底下的連結下載 dsPIC30F Peripheral Module - Output Compare Module 講義及教材看看順便做一下練習 :

http://www.microchip.com.tw/modules/mydownloads/viewcat.php?cid=4

發表於: 2007/7/25 10:31
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於輸出PWM訊號
#3
新會員
新會員


查看用戶資訊
我已經有去下載你說的範例了,但是範例的PWM的duty cycle好像是固定的,我想要外接一個可變電組來調變,不知道行嗎

發表於: 2007/7/25 11:31
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於輸出PWM訊號
#4
新會員
新會員


查看用戶資訊
不是有ADC么!!

發表於: 2007/7/25 13:28
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於輸出PWM訊號
#5
新會員
新會員


查看用戶資訊
版主你好,因為我是DSPIC的新手,所以我還有很多都不太懂,
我也有買DSPIC的書來看,但是很多還是都不太懂,所以可以請教一下,如果我只是單純要利用可變電組來輸出PWM的話要怎麼學程式會比較快阿,還有我可以請版主跟我說一下,你叫我下載的那個範例,大概的結構嗎,真不好意思

發表於: 2007/7/25 14:17
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 關於輸出PWM訊號
#6
版主
版主


查看用戶資訊
再看一下 ADC 的講義,將 ADC 轉換出來的結果送到調整 Duty Cycle 的站存器就可以了。

逆可以先用一個固定值先給 Duty Cycle Register 看看 Duty Cycle 是否會改變,注意 !!!! Duty value 一定要小於 Period 的值才可以。

發表於: 2007/7/25 14:18
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







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.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------