Quote Originally Posted by vikash23 View Post
I initially set TRISB bits (12, 13, 14 and 15) to '0' and it didnt work so I removed it completely. Later when I got your response about looking at the section 14.12 then again I want to try it. as you mentioned 0 is output and 1 is input. TRIS is used to set output. LAT is to set high or low. I set LAT as 0 which is low. So I didnt bother about setting TRIS as input or output as any how the pin is low.
Firstly, don't guess these things. The data sheet states very clearly what each of the PORT, LAT and TRIS registers do (as well as the various analog/digital registers where appropriate). TRIS (short for tri-state) selects whether the pin is used for input (tri-state of the output drivers) or output (the output drivers control the pin state). PORT is used to read the status of the pin (except in analog mode when it will always read 0). The LAT register is used to set the value of the pin when it is in output mode.
Because the power-on defaults are for all TRIS registers to be input, unless you set a pin for output then the LAT register setting is ignored.
Quote Originally Posted by vikash23 View Post
If you see my code I started with the following initialisation
void initPWM(void){

}
That is a function definition and the call to that function in the 'main()' function is commented out. Further the function definition is empty so, even if you did call it, nothing would happen.
However, on each call to he (badly named) 'initPWM1()' and 'initPWM2()' funciotns, you go through the complete initialisation sequence.
Quote Originally Posted by vikash23 View Post
Initially I started with "xc.h" and it throwed an error while compiling saying it cant find the path. hence I added the complete path.
Again, if you get an error while doing something that the documentation tells you to do, then you have some other problem that needs to be sorted out.
However, you do have some code modules that (apparently) compile successfully with just the ''. Are you compiling the ones that give you an error differently to those that work? If so, that may well be the source of your problem.
Quote Originally Posted by vikash23 View Post
All I am trying to do here is to disable any one signal of the complementary PWM . wither high or low. But yes I am trying to turn off all outputs because just to see if I can do it. Sorry I am totally vexed of trying this again and again.
Please show us the code that exhibits your problem. We also get 'vexed' trying to help you when we are shown the wrong code.
So - when you leave out that code to disable the outputs do you get the waveforms from the PWM?
And when you put in the code to disable one of the pins, does it stop and the other one continue?
Quote Originally Posted by vikash23 View Post
Initially I started with learning PIC24HJ and I find dspic33 is bit difficult and advanced in terms of learning ADC and PWM. So I used PIC24F as a base and developing from it.
So what MCU have you defined in the IDE and therefore are compiling this code for? (And please provide the full device name.)
This information is critical as there can be subtle (and not so subtle) differences families of devices (and even within a family).
Quote Originally Posted by vikash23 View Post
Now I have 3 ADC's working, UART working, PWM working and when I vary the potentiometer I can see my PWM duty cycle varies.
So all of the code you have shown us (above) is actually working. Great news but I think that is the first time you have told us this.
Quote Originally Posted by vikash23 View Post
the following first three line is always like the below.

"Potentiometer Value = 0.00
Onboard Temperature Value = -20.51
Sensor Value = 0"

when do I get -20.5 when my actual,onbaord temp is 21.85 ?

am I doing anything wrong with initialising ADC ?
And now we get to a new problem!
Lets look at your code to calculate the onboard temperature value
Code:
TempVal = readTempADC(); //Read onboard Temp value

//Onboard temperature measurement (MCP9701A)
TempADC = (TempVal * 4.8875)/1000; //10-bit to voltage
Temperature = (TempADC - 0.4)/0.0195; //to calculate the temperature
printf("Onboard Temperature Value = %.2f", Temperature);
Firstly I would try to set a breakpoint after you read 'readTempADC()' and see what value you ACTUALLY have in 'TempVal'.
But working backwards through your calcuations, if 'Temperature' is -20.5, then 'TempADC' = ('Temperature' * 0.0195) + 0.4 and substituting in we get (-20.5*0.0195)-0.4 = 0.4-0.4 = 0
So 'tempADC' starts off at 0.
That also means that 'TempVal' will be 0.
Therefore you must have read a value of 0 from 'readTempADC()'!
[Side issue: I note that you divide by 0.0195: floating pint division can be quite an 'expensive' operation on these devices and so it is better to perform floating point multiplications where possible (and even better if you can keep thing as integer so the hardware multiply/device hardware can be used). Therefore it is better to multiply by 51.282 than divide by 0.0195.
Taking this further, if you put everything into a single equation from 'TempVal' to 'Temperature' you get "Temperature = ((tempVal*4.8875/1000) - 0.4) * 51.282". Expanding out you get
"Temperature = TempVal * 4.8875 * 51.282/1000 - 0.4 * 51.282 = TempVal * 0.25 - 20.5128. Now as TempVal is an integer, we can re-write this as "TempVal/4 - 20.5128" which means that we use the integer division hardware for the division, once integer to floating point conversion and then a floating point subtraction. (If you want to you can go further and note that division by 4 is shift-right by 2 bits for '(tempVal>>2)' is the same thing but even faster. I'd not recommend going that far in this case - you already have a 1 second delay in the main loop so you can't be worried bout the time the calculation takes.)]
As this only happens the first time, you need to look at how you initialise the ADC for that channel and what is different the 2nd time around. Again the debugger will help you greatly here.
By the way, this is classic debugging and fault-finding.
Susan