It is currently 17 Dec 2017 17:12

All times are UTC + 1 hour




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Problem with PWM
PostPosted: 27 Nov 2017 16:27 
Offline

Joined: 28 Aug 2017 23:20
Posts: 10
Hello friends, I am a beginner in programming, could you explain why and when setting CCPR1L> = 255 and pressing button s1 again, CCCR1L does not stop at 255 and resumes the pulses in the PWM, and also does not stop at zero the button s2 less.

// --- Hardware ---
#define S1 RA0_bit
#define S2 RA1_bit
#define led2 RA2_bit
#define led1 RA3_bit
int valor;
void interrupt()
{
if(T0IF_bit) //Houve estouro do Timer0?
{ //Sim...

T0IF_bit = 0x00; //Limpa a flag

TMR0 = 0x6C; //Reinicia o timer0

if(!S1) //S1 pressionado?
{
delay_ms(300); //Sim...
//CCPR1L = 127;
CCPR1L= CCPR1L+51;
led1 = CCPR1L;

if ( CCPR1L == 255)
{
CCPR1L = 255;
}
} //end if

else if(!S2) //S2 pressionado?
{ //Sim...
//CCPR1L--;
CCPR1L = CCPR1L - 51;
led1 = CCPR1L;
if ( CCPR1L == 0)
{
CCPR1L = 0;
}
} //end else if


}

} //end interrupt


// --- Função Principal
void main()
{
CMCON = 0x07; //Desabilita os comparadores
OPTION_REG = 0x86; //Timer0 incrementa com ciclo de instrução, prescaler 1:128
GIE_bit = 0x01; //Habilita interrupção global
PEIE_bit = 0x01; //Habilita interrupção por periféricos
T0IE_bit = 0x01; //Habilita interrução do Timer0
TMR0 = 0x6C; //Inicia o timer0

//******COnfiguracao do timer2*** ele configra a freq do PWM e do ciclo ativo***
// periodo = (PR2 + 1) x ciclo de máquina x prescaler do Timer2
// periodo = 256 x 1us x 16 = 4,096ms
// frequencia = 1/periodo = 244,14Hz

// Ciclo Ativo= Estouo do TMR1= PR2+1 = TMR2 (quando há o estouro do Timer2, low para high) / CCPR1L:CCP1CON<5:4>
// A borda de descida é a combinacao dos 10bits .... CCPR1L:CCP1CON<5:4>


PR2 = 0xFF; //Inicializa o registrador de controle (estouro)do timer2 em 255
T2CON = 0x06; //Registrador q Liga o timer2 e prescaler 1:16
CCPR1L = 0x00; //LED iniciará desligado
CCP1CON = 0x0C;//Habilita o modo PWM, este registrador tambem possui s bits menos significativos para conf CCPR1L a 10 bits


TRISA = 0x03; //Entrada em RA0 e RA1
TRISB = 0x00; //Todo PORTB será saída
PORTA = 0x03; //RA0 e RA1 iniciam em high
PORTB = 0x00; //Todo PORTB inicia em nível low


while(1) //Loop Infinito
{
//

} //end while

} //end main


Top
 Profile  
 
 Post subject: Re: Problem with PWM
PostPosted: 29 Nov 2017 18:25 
Offline
mikroElektronika team
User avatar

Joined: 15 Jan 2016 12:50
Posts: 1599
Hi,

I suggest you to try to use our PWM library,
you can take a look in PWM example in the installation folder of the compiler,
which is located in for example:

c:\Users\Public\Documents\Mikroelektronika\mikroC PRO for PIC\Examples\Internal MCU modules\P16F887\PWM\

Kind regards,
Lana


Top
 Profile  
 
 Post subject: Re: Problem with PWM
PostPosted: 06 Dec 2017 22:40 
Offline

Joined: 28 Aug 2017 23:20
Posts: 10
Thank you friend lana.arsic


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: