Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

ATXMEGA and RTC PCF8583

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
smatjaz

Bascom Member



Joined: 28 Jun 2004
Posts: 62

slovenia.gif
PostPosted: Wed Nov 19, 2014 8:08 pm    Post subject: ATXMEGA and RTC PCF8583 Reply with quote

Hello.

Couple of days I'm trying to connect PCF8583 - I2C on ATXMEGA128A3U.
It doesn't work.
In help is described, how I2C works on ATXMEGA, but for me unsucssefull.
I don't know, where is a problem. Microcontroler(ATXMEGA128A3U) don't answer on I2c statements.

It hangs. With changing hardware - other PCB-with PCF8583, nothing changed. I also changed port (PortF to portE), changed settings to software I2C, but the problem is the same.

In addition is my testing software.
Could you somebody tell me, where is the problem?

P.S. In many my projects it works OK with ATMEGA chips.

Best regards.


Code:
'-----------------------------------------------------------
' micro : ATXMEGA128A3U
'-----------------------------------------------------------
'$sim
$regfile = "xm128a3udef.dat"
$crystal = 32000000                                         '32MHz
$hwstack = 150
$swstack = 150
$framesize = 150

$lib "xmega.lib"
$external _xmegafix_clear
$external _xmegafix_rol_r1014

'-EXT OSC-----------------------------------------------------------------
Config Osc = Enabled , Extosc = Enabled , Pllosc = Enabled , Range = 12mhz_16mhz , Startup = Xtal_16kclk , Pllsource = Extclock , Plldiv2 = Disabled , Pllmul = 2
Config Sysclock = Pll , Prescalea = 1 , Prescalebc = 1_1
Config Priority = Static , Vector = Application , Lo = Enabled

'-EEPROM XMEGA-----------------------------------------------------------------
Config Eeprom = Mapped

'-RTC-----------------------------------------------------------------------------
'Config Date = Dmy , Separator = -
'Config Clock = Soft , Rtc = 1khz_32khz_crystosc , Gosub = Sectic       ' we select the internal 1 KHz clock from the 32KHz external oscillator

'PCF8583----------------------------------
Open "twie" For Binary As #4                                ' Use TWI on Port F
Dim Twi_start As Byte
Config Twie = 400000                                        ' 100KHz
I2cinit #4

'-Software I2C-----------------------------
'$forcesofti2c                                               ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
'$lib "i2c.lbx"
'Dim E As Byte
'Config Vport0 = E
'Config Sda = Porte.0                                        '10K pullup !!!!!!
'Config Scl = Porte.1
'Config Scl = Port0 .1                                       ' Pin to use as SCL (The hardware pin is Pinb.1)
'Config Sda = Port0 .0                                       ' Pin to use as SDA (The hardware pin is Pinb.0)
'I2cinit                                                     ' Bring the Pin's in the proper state


Dim Tm_i As Byte , Tm(16) As Byte , Answer As String * 1
Dim Second As Byte , Minute As Byte , Hour As Byte , Day As Byte , Month As Byte , Year As Byte

Declare Sub Settime()
Declare Sub Gettime()

Enable Interrupts                                           ' as usual interrupts must be enabled
Config Com7 = 9600 , Mode = Asynchroneous , Parity = None , Stopbits = 1 , Databits = 8       'F2, F3
Open "COM7:" For Binary As #7
Enable Interrupts
Config Serialin6 = Buffered , Size = 100
Waitms 500

'------------------------------------------------------------------------------
 Print #7 , "RTC-PCF8583 2014-11"
 Print #7 , "Ver. " ; Version(1) ; "  " ; Version(3)

 Dim Casxx As String * 10
 Dim Casx As Byte
 Dim I As Byte
 Dim Temp As Byte
'-Main ------------------------------------------------------------------------
Main:
 Do
  Print #7 , Hex(hour) ; ":" ; Hex(minute) ; ":" ; Hex(second)
  Call Gettime()
  Waitms 500
 Loop
End

'------------------------------------------------------------------------------
Sub Gettime()
   For I = 1 To 5
       Temp = I + 4
       I2cstart #4
       I2cwbyte &HA0 , #4                                   'write addres of PCF8583
       I2cwbyte Temp , #4                                   'select register
       I2crepstart #4
       I2cwbyte &HA1 , #4
       I2crbyte Tm(i) , Nack , #4
   Next

I2cstop #4

Print #7 , Hex(tm(4)) ; "-" ; Hex(tm(5)) ; "-201" ; Year ; " " ; Hex(tm(3)) ; ":" ; Hex(tm(2)) ; ":" ; Hex(tm(1))
End Sub Gettime()

'--------------------[Interrupt Service Routines]-------------------------------

