Generating Square Wave

Go To Last Post
36 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hi all, pls i am trying to generate a 50Hz square wave which i planned to use for PWM inverter and i am having few issues to resolve.

Firstly, do i have to make the square wave 25Hz each or make it 50Hz?

Secondly, Can i connect the output pin of my mcu directly to the gate of the mosfet?

And in the process of my simulation, i discovered that my square wave is not perfect like i thought. my program is as follow and the square wave i got.

i am using atmega328p as my mcu with 8MHz .

 

    DDRB |= (1 << DDB1)|(1 << DDB2);
        

    OCR1A = 624;
    
    TCCR1A = 0x51;
    TCCR1B =0x13;
    TCCR1C =0xC0;
    

    

 

thanks.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You counter period needs to be 50Hz. The OCR register that controls the duty cycle has to be set to 1/2 the 50Hz period count.

 

You MIGHT be able to directly drive a FET gate. It needs to have a "logic" threshold. And, you have to accept slow switching. An MCU port pin cannot deliver the 100's of mAmp (or more) needed to switch a power FET gate fast.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

MCU cannot deliver up to 100mA. Can I then add a power transistor just before the MOSFET or will an optocoupler works just fine?
Do I have to apply the 1/2of my needed frequency to the two output pins or just one one pin? I am asking this because I feel the two pins will amount to 100Hz

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Opto-coupler is NOT needed unless you need the isolation. It will actually hurt instead of help. And, no, you don't need multiple pins unless you have more than one FET to drive.

 

The mention of 100mA or much more simply indicates what is needed to switch a power FET fast (as in nanoseconds). Such fast switching would be needed for a high frequency (say 100s of KHz) PWM which it does not appear that you need, yet. 

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

And in the process of my simulation, i discovered that my square wave is not perfect like i thought. 

Do you think a simulation is perfect in the first place?  And if the simulation looked perfect, do you think it really would be?  Use actual chips! 

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Your 'scope will do that if you have it switched to AC coupling.  It's not the signal - it's your test gear.  S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should read the specification sheet for the MOSFET you are using. Usually you are switching between two states - ON and OFF. OFF in an enhancement-mode MOSFET is 0 volt. ON is not so easy, there is a threshold voltage where the MOSFET starts to conduct, but a higher voltage is required to get the lowest resistance. In a power MOSFET this is often higher than 5V, e.g. 10V. So the output of a 5V device may not be sufficient.

 

Also, when switching between the two states, the gate is a capacitor that has to be charged and discharged. While this happens, the MOSFET is neither ON or OFF, but acts as a variable value resistor and has to dissipate a lot of heat. The specification sheet will say how long it can do this - depending on what current and voltage is being switched this can vary from 'not a problem' to 'must be done in nanoseconds'. So whatever drives the MOSFET must provide and dump the charge on the capacitor within the specified time. A microprocessor may not be able to do this quick enough.

 

For a 50Hz square wave, you switch ON for 10 milliseconds and OFF for 10 milliseconds. Period of 20 milliseconds = 50Hz. Only one output pin is needed, the processor has both active high (provides current) and active low (sinks current). If you also require an inverted signal you could use a second pin or external logic. All depends on the rest of your circuit.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Re: Scroungre

 

It's not the signal - it's your test gear.

I argue that its not the test gear, its the operator of the test gear.

 

Jim 

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Fair point.  S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Part of the problem is that although we are dealing with 'AC coupling' / 'DC coupling' the corresponding oscilloscope control is just labeled AC / DC.  This leads the beginner into believing that you use the AC position to look at AC signals and the DC position to look at DC signals (let's not deal with why you would use a 'scope for that).  I always thought that a little picture showing a capacitor with a switch in parallel would be the way to label the control.

 

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Don't be fooled....always use DC coupling to see the true signal...Use AC only if needed (small sig in presence of large DC offset).

When in the dark remember-the future looks brighter than ever.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

