• AVR Freaks

Newbie: controlling duty cycle of PWM set up with MCC

Page: << < ..678910.. > >> Showing page 9 of 14
Author
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/19 06:55:17 (permalink)
0
The idea was to test one change from the initialised values i.e. see if I could stimulate a change from 25c and go from there. There is a box in the project settings which needs to be checked "use MPLAB 8 Style Stimulus/SCL for ADCxBUF0". I'll figure it out or, as you say, manually add in the value.  Thanks for all the help!
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 13:07:14 (permalink)
0
Hi guys.  Can you offer me some guidance on how to debug my programming in-circuit?  I have a board up and running and can interface via serial communication.  My menus seem to work fine. I have an NTC wired into one of the 4 NTC connection points.  I measure 3.2V at pin 16 which is associated with NTC1 (versus 4.9V, very slightly below the supply voltage, at the pins associated with the other 3 unpopulated NTC). Yet, the temperature of NTC1 is still reported as its initialised value of 25C. I've been through everything with the simulator and it looks ok.  Presumably either it's not getting to the ADC conversion or it's not working properly.  After an afternoon looking at this I'm rather stuck. 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 15:22:51 (permalink)
0
I should add that I have managed to identify and fix a number of issues debugging via having the program print various info.  I'm stuck on the temperature part though.
 
If I print ADC_value in float format I get negative numbers:
 
ADCvalue -17126891520000000000000000000000000000.000000
ADCvalue -35659612160000000000000000000000000000.000000
ADCvalue -35659612160000000000000000000000000000.000000
ADCvalue -35659612160000000000000000000000000000.000000
 
The first number is for NTC1 which has an NTC populated. (The others are not.)
 

float readNTC(adc_channel_t channel)
{
int ADC_value = ADC_GetConversion(channel);
printf("ADCvalue %f\n", ADC_value);
float rNTC = NTC_Balance_Resistor * ((Max_ADC / ADC_value)-1);
float Temp = ((NTC_Beta * Room_TempK) / (NTC_Beta + (Room_TempK * log(rNTC / Res_Room_Temp)))) - 273.15;
return Temp;
}

 
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 15:27:33 (permalink)
0
You are taking the value from ADC_GetConversion(), saving it into a signed integer variable, then trying to print it as a float.
Why?
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 15:27:49 (permalink)
0
As integers
 
ADCvalue -21504
ADCvalue -64
ADCvalue -64
ADCvalue -64
 
I'm expecting numbers between 0 and 1023 (10 bit ADC).
 
EDIT: Just saw your post as I sent this.
 
post edited by SGK - 2018/06/26 16:17:11
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 16:52:47 (permalink)
0
And despite different ADC values Temp is calculated as equal to 25 each time.  It did not do this in simulation.
 
ADCvalue 43136
rNTC -4588.500000
Temp = 25.0
ADCvalue 65472
rNTC -4626.625008
Temp = 25.0
ADCvalue 65472
rNTC -4626.625008
Temp = 25.0
ADCvalue 65472
rNTC -4626.625008
Temp = 25.0
 

//Calculate temp at an NTC in degrees Celsius
float readNTC(adc_channel_t channel)
{
uint16_t ADC_value = ADC_GetConversion(channel);
printf("ADCvalue %u\n", ADC_value);
float rNTC = NTC_Balance_Resistor * ((Max_ADC / ADC_value)-1);
printf("rNTC %f\n", rNTC);
float Temp = ((NTC_Beta * Room_TempK) / (NTC_Beta + (Room_TempK * log(rNTC / Res_Room_Temp)))) - 273.15;
printf("Temp = %.1f\n", Temp);
return Temp;
}

 

#define NTC_Balance_Resistor 4700 //Balance resistor used in NTC voltage divider
#define Max_ADC 1023.0 //10 bit DAC
#define Room_TempK 298.15 //25C in Kelvin x 100
#define Res_Room_Temp 10000 //NTC resistance at 25C from data sheet
#define Not_Over_Heating ((High_Temp_Fan1 < Over_Temp) && (High_Temp_Fan2 < Over_Temp))
eeprom int NTC_Beta = 3435;

PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 17:47:46 (permalink)
+1 (1)
If you are using the same project files as previously posted, there is a problem with your setup of the ADC as left-justified:
    // ADFM left; ADNREF VSS; ADPREF VDD; ADCS FOSC/8; 
    ADCON1 = 0x10;
 
adc_result_t ADC_GetConversion(adc_channel_t channel)
{
    // select the A/D channel
    ADCON0bits.CHS = channel;    
    
    // Turn on the ADC module
    ADCON0bits.ADON = 1;

    // Acquisition time delay
    __delay_us(ACQ_US_DELAY);

    // Start the conversion
    ADCON0bits.GO_nDONE = 1;

    // Wait for the conversion to finish
    while (ADCON0bits.GO_nDONE)
    {
    }

    // Conversion finished, return the result
    return ((adc_result_t)((ADRESH << 8) + ADRESL));
}

 
For an ADC value of 511, ADRESH = 0b01111111 and ADRESL = 0b11000000. So after shifting, the returned value will be 0b0111111111000000 or 0x7fa0 or 32672. Values of 512 to 1023 counts will be interpreted as negative integers.
 

 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/26 18:59:45 (permalink)
0
Yes I left it at a default left alignment.  I will change it to right. Many thanks again.
 
The good news is that, after some minor adjustments, my case detection seems to work correctly.
 
