I need help in PIC18 compare module.

Status
Not open for further replies.

Prince Vegeta

Member level 5
Joined
May 29, 2012
Messages
84
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
2,011
Hi guys ^_^

I have a problem in PIC18F6520 CCP4 compare module.

I work in a project that includes controlling a DC-to-AC inverter using PIC18, all I need is a 50Hz signal with 50% duty cycle.

so 50Hz = 20ms, 1st 10ms will be + and last 10ms will be - in order to control the inverter circuit.

I saw Tahmid's code, modified it so it can suit the PIC18F6520 I want to use... then I ran the simulation in Proteus but the interval of the + half cycle is too fast (in us) which isn't proper (it should be 10ms)...

After many trials I finally decided to post the issue and the code here so everyone can help, here's the code:

Code:
void interrupt(){
 if (CCP4IF_bit == 1){
        RE4_bit = ~ RE4_bit; //Toggle RC0
        CCP4IF_bit = 0;
        RE3_bit = RE4_bit;
        TMR3L = 0;
        TMR3H = 0;
     }



}

void main() {
     PORTE = 0;
     TRISE = 0;
     CCP4CON = 10;
     CCP4IE_bit = 1;
     GIE_bit = 1;
     PEIE_bit = 1;
     CCPR4H = 0x9C;
     CCPR4L = 0x40;

     T3CON = 0x41; //Prescaler 1:1, start Timer 1

     while(1){
     //Do whatever else is required

     }
I PM Tahmid yesterday for this issue but it seems like he is busy or so...

It may have wrong Timer3 configurations due to many trials made...

Thank you in advance ^______________^
 

Hi,

Indeed I was busy yesterday.

Anyways, here is the code you should use:

Code:
void interrupt(){
 if (CCP4IF_bit == 1){
        RE4_bit = ~ RE4_bit; //Toggle RC0
        CCP4IF_bit = 0;
        //RE3_bit = RE4_bit;
        TMR3L = 0;
        TMR3H = 0;
     }

}

void main() {
     PORTE = 0;
     TRISE = 0;
     
     CCP4CON = 10;
     CCPR4H = 0x9C;
     CCPR4L = 0x40;
     
     CCP4IE_bit = 1;
     CCP4IF_bit = 0;
     GIE_bit = 1;
     PEIE_bit = 1;

     T3CON = 0x41; //Prescaler 1:1, start Timer 1
     
     while(1){
     //Do whatever else is required

     }
}

I have noticed that there is a problem when you set the CCPR4H and CCPR4L registers later, that does not happen when you set the CCPR4H and CCPR4L registers right after you set CCP4CON.

Hope this helps.
Tahmid.
 

thank you... I will try the new code soon. But, did you tried it in proteus?

BTW, why you disabled RE3_bit = RE4_bit in the interrupt service routine?

I need to use 2 pins to control the inverter, these 2 pins depends on CCP4 pin status...

should I assign the pin status in interrupt or in while sentence?

I need to do more stuff like ADC reading and changing output pins' status... etc. Will that affect the behavior of the this code?

Thanks ^_^
 

thank you... I will try the new code soon. But, did you tried it in proteus?

Yes I did. I used 16MHz oscillator frequency. It works fine. Here's a screenshot:



BTW, why you disabled RE3_bit = RE4_bit in the interrupt service routine?
I just did that for my testing. You can re-enable it.

I need to use 2 pins to control the inverter, these 2 pins depends on CCP4 pin status...

should I assign the pin status in interrupt or in while sentence?

You already used 2 pins. Do you plan on a different control method?

I need to do more stuff like ADC reading and changing output pins' status... etc. Will that affect the behavior of the this code?

Thanks ^_^

You can see that all of the functionality (in the code) is being handled on a hardware and interrupt level. The while loop is free - nothing is being done here. So, you can carry out all other functions in the while loop and the CCP module will function without any problem. You use the interrupt to do whatever is required there.

Hope this helps.
Tahmid.
 
aha, thanks... I would like you to give me the Proteus testing file you used if you can. upload it to any host or just here by attachment.

^ I asked that cuz I still a beginner in proteus and I wanna know how to properly set the circuit...

how can I measure the voltage at each pin like the CCP4 compare one? I used a voltmeter to do so in Proteus but it gave me a voltage less than 3v... it's supposed to be 5v right?

Thanks, you really helped me ^_^...
 

aha, thanks... I would like you to give me the Proteus testing file you used if you can. upload it to any host or just here by attachment.

^ I asked that cuz I still a beginner in proteus and I wanna know how to properly set the circuit...

In that case, I think it'll be better if you do it yourself so that you can learn it properly. :smile:

how can I measure the voltage at each pin like the CCP4 compare one? I used a voltmeter to do so in Proteus but it gave me a voltage less than 3v... it's supposed to be 5v right?

Thanks, you really helped me ^_^...

The voltmeter shows average reading. You have a 50Hz signal with 50% duty cycle. The peak is 5V since the PIC output is 5V when high and 0V when low. So the signal 'oscillates' between 5V and 0V, staying at 5V for half the time and 0V the remaining half. Thus the average voltage is (0.5 * 5V) = 2.5V which is probably what the voltmeter shows.

Hope this helps.
Tahmid.
 

thanks... I wonder how I forgot that it reads avg voltage!

If I couldn't do it properly after many trials, I will contact you.

THX
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…