floresta1212 wrote:
I always thought that a little picture showing a capacitor with a switch in parallel would be the way to label the control.
I'm not sure that would be helpful for a beginner.  "Is this how I measure a capacitor?"

 

If this (i.e. coupling) is part of the issue for the OP (or any beginner), and the screenshot in #1 suggests it is, I think it's wishful thinking to suggest that the label on a panel could completely communicate fundamental knowledge like signal coupling.  That expectation is a symptom of the iPhone generation.  Nobody reads any more.

 

Well that was a bit of an unintended rant ;-)

 

holawaleh wrote:
DDRB |= (1 << DDB1)|(1 << DDB2);

 

OCR1A = 624;

 

TCCR1A = 0x51;

TCCR1B =0x13;

TCCR1C =0xC0;

Don't use magic numbers.  Use the named bits.  Otherwise you're forcing us to dig out a datasheet and do your work for you, just to understand what you've written.  And get into the habit of commenting your code.  This communicates to the reader (including you six months from now) what the programmer's intention was.  Like this:

// OC1A and OC1B as outputs.
DDRB |= (1 << DDB1) | (1 << DDB2);  

// Timer period of 1248 ticks (dual slope mode).
OCR1A = 624;                        

// WGM mode 9 (bits WGM13 and WGM10)
//  - PWM, Phase and Frequency Correct - TOP via OCR1A.
// COM mode 1 for both OC1A and OC1B.
//  - WGM13:0 = 9 or 11: Toggle OC1A on Compare
//    Match, OC1B disconnected (normal port operation).
//    For all other WGM1 settings, normal port operation,
//    OC1A/OC1B disconnected.
// Prescaler of /64.
TCCR1A = (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);
TCCR1B = (1 << WGM13) (3 << CS10);

// Force output compare on both channels (why?).
TCCR1C = (1 << FOC1A) | (1 << FOC1B);

 

There is some confusion.  Your choice of Phase and Frequency Correct PWM mode seems inconsistent with your choice of COM mode for toggling.  And, your choice of OCR1A=624 and prescaler of /64 means that OC1A will toggle every 79,872 cpu cycles.  At 8 MHz, that will yield a square wave of 50.08 Hz on OC1A.

 

However with COM mode 1 on OC1B and WGM mode 9, OC1B is disconnected from the timer, and should not toggle at all.  The screenshot suggests that it is.  Either you've not shown us the correct code that yielded that 'scope result, or your simulation environment is flawed (or my analysis is flawed... or there is a long-standing datasheet errata).

 

And please:

 

holawaleh wrote:
Do I have to apply the 1/2of my needed frequency to the two output pins or just one one pin? I am asking this because I feel the two pins will amount to 100Hz
Frequencies don't add like that.

 

I think you need to clearly state what your objective is, in as much detail as you can.  What are you trying to achieve, how are you trying to achieve it, and what have you tried so far.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's something fundamental to knowing 'how to drive an oscilloscope', akin to knowing what "PRNDL" means while trying to drive an (automatic transmission) car.  S.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm surprised the "AC" signal doesn't look worse in your simulation than it does, it would probably look even worse on a real scope if you set it to AC. At least the ones I've tried!

Switch it back to DC.

 

Scroungre wrote:

It's something fundamental to knowing 'how to drive an oscilloscope', akin to knowing what "PRNDL" means while trying to drive an (automatic transmission) car.  S.

 

Mine says PRNDS, and a few other automatics I've been driving only had "PRND", had to look up what the "L" was about cheeky

- Brian

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PRND21

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1N2345  Oh wait, that ones a motorcycle!

 

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274

 

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PNDLR  - Good for rocking the car to get out of snowbanks. 

 

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ki0bk wrote:
1N2345 Oh wait, that ones a motorcycle!

Since we are digressing, farm tractors had/have some interesting patterns.  While as a kid I was a Farmall boy with a nice pattern

one also ran into "interesting" patterns:

