• AVR Freaks

Newbie: controlling duty cycle of PWM set up with MCC

Page: << < ..678910.. > >> Showing page 8 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/10 19:09:24 (permalink)
0
Hi.  I've had guests in town and so haven't been able to spend much time on this over the last week. I did manage to complete the PCB side of things and get some ordered as that was easier and could tolerate interruption. ;)  I'm back now looking at the code side of things again. I followed your suggestion for replacing the MCC generated EUSART_Write code and added the if (RCIF).  Without the latter the program/sim was waiting for keyboard input. Of course at the moment, without the RXDsim stuff inserted it merely skips the EUSART_Read. Rather than using the simulator for this bit it may simply be easier and more flexible to flash a PIC and connect to it via serial - at least in the first instance. 
 
PStechPaul
 
char    RXDsim[] = {'B','1','2','3','4','\r','S',' ','7','6','5','\r', 'X','\r'};
int     RXDptr = -1;

char    EUSART_Read(void) {
//    while(!RCIF);
//    return  RCREG;
    if(RXDptr < sizeof(RXDsim)-1)
        RXDptr++;
    else
        RXDptr = 0;
    return RXDsim[RXDptr];
}

The commented code can be used in place of the MCC generated function. You can do something similar for EUSART_write();

 
Here is my full edited eusart.c file.  I commented out the MCC-generated code for EUSART_Write and inserted your suggested code. The simulator no longer gets stuck when it gets to a printf.
 
I want to double-check that I can simply replace the full EUSART_Read code with the two lines you suggested 
 

