Yeah I had it set to 8 bit resolution for that test. It is driving power electronics for a brushed DC motor. When the PID loop hit 256 it shut off!
max = 1 << analog_write_res;
if (val <= 0) {
digitalWrite(pin, LOW);
pinMode(pin, OUTPUT); // TODO: implement OUTPUT_LOW
return;
} else if (val >= max) {
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT); // TODO: implement OUTPUT_HIGH
return;
}
void analogWrite(uint8_t pin, int val)
{
const struct pwm_pin_info_struct *info;
if (pin >= CORE_NUM_DIGITAL) return;
//printf("analogWrite, pin %d, val %d\n", pin, val);
info = pwm_pin_info + pin;
if (info->type == 1) {
// FlexPWM pin
IMXRT_FLEXPWM_t *flexpwm;
switch ((info->module >> 4) & 3) {
case 0: flexpwm = &IMXRT_FLEXPWM1; break;
case 1: flexpwm = &IMXRT_FLEXPWM2; break;
case 2: flexpwm = &IMXRT_FLEXPWM3; break;
default: flexpwm = &IMXRT_FLEXPWM4;
}
flexpwmWrite(flexpwm, info->module & 0x03, info->channel, val);
} else if (info->type == 2) {
// QuadTimer pin
IMXRT_TMR_t *qtimer;
switch ((info->module >> 4) & 3) {
case 0: qtimer = &IMXRT_TMR1; break;
case 1: qtimer = &IMXRT_TMR2; break;
case 2: qtimer = &IMXRT_TMR3; break;
default: qtimer = &IMXRT_TMR4;
}
quadtimerWrite(qtimer, info->module & 0x03, val);
} else {
return;
}
*(portConfigRegister(pin)) = info->muxval;
// TODO: pad config register
}
const int ledPin = 13;
const int pwm_freq = 3000; //pwm hz
const int pwm_res = 8; //pwm resolution, also pid output bits
int PWM = 0;
int PWM_max = 0;
void setup() {
Serial.begin(9600);
analogWriteFrequency(ledPin, pwm_freq);
analogWriteResolution(pwm_res);
PWM_max = 1 << pwm_res;
//--PWM_max;
}
void loop() {
Serial.print("PWM: ");
Serial.println(PWM);
analogWrite(ledPin, PWM);
++PWM;
if (PWM > PWM_max) {
--PWM;
}
delay(20);
}
So it went up properly from 0-255 then at 256 shut off?
const int ledPin = 13;
const int pwm_freq = 3000; //pwm hz
const int pwm_res = 12; //pwm resolution, also pid output bits
int PWM = (1 << pwm_res) - 30;
int PWM_max = 0;
void setup() {
Serial.begin(9600);
analogWriteFrequency(ledPin, pwm_freq);
analogWriteResolution(pwm_res);
analogReadResolution(pwm_res);
PWM_max = 1 << pwm_res;
//--PWM_max;
}
int aList[] = { 1,5,9,13,15,19,23,25,255 };
void loop() {
Serial.print("PWM: ");
Serial.print(PWM);
int ii=0;
while( aList[ii] < 200 ) {
analogWrite(aList[ii], PWM);
ii++;
}
++PWM;
if (PWM > PWM_max+2) {
PWM-=4;
}
delay(2);
Serial.print( "\tanalog Read: ");
Serial.println( analogRead( 14 ) );
delay(100);
}
Thanks, nice sketch - some mods to work with edits combining code in p#8 above - I wired pin #14 to LED #13:
Code:const int ledPin = 13; const int pwm_freq = 3000; //pwm hz const int pwm_res = 12; //pwm resolution, also pid output bits int PWM = (1 << pwm_res) - 30; int PWM_max = 0; void setup() { Serial.begin(9600); analogWriteFrequency(ledPin, pwm_freq); analogWriteResolution(pwm_res); analogReadResolution(pwm_res); PWM_max = 1 << pwm_res; //--PWM_max; } int aList[] = { 1,5,9,13,15,19,23,25,255 }; void loop() { Serial.print("PWM: "); Serial.print(PWM); int ii=0; while( aList[ii] < 200 ) { analogWrite(aList[ii], PWM); ii++; } ++PWM; if (PWM > PWM_max+2) { PWM-=4; } delay(2); Serial.print( "\tanalog Read: "); Serial.println( analogRead( 14 ) ); delay(100); }
It seems to work for pin #13. As you can see in p#8 2nd code there are two types of 'Analog PWM pins' - without looking I'm not sure where the breaks are? I tested the aList above {1,5,9,13,15,19,23,25} against pin #14 to work.
Not sure if you want to swap out your files: {Arduino}\hardware\teensy\avr\cores\teensy4\pwm.c with this file and test?
I'll put in a Pull Request then for review/inclusion.
The teensy4 code is different.
I notice this line in cores/teensy4/pwm.c :
if (cval > modulo) cval = modulo; // TODO: is this check correct?
Without studying this too closely, I'm thinking this might be relevant.
I have no idea what that is about … I saw the Teensy3 code for overt set to HIGH or LOW is missing … attachment p#11 should work on pin 12 or any other - goes digital HIGH > res or LOW <= ZERO. Then should return to analog function between those values.
Yup! I confirmed it works for both pin 13 and pin 12!
Thanks defragster!