Sectic:

Return
'------------------------------------------------------------------------------

[/code]

(BASCOM-AVR version : 2.0.7.7 )
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Wed Nov 19, 2014 8:51 pm    Post subject: Re: ATXMEGA and RTC PCF8583 Reply with quote

smatjaz wrote:
I also changed port (PortF to portE)

The ATXMega128A3U provides two TWI interfaces, one on port C and one on E. It's no surprise, that you had no success on port F. Very Happy
Checkpoints:
- device address correct? Pin 3 = A0 attached to GND/VCC?
- pullups attached?
- SDA/SCL on PE0..1?
- other HW on PE0..1?
Back to top
View user's profile
smatjaz

Bascom Member



Joined: 28 Jun 2004
Posts: 62

slovenia.gif
PostPosted: Thu Nov 20, 2014 2:13 pm    Post subject: Re: ATXMEGA and RTC PCF8583 Reply with quote

MWS wrote:
smatjaz wrote:
I also changed port (PortF to portE)

The ATXMega128A3U provides two TWI interfaces, one on port C and one on E. It's no surprise, that you had no success on port F. Very Happy
Checkpoints:
- device address correct? Pin 3 = A0 attached to GND/VCC?
- pullups attached?
- SDA/SCL on PE0..1?
- other HW on PE0..1?


Hello.

I checked:
- Device address is pin3 A0=GND- OK
- Pullups attached- 10KResistor Pullup on both wires
- SDA/SCL on PE0/PE1 - OK
- other HW on PE0/1- Any other hardware.

... but microcontroler still hangs.

Does somebody have some test software for PCF8583?

Best regards. M.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Nov 20, 2014 8:04 pm    Post subject: Re: ATXMEGA and RTC PCF8583 Reply with quote

Mods: the post I've wrote doubled, and surprisingly I see no option to delete, so I ask you, to do this job.

Last edited by MWS on Thu Nov 20, 2014 8:15 pm; edited 2 times in total
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Nov 20, 2014 8:05 pm    Post subject: Re: ATXMEGA and RTC PCF8583 Reply with quote

smatjaz wrote:
Code:
Sub Gettime()
   For I = 1 To 5
       Temp = I + 4
       I2cstart #4
       I2cwbyte &HA0 , #4                                   'write addres of PCF8583
       I2cwbyte Temp , #4                                   'select register
       I2crepstart #4
       I2cwbyte &HA1 , #4
       I2crbyte Tm(i) , Nack , #4
   Next

I2cstop #4

I've noticed, that you've put the I2CStop in the wrong place. You do absolute register addressing, which btw. is not neccessary, see the sample here: http://www.mcselec.com/index.php?option=com_content&task=view&id=44&Itemid=57.
The way you do it, you always have to complete one sequence, which is done by an I2CStop before the next.
Else, see the linked sample for a more efficient way to read the registers.
Back to top
View user's profile
smatjaz

Bascom Member



Joined: 28 Jun 2004
Posts: 62

slovenia.gif
PostPosted: Wed Nov 26, 2014 10:24 pm    Post subject: Reply with quote

Thank you for help.

I solved the problem. ATXMEGA128A3U doesn't have I2c on portF. It was the reason that microocontroler hangs.
I changed hardware with ATXMEGA128A1 and It works. It also works on ATXMEGA128A3U- Port C and Port E.

There are statements:

Code:
Dim Twi_start As Byte                                       ' This Variable is used by the I2C functions
Open "TWIE" For Binary As #4                                ' Pull-Up resistor (4K7) on SDA and SCL to Vcc = 3,3V
Config Twie = 100000
I2cinit #4
...


I have one question more. Why doesn't work software I2C/TWI?

I connect PCF8583 on portF.3 and portF.4 - ATXMEGA128A3U:

Code:
$forcesofti2c                                               ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
$lib "i2c.lbx"                                              ' override the normal xmega i2c lib
Dim Twi_start As Byte                                ' This is important because otherwise the compiler returns:
                                         ' Error: Line 5: 245 No more space for BIT [TWI_START], and File: E: \ $ Elektro \ Projects-Xmega \ Xmega128-ME test LCD \ 128A1_xmega-I2C1.bas

Dim F As Byte

                                                                     'We Use Here Virtual Port 0
Config Vport0 = F                                           ' 'map portB to virtual port0
Config Scl = Port0.4                                        ' Pin to use as SCL (The hardware pin is Pinb.1)
Config Sda = Port0.3                                        ' Pin to use as SDA (The hardware pin is Pinb.0)
I2cinit    


... but nothing happen.

Any suggestion?

Best regards.
M.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Nov 27, 2014 12:45 am    Post subject: Reply with quote

