Why this code of PWM doesn't run in pic18f4520 or pic18f4550

General discussion on mikroC.
Post Reply
Author
Message
Anderson Liege
Posts: 3
Joined: 18 Jul 2014 18:15

Why this code of PWM doesn't run in pic18f4520 or pic18f4550

#1 Post by Anderson Liege » 18 Jul 2014 18:29

I'm trying to run this code but for some reason this code doesn't work, but if you put in any pic16 doing some adjusted this code runs.
This code is a SPWM (Pwm sinusoidal) , the crystal that I'm using is 20MHz and carrier frequency of 20Khz .

/* PROJETO SPWM COM PIC18f4520
ANDERSON LIEGE
*/

unsigned char sin_table[32]={0, 25, 50, 75, 99, 121, 143, 163, 181,
198, 212, 224, 234, 242, 247, 250, 250, 247, 242, 234, 224, 212, 198,
181, 163, 143, 121, 99, 75, 50, 25,0};

unsigned int TBL_POINTER_NEW, TBL_POINTER_OLD, TBL_POINTER_SHIFT, SET_FREQ;
unsigned char DUTY_CYCLE;

sbit MOSA at RD0_bit;
sbit MOSB at RD1_bit;
sbit MOSC at RD2_bit;
sbit MOSD at RD3_bit;

unsigned char FlagReg;
sbit Direction at FlagReg.B0;
//0 -> MOS A + D
//1 -> MOS B + C

//*******************CONFIGURAÇÃO DOS REGISTRADORES*****************************
void Config_SFR(){
SET_FREQ = 190; //29Hz
TRISB = 0b00010001; //Rb0 e Rb4 como entrada
TRISC = 0; //Porta C como saída
TRISD = 0; //Porta D como saída
PORTB = 0; //Limpa porta B
PORTC = 0; //Limpa porta C
PORTD = 0; //Limpa porta D
PR2 = 249; //Frequência portadora 20kHz
CCPR1L = 0; //Zera Duty
CCP1CON = 0b00001100; //modo PWM
TMR2IF_bit = 0; //Limpa flag da interrupção do Timer 2
INTCON.RBIF = 0; //Limpa flag da interrupção externa de Rb4
T2CON = 0b00000100; //TMR2 on; prescaler e postscaler = 1
INTCON.RBIE = 1; //Habilita interrupção externa no portal B pinos 4-7
PEIE_bit = 1; //Habilita interrupção de periféricos
TMR2IE_bit = 1; //Habilita interrupção do Timer 2
GIE_bit = 1; //Habilita interrupção Global
}
//******************************************************************************
//************************ROTINA DE INTERRUPÇÕES********************************
void interrupt(){
if (TMR2IF_bit == 1){
TBL_POINTER_NEW = TBL_POINTER_OLD + SET_FREQ;
if (TBL_POINTER_NEW < TBL_POINTER_OLD){
if (Direction == 0){
MOSA = 0;
MOSD = 0;
MOSB = 1;
MOSC = 1;
Direction = 1;
}
else{
MOSB = 0;
MOSC = 0;
MOSA = 1;
MOSD = 1;
Direction = 0;
}
}
TBL_POINTER_SHIFT = TBL_POINTER_NEW >> 11;
DUTY_CYCLE = TBL_POINTER_SHIFT;
CCPR1L = sin_table[DUTY_CYCLE];
TBL_POINTER_OLD = TBL_POINTER_NEW;
TMR2IF_bit = 0;
}
}
//******************************************************************************
//*****************************ROTINA PRINCIPAL*********************************
void main(){
Config_SFR();
while (1);
}
//******************************************************************************
Attachments
Here is the signal of pwm.
Here is the signal of pwm.
generated signals.png (143 KiB) Viewed 1940 times
Here is the pic conection.
Here is the pic conection.
pic config.png (57.6 KiB) Viewed 1940 times

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Why this code of PWM doesn't run in pic18f4520 or pic18f

#2 Post by filip » 21 Jul 2014 13:08

Hi,

I believe I have answered you on the other topic.

Regards,
Filip.

Post Reply

Return to “mikroC General”