while(!RCIF);
return RCREG;

 
and am I correct that this simply says "for as long as there is a keyboard character entry return that entry"? Seems much more straightforward. 
 

 
Section: Included Files
*/
#include "eusart.h"
/**
Section: Macro Declarations
*/
#define EUSART_TX_BUFFER_SIZE 32
#define EUSART_RX_BUFFER_SIZE 32
/**
Section: Global Variables
*/
volatile uint8_t eusartTxHead = 0;
volatile uint8_t eusartTxTail = 0;
volatile uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE];
volatile uint8_t eusartTxBufferRemaining;
volatile uint8_t eusartRxHead = 0;
volatile uint8_t eusartRxTail = 0;
volatile uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE];
volatile uint8_t eusartRxCount;
/**
Section: EUSART APIs
*/
void EUSART_Initialize(void)
{
// disable interrupts before changing states
PIE1bits.RCIE = 0;
EUSART_SetRxInterruptHandler(EUSART_Receive_ISR);
PIE1bits.TXIE = 0;
EUSART_SetTxInterruptHandler(EUSART_Transmit_ISR);
// Set the EUSART module to the options selected in the user interface.
// ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
BAUDCON = 0x08;
// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
RCSTA = 0x90;
// TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
TXSTA = 0x24;
// SPBRGL 16;
SPBRGL = 0x10;
// SPBRGH 0;
SPBRGH = 0x00;

// initializing the driver state
eusartTxHead = 0;
eusartTxTail = 0;
eusartTxBufferRemaining = sizeof(eusartTxBuffer);
eusartRxHead = 0;
eusartRxTail = 0;
eusartRxCount = 0;
// enable receive interrupt
PIE1bits.RCIE = 1;
}
uint8_t EUSART_is_tx_ready(void)
{
return eusartTxBufferRemaining;
}
uint8_t EUSART_is_rx_ready(void)
{
return eusartRxCount;
}
bool EUSART_is_tx_done(void)
{
return TXSTAbits.TRMT;
}
uint8_t EUSART_Read(void)
{
uint8_t readValue = 0;

while(0 == eusartRxCount)
{
}
readValue = eusartRxBuffer[eusartRxTail++];
if(sizeof(eusartRxBuffer) <= eusartRxTail)
{
eusartRxTail = 0;
}
PIE1bits.RCIE = 0;
eusartRxCount--;
PIE1bits.RCIE = 1;
return readValue;
}
void EUSART_Write(uint8_t txData)
{
char* EUSARTbuf[20];

int EUSARTptr = -1;

if(EUSARTptr<18)

EUSARTptr++;

else

EUSARTptr=0;

EUSARTbuf[EUSARTptr] = txData;
}
// while(0 == eusartTxBufferRemaining)
// {
// }
// if(0 == PIE1bits.TXIE)
// {
// TXREG = txData;
// }
// else
// {
// PIE1bits.TXIE = 0;
// eusartTxBuffer[eusartTxHead++] = txData;
// if(sizeof(eusartTxBuffer) <= eusartTxHead)
// {
// eusartTxHead = 0;
// }
// eusartTxBufferRemaining--;
// }
// PIE1bits.TXIE = 1;
//}
char getch(void)
{
return EUSART_Read();
}
void putch(char txData)
{
EUSART_Write(txData);
}
void EUSART_Transmit_ISR(void)
{
// add your EUSART interrupt custom code
if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining)
{
TXREG = eusartTxBuffer[eusartTxTail++];
if(sizeof(eusartTxBuffer) <= eusartTxTail)
{
eusartTxTail = 0;
}
eusartTxBufferRemaining++;
}
else
{
PIE1bits.TXIE = 0;
}
}
void EUSART_Receive_ISR(void)
{

if(1 == RCSTAbits.OERR)
{
// EUSART error - restart
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
// buffer overruns are ignored
eusartRxBuffer[eusartRxHead++] = RCREG;
if(sizeof(eusartRxBuffer) <= eusartRxHead)
{
eusartRxHead = 0;
}
eusartRxCount++;
}
void EUSART_SetTxInterruptHandler(void (* interruptHandler)(void)){
EUSART_TxDefaultInterruptHandler = interruptHandler;
}
void EUSART_SetRxInterruptHandler(void (* interruptHandler)(void)){
EUSART_RxDefaultInterruptHandler = interruptHandler;
}
/**
End of File
*/

 
FYI here is my current Program_Constants code (for now my older stuff is just commented out):
 

//Program key constants
void Program_Constants(void)
{
Send_Programming_String();
char Cmd[]="000000";
//char Cmd_Val[] = "0000";
int i=0;
//int Shift = 2;
//int Extract_Num;
char Input_Char = '£';
while (Input_Char != 'X') { //Process multiple commands until X
while (Input_Char != 'X' && i < 6 && Input_Char != '\r'){ //Read up to 6 characters only and stop early if X or \r.
Input_Char = EUSART_Read();
Cmd[i] = Input_Char;
i++;
}
i=0;
if (Input_Char != 'X') { //Examine and process command. Skip if X was entered.
if ((Cmd[0] == 'B' || Cmd[0] == 'S' || Cmd[0] == 'F') && (Cmd[1] == ' ')){ //Check command for input errors.
// if (Cmd[0] = 'B') {
// Extract_Num = 4;
// Shift = -1;}
// else {
// Extract_Num = 2;
// Shift =0;
// }
// while (Extract_Num >=1){
// Cmd_Val[Extract_Num + Shift] = Cmd[Extract_Num-Shift];
// Extract_Num--;
// }
switch (Cmd[0]) {
case 'B':
NTC_Beta = atoi(Cmd+1);
break;
case 'S':
Cmd[4]=0;
Over_Temp = atoi(Cmd+1);
break;
case 'F':
Cmd[4]=0;
DC50_Limit = atoi(Cmd+1);
break;
}
Send_Programming_String();
}
else {
printf("INPUT ERROR\n\n");
Send_Programming_String();
}
}

}

}

 
I really appreciate the assistance. 
 
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/11 14:23:54 (permalink)
0
Out of desperation, exasperation and pure frustration I created a new project to simply test reading and writing to serial.  I used MCC to generate the EUSART files. I did not make the mistake of enabling interrupts. The MCC-generated code is simpler. 
 

 
#include "eusart.h"
 

/**
Section: EUSART APIs
*/
void EUSART_Initialize(void)
{
// Set the EUSART module to the options selected in the user interface.
 
// ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
BAUDCON = 0x08;
 
// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
RCSTA = 0x90;
 
// TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
TXSTA = 0x24;
 
// SPBRGL 16;
SPBRGL = 0x10;
 
// SPBRGH 0;
SPBRGH = 0x00;
 

}
 
bool EUSART_is_tx_ready(void)
{
return (bool)(PIR1bits.TXIF && TXSTAbits.TXEN);
}
 
bool EUSART_is_rx_ready(void)
{
return PIR1bits.RCIF;
}
 
bool EUSART_is_tx_done(void)
{
return TXSTAbits.TRMT;
}
 
uint8_t EUSART_Read(void)
{
while(!PIR1bits.RCIF)
{
}
 

if(1 == RCSTAbits.OERR)
{
// EUSART error - restart
 
RCSTAbits.CREN = 0;
RCSTAbits.CREN = 1;
}
 
return RCREG;
}
 
void EUSART_Write(uint8_t txData)
{
while(0 == PIR1bits.TXIF)
{
}
 
TXREG = txData; // Write the data byte to the USART.
}
 
 
 
 
 
/**
End of File
*/

 
I then created the simplest of main.c files:
 

 
#include "mcc_generated_files/mcc.h"
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/*
Main application
*/
 

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

void main(void)
{
// initialize the device
SYSTEM_Initialize();
 
// When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
// Use the following macros to:
 
// Enable the Global Interrupts
//INTERRUPT_GlobalInterruptEnable();
 
// Enable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptEnable();
 
// Disable the Global Interrupts
//INTERRUPT_GlobalInterruptDisable();
 
// Disable the Peripheral Interrupts
//INTERRUPT_PeripheralInterruptDisable();

char Keyboard_Input;

Send_String("Hello World\n");
Send_String("How's it going?\n");

while (1) {
Keyboard_Input = EUSART_Read(); //Monitor for keyboard instructions
switch(Keyboard_Input){
case 'T': Send_String(Keyboard_Input);
break;
case 'P': Send_String("Yes P\n");
break;
default: Send_String("Try Again\n");
}
}

}
 
/* End of File
*/

 
I can Make and Program the device and with a concoction of wires power it from my USB-Serial adaptor cable. I can now write to serial which is massive progress. Printf (rather than the Send_String function) does not work. EUSART_Read seems to work fine as well. 
 
I'm still unsure, however, given that printf doesn't work how I can print the values of variables. 
post edited by SGK - 2018/06/11 14:25:09
jack@kksound
code tags!
  • Total Posts : 3227
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/11 15:19:47 (permalink)
+1 (1)
For printf() to send characters (data) through your UART you must follow the instructions in the XC8 Compiler User Guide Section 3.5.8 How Do I Use Printf to Send Text to a Peripheral?
 
 
PStechPaul
Super Member
  • Total Posts : 2730
  • 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/11 15:56:17 (permalink)
0
Welcome back! I think I see some problems with your previous code, but I'm not going to pick my way through it, especially since you have gotten the basic communication working in your new project. If printf() is not working on the serial port you might not have redefined putch() as you did in the previous project.
 
Perhaps you should post your whole project so that I or others might be able to build it and run it on the simulator. You are doing the right thing to code simpler projects to get experience with each peripheral, before proceeding to add all the functionality you need.

 
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/11 16:30:34 (permalink)
0
Thanks Paul and Jack.  The thing is I didn't redefine putch() in the previous (full) project.  MCC added the following into eusart.c automatically
 

char getch(void)
{
return EUSART_Read();
}
void putch(char txData)
{
EUSART_Write(txData);
}

 
This time it didn't. Why? I have no idea.
 
On the face of it I think this is all that is required to reconfigure pitch for printf and EUSART.  If I am wrong please let me know.
 
I will add the code to the eusart.c file (see post above) and persevere a little before posting the full project (as I did a few posts back). Once I get basic input / output communication working in the simple file I'm going to start a fresh project, configuring the PIC from scratch (not making the mistake of enabling interrupts) and copy in my main.c code.  The biggest frustration has been making progress with the simulator but still seeing absolutely nothing when I program the PIC and connect to it directly. At least I'm getting something back now. I'll be very happy to actually get to the point of seeing whether my core program code does what it is meant to. 
mbrowning
USNA79
  • Total Posts : 1775
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: online
Re: Newbie: controlling duty cycle of PWM set up with MCC 2018/06/11 19:40:43 (permalink)
0
In MCC UART setup there’s a checkbox to redirect printf to the UART. You must have neglected to click it
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/12 05:34:14 (permalink)
0
Ah "Redirect STDIO to USART". Thanks!
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/12 14:25:33 (permalink)
0
Phew!  It's unbelievable what a pain it created when I enabled interrupts in my first version. I started a new project, set up the PIC as before - without interrupts! - copied in the last version of my main code and at least I now have a 'working' version with what appears to be good serial communication. I've already fixed a few things. (For example, I realised that I needed to write back to serial the characters that were being typed. And of course things are case sensitive.) 
 
I've got a couple of things to work on but two things are puzzling me at the moment. They may be related. The keyboard <Enter / Return> key seems to be interpreted as two character entries (I assume a carriage return and new line). Is there an easy way to avoid this? It means if someone hits <enter> this code prints the welcome twice:
 

if (RCIF){
Keyboard_Input=EUSART_Read(); //Monitor for keyboard instructions
switch(Keyboard_Input){
case 't':
Send_Temps_DC(Temp_NTC1, Temp_NTC2, Temp_NTC3, Temp_NTC4, Fan1_DC, Fan2_DC);
break;
case 'p':
Program_Constants();
break;
default:
Send_Welcome();
}
}

 
That's not a biggie.  But in my Program_Constants function hitting <enter> creates a perpetual loop of running Send_Programming_String. So, for example, typing "s 75<enter>" updates the Over_Temp constant to 75 but then drops into a perpetual loop. 
 

//Program key constants
void Program_Constants(void)
{
Send_Programming_String();
char Cmd[]="000000";
//char Cmd_Val[] = "0000";
int i=0;
//int Shift = 2;
//int Extract_Num;
char Input_Char = '£';
while (Input_Char != 'x') { //Process multiple commands until X
while (Input_Char != 'x' && i < 6 && Input_Char != '\r'){ //Read up to 6 characters only and stop early if X or \r.
Input_Char = EUSART_Read();
EUSART_Write(Input_Char);
Cmd[i] = Input_Char;
i++;
}
i=0;
printf("\n\n");
if (Input_Char != 'x') { //Examine and process command. Skip if X was entered.
if ((Cmd[0] == 'b' || Cmd[0] == 's' || Cmd[0] == 'f') && (Cmd[1] == ' ')){ //Check command for input errors.
// if (Cmd[0] = 'B') {
// Extract_Num = 4;
// Shift = -1;}
// else {
// Extract_Num = 2;
// Shift =0;
// }
// while (Extract_Num >=1){
// Cmd_Val[Extract_Num + Shift] = Cmd[Extract_Num-Shift];
// Extract_Num--;
// }
switch (Cmd[0]) {
case 'b':
NTC_Beta = atoi(Cmd+1);
break;
case 's':
Cmd[4]=0;
Over_Temp = atoi(Cmd+1);
break;
case 'f':
Cmd[4]=0;
DC50_Limit = atoi(Cmd+1);
break;
}
Send_Programming_String();
}
else {
printf("INPUT ERROR\n\n");
Send_Programming_String();
}
}
else Send_Welcome();

}

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/12 15:47:21 (permalink)
0
SGK
...
The keyboard <Enter / Return> key seems to be interpreted as two character entries (I assume a carriage return and new line). Is there an easy way to avoid this?

What software are you running on the PC?
If it is a terminal program, it should have a setting somewhere to control if Enter sends just CR, or CR+LF.
If you can't stop it, get your PIC to ignore LF characters.
 
 

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/12 18:22:04 (permalink)
0
Got it.  Thx.
 
I also see why it loops. I need to reset Input_Char (there's no further read if Input_Char = '\r' and so it keeps skipping down to Send_Programming_String).
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/12 20:42:42 (permalink)
0
I've made a decent amount of progress.  At least I think the EUSART stuff is sorted. I'm posting the full project package. I'm encountering a rather peculiar issue.  My Set_Duty_Cycle code (extract copied below) seems to work fine with the initial global constants. I can press t and even though there isn't any NTC or other circuitry connected to the ADC input pins it works almost like a random number generator for temps and I can cross check duty cycles against those. However, if I change either of the two EEPROM global constants related to this function (Over_Temp or DC50_Limit) I get incorrect values for duty cycle (and presumably CCPR_Val). I have these same formulae working an Excel spreadsheet and they're not particularly complex so I am puzzled. I will need to try to use the simulator tomorrow to try to narrow down what is going on. 
 
CCPR_Val should only be from 9 to 19 (and duty cycle from 50 to 100) and be at 19 at Over_Temp -10. As an example of this puzzling behaviour, with DC50_Limit left at 30 but Over_Temp changed to 65, a temp reading of 53.6 produces a duty cycle of 110 (as displayed by pressing t)!
 

//Set CCPR value for duty cycle and return Duty Cycle
int Set_Duty_Cycle(int Fan_Num, float Fan_Temp)
{
int CCPR_Val = 9;
int Duty_Cycle = 50;
if (Fan_Temp > DC50_Limit){
if (Fan_Temp >= (Over_Temp - 10)) {
CCPR_Val = 19;
Duty_Cycle = 100;
}
else {
float Temp_Increment = (Over_Temp - 10 - DC50_Limit) / 10;
float Adj_Temp = (Fan_Temp - DC50_Limit)/Temp_Increment;
int Round_Adj_Temp = round(Adj_Temp);
float Residual = (Adj_Temp - Round_Adj_Temp);
if (Residual >= 0) 
CCPR_Val = (Round_Adj_Temp + 10); 
else 
CCPR_Val = (Round_Adj_Temp + 9);
Duty_Cycle = (CCPR_Val + 1) * 5;
}

switch (Fan_Num){ 
case '1': PWM4_LoadDutyValue(CCPR_Val);
break;
case '2': PWM3_LoadDutyValue(CCPR_Val);
break;
}

return (Duty_Cycle);
}

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/12 20:43:50 (permalink)
0
Adding project file
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/14 05:19:43 (permalink)
0
For the code posted in #150, why - when Over_Temp is set to 65 and DC50_Limit set to 32 - is 
 
float Temp_Increment = (Over_Temp - 10 - DC50_Limit) / 10
 
computed as 2.0 rather than 2.3?
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/14 05:36:22 (permalink)
0
Because it doesn't get converted to a float until AFTER all the calculations are done.
Try changing that to:
float Temp_Increment = (Over_Temp - 10 - DC50_Limit) / 10.0

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/14 06:45:59 (permalink)
0
Ok that fixes it.  Thx. Gosh, another land mine. 
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/14 09:33:37 (permalink)
0
From my simulator testing firing pins it seems my code for CASE / NO_CASE and Switch_Cleared evaluation works correctly. (The idea is that the machine should never start from a condition where something is sitting in the coil and blocking the IR switch. Triggering of Timer_Start is done dependent on whether anything is in the coil and blocking the IR switch.) 
 
I run into a problem when I simulate an overheating condition. It's not clear to me whether this is an actual problem or just a simulator warning. When the simulation gets to the for statement after OvrTmpLED_Toggle() in the code copied below I get the following message in the simulator output window.
 
No source code lines were found at current PC 0xff6. Open program memory view to see instruction code disassembly.
User program running
User program stopped

 
The idea is that the program keeps cycling to read the temperatures but does nothing other than flash the Over Temp LED until things cool below the Over_Temp threshold. 
 
Not_Over_Heating is defined by:
 
#define Not_Over_Heating ((High_Temp_Fan1 < Over_Temp) && (High_Temp_Fan2 < Over_Temp))
 
 

if (Not_Over_Heating) { //Maintain normal operation of optical switch and timer management
OvrTmpLED_SetLow(); //Make sure Over-Temp LED is off
if (CASE && (Switch_Cleared=='Y')) {
for (int countDelay=0; countDelay <200; countDelay ++)__delay_ms(5); //Delay 1s for case to settle
if (CASE) {
TimerStart_SetHigh();
for (unsigned char countDelay=0; countDelay <30; countDelay ++)__delay_ms(5);
TimerStart_SetLow();
}
}
else {
if (NO_CASE) Switch_Cleared='Y';
}
}
else { //Overheating. Blink LED. Operation can't commence until no longer overheating
OvrTmpLED_Toggle();
for (int countdelay=0; countdelay<10; countdelay++)__delay_ms(5);
}

 
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/17 13:24:13 (permalink)
0
No takers on that one?
 
@PStechPaul I'm not sure if you have had the time or inclination to load the project and do any testing.  I'm wondering how to test the temperature reading. Is there a way to simulate a voltage at a pin?
PStechPaul
Super Member
  • Total Posts : 2730
  • 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/17 16:51:37 (permalink)
0
I've been busy with various projects, including outdoor yard and house maintenance, and renovating some old electronic equipment that I built 40-50 years ago.
 

 

 

 
But, also, I think it's time for you to proceed more on your own, learning to do effective problem solving and debugging, using the tools you have. The MPLABX simulator allows you to simulate a voltage on a pin, or I think it has a legacy mode where you can use register injection from a file to update the value of ADRESH or ADRESL. There should be tutorials and forum threads describing the process.

 
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/18 07:52:51 (permalink)
0
Oh I didn't expect you to debug my project. I was merely enquiring given your suggestion that I post the project file to allow people to debug if they had the inclination and time.  I certainly understand you have better things to do. :)
 
I'm having trouble getting the register injection to work despite having looked at several tutorials. I have created a text file with one value in it, merely "512", to test.  I have a 10 bit DAC with a max value of 1023 and want, in this first instance, to test 2.5V.  My register injection line the following 
 
Label: Temp
Reg/Var: ADRESH (for some reason these are all duplicated i.e. there are two ADRESH in the drop-down list)
Trigger: Demand
PC value: 0x0
Width: 4
Data filename: full path to text file
Wrap: yes
Format: Dec
 
Apply Synchronous Stimulus is active. 
 
Yet I never see any changes to the value produced by read(NTC).  I also tried using the PC= trigger and selecting read(NTC) in the PC value drop down list but this didn't work either. It doesn't matter whether I attempt to update the value of ADRESH, ADRESL or both. So I'm rather puzzled as to how to get this register injection stuff working. 
PStechPaul
Super Member
  • Total Posts : 2730
  • 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/18 11:31:50 (permalink)
0
With only one value, ADRESH or ADRESL will only be updated with that, and won't change. You need to have at least two different values. Also, be sure to select decimal or hex depending on your preference. I have also seen multiple instances of ADRESH in the drop-down control. That seems to be a bug. I think ADRESH also updates ADRESL in the simulator.
 
It is also possible that you need to set the ADC simulator to legacy mode for register injection to work. I think the default for MPLABX uses voltage at the pin for simulation. I have mostly used 8.92 for register injection of the ADC.
 
If you can't get register injection to work, you could put a breakpoint at the place in the code that takes the reading, and manually update the value.

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