Strange Problem Encountered Using MicroChip MPLAB IDE C Compiler

Status
Not open for further replies.

Regarding the max voltage of the ADC i think i have read somewhere that the max positive voltage is = to the max positive voltage reference to the ADC, if no voltage reference is specified then the max is the chip positive voltage, i guess that yours is the last option... only the more crafty chips have voltage reference inputs
In any case try tho ready on the datasheet of your Pic everything regarding the internal ADC, try to find something about the voltage references and if it is internal then you won't have to connect anything else, but you'l be limited... usually it might sufice


Regarding the PWM output, you can *try* (on the simulator first might be a good idea ) to use an bipolar transistor to drive the gate on the IR2101 !

Have fun !

P.S. : Regarding the logic you can try to find some ASM mnemonic to do the One's Complement or if using C/C++ use the "~" bitwise operator !
 
Last edited:
I don't know how you have the PWM setup but lets say you have it setup for 8 bits of duty cycle resolution. When setup like this, zero will be completely off and 255 will be completely on. If you want this to be the reverse then you complement the value before you put it in CCPRxL by doing xorlw 0xff.

Mike.
 

HI,
Actually I have already tried using a few comparator to generate the pwm signal to drive IR2101 and it works. But I wanted to reduce it to a single chip which microcontroller can achieve it.

I have already tired the ~ bitwise operator but i won't work. Perhaps I should use an Inverter externally.
 
Last edited:

Hi,

I have tried this method before but using the Exclusive OR function on the CCPR1L with a logic 1 but it seem not working. I have tried using the ~ bitwise function but same, it won't works.

perhaps I should use an external Inverter to invert the signal.
 
Hello,
weird stuff... have you checked if you're on the right register adress (to get the desired data) ???

Sometimes we can err on simple things and start looking for the more complex lol

Have a nice weekend or whats left of it !
 
Hello,
weird stuff... have you checked if you're on the right register adress (to get the desired data) ???

Sometimes we can err on simple things and start looking for the more complex lol

Have a nice weekend or whats left of it !

