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 https://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