with the latter two from the same Oliver breed.

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

floresta1212 , thank you for your elaborate explanation and i am more aware now. the force output compare on both channels is to make sure the frequency is not exceeded.

 what i am actually writing the code is that i want to use it design an DC/AC inverter. To generate 2 square wave of frequencies 50Hz alternating each other

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Not guilty - thank joeymorin instead.

 

Don

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
void set_wave()
{
OCR1A = 625;
TCCR1A = (1 << COM1A0) | (1 << COM1B0) | (1 << WGM10);	//PWM, Phase and Frequency Correct

TCCR1B |= (1<<WGM13);		// 
TCCR1B |= (1<<CS12);		//prescalar 256 to generate output frequency of 50Hz

	
}	


int main(void)
{
DDRB |= (1 << DDB1)|(1 << DDB2);
PORTB |= (1<<PORTB2);    
	
	
	
	while(1)
    {
       set_wave();			//TODO:: Please write your application code 
    }
}

i changed few things in my program and i still some issues, the program works fine on simulation but my PB2 port doesnt not turn on. Only PB1 seems to be responding to the program. i tweak my COM  in the register but nothing seem to happen. what am i missing and i can i add to make my PB2 and PB1 alternate 50Hz. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show your schematic.  Tell how you are testing that PB2 doesn't turn on.  What do you expect?  What do you observe?

 

What AVR model are you working with?  I cannot see that above.  All AVR timers aren't exactly the same. [sorry; '328 is buried in the OP text.]

What do you mean "alternate 50Hz"?  The two signals with 180 degree phase difference; aka "complementary"?

 

So then PB1 has the alternate function of OC1A.  PB2 has the alternate function of OC1B.

 

I always start with that Modes of Operation table.  Your code and comment indicate mode 9.  OCR1A is TOP.  How do you expect to get a PWM with OCR1A setting the period?

 

If you want a PWM signal on OC1B, then you will need to set OCR1B to yuor duty cycle value with respect to OCR1A.

 

