DALLAS 1-wire and PWM by OC2B

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

Hi all,

 

I have a trouble with a 1-wire setup on an ATMEGA1284 where I would like to use PWM.

I have just isolated the trouble but I would like to understand.

 

To isolate the trouble I implemented exactly and ONLY the code available here http://www.avrfreaks.net/project...

where I added, just before the sei(), ONLY the following rows to activate the timers. (the CTC timer works well in many other codes I wrote and tested)

 

// CTC Timer0 tick 100uS

TCCR0A |= (1<<WGM01); 

OCR0A = 99; 

TCCR0B |= (1 << CS01); 

TIMSK0 |= (1<<OCIE0A ); 

 

then there is an "empty" Interrupt Service Routine and everything works fine.

ISR(TIMER0_COMPA_vect) { }

 

the delay_us() inside the code work as expected and I tried also _delay_us() and again without (of course) any trouble.

So the DS18x20 are detected and they work as expected. 

_____________________________________________________________________________________________________________

 

When I add, always just before the sei() and together the CTC timer just showed, the following rows to active the PWM

 

TCCR1A |= _BV(COM1A1) | _BV(COM1B1) | _BV(WGM11) | _BV(WGM10); // Non inverting mode on OC1A and OC1B + Mode 3 FAST PWM

TCCR2A |= _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Non inverting mode on OC2A and OC2B + Mode 3 FAST PWM

DDRD |= (1<<PD4) | (1<<PD5) | (1<<PD6);

TCCR1B |= _BV(CS10); // Start Clock Source - No prescaling & Start PWM on the 16bit TimerA & TimerB present in TIMER-GROUP#1 ... OCR1A OCR1B

TCCR2B |= _BV(CS21); // Start Clock Source - Fclock/8 prescaling & Start PWM on 8bit Timer-GROUP#2 ... OCR2A OCR2B

OCR2B = 127; // range is 0-255 ... 8 bit timer

OCR1A = 511; // range is 0-1023 ... 16bit timer

OCR1B = 511; // range is 0-1023 ... 16bit timer

 (note: the PWM timers works well in many other codes I wrote and tested where there is not the 1-wire bus to manage)

 

the 1-wire isn't able to read the ROM of the DS18x20 and checking with the oscilloscope the communication on the 1-wire bus starts properly but doesn't arrive at the end and it is irregular ... sometimes 16 ROM bits are read, sometimes more than 30, sometimes 27 ....

 

The delay_us() (same for _delay_us()) are ruined by the use of PWM.

And this even into the code if there is

sreg=SREG; cli(); {.... } SREG=sreg; where needed (the code i used is original, nothing changed .. and with CRC works well).

 

I tried also the ATOMIC_BLOCK but, and cannot be different, the behaviour is always the same.

Impossible to communicate with the DS18x20. The communiction starts, but the 64 bits cannot be read, prematurely the communication fails. 

 

OCR2B, OCR1A, OCR1B eat microprocessor cycles?

why? 

 

Thank you for your attention.

Mario

oiram revenac

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

How to properly post source code: http://www.avrfreaks.net/comment...

 

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

It looks like you have chosen MODE 3 PWM, Phase Correct 10 bit mode for T1, and not Fast 10 bit mode claimed, so code comments do not match code.

 

But that should not be the cause of your symptoms, but the timer0 overflow interrupt may cause errors in your timing loops unless interrupts are blocked while spinning.

What is the purpose of this timer tick?

One wire is very dependent on correct timing, so I would start with disabling the overflow interrupt and see if that helps.

 

I don't see where adding timer based pwm should be a problem, unless there are additional interrupt sources involved, and not disclosed in the above code.

 

Jim

 

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

I upload the whole code

Attachment(s): 

oiram revenac

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

ki0bk wrote:
One wire is very dependent on correct timing

Well, yes & no: it is very dependent on the timing within each bit - but not on timings between bits.

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

I know.

 

With CTC only everything works fine. All the 4 DS18x20 work fine.

With CTC + PWM generated by OCR2B, OCR1A, OCR1B the DS18x20 are not detected.

 

Cause the delay_us() are made only of a few "stupid" instructions so to loose time, when I have the PWMs active the delay_us() are corrupted/influenced.

And this even if I do everything inside an ATOMIC_BLOCK, so the interrupts are suspended?

 

Do the PWM counter management eat some microprocessor cycles? 

 

The reply seems obvious but how can I have the PWM and the 1-wire management together?

Better how can I have the delay_us() not influenced by the PWMs?

 

 

oiram revenac

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

--- And this even if I do everything inside an ATOMIC_BLOCK, so the interrupts are suspended. (sorry for the ? of before. A mistake)

oiram revenac

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

I use the PWM to manage a RGB light source.

 

... but the timer0 overflow interrupt may cause errors in your timing loops unless interrupts are blocked while spinning ... 

this should be the cause. I agree.

 

On a 1-wire bus the timing within each bit is a MUST: reading a bit, and immediately after its complementary, they must be coherent, 01 or 10. This is the MUST to check each bit. 

The time between bits is "not" important. Always the slaves (the DS18x20 in my case) before to "release" a bit wait for the "enquiry" from the master.

oiram revenac

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

Can you post a schematic or better a clear picture of your setup?

 

I'm wanting to see your power supply (led and mpu). 

Proper bypassing of mpu, filtering on the LED's, etc....

It looks like LED's and 1-wire share the same port, PD4/5/6/7  correct?

 

Jim

 

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

1-wire is PD7, no parasite power, external 4K7 pull up, 4 x DS18x20.

RGB is PD6 PD5 PD4

 

tomorrow the schematic

oiram revenac

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

attached the schematic

Attachment(s): 

oiram revenac