คือจะสร้างสัญญาณ sine3phase ได้อย่างไรครับใช้ c18 compiler พี่ๆช่วนเขียนต่อให้หน่อยครับไแล้วจำเป็นหรือไม่ที่ต้องสร้าง spwm ในการควบคุมความเร็วมอเตอร์เหี่ยวนำสามเฟส แล้วมันแตกต่างยังไงกับ pwm ปกติครับ
pic 18f4431 ใช้ PWM 6ช่องไปขับIGBT แล้วจะมาสร้างสัญญาญsine 3เฟสได้ยังไงผมเคยเห็นแต่ pic 16f ใช้ PWM 3 ช่องมาสร้างสัญญาณ sine3 เฟสผมเลยงงครับเคยอ่านเค้าบอกว่าต้องใช้ sinetable แต่ผมก็ยังไม่ค่อยเค้าใจทำก็ไม่ค่อยถูก ขอบคุณมากๆๆครับ ^__^
//----------------------------------------------------------------------------
// configuration bits for PIC18F4431
#pragma config OSC = IRC // Internal oscillator, clock output at RA6
#pragma config FCMEN = OFF // Fail-Save Clock Monitor: Disabled
#pragma config IESO = OFF //Internal/External Switch-Over: Disabled
#pragma config PWRTEN = OFF // Power-up Timer: Disabled
#pragma config BOREN = ON // Brown-out Reset: Enabled
#pragma config BORV = 42 // 4.2 V Brown-out
#pragma config WDTEN = OFF // Watchdog Timer: Disabled
#pragma config WINEN = OFF // Watchdog Timer Enable Window: Disable
#pragma config WDPS = 32768 // Watchdog Postscaler: not used
#pragma config T1OSCMX = OFF // inactive
#pragma config HPOL = HIGH // high-side transistor is active HIGH
#pragma config LPOL = HIGH // low-side transistor is active HIGH
#pragma config MCLRE = ON // MASTER Clear: ENABLED
#pragma config PWMPIN = OFF // PWM pins INACTIVED upon RESET
#pragma config EXCLKMX = RC3 // External clock input at RC3
#pragma config PWM4MX = RB5 // PWM4 output at RB5
#pragma config SSPMX = RD1 // SSP I/0 at RD1
#pragma config FLTAMX = RD4 // Fault detection at RD4
#pragma config STVREN = OFF // Stack overflow RESET ENABLED
#pragma config LVP = OFF // Low voltage programming OFF
#pragma config DEBUG = OFF // Debug ENABLED
#pragma config CP0 = OFF // no code protection
#pragma config CP1 = OFF // no code protection
#pragma config CP2 = OFF // no code protection
#pragma config CP3 = OFF // no code protection
#pragma config CPB = OFF // no code protection for boot block
#pragma config CPD = OFF // no code protection for data EEPROM
#pragma config WRT0 = OFF // no write protection for block 0
#pragma config WRT1 = OFF // no write protection for block 1
#pragma config WRT2 = OFF // no write protection for block 2
#pragma config WRT3 = OFF // no write protection for block 3
#pragma config WRTB = OFF // no write protection for boot block
#pragma config WRTC = OFF // no write protection for configuration register
#pragma config WRTD = OFF // no write protection for data EEPROM
#pragma config EBTR0 = OFF // no table read protection for block 0
#pragma config EBTR1 = OFF // no table read protection for block 1
#pragma config EBTR2 = OFF // no table read protection for block 2
#pragma config EBTR3 = OFF // no table read protection for block 3
#pragma config EBTRB = OFF // no table read protection for boot block
//----------------------------------------------------------------------------
/** I N C L U D E S **************************************************/
#include <p18f4431.h>
//----------------------------------------------------------------------------
// Function prototype
//----------------------------------------------------------------------------
void osc_init(void);
void pwm_init(void);
//----------------------------------------------------------------------------
// constants
//----------------------------------------------------------------------------
#define PWM_DEFAULT_PERIOD 0x00FF
#define PWM_PAIR0_DEFAULT_DUTY 0x007F
#define PWM_PAIR1_DEFAULT_DUTY 0x003F
#define PWM_PAIR2_DEFAULT_DUTY 0x001F
#define PWM_DEFAULT_DEADTIME 0x02;
//----------------------------------------------------------------------------
// global variables and constants
//----------------------------------------------------------------------------
unsigned int pwm_current_period = PWM_DEFAULT_PERIOD;
unsigned int pwm_pair0_current_duty = PWM_PAIR0_DEFAULT_DUTY;
unsigned int pwm_pair1_current_duty = PWM_PAIR1_DEFAULT_DUTY;
unsigned int pwm_pair2_current_duty = PWM_PAIR2_DEFAULT_DUTY;
//----------------------------------------------------------------------------
// main function
//----------------------------------------------------------------------------
void main(void){
OSCCON |= 0b11110011; // internal oscillator run at 8 Mhz
pwm_init();
while(1){
}
}
//----------------------------------------------------------------------------
// set up oscillator
//----------------------------------------------------------------------------
void osc_init(void){
OSCCON |= 0b11110011; // internal oscillator run at 8 Mhz
}
//----------------------------------------------------------------------------
// set up pwm
//----------------------------------------------------------------------------
void pwm_init(void){
PTCON0 = 0b00000000; // PTOS = 0000 -> 1:1 Postscale
// PTKPS = 00 -> PWM Time base input clock is Fosc/4
// PTMOD = 00 -> Free running mode
PWMCON0 = 0b01000000; // enable PWM0 - PWM5
// all PWM outputs are complimentary
PWMCON1 = 0b00001001; // PWM special event post sclase = 1:1
// special event trigger when PWM is count down
// update duty cycle and period buffer is enable
// output override is synchronized
// set up the initial pwm timer base count
// PTPER = PTPERH<3:0>:PTPERL; 12-bit wide
// Tpwm = (PTPER+1)*Pre-scaler/(Fosc/4) in free running/single-shot mode
// Tpwm = (2*PTPER)*Pre-scaler/(Fosc/4) in up/down count double edge aligned
PTPERH = (PWM_DEFAULT_PERIOD >> 8) & 0xF;
PTPERL = PWM_DEFAULT_PERIOD & 0xFF;
PDC0H = (PWM_PAIR0_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM0 and PWM1
PDC0L = (PWM_PAIR0_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM0 and PWM1
PDC1H = (PWM_PAIR1_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM2 and PWM3
PDC1L = (PWM_PAIR1_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM2 and PWM3
PDC2H = (PWM_PAIR2_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM4 and PWM5
PDC2L = (PWM_PAIR2_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM4 and PWM5
// set up dead time control
// DTPS = 01 -> Fosc/4 is the clock source
// DTCON<5:0> is the deadtime value
DTCON = 0b01000000 | PWM_DEFAULT_DEADTIME;
PTCON1bits.PTEN = 1; // enable PWM
}
จากคุณ B(Nut) (A:192.168.0.127 X:113.53.77.181)
สร้างสัญญาณ sine3 เฟส
pic 18f4431 ใช้ PWM 6ช่องไปขับIGBT แล้วจะมาสร้างสัญญาญsine 3เฟสได้ยังไงผมเคยเห็นแต่ pic 16f ใช้ PWM 3 ช่องมาสร้างสัญญาณ sine3 เฟสผมเลยงงครับเคยอ่านเค้าบอกว่าต้องใช้ sinetable แต่ผมก็ยังไม่ค่อยเค้าใจทำก็ไม่ค่อยถูก ขอบคุณมากๆๆครับ ^__^
//----------------------------------------------------------------------------
// configuration bits for PIC18F4431
#pragma config OSC = IRC // Internal oscillator, clock output at RA6
#pragma config FCMEN = OFF // Fail-Save Clock Monitor: Disabled
#pragma config IESO = OFF //Internal/External Switch-Over: Disabled
#pragma config PWRTEN = OFF // Power-up Timer: Disabled
#pragma config BOREN = ON // Brown-out Reset: Enabled
#pragma config BORV = 42 // 4.2 V Brown-out
#pragma config WDTEN = OFF // Watchdog Timer: Disabled
#pragma config WINEN = OFF // Watchdog Timer Enable Window: Disable
#pragma config WDPS = 32768 // Watchdog Postscaler: not used
#pragma config T1OSCMX = OFF // inactive
#pragma config HPOL = HIGH // high-side transistor is active HIGH
#pragma config LPOL = HIGH // low-side transistor is active HIGH
#pragma config MCLRE = ON // MASTER Clear: ENABLED
#pragma config PWMPIN = OFF // PWM pins INACTIVED upon RESET
#pragma config EXCLKMX = RC3 // External clock input at RC3
#pragma config PWM4MX = RB5 // PWM4 output at RB5
#pragma config SSPMX = RD1 // SSP I/0 at RD1
#pragma config FLTAMX = RD4 // Fault detection at RD4
#pragma config STVREN = OFF // Stack overflow RESET ENABLED
#pragma config LVP = OFF // Low voltage programming OFF
#pragma config DEBUG = OFF // Debug ENABLED
#pragma config CP0 = OFF // no code protection
#pragma config CP1 = OFF // no code protection
#pragma config CP2 = OFF // no code protection
#pragma config CP3 = OFF // no code protection
#pragma config CPB = OFF // no code protection for boot block
#pragma config CPD = OFF // no code protection for data EEPROM
#pragma config WRT0 = OFF // no write protection for block 0
#pragma config WRT1 = OFF // no write protection for block 1
#pragma config WRT2 = OFF // no write protection for block 2
#pragma config WRT3 = OFF // no write protection for block 3
#pragma config WRTB = OFF // no write protection for boot block
#pragma config WRTC = OFF // no write protection for configuration register
#pragma config WRTD = OFF // no write protection for data EEPROM
#pragma config EBTR0 = OFF // no table read protection for block 0
#pragma config EBTR1 = OFF // no table read protection for block 1
#pragma config EBTR2 = OFF // no table read protection for block 2
#pragma config EBTR3 = OFF // no table read protection for block 3
#pragma config EBTRB = OFF // no table read protection for boot block
//----------------------------------------------------------------------------
/** I N C L U D E S **************************************************/
#include <p18f4431.h>
//----------------------------------------------------------------------------
// Function prototype
//----------------------------------------------------------------------------
void osc_init(void);
void pwm_init(void);
//----------------------------------------------------------------------------
// constants
//----------------------------------------------------------------------------
#define PWM_DEFAULT_PERIOD 0x00FF
#define PWM_PAIR0_DEFAULT_DUTY 0x007F
#define PWM_PAIR1_DEFAULT_DUTY 0x003F
#define PWM_PAIR2_DEFAULT_DUTY 0x001F
#define PWM_DEFAULT_DEADTIME 0x02;
//----------------------------------------------------------------------------
// global variables and constants
//----------------------------------------------------------------------------
unsigned int pwm_current_period = PWM_DEFAULT_PERIOD;
unsigned int pwm_pair0_current_duty = PWM_PAIR0_DEFAULT_DUTY;
unsigned int pwm_pair1_current_duty = PWM_PAIR1_DEFAULT_DUTY;
unsigned int pwm_pair2_current_duty = PWM_PAIR2_DEFAULT_DUTY;
//----------------------------------------------------------------------------
// main function
//----------------------------------------------------------------------------
void main(void){
OSCCON |= 0b11110011; // internal oscillator run at 8 Mhz
pwm_init();
while(1){
}
}
//----------------------------------------------------------------------------
// set up oscillator
//----------------------------------------------------------------------------
void osc_init(void){
OSCCON |= 0b11110011; // internal oscillator run at 8 Mhz
}
//----------------------------------------------------------------------------
// set up pwm
//----------------------------------------------------------------------------
void pwm_init(void){
PTCON0 = 0b00000000; // PTOS = 0000 -> 1:1 Postscale
// PTKPS = 00 -> PWM Time base input clock is Fosc/4
// PTMOD = 00 -> Free running mode
PWMCON0 = 0b01000000; // enable PWM0 - PWM5
// all PWM outputs are complimentary
PWMCON1 = 0b00001001; // PWM special event post sclase = 1:1
// special event trigger when PWM is count down
// update duty cycle and period buffer is enable
// output override is synchronized
// set up the initial pwm timer base count
// PTPER = PTPERH<3:0>:PTPERL; 12-bit wide
// Tpwm = (PTPER+1)*Pre-scaler/(Fosc/4) in free running/single-shot mode
// Tpwm = (2*PTPER)*Pre-scaler/(Fosc/4) in up/down count double edge aligned
PTPERH = (PWM_DEFAULT_PERIOD >> 8) & 0xF;
PTPERL = PWM_DEFAULT_PERIOD & 0xFF;
PDC0H = (PWM_PAIR0_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM0 and PWM1
PDC0L = (PWM_PAIR0_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM0 and PWM1
PDC1H = (PWM_PAIR1_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM2 and PWM3
PDC1L = (PWM_PAIR1_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM2 and PWM3
PDC2H = (PWM_PAIR2_DEFAULT_DUTY >> 6) & 0x3F; // set up duty cycle for PWM4 and PWM5
PDC2L = (PWM_PAIR2_DEFAULT_DUTY << 2) & 0xFF; // set up duty cycle for PWM4 and PWM5
// set up dead time control
// DTPS = 01 -> Fosc/4 is the clock source
// DTCON<5:0> is the deadtime value
DTCON = 0b01000000 | PWM_DEFAULT_DEADTIME;
PTCON1bits.PTEN = 1; // enable PWM
}
จากคุณ B(Nut) (A:192.168.0.127 X:113.53.77.181)