The bad news is I made a very stupid mistake with my circuit with respect to the fan Mosfet switches and unfortunately that will require new boards.  
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/02 06:45:13 (permalink)
0
I have just a few instances of printf which cost me dearly in terms of code memory consumption.
 

void Send_Temps_DC(float Temp1, float Temp2, float Temp3, float Temp4, int DC1, int DC2)
{
Send_String("Temperatures in degrees Celsius\n");
printf("NTC1 %.1f\n", Temp1);
printf("NTC2 %.1f\n", Temp2);
printf("NTC3 %.1f\n", Temp3);
printf("NTC4 %.1f\n\n", Temp4);
Send_String("Duty cycles in per cent.\n");
printf("Fan 1 %d\n", DC1);
printf("Fan 2 %d\n\n\n\n", DC2);
}
//Send Programming String
void Send_Programming_String(void)
{
Send_String("Programming mode.\n\n");
printf("b for NTC beta = %d\n", NTC_Beta);
printf("s for thermal protection limit degC = %d\n", Over_Temp);
printf("f for temp limit for 50 per cent duty cycle fan operation = %d\n", DC50_Limit);

 
Is there a smarter way to write these values to serial that will not require invoking printf?
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/02 13:10:42 (permalink)
+1 (1)
Floating point in general will "cost you dearly".
If you just did all your calculations using integers scaled by 10, you would save heaps.
e.g. 23.5 stored as 235.
Then just have a custom output function to print the last digit separately, and avoid all floating point code.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/02 13:39:56 (permalink)
0
It is unlikely that 0.1 degree resolution is required for an annealer, or a PWM fan control. Integer math and print formatting is much more efficient. You can also do qyb suggests, and write your own function to produce a string with the values in a specific format.
 
Another "trick" is to re-use a string constant, something like this:
void printTemp(int num, int temp) {
  char    buf[4];
  char     str[20] = "NTC";
  strcat( str, itoa(buf,num,10) );
  strcat( str, " ");
  strcat( str, itoa(buf,temp,10) );
  strcat( str, "\n" );
  printstr( str ); }

 
You can also replace the printf() function with a simple version for strings only:
void printstr( char* str ){
while( *str ) {
  putc( str );
  *str++; }
}

 
I haven't checked the syntax for these functions, so some minor changes might be needed. But you can see the idea...
I corrected the syntax and made other changes to get this to work.
post edited by PStechPaul - 2018/07/03 23:27:11

 
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 19:39:06 (permalink)
0
Ugh the forum would not let me post for quite some time due to ‘Access Denied’.

Ok let me think about the integer math side of things. I agree that float resolution isn't necessary but I am somewhat loath to mess with it.

I guess I already have a 'simple version' for writing strings only.


//Send String
void Send_String(const char *x)
{
while (*x)
{
EUSART_Write(*x++);
}
}


The challenge occurs when I need to print a variable's value. I need to look more closely at your 'trick'.

I see avoiding the bloated printf is a common problem. In my case one use of printf almost doubles the code memory consumption. I'm wondering about a 'tiny' printf implementation such as this one:


SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 19:40:18 (permalink)
0
Stupid 'Access Denied' issue.  I think it is the link to Github
 
https://github.com/mpaland/printf
 
Nope.  Ugh!!!!
 
post edited by SGK - 2018/07/03 19:41:28
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 19:42:09 (permalink)
0
(I think I could even turn off the long long implementation.) I'm trying to understand the requisite 'low level output function' _putchar versus what's currently in my eusart.c file


void EUSART_Write(uint8_t txData)
{
while(0 == PIR1bits.TXIF)
{
}
TXREG = txData; // Write the data byte to the USART.
}

void putch(char txData)
{
EUSART_Write(txData);
}

SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 19:43:47 (permalink)
0
Ugh I can't post the final bit of my message.  This forum software is unbelievable.
 
Is it as simple as creating:
void _putchxr (char character)
{
EUSART_Write(character);
}
 




post edited by SGK - 2018/07/03 19:50:29
SGK
Senior Member
  • Total Posts : 158
  • Reward points : 0
  • Joined: 2018/05/14 13:55:08
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 19:51:43 (permalink)
0
For some reason the forum won't let me replace the x above with an a....
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 20:10:42 (permalink)
0
SGK
For some reason the forum won't let me replace the x above with an a....

Old news...
Expressions which trigger the firewall
Reported to Microchip in 2014, and we're still waiting for it to be fixed.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 23:38:02 (permalink)
0
I corrected my code above so it works.
 
In 2014 Cindy (the admin) said fixing the forum is their #1 priority.

 
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 23:46:43 (permalink)
0
PStechPaul
In 2014 Cindy (the admin) said fixing the forum is their #1 priority.

I think the complicating factor is that we now know that you don't get any of the firewall problems if you access the server via the Microchip corporate network.
That's why they think it's no big thing.
Meanwhile, all us poor customers have more and more difficulty just accessing the board.

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
PStechPaul
Super Member
  • Total Posts : 2558
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/07/03 23:56:42 (permalink)
0
You would think somebody at Microchip would try accessing the forum from a public access point like Starbucks or McDonalds or a library, to see what the problem is. It should not be difficult to switch to an alternate server for the forum. Or they could put a link to http://picforum.ric323.com/ as an alternate forum, especially for newbies who come here as a first attempt to get help.

 
Page: << < ..678910.. > >> Showing page 9 of 14
Jump to:
© 2020 APG vNext Commercial Version 4.5