[Now I'm really confused -- how could the posted code possible "work fine in simulation"?!?]

 

I'm pretty sure you can get simple complementary signal pair setting both channels to CTC mode, and use a different COM combination for each.

 

The PWM way would be to use mode 14, set ICR1 (TOP) as the period, set OCR1A to half that and Normal PWM.  Set OCR1B to half the ICR1 value also, with inverted PWM.

 

 

 

 

 

 

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Going over the Modes of Operation table, mode 9 may be one of the more difficult modes to generate 50Hz complementary signals.

 

Mode 4 CTC setup from CodeVision Wizard is below.  You need to dance a bit to get PB1 and PB2 into opposite states when starting out.

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: CTC top=OCR1A
// OC1A output: Toggle on compare match
// OC1B output: Toggle on compare match
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 10 ms
// Output Pulse(s):
// OC1A Period: 20 ms Width: 10 ms
// OC1B Period: 20 ms Width: 10 ms
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (1<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (1<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x27;
OCR1AL=0x0F;
OCR1BH=0x27;
OCR1BL=0x0F;

I'd use one of the modes with ICR1 as TOP.  If you really need phase/frequency correct, then use mode 8 instead of mode 9. [setting pin direction is left as an exercise for the reader]

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1000.000 kHz
// Mode: Ph. & fr. cor. PWM top=ICR1
// OC1A output: Non-Inverted PWM
// OC1B output: Inverted PWM
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 20 ms
// Output Pulse(s):
// OC1A Period: 20 ms Width: 10 ms
// OC1B Period: 20 ms Width: 10 ms
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (1<<COM1B1) | (1<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x27;
ICR1L=0x10;
OCR1AH=0x13;
OCR1AL=0x88;
OCR1BH=0x13;
OCR1BL=0x88;

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

LOL, Lee beat me to it, but with a better solution:

 

I too used the Wizard and went with a Timer Overflow instead:

#define F_CPU 8000000ul
#include <avr/io.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <util/delay.h>
#include <avr/interrupt.h>

// Timer1 overflow interrupt service routine
ISR(TIMER1_OVF_vect)
{
	// Reinitialize Timer1 value
	TCNT1H=0xFFB0 >> 8;
	TCNT1L=0xFFB0 & 0xff;
	// Place your code here

}


int main(void)
{
	// Input/Output Ports initialization
	// Port B initialization
	// Function: Bit7=In Bit6=In Bit5=In Bit4=In Bit3=In Bit2=Out Bit1=Out Bit0=In
	DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (1<<DDB2) | (1<<DDB1) | (0<<DDB0);
	// State: Bit7=T Bit6=T Bit5=T Bit4=T Bit3=T Bit2=0 Bit1=0 Bit0=T
	PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
	
	// Timer/Counter 1 initialization
	// Clock source: System Clock
	// Clock value: 8000.000 kHz
	// Mode: Normal top=0xFFFF
	// OC1A output: Toggle on compare match
	// OC1B output: Toggle on compare match
	// Noise Canceler: Off
	// Input Capture on Falling Edge
	// Timer Period: 0.01 ms
	// Output Pulse(s):
	// OC1A Period: 0.02 ms Width: 0.01 ms
	// OC1B Period: 0.02 ms Width: 0.01 ms
	// Timer1 Overflow Interrupt: On
	// Input Capture Interrupt: Off
	// Compare A Match Interrupt: Off
	// Compare B Match Interrupt: Off
	TCCR1A=(0<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (1<<COM1B0) | (0<<WGM11) | (0<<WGM10);
	TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);
	TCNT1H=0xFF;
	TCNT1L=0xB0;
	ICR1H=0x00;
	ICR1L=0x00;
	OCR1AH=0x00;
	OCR1AL=0x00;
	OCR1BH=0x00;
	OCR1BL=0x00;
	
	sei();
	
    while (1) 
    {
		//do nothing		
    }
}

 

 

In my example both PB1 and PB 2 are in phase though.

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

Last Edited: Wed. Feb 20, 2019 - 08:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Shematicsoutput

What i actually expected is to have a signal at portB1 and PortB1 at every 200ms. I am using Atmega328P. Everything seems fine on proteus simulation but only PortB1 responds on breadboard.

In a layman's language, i intend to use the two pins (PORTB1 and PORTB2) to generate frequency at 50Hz which will now be used to form PWM for an inverter power supply. I plan to connect the PINS to MOSFET. Thanks  

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

holawaleh wrote:
What i actually expected is to have a signal at portB1 and PortB1 at every 200ms.

 

Thats 5Hz, not 50Hz

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do we have another case of failing to set up the modes correctly because |= is used instead of =. Some simulators assume a value of zero for bits not explicitly set. Hardware is not always as forgiving. I suspect the mode being set is not what is expected. DON'T use |= or &= UNLESS = will corrupt something with an already known value. Otherwise set and clear all bits explicitly to get a known result. Read the datasheet, it says what each bit does, and if a bit that has no defined use should be set, clear, or don't care.

[edit. If you are using = then only the bits to be set need a mention. The other bits are clear by definition. I note the Wizard does this

TCCR1A=(0<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (1<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (0<<CS11) | (1<<CS10);

but all the (0<<...) are redundant and it's programmer's choice to put them in or not. I tend to abbreviate code and would use

TCCR1A=(1<<COM1A0) | (1<<COM1B0); // Toggle OC1A/OC1B on Compare Match
TCCR1B=(1<<CS10); // Start timer (No prescaling)

as it makes more sense to me]

According to the specification, like fine print on a contract, it says if OCR1A is used as top for a dual slope mode it will toggle producing a 50% duty cycle, which is what the simulation shows. But it is useless as the produced waves are not 180 degrees out of phase.

My opinion, Mode 8 is the right mode to use. ICR1 is used to set TOP. To get 50Hz needs a 20mS period, it upcounts for 10, downcounts for 10. With a prescaler of 64 that is a value of 1250

(for each cycle only one count at values 0 and 1250, 2 counts at all other values = 2500 * 64/8000000 seconds per cycle  = 0.02 seconds)

Then set appropriate values for OCR1A OCR1B and enable one as normal output and one as inverting output. For symmetric 50% duty square waves that would be 625. However, driving MOSFETS requires time for them to switch on and off so this would result in one being partially on while another is partially off. This may be acceptable, who knows what circuit this is driving? But in most applications one would have some dead time and some diodes to snub inductive currents so the normal output OCR1x is set lower (e.g. 600) and the inverting output OCR1x is set the same amount higher (650). Just example values, real ones could be anything symmetrical around 625.

The Datasheet is rather obscure about the timers, no surprise it needs time for it to sink in.

Last Edited: Thu. Feb 21, 2019 - 12:33 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

holawaleh wrote:
, i intend to use the two pins (PORTB1 and PORTB2) to generate frequency at 50Hz which will now be used to form PWM for an inverter power supply.

 

While it can be done, square waves are not ideal at all for inverters....especially DC to AC.

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

holawaleh wrote:
What i actually expected is to have a signal at portB1 and PortB1 at every 200ms.

???  With the code you show ed earlier?  That code will not generate that 'scope picture.

ajcashin wrote:
but all the (0<<...) are redundant and it's programmer's choice to put them in or not. I tend to abbreviate code and would use

???  What does the convention for the CodeVision Wizard have to do with OP's situation?

ajcashin wrote:
Do we have another case of failing to set up the modes correctly because |= is used instead of =.

I wouldn't think so, as the posted code is a complete program and not e.g. Arduino sketch.  So, what mode do you think might be invoked by said simulator, to generate that 'scope picture with the code geven?

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:

holawaleh wrote:

What i actually expected is to have a signal at portB1 and PortB1 at every 200ms.

 

???  With the code you show ed earlier?  That code will not generate that 'scope picture.

 

Maybe the simulator doesn't honour the note: "WGM1[3:0] = 9 or 11: Toggle OC1A on Compare Match, OC1B disconnected (normal port operation)" but the real circuit does. COM1A0 is set, asks for toggle on OC1A. Mode 9 uses OCR1A as top and that will cause toggle of OC1A at TOP. COM1B0 is set, simulator may think it should toggle OC1B at whatever it thinks is the value of OCR1B.

theusch wrote:
ajcashin wrote:

but all the (0<<...) are redundant and it's programmer's choice to put them in or not. I tend to abbreviate code and would use

 

???  What does the convention for the CodeVision Wizard have to do with OP's situation?

 

I thought I was clarifying that it is necessary, when using =, to explicitly set bits but it is unnecessary to say they are clear because they are by default. Before my edit this was ambiguous. The Wizard simply provided an example.

theusch wrote:
ajcashin wrote:

Do we have another case of failing to set up the modes correctly because |= is used instead of =.

 

I wouldn't think so, as the posted code is a complete program and not e.g. Arduino sketch.  So, what mode do you think might be invoked by said simulator, to generate that 'scope picture with the code geven?

 

Maybe not explicitly setting the whole of TCCR1B can result in a problem. I suggested this on another post where simulator and real results differed, it was the solution. In that case the symptoms indicated WGM12 was left set. It would appear that some variants of the bootloader don't clean up the timer registers. In this case if WGM12 was set the Mode would be 13 with who knows what output. More likely in this case is a bad simulation, the datasheet says when WGM1[3:0] = 9  OC1B is disconnected (normal port operation).

 

All this is a side issue. For the application the it's the wrong Waveform Generation Mode, the Compare Output Mode is wrong, OCR1B not initialised, the counters are wrong, possibly the duty cycles are wrong. I gave my solution which was the crux of my post. If that doesn't work then we can have fruitful discussion.