lost in PWM space

Im having trouble trying to figure out what I thought would be simple--generate a 60hz square wave
with variable duty cycle..
what I have seems to 'hang' when the pwm value is around 78..I cant see why it would do that..
i am using a common minipro 5v 16m 328 on pin3 output
here is my code--

 //Initialize Timer2
  TCCR2A = 0;
  TCCR2B = 0;
  TCNT2 = 0;

  // Set OC2B for Compare Match (digital pin3)
  pinMode(3, OUTPUT);
  //bitSet(TCCR2A, COM2B0);//not valid when used by itself
  bitSet(TCCR2A, COM2B1);//clear OC2B on up count compare match
  //set OC2B on down count compare match

  // Set mode 5-- Phase correct PWM to OCR2A counts up and down
  bitSet(TCCR2A, WGM20);
  bitSet(TCCR2B, WGM22);

  // Set up /1024 prescale
  bitSet(TCCR2B, CS20);
  bitSet(TCCR2B, CS21);
  bitSet(TCCR2B, CS22);

  //OCR2A = 32;      // Sets freq 50hz?    OC2A PIN 11
  OCR2A = 156; //Sets freq 50Hz
  //Timer 2 counts up and down for 312 total counts
  //312 x 1024 x.0625 = 19.968ms 50.08 Hz

  // enable timer compare interrupt
  //bitSet(TIMSK2, OCIE2A);

  OCR2B = 78;//50% duty cycle valid values 1-155

maybe someone has better code for this or can see what I have wrong--thanks for any help--
jack

There are no obvious errors in what you have posted, but of course that is not a complete Arduino program.

Please post ALL the code, and define “seems to hang”.

I appreciate ur response and checking my code…I got it somewhere I forget and have tried to understand exactly how it works, but its still not clear… however apparently I have located the ‘hang’… if I accidently set the pwm higher than 155 it then hangs…low output continous…so i will add several lines to check that the pwm value is within range first before i set pwm…
attached is my program…it takes solar PV panels DC power and chops it so I can run a standard electric water heater… works great when it doesnt hang…

solarHotWaterHeater.ino (25.5 KB)

This works as expected for me:

void setup() {
  //Initialize Timer2
 
  TCCR2A = 0;
  TCCR2B = 0;
  TCNT2 = 0;

  // Set OC2B for Compare Match (digital pin3)
  pinMode(3, OUTPUT);

  bitSet(TCCR2A, COM2B1);//clear OC2B on up count compare match
  //set OC2B on down count compare match

  // Set mode 5-- Phase correct PWM to OCR2A counts up and down
  bitSet(TCCR2A, WGM20);
  bitSet(TCCR2B, WGM22);

  // Set up /1024 prescale
  bitSet(TCCR2B, CS20);
  bitSet(TCCR2B, CS21);
  bitSet(TCCR2B, CS22);


  OCR2A = 156; //Sets freq 50Hz
  //Timer 2 counts up and down for 312 total counts
  //312 x 1024 x.0625 = 19.968ms 50.08 Hz

  OCR2B = 78;//50% duty cycle valid values 1-155
 }
 void loop() {}

I cut and glued that in… but same thing… it runs a while sometimes, then hangs… now it lately is hanging on second iteration thru the loop looks like…

for some strange reason i cant copy the monitor output to show u… it shows this–

821 90 64
349 58 202
842 90 64
842 90 64
842 90 64
842 90 64

842 is watts 90 is Vsupply and last column is the chopperPWM value…how can it ever be over 150… i have set clip limits to avoid this… i guess my code is pretty bad somewhere

solarHotWaterHeater.ino (26.5 KB)

The problem is not in the timer setup code.

Using Serial.print() to see where things are going wrong is an excellent approach.

I think u are right…

i found bracket in wrong place in this procedure–this is correct now–

else if(chopperPWM!=chopperPWM_mem){if(chopperPWM>150)chopperPWM=150;
if(chopperPWM<20)chopperPWM=20;
chopperPWM_mem=chopperPWM;

if(chopperPWM>=150){digitalWrite(chopperDrv,HIGH);
digitalWrite(greenLED,LOW);}
}
else {if(chopperPWM>150)chopperPWM=150;
if(chopperPWM<20)chopperPWM=20;
analogWrite(chopperDrv,chopperPWM);
digitalWrite(greenLED,HIGH);

loadPWM_mem(chopperPWM); }

so i must have something similar wrong somewhere else…it still runs and show chopperPWM values OVER 150… impossible…
thanks for ur time and help sir…

i found it!!!

chopperPWM=map(Vsupply,VstartUp,Vopen,150,20);
if(chopperPWM>150)chopperPWM=150;
if(chopperPWM<20)chopperPWM=20;

i didnt know map() would produce values beyond what is specified…
apprently it does when the input value is outside the input constrains…

i learned something today…!!!

backwoodsjack:
i found it!!!

chopperPWM=map(Vsupply,VstartUp,Vopen,150,20);
if(chopperPWM>150)chopperPWM=150;
if(chopperPWM<20)chopperPWM=20;

i didnt know map() would produce values beyond what is specified…
apprently it does when the input value is outside the input constrains…

i learned something today…!!!

Or, you could have read the documentation

learning the hard way will stay in my head NOW...
I probably read over the documentation way back and so easy to forget when not causing a problem..