I think to remember, that I've solved this XMega/SoftI2C issue one time, was like that:
- remove $lib "xmega.lib"
- use $lib "i2c.lbx"
- use $forcesofti2c
- remove the VPort
- config SCL/SDA to the real portpins
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5089
Location: Holland

blank.gif
PostPosted: Thu Nov 27, 2014 10:32 am    Post subject: Reply with quote

from the help:

$FORCESOFTI2C
Top Previous Next
Action

The $forcesofti2c directive force the ATXMEGA to use software I2C/TWI Library instead of the hardware I2C registers of ATXMEGA.





Syntax

$forcesofti2c





Remarks

ATXMEGA have usually enough I2C interfaces with fixed SDA and SCL pins but if you want to use other pins as SDA/SCL you can use this directive.

Required Library: $lib "i2c.lbx"



noticeYou can not combine the soft mode with the hardware TWI. Thus when using $forcesofti2c, you can not add an additional TWI channel.





$forcesofti2c ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
$lib "i2c.lbx" ' override the normal xmega i2c lib



Then you need to configure the SDA and SCL Pin and initialize the pins:



Config Scl = Port0 .1 ' Pin to use as SCL (The hardware pin is Pinb.1)
Config Sda = Port0 .0 ' Pin to use as SDA (The hardware pin is Pinb.0)
I2cinit ' Bring the Pin's in the proper state








See also

Using the I2C protocol







Example



' Using ATXMEGA with software I2C routines to use also pins which are no hardware SDA/SCL pins
' Needed Library: $lib "i2c.lbx"
' The $forcesofti2c directive force the ATXMEGA to use software I2c/TWI Library

' The hardware for this example is XMEGA-A3BU XPlained board from Atmel
' Don't forget the pull-ups on SDA/SCL pin !
' Bascom Version 2.0.7.6 or higher needed

Code:
$regfile = "XM256A3BUDEF.DAT"
$crystal = 32000000                                         '32MHz
$hwstack = 64
$swstack = 40
$framesize = 80
 
$forcesofti2c                                               ' with this the software I2C/TWI commands are used when inlcuding i2c.lbx
$lib "i2c.lbx"                                             ' override the normal xmega i2c lib
 
Config Osc = Enabled , 32mhzosc = Enabled
Config Sysclock = 32mhz , Prescalea = 1 , Prescalebc = 1_1
 
Config Portr.0 = Output
Led0 Alias Portr.0                                         'LED 0 (XMEGA-A3BU XPlained board from Atmel )
 
Config Portr.1 = Output
Led1 Alias Portr.1                                         'LED 1 (XMEGA-A3BU XPlained board from Atmel )
 
Dim B As Byte
 
'We use here  Virtual port 0
Config Vport0 = B                                           ' 'map portB to virtual port0
Config Scl = Port0 .1                                       ' Pin to use as SCL (The hardware pin is Pinb.1)
Config Sda = Port0 .0                                       ' Pin to use as SDA (The hardware pin is Pinb.0)
I2cinit                                                     ' Bring the Pin's in the proper state
 
 
Do
 
Waitms 500
Set Led1
Reset Led0
Waitms 500
Reset Led1
Set Led0
 
Incr B
 
I2cstart
I2cwbyte &H24                                             ' address of I2C Slave
I2cwbyte B                                                 ' databyte to send to slave
I2cstop
 
Loop
 
End                                                         'end program
 
 

_________________
Mark
Back to top
View user's profile Visit poster's website
smatjaz

Bascom Member



Joined: 28 Jun 2004
Posts: 62

slovenia.gif
PostPosted: Thu Nov 27, 2014 6:53 pm    Post subject: Reply with quote

Thank you MWS, Thank you Mark.

Software I2c works now, when I remove

Code:
$lib "xmega.lib"


Another question:

How will operate other features XMEGA without a library?

Best regards. M.

P.S. XMEGA is full of secrets.

Very Happy
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 1827

blank.gif
PostPosted: Thu Nov 27, 2014 7:04 pm    Post subject: Reply with quote

smatjaz wrote:
How will operate other features XMEGA without a library?

Normal.

Afaik the xmega.lib was a workaround for some cliffs in the beginning and in the meantime should be not necessary anymore.
I may be wrong, Mark surly can tell better.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5089
Location: Holland

blank.gif
PostPosted: Thu Nov 27, 2014 8:49 pm    Post subject: Reply with quote

xmega.lib is still used but you do not need to specify it. it is included when the processor is an xmega.
but the order of the libs is important. when you add $lib you want this lib to be processed first. So the compiler adds xmega as last. This way the code from the $lib specified library is used first and the code from xmega is ignored.

_________________
Mark
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum