twi stop when conecting two atmega8 communication

General discussion on mikroBasic PRO for AVR.
Post Reply
Author
Message
elasa_group
Posts: 2
Joined: 19 Jun 2017 17:03

twi stop when conecting two atmega8 communication

#1 Post by elasa_group » 09 Jul 2017 15:30

HI dears
I have two atmega8i which needed to communicate with each other, so I used TWI Protocol it works for some seconds and after that, it stops randomly at some pattern of TWI signal, why this happening?

transmitter :

Code: Select all

  TWI_Init(100000)    DDB7_bit = 1      ' initialize TWI communication
  TWI_Start()              ' issue TWI start signal

  TWI_Write(1)             ' send byte (address of EEPROM location)

  TWI_Stop()  timer1_init()             ' issue TWI stop signal

  while TRUE
    inc(jk)
    TWI_Start()              ' issue TWI start signal
    TWI_Write(jk)             ' send byte (address of EEPROM location)
    TWI_Stop()
    Delay_ms(100)
 wend


reciver

Code: Select all

 TWI_Init(100000) 
 while TRUE
  TWI_Start()
   if  TWI_Busy =0 then
                ' issue TWI start signal
  target_min=TWI_Read(1)      ' read data (NO acknowledge)

  end if
  TWI_Stop()

  Display_Number(target_min)
 Delay_ms(10)

  wend
the whole code with its proteus simulation is attached.
Attachments
resiver test.zip
(2.39 MiB) Downloaded 113 times

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: twi stop when conecting two atmega8 communication

#2 Post by darko.ilijevski » 11 Jul 2017 23:32

Hello,

Your code seems rather complex, have you tried to make only the TWI communication working properly before moving on ? Also in the piece of code you have provided, I can see that you've rather used the "brute force" method to communicate between the 2 MCUs, the receiver doesn't care about the address or if it was addressed - those modules actually have built in mechanisms to automatically answer to TWI requests. Your example behaves erratically while running in Proteus, generating a lot of exceptions. It stops somewhere at 141 on the display.

In the attachment, you can see how to properly initialize and use the TWI in receiver mode. Sadly the MikroE library has only master functions, so to use it in the slave mode, you need to make some on your own. You can see those in my TWI_Test_Rx example. The other one (TWI_Test_Tx) uses only the regular mikroE TWI functions. Open up the project group and you can play with both RX and TX codes. Changing the addresses will break the communications, like it should.

The idea is that you send one data byte to the slave, write_data = 0x01. The slave inverts the received value and the master reads it back, displaying it on the LEDs, connected to port B. The write data value gets multiplied by 2 and the process repeats. So the PORT B of the master will show the inverted PORTB of the slave, while the LED will move through all 8 bits (multiplying with 2 actually shifts bits one place right) I have used the internal 8MHz clock.

P.S. You should always evaluate your results with the real hardware.

I hope this helps you in your project.
Best regards
Attachments
TWI_ATMEGA8_ 8MHzIntClock.zip
(75.97 KiB) Downloaded 103 times
BR,
Darko

dibor
Posts: 208
Joined: 29 Dec 2014 18:59

Re: twi stop when conecting two atmega8 communication

#3 Post by dibor » 12 Jul 2017 06:27

Hello.
If MikroE team using Proteus we have good chance to get COFF file compatible with Proteus and AVR Studio for debugging.

Regards.

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: twi stop when conecting two atmega8 communication

#4 Post by darko.ilijevski » 12 Jul 2017 07:35

dibor wrote:Hello.
If MikroE team using Proteus we have good chance to get COFF file compatible with Proteus and AVR Studio for debugging.

Regards.
Hi,
I believe I was clear about software simulations of any kind. It is hard to have really accurate software model, I've seen more than a few working examples which simply failed on the sim, due to a bottlenecking of the CPU, graphics, the algorithm itself and so on. Software only has so much power... But it's neat for simpler tasks :)

Best regards
BR,
Darko

dibor
Posts: 208
Joined: 29 Dec 2014 18:59

Re: twi stop when conecting two atmega8 communication

#5 Post by dibor » 12 Jul 2017 18:17

I do not talk about software simulation and problem of software simulation.
I'm talking about COFF file generated by MikroC for AVR compiler.
Coff file generated by IAR, CodeVision, ImageCraft and BascomAVR can be used in Proteus and AVR studio, but file generated by MikroC - NOT!!
I'm waiting many years but ME team do NOTHING for possibility to use coff file in any simulators.

Regards.

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: twi stop when conecting two atmega8 communication

#6 Post by darko.ilijevski » 13 Jul 2017 17:16

Hello,

I am sure that if there are enough requests about that feature, mikroE development team could decide to implement it. Also it's not that simple to support the Proteus COFF just like that - same as if all the cars could be able to use the same parts - if you understand me - it would be better for us, but it's simply not how it goes in the reality.

Anyway, I will pass the suggestion to our development team.
Also I think that it would be better to open a separate thread about that issue, let's keep this one clean, OP was asking about how to use TWI.

Best regards
BR,
Darko

Post Reply

Return to “mikroBasic PRO for AVR General”