Haha.. Basically I sense my analog input and convert it to generate PWM by feeding the adc to CCP1L (only using 8bits instead of 10bits so, i'm only using CCP1L and not CCP1H) where my PWM is output on CCP1.

so when i try to invert the adc or bitwise it and feed it to CCP1L, it won't work. maybe i should just try to set any of the port to be = ~CCP1. Haha..
 
Hi,
It's better to post the code here so that we can see what's going wrong and anything missing.

Below is the code that I wrote for generating pwm using analog input.
Haha... debugging time start now.. haha....

#include <p18f4520.h>

#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config MCLRE = OFF

void configure_pins()
{
OSCCONbits.IRCF2 = 1;
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;

TRISA = 0b11111111;
ADCON1 = 0b00000111;
PR2 = 255;
CCPR1L = 255;
CCPR2L = 255;
CCP1CON = 0b00001100;
CCP2CON = 0b00001100;
TRISC = 0b11111001;
T2CONbits.T2CKPS1 = 0;
T2CONbits.T2CKPS0 = 0;
T2CONbits.TMR2ON = 1;
}

unsigned int read_analog_channel(unsigned int n)
{
ADCON0bits.ADON = 1;
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;
ADCON0bits.GO = 1;
while (ADCON0bits.GO);
return ADRESH;

}

void configure_pins(void);
unsigned int read_analog_channel(unsigned int);

void main(void)
{
unsigned int n;

configure_pins();
while(1)
{
n = read_analog_channel(0);
CCPR1L = n;
CCPR2L = 255-n
}

}
 
Last edited:
Hi,
May I know which one of them is not working? PWM of CCP1 or PWM of CCP2? Or both of them?
For the ADC part, if you're using only a single channel, why don't you use AN0 and set PCFG3CFG0 to 1110. And I don't see why do you need these:
Code:
ADCON0bits.ADON = 1;
ADCON0bits.CHS0 = 1;
ADCON0bits.CHS1 = 0;
ADCON0bits.CHS2 = 0;
ADCON0bits.CHS3 = 0;
while sampling, just put them in the initialize portion. Also, ADCON2 is not configured. Since you're using only 8-bit, the AD result should be set left justified. You should set the acquisition time and the AD clock as well.

*EDIT:
For your case, ADCON2 should be set 0x11. Since the device is running at 8 MHz, as recommended in the datasheet, the AD clock is 8 Tosc, which is 1:mu:s. Also the minimum acquisition time (sample capacitor charging time) is 2.4:mu:s minimum. It's set to 4 Tad in this case, which is 4:mu:s. And the ADC result is left justified.
 
Last edited:

Oppz.. Sorry man... I forgot to tell you that my AN0 is malfunction, that is the reason I use AN1 instead. Everything works already. The only that is not working is that I can't complement my signal from CCP1.

The code that I write on CCP2 is just having complement duty cycle.
It is just like from 1001 1111 (CCP1) become 1111 1001 (CCP2).

The problem that i'm facing is that I can't make my 1001 1111 to 0000 0110 which is bit by bit complemented or inverted.

Any suggestion?
 
Hi,
Don't really get you
Do you mean, you want the ADC result to be PWMed on CCP2 and the inverted result on CCP1?
If it is, you can use the complement operator, such as
Code:
CCPR2L = n;
CCPR1L = ~n;
 
Last edited:
Hi,
Don't really get you
Do you mean, you want the ADC result to be PWMed on CCP2 and the inverted result on CCP1?

NO..

My ADC Result will generate PWM signal on CCP1 (already achieved this part).
than I want my CCP2 to be an inverted signal of CCP1 (still cannot achieve this part).

In short,
If my CCP1 is 1001 1111, than my CCP2 must be 0110 0000.
 
That's what I meant. The complement operator (~) doesn't work?

Ya... It doesn't work... Just now u said ADCON2 I nv set up? How should I come about setting it?

Edited:
Just saw your previous reply... So I should set ADCON to 0x11. I will give it a try tml.
 
Last edited:
Ya... It doesn't work... Just now u said ADCON2 I nv set up? How should I come about setting it?
Yes, that's the proper setting for ADC. Read the datasheet under ADCON2.
This register is used to select the AD clock source, the acquisition time and the result (left or right justified).

Can I know how to you see the CCP2 result? From the oscilloscope? What's the analog input voltage?
 

Ya.. I see my CCP2 result from the oscilloscope. I only apply a variable analog input 5V as I think the max voltage that i can apply is Vcc+0.3 only right.
 
Yes you're right.
But have you tried other than 5 V? For example, if the analog input is 5 V, then the PWM duty cycle is almost 100 % and the complemented is almost 0 %. It is very difficult to read from the oscilloscope. Try 1.5 V or 2 V and see both the output.
 

Ya... it is about 94% duty cycle. I can see clearly that CCP2 is not inverted of CCP1. The thing that is complement is the duty cycle. meaning CCP1 having 94% duty cycle, than CCP2 is having 6% duty cycle which is not I want.

I want to have bit by bit complement but still thinking don't know why cannot. Maybe I should make portB to be ~CCP1. Wondering don't know will it works.

By the way, Is there a good way to prevent damaging the PIC chips?
 
Oh, you get the correct result, but not the wanted result
You want the waveform to be inverted, but not the duty cycle to be complemented.
You cannot use this: PORTB = ~CCP1 because the result on PORTB is parallel. The easiest way is to use a hardware inverter at the output of CCP1.

Or, is the program running anything else? If not then you can update the duty cycle of CCP1 in the timer 2 ISR. In the main, poll RC2 (CCP1) and invert this pin to any output pin. Or anyone has better solution?
 
Last edited:
Ya... it is about 94% duty cycle. I can see clearly that CCP2 is not inverted of CCP1. The thing that is complement is the duty cycle. meaning CCP1 having 94% duty cycle, than CCP2 is having 6% duty cycle which is not I want.

Finally, you actually describe what you are trying to achieve.

If you have a (on chip) comparator available then use it to invert the output of CCP1. If not then you need additional circuitry.

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