Tình hình là thế này.em chơi robot cần điều khiển nhiều động cơ.e datasheet con này thấy 9 lỗ PWM nhưng không biết lập trình nó thế nào.e có đoạn code thế này.
#include <p18f4431.h>
#include <stdio.h>
#include <delays.h>
//---------------------------------Cau hinh chip------------------------------------
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config WDTEN = OFF
#pragma config T1OSCMX = OFF
#pragma config HPOL = HIGH
#pragma config LPOL = HIGH
#pragma config PWMPIN = OFF
#pragma config MCLRE = ON
#pragma config EXCLKMX = RD0
#pragma config PWM4MX = RB5
#pragma config SSPMX = RD1
#pragma config FLTAMX = RC1
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
//-----------------------------------end---------------------------------------------
//-------------------Khoi ham khoi tao-----------------------------
void Init_Interrupt(void)
{
//Cho phep ngat ngoai 0,1,2; Ngat uu tien cao
RCONbits.IPEN = 1;
INTCON = 0b11010000;
INTCON2 = 0x00;
INTCON3 = 0b11011000;
PIR1 = 0x00;
PIR2 = 0x00;
PIR3 = 0x00;
PIE1 = 0x00;
PIE2 = 0x00;
PIE3 = 0x00;
IPR1 = 0x00;
IPR2 = 0x00;
IPR3 = 0x00;
}
void Init_ADC(void)
{
ADCON0 = 0x00;
ADCON1 = 0x00;
ADCON2 = 0x80;
ADCON3 = 0x00;
ADCHS = 0x00; //D-C-B-A
}
void Init_PWM(void)
{
PTCON0 = 0x00; //Postscale 1:1, Prescale 1:1, Free Running
PTCON1 = 0x80; //PWM time base is on, Count Up!
PWMCON0 = 0x4F; //Enable PWM0-5, Independent Mode
PWMCON1 = 0x00;
PTMRH = 0;
PTMRL = 0;
PTPERH = ((2000>>8)&0x00ff);
PTPERL = (2000&0x00ff); // 1KHz PWM @ 8MHz, Duty_cycle max = 4*PTPER = 8000
DTCON = 0x00; //0us dead-time
PDC0H = 0;
PDC0L = 0;
PDC1H = 0;
PDC1L = 0;
PDC2H = 0;
PDC2L = 0;
OVDCOND = 0x18; //1= Enable PWM,0= Override
OVDCONS = 0xFF;
FLTCONFIG = 0; //Fault condition disabled
SEVTCMPH = 0;
SEVTCMPL = 0;
}
//------------------------------end----------------------------------
#include <p18f4431.h>
#include <stdio.h>
#include <delays.h>
//---------------------------------Cau hinh chip------------------------------------
#pragma config OSC = HS
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRTEN = OFF
#pragma config BOREN = OFF
#pragma config WDTEN = OFF
#pragma config T1OSCMX = OFF
#pragma config HPOL = HIGH
#pragma config LPOL = HIGH
#pragma config PWMPIN = OFF
#pragma config MCLRE = ON
#pragma config EXCLKMX = RD0
#pragma config PWM4MX = RB5
#pragma config SSPMX = RD1
#pragma config FLTAMX = RC1
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config CP1 = OFF
#pragma config CP2 = OFF
#pragma config CP3 = OFF
#pragma config CPB = OFF
#pragma config CPD = OFF
#pragma config WRT0 = OFF
#pragma config WRT1 = OFF
#pragma config WRT2 = OFF
#pragma config WRT3 = OFF
#pragma config WRTB = OFF
#pragma config WRTC = OFF
#pragma config WRTD = OFF
#pragma config EBTR0 = OFF
#pragma config EBTR1 = OFF
#pragma config EBTR2 = OFF
#pragma config EBTR3 = OFF
#pragma config EBTRB = OFF
//-----------------------------------end---------------------------------------------
//-------------------Khoi ham khoi tao-----------------------------
void Init_Interrupt(void)
{
//Cho phep ngat ngoai 0,1,2; Ngat uu tien cao
RCONbits.IPEN = 1;
INTCON = 0b11010000;
INTCON2 = 0x00;
INTCON3 = 0b11011000;
PIR1 = 0x00;
PIR2 = 0x00;
PIR3 = 0x00;
PIE1 = 0x00;
PIE2 = 0x00;
PIE3 = 0x00;
IPR1 = 0x00;
IPR2 = 0x00;
IPR3 = 0x00;
}
void Init_ADC(void)
{
ADCON0 = 0x00;
ADCON1 = 0x00;
ADCON2 = 0x80;
ADCON3 = 0x00;
ADCHS = 0x00; //D-C-B-A
}
void Init_PWM(void)
{
PTCON0 = 0x00; //Postscale 1:1, Prescale 1:1, Free Running
PTCON1 = 0x80; //PWM time base is on, Count Up!
PWMCON0 = 0x4F; //Enable PWM0-5, Independent Mode
PWMCON1 = 0x00;
PTMRH = 0;
PTMRL = 0;
PTPERH = ((2000>>8)&0x00ff);
PTPERL = (2000&0x00ff); // 1KHz PWM @ 8MHz, Duty_cycle max = 4*PTPER = 8000
DTCON = 0x00; //0us dead-time
PDC0H = 0;
PDC0L = 0;
PDC1H = 0;
PDC1L = 0;
PDC2H = 0;
PDC2L = 0;
OVDCOND = 0x18; //1= Enable PWM,0= Override
OVDCONS = 0xFF;
FLTCONFIG = 0; //Fault condition disabled
SEVTCMPH = 0;
SEVTCMPL = 0;
}
//------------------------------end----------------------------------
Comment