What's the value for TWBR on ATMEGA128 16Mhz ?

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

Guys,
What's the value for TWBR on ATMEGA128 16Mhz ?
is it 0x48 ?
TWSR = 0x00

I want to use it with DS1307 ?
thanks

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

bianchi77 wrote:
Guys,
What's the value for TWBR on ATMEGA128 16Mhz ?
is it 0x48 ?
TWSR = 0x00

I want to use it with DS1307 ?
thanks


Hi there! At first define the desirable frequency (speed) you want your devices exchange at. At second use datasheet of ATMega128 to evaluate needed divider factor. But don't forget about I2C requirements about maximum speed, line length and capacitance.

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

I don't understand, the code is working on 8Mhz but it's not on 16Mhz, I have changed TWBR already,
is it because of the circuit ?
I have double checked the connection and nothing is wrong....any ideas guy ?
Is it because of the fuse bit again ?
I put CKOPT = 1.....

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

Does your another code work well? I suppose there are something else but I2C code (terminal, for example)?
Do you have an oscilloscope? If so, you could check all wires in the bus. You could also check the main frequency (on the XTAL1, XTAL2 pins). I bet if I2C communication works well on 8 MHz, it will works well on 16 MHz (with correcting of TWBR). I2C is truly simple bus, there are no any traps at all, unless you have only device and only master.

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

The DS1307 is a standard I2C device that works at the standard 100kHz I2C bus frequency.

TWBR = 0.5 * (F_CPU / 100kHz - 16)
TWBR = 0.5 * (160 - 16)
TWBR = 72

Ah-ha. 72 is 0x48 in hexadecimal.
You always use a div1 prescaler (TWSR=0). Unless you want to use a really SLOW non-standard bus frequency.

So what was the problem ? Did you not see the TWBR formula in the datasheet?

David.

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

Yes I did :
TWSR = 0x00; // Select Prescaler of 1
TWBR = 0x48; //72 decimal actual result

Yes I read the datasheet already,

Now I'm gonna check with logic analyzer...

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

no response...nothing in logic analyzer

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

The most common problem is wrong Slave address.
The DS1307 is 0xD0/0xD1 (8-bit) or 0x68 (7-bit)

The next problem is users cannot afford both 4k7 pull-up resistors.

And you need VCC of 5V or so.
At 3.3V, the DS1307 runs off its VBAT.

David.

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

I check on X2 pin of DS1307, it reads 200Khz, is my crystal broken ?

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

I would never risk an oscilloscope probe on either X1 or X2. If you are determined to do so, use a divide x 10 probe.

The DS1307 should talk on the I2C bus whether it has a XTAL or not. Have you measured VCC, VBAT etc?

It sounds as if you are not using a respected library. I suggest that you download a library and use the proper functions.

Most importantly, you should use the return values from the functions. Then you can tell exactly what and where is going wrong.

David.

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

I pull out the crystal already, is DS1307 still working ?
how can I see it ? check on SCK and SDA ?
thanks

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

Vcc=4.91V
VBAT = 3.02V

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

i didn't see anything on SCK and SDA....

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

voltage on SDA and SCL = 1.84V...
something wrong here ?

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

Quote:

voltage on SDA and SCL = 1.84V...
something wrong here ?

What pull-up resistors are you using?

As noted above:

Quote:
The next problem is users cannot afford both 4k7 pull-up resistors.

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

is it because I put LED on PD0 and PD1 ? thanks

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

Quote:

is it because I put LED on PD0 and PD1 ? thanks

Is this a wind up? You do know how the I2C open-collector bus works don't you? You understand the need for pull-ups? The need for the bus to be able to be pulled up and the general operation of I2C?

If you don't understand I2C start here:

http://www.i2c-bus.org/how-i2c-h...
http://en.wikipedia.org/wiki/I%C...

and from there on to:

http://www.nxp.com/documents/use...

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

You could have used active-low LEDs (with resistor) for the 'pull-ups'.

In which case, you would see the I2C activity.

As a general rule, you drive the LEDs via a buffer. Then they will not affect the bus. And you can use wierd active-high or regular active-low LEDs as you desire.

David.

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

I made like this :

Is it possible ?
thanks

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

I got -0.54V to -0.72V on SDA and SCL pin, up and down, is it a good sign ?

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

No, do you have 2 pull up resistors on those lines?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I've just put 10K pull up on both lines now the voltage are 0.12V.....is it ok?
is it possible that my DS1307 has been broken ?

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

Quote:
is it ok?
No it should be pretty much the same as VCC (5V?) without any activity on the bus.

edit so I have gone back a bit through the thread and I really don't understand what YOU understand. :evil:

Quote:
Is it possible ?
Of course NOT!! What makes you think you can load up the I2C lines with leds? I give up. :roll:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

bianchi77 wrote:
I made like this :

Is it possible ?
thanks


I also give up.

Your schematic is ACTIVE-HIGH. i.e. if the voltage is high, the LED would light.

You should really use buffers. However, active-low LED would work too. e.g. 1k0 resistor connected to 5V. the other end to the LED anode. then connect the cathode to the AVR pin.

i.e. when the AVR pin is low, the LED lights.

In 99% of cases, active-low is a lot easier to use from both an electrical and a software point of view.

When you were at school, did they teach you anything in Science class ?

David.

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

Quote:

When you were at school, did they teach you anything in Science class ?

Even if they missed out here, did the OP read any of the stuff that Cliff linked to?

The Wikipedia article should make most of the basics clear. The NXP document is good, and section 3.1.1 and 3.1.2 in particular should be read by the OP.

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

David the point I was trying to make is that he has LEDs connected across the SCL and SDA lines (PD0 and PD1 above) as well as trying to talk to the I2C chip on those same pins. :roll:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

1000R with a 2V LED should sink 3mA from a 5V supply. This should be absolutely fine as a 'pull-up' on the I2C.

330R with a 2V LED should sink 10mA from a 5V supply.
This is a bit high for I2C but will probably work.

I find it difficult to understand why people attempt to use active high switches or LEDs. It seems to go hand in hand with capacitor aversion.

Quite honestly, you don't need to have a wide knowledege of electricity to use AVRs:
1. current flows from +ve to -ve. Current = Voltage / Resistance.
2. If a data sheet says "connect a 100nF ceramic capacitor" here, do what it asks.
3. If in doubt, copy a commercial schematic.

Rule (1) can show how active-high or active-low works.
Rule (3) should get you out of most difficulties.

David.

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

even I put 10K pull up and pull away the LED,
still got nothing from DS1307, voltage on pin =0.11V...

I don't understand what happens here,

Any ideas folks ? I can send you the complete schematic..
and for DS1307 I put 10K pull up, on schematic, I didn't draw it..

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

ups I didn't put filter caps for it...hehe, could it be the cause ?

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

Quote:

could it be the cause ?

The cause of seeing 0.11V on a floating pin that should be pulled up to Vcc? Err, no.

Maybe try disconnecting from the DS1307 all together and just have the SDA/SCL lines with nothing but the 10K resistors pulling them to Vcc. If you don't then read Vcc on those lines either something is seriously wrong or you have code in the AVR that is driving them to Gnd (and the 10K will then pull the signal just a bit above Gnd - like 0.1V in fact).

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

david.prentice wrote:
I would never risk an oscilloscope probe on either X1 or X2. If you are determined to do so, use a divide x 10 probe.

Excuse me, why? What may go wrong? And what can be damaged?

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

Quote:

Excuse me, why? What may go wrong? And what can be damaged?

http://www.elexp.com/t_probe.htm

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

or is it because ATmega103 compatibility mode ?

how can I disable it on fuse ? is it 1 or 0 ?
thanks

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

(I am beginning to suspect you may be trolling us. I hope my suspicions are not grounded).

Anyway, you have two options with M103C. Either you leave it as Atmel supplied the mega128 and you actually program as if you own a mega103 (though you lose a lot of the 128 features). Or you use your ISP programmer and change the M103C fuse from 0 to 1 and then the chip becomes a mega128.

But if you have been writing 128 code for a 103 nothing you have tried so far would have worked at all as the stack is not set correctly and the first use of CALL/RET will fail. Surely you have seen SOME code running in this chip before you moved on to attempt I2C?

When in 103 mode you will have read this in the m128 datasheet:

Quote:
Two-wire serial interface is not supported.

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

I think, this fuse thing cause me trouble, I check it and it's zero, now I'll make it 1 and see what happens,thanks guys...

is it the right atmega128 configuration fuse ?

Yes I read about that...

I can see SDA 3.98V now, without DS1307 on board

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

it's working now, thanks a lot guys...;)

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

Quote:

how can I disable it on fuse ? is it 1 or 0 ?

Can you explain your repeated aversion to reading technical documentation? Is this just about laziness again?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:

it's working now,

So up to this date you have never actually run a program in the AVR? (or at least one that called a function from main)?

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

I haven't updated the fuse bit that's why it's not running, now I got something quirky, the month is displaying a day of the week, I put the right address for it already, double check it and it's fine one the other board, is there any possibilities if my DS1307 has a defect ?

I can't write to DS1307 too....I still don't understand, that's why I asked you guys, in case some of you have a same or almost the same experience...
thanks in advance

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

I tried to do :

void Write_DS1307(void)
{
	unsigned char i, hour_format;

	// Make sure we enable the Oscillator control bit CH=0 on Register 0x00
	ds1307_addr[0]=ds1307_addr[0] & 0x7F;

	// Start the I2C Write Transmission
	i2c_start(DS1307_ID,DS1307_ADDR,TW_WRITE);

	// Start from Address 0x00
	i2c_write(0x00);
   
	// Write the data to the DS1307 address start at 0x00
	// DS1307 automatically will increase the Address.
	
	ds1307_addr[0] = 0x00;
	i2c_write(ds1307_addr[0]);//second
    _delay_ms(10);
    ds1307_addr[1] = 0x37;
    i2c_write(ds1307_addr[1]);//minute
	_delay_ms(10);
	ds1307_addr[2] = 0x21;
	i2c_write(ds1307_addr[2]);//hour
	_delay_ms(10);
	ds1307_addr[3] = 0x02;
	i2c_write(ds1307_addr[3]);//day of the week
    _delay_ms(10);
    ds1307_addr[4] = 0x03;
    i2c_write(ds1307_addr[4]);//day of the month
    _delay_ms(10);
    ds1307_addr[5] = 0x09;
    i2c_write(ds1307_addr[5]);//month
    _delay_ms(10);
    ds1307_addr[6] = 0x13;
    i2c_write(ds1307_addr[6]);//year
	
	
	
	// Stop I2C Transmission
	i2c_stop();
}

but when I called the function, the system is just freezed, any ideas ?
thanks

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

I got it,

I didn't put WriteDS1307 after TWBR init, that's why it can't write....hehehe
thanks guys