Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

I2C problem with 90s2313 and DS1621 thermometer

Status
Not open for further replies.

pkedvessy

Member level 5
Joined
Feb 17, 2005
Messages
81
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,286
Activity points
2,077
Hy!

I connect a DS1621 thermometer to 90s2313 with this little code, developed in BASCOM-AVR:


$crystal = 4000000
Config Portb = Output
Config Sda = Portd.6
Config Scl = Portd.5
Config I2cdelay = 10
I2cinit

Dim B1 As Byte
Dim B2 As Byte
Portb = 0
Do
I2cstart
I2cwbyte &H90
I2cwbyte &HAA
I2cstart
I2cwbyte &H91
I2crbyte B1 , Ack
I2crbyte B2 , Nack
I2cstop
Portb = B1
Waitms 200
Loop
End

I hope, this code read out the temperature from the DS1621 and put it's first byte to the PortB, but it doesnt work! Anyone can halp me what's the problem???
 

Before sending &HAA command you must write the configuration register (command &HAC) according with either continuous or one shot mode conversion.
Then Start Convert T command (&HEE) must be issued in order to start temperature conversion.
You must wait max. 750 ms (according with data sheet specs) required by temperature conversion time or you can poll the MSB of configuration register until goes from "0" meaning conversion in progress to "1" - conversion complete.
Then you can read the conversion results sending &HAA Read temperature.
Note that the DS1621 stores the temperature in two's complement.
 

I tried to make some changes in the code! Now should it work?

$crystal = 4000000
Config Portb = Output
Config Sda = Portd.6
Config Scl = Portd.5
Config I2cdelay = 10
I2cinit

Dim B1 As Byte
Dim B2 As Byte
Portb = 0

I2cstart 'Write config byte
I2cwbyte &H90
I2cwbyte &HAC
I2cwbyte 8
I2cstop

I2cstart 'Start temperature conversion
I2cwbyte &H90
I2cwbyte &HEE
I2cstop

Do

I2cstart 'Read te temperature
I2cwbyte &H90
I2cwbyte &HAA
I2cstart
I2cwbyte &H91
I2crbyte B1 , Ack 'The temperature's first byte
I2crbyte B2 , Nack 'The temperature's second byte
I2cstop

Portb = B1 'Write the firs byte to the portB
Waitms 200
Portb = B2 'Write the second byte to the portB
Waitms 200
Loop
End
 

It should work.
However, after issuing command (&HEE) you must wait max. 750 ms for complete conversion.
You must insert this immediately after do statement at the loop begining, before proceeding to read command (&HAA).
You can remove write of variable B2 to portb since it's useless (10000000 into B2, means you must add 0,5 celsius degrees to the B1 variable; only MSB of B2 variable it's set).
You can remove both 200ms delays, because you have the big one (750ms) at the begining of the loop.
 

Now I'm at home, and I tried the last code, what I wrote here, and something still wrong! I put a led display to the portb and edited the code to this, to see, where is the problem!

$crystal = 4000000
Config Portb = Output
Config Sda = Portd.6
Config Scl = Portd.5
Config I2cdelay = 10
I2cinit

Dim B1 As Byte
Dim B2 As Byte
Portb = 255
I2cstart 'Write config byte
Portb = &B10100000 '0
I2cwbyte &H90
Portb = &B11100111 '1
I2cwbyte &HAC
Portb = &B00110001 '2
I2cwbyte 8
Portb = &B10100001 '3
Waitms 100
I2cstop
Portb = &B11100100 '4
I2cstart 'Start temperature conversion
Portb = &B10101000 '5
I2cwbyte &H90
Portb = &B00101000 '6
I2cwbyte &HEE
Portb = &B11100011 '7
I2cstop
Portb = &B00100000 '8
Do

I2cstart 'Read te temperature
Portb = &B10100000 '9
I2cwbyte &H90
Portb = &B00000010 '0.
I2cwbyte &HAA
Portb = &B11000111 '1.
I2cstart
Portb = &B00010001 '2.
I2cwbyte &H91
Portb = &B10000001 '3.
I2crbyte B1 , Ack 'The temperature's first byte
Portb = &B11000100 '4.
I2crbyte B2 , Nack 'The temperature's second byte
Portb = &B10001000 '5.
I2cstop
Portb = &B00001000 '6.

Loop
End

I found that the chip is freezen after command I2cwbyte &H90 in the Do cyrcle, and I see "9" on the led display! Do you now what's the problem??? If you now the problem, please edit my code! Thx
 

Try to add:


Do

Waitms 750 'insert this delay
I2cstart 'Read the temperature

If you see "9" on LED display, means you stop before not after command I2cwbyte &H90.
I believe that after starting conversion the DS1621 will refuse any commands until first conversion is complete.
Use a DVM and measure the voltage on portd6 when the micro is frozen.
 

I inserted the line, what you wrote!
It's funny! If I touch the Pover 5V line with my hand, its work! But just, if I touch it :) Why is it???

Added after 11 minutes:

Oh! I forgot, that I removed the thermometer chip! So, if it removed, the program is running, but just if I touch the Pover 5V line :-D. If the thermometer is in the circuit, the program is frozzen at "9" (with your waitms 750 line), and I have 0v on portd6
 

Oh!
I apologize. portd5 should be measured.
Look in the disassembled code at offset 00B0, label avr00B0. Just inside i2cwbyte subroutine.
It's there a bit test. You'll perform the jump avr00B0 as long as SCK line is low.
To go further, the micro which is I2C master is expecting that line to be high not pull down by slave or whatever else.
There your micro froze.

Are you sure that you fit the pull up resistor on portd6 and portd5 ?
Look at label sub0081.
The internal MOS pullup of pins portd6 and portd5 are disabled.
Is your micro power supply pins decoupled with ceramic capacitor as close as possible ?

Code:
;-------------------------------------------------------------------------
		.cseg
		.org 0
;-------------------------------------------------------------------------
		rjmp	avr000B		; 0000 C00A
		reti				; 0001 9518
;-------------------------------------------------------------------------
		reti				; 0002 9518
;-------------------------------------------------------------------------
		reti				; 0003 9518
;-------------------------------------------------------------------------
		reti				; 0004 9518
;-------------------------------------------------------------------------
		reti				; 0005 9518
;-------------------------------------------------------------------------
		reti				; 0006 9518
;-------------------------------------------------------------------------
		reti				; 0007 9518
;-------------------------------------------------------------------------
		reti				; 0008 9518
;-------------------------------------------------------------------------
		reti				; 0009 9518
;-------------------------------------------------------------------------
		reti				; 000A 9518
;-------------------------------------------------------------------------
avr000B:	ldi	r24, 0x1F		; 000B E18F
		out	$21, r24		; 000C BD81
		ldi	r24, 0x17		; 000D E187
		out	$21, r24		; 000E BD81
		ldi	r24, 0xDF		; 000F ED8F
		out	SPL, r24		; 0010 BF8D
		ldi	YL, 0xC0		; 0011 ECC0
		ldi	ZL, 0xB8		; 0012 EBE8
		mov	r4, ZL		; 0013 2E4E
		clr	YH			; 0014 27DD
		mov	r5, YH		; 0015 2E5D
		ldi	ZL, 0x7E		; 0016 E7EE
		ldi	ZH, 0x00		; 0017 E0F0
		ldi	XL, 0x60		; 0018 E6A0
		ldi	XH, 0x00		; 0019 E0B0
		clr	r24			; 001A 2788
avr001B:	st	X+, r24		; 001B 938D
		sbiw	ZL, 0x01		; 001C 9731
		brne	avr001B		; 001D F7E9
		clr	r6			; 001E 2466
		ser	r24			; 001F EF8F
		out	DDRB, r24		; 0020 BB87
		rcall	sub0081		; 0021 D05F
		ser	r24			; 0022 EF8F
		out	PORTB, r24		; 0023 BB88
		rcall	sub0086		; 0024 D061
		ldi	r24, 0xA0		; 0025 EA80
		out	PORTB, r24		; 0026 BB88
		ldi	r17, 0x90		; 0027 E910
		rcall	sub009C		; 0028 D073
		ldi	r24, 0xE7		; 0029 EE87
		out	PORTB, r24		; 002A BB88
		ldi	r17, 0xAC		; 002B EA1C
		rcall	sub009C		; 002C D06F
		ldi	r24, 0x31		; 002D E381
		out	PORTB, r24		; 002E BB88
		ldi	r17, 0x08		; 002F E018
		rcall	sub009C		; 0030 D06B
		ldi	r24, 0xA1		; 0031 EA81
		out	PORTB, r24		; 0032 BB88
		ldi	r24, 0x64		; 0033 E684
		ldi	r25, 0x00		; 0034 E090
		rcall	sub0072		; 0035 D03C
		rcall	sub0095		; 0036 D05E
		ldi	r24, 0xE4		; 0037 EE84
		out	PORTB, r24		; 0038 BB88
		rcall	sub0086		; 0039 D04C
		ldi	r24, 0xA8		; 003A EA88
		out	PORTB, r24		; 003B BB88
		ldi	r17, 0x90		; 003C E910
		rcall	sub009C		; 003D D05E
		ldi	r24, 0x28		; 003E E288
		out	PORTB, r24		; 003F BB88
		ldi	r17, 0xEE		; 0040 EE1E
		rcall	sub009C		; 0041 D05A
		ldi	r24, 0xE3		; 0042 EE83
		out	PORTB, r24		; 0043 BB88
		rcall	sub0095		; 0044 D050
		ldi	r24, 0x20		; 0045 E280
		out	PORTB, r24		; 0046 BB88
avr0047:	rcall	sub0086		; 0047 D03E
		ldi	r24, 0xA0		; 0048 EA80
		out	PORTB, r24		; 0049 BB88
		ldi	r17, 0x90		; 004A E910
		rcall	sub009C		; 004B D050
		ldi	r24, 0x02		; 004C E082
		out	PORTB, r24		; 004D BB88
		ldi	r17, 0xAA		; 004E EA1A
		rcall	sub009C		; 004F D04C
		ldi	r24, 0xC7		; 0050 EC87
		out	PORTB, r24		; 0051 BB88
		rcall	sub0086		; 0052 D033
		ldi	r24, 0x11		; 0053 E181
		out	PORTB, r24		; 0054 BB88
		ldi	r17, 0x91		; 0055 E911
		rcall	sub009C		; 0056 D045
		ldi	r24, 0x81		; 0057 E881
		out	PORTB, r24		; 0058 BB88
		rcall	sub00B9		; 0059 D05F
		ldi	XL, 0x60		; 005A E6A0
		st	X, r17			; 005B 931C
		ldi	r24, 0xC4		; 005C EC84
		out	PORTB, r24		; 005D BB88
		rcall	sub00B7		; 005E D058
		ldi	XL, 0x61		; 005F E6A1
		st	X, r17			; 0060 931C
		ldi	r24, 0x88		; 0061 E888
		out	PORTB, r24		; 0062 BB88
		rcall	sub0095		; 0063 D031
		ldi	r24, 0x08		; 0064 E088
		out	PORTB, r24		; 0065 BB88
		rjmp	avr0047		; 0066 CFE0
		cli				; 0067 94F8
avr0068:	rjmp	avr0068		; 0068 CFFF
;-------------------------------------------------------------------------
sub0069:	sbiw	ZL, 0x01		; 0069 9731
		brne	sub0069		; 006A F7F1
		ret				; 006B 9508
;-------------------------------------------------------------------------
		set				; 006C 9468
		bld	r6, 2		; 006D F862
		ret				; 006E 9508
;-------------------------------------------------------------------------
		clt				; 006F 94E8
		bld	r6, 2		; 0070 F862
		ret				; 0071 9508
;-------------------------------------------------------------------------
sub0072:	push	ZL			; 0072 93EF
		push	ZH			; 0073 93FF
		clr	ZL			; 0074 27EE
		or	ZL, r24		; 0075 2BE8
		or	ZL, r25		; 0076 2BE9
		breq	avr007E		; 0077 F031
avr0078:	ldi	ZL, 0xE8		; 0078 EEE8
		ldi	ZH, 0x03		; 0079 E0F3
avr007A:	sbiw	ZL, 0x01		; 007A 9731
		brne	avr007A		; 007B F7F1
		sbiw	r24, 0x01		; 007C 9701
		brne	avr0078		; 007D F7D1
avr007E:	pop	ZH			; 007E 91FF
		pop	ZL			; 007F 91EF
		ret				; 0080 9508
;-------------------------------------------------------------------------
sub0081:	cbi	PORTD, 6	; 0081 9896
		cbi	PORTD, 5	; 0082 9895
		cbi	DDRD, 6	; 0083 988E
		cbi	DDRD, 5	; 0084 988D
		ret				; 0085 9508
;-------------------------------------------------------------------------
sub0086:	sbi	DDRD, 5	; 0086 9A8D
		cbi	DDRD, 6	; 0087 988E
		rcall	sub008D		; 0088 D004
		cbi	DDRD, 5	; 0089 988D
		rcall	sub008D		; 008A D002
		sbi	DDRD, 6	; 008B 9A8E
		rjmp	sub008D		; 008C C000
;-------------------------------------------------------------------------
sub008D:	push	ZL			; 008D 93EF
		push	ZH			; 008E 93FF
		ldi	ZL, 0x06		; 008F E0E6
		ldi	ZH, 0x00		; 0090 E0F0
		rcall	sub0069		; 0091 DFD7
		pop	ZH			; 0092 91FF
		pop	ZL			; 0093 91EF
		ret				; 0094 9508
;-------------------------------------------------------------------------
sub0095:	sbi	DDRD, 5	; 0095 9A8D
		sbi	DDRD, 6	; 0096 9A8E
		rcall	sub008D		; 0097 DFF5
		cbi	DDRD, 5	; 0098 988D
		rcall	sub008D		; 0099 DFF3
		cbi	DDRD, 6	; 009A 988E
		rjmp	sub008D		; 009B CFF1
;-------------------------------------------------------------------------
sub009C:	sec				; 009C 9408
		rol	r17			; 009D 1F11
		rjmp	avr00A0		; 009E C001
avr009F:	lsl	r17			; 009F 0F11
avr00A0:	breq	avr00AC		; 00A0 F059
		sbi	DDRD, 5	; 00A1 9A8D
		brsh	avr00A6		; 00A2 F418
		nop				; 00A3 0000
		cbi	DDRD, 6	; 00A4 988E
		rjmp	avr00A8		; 00A5 C002
avr00A6:	sbi	DDRD, 6	; 00A6 9A8E
		rjmp	avr00A8		; 00A7 C000
avr00A8:	rcall	sub008D		; 00A8 DFE4
		cbi	DDRD, 5	; 00A9 988D
		rcall	sub008D		; 00AA DFE2
		rjmp	avr009F		; 00AB CFF3
avr00AC:	sbi	DDRD, 5	; 00AC 9A8D
		cbi	DDRD, 6	; 00AD 988E
		rcall	sub008D		; 00AE DFDE
		cbi	DDRD, 5	; 00AF 988D
avr00B0:	sbis	PIND, 5	; 00B0 9B85
		rjmp	avr00B0		; 00B1 CFFE
		clt				; 00B2 94E8
		sbic	PIND, 6	; 00B3 9986
		set				; 00B4 9468
		bld	r6, 2		; 00B5 F862
		rjmp	sub008D		; 00B6 CFD6
;-------------------------------------------------------------------------
sub00B7:	sec				; 00B7 9408
		rjmp	avr00BA		; 00B8 C001
;-------------------------------------------------------------------------
sub00B9:	clc				; 00B9 9488
avr00BA:	rol	r19			; 00BA 1F33
		ldi	r17, 0x01		; 00BB E011
avr00BC:	sbi	DDRD, 5	; 00BC 9A8D
		cbi	DDRD, 6	; 00BD 988E
		rcall	sub008D		; 00BE DFCE
		cbi	DDRD, 5	; 00BF 988D
avr00C0:	sbis	PIND, 5	; 00C0 9B85
		rjmp	avr00C0		; 00C1 CFFE
		rcall	sub008D		; 00C2 DFCA
		clc				; 00C3 9488
		sbic	PIND, 6	; 00C4 9986
		sec				; 00C5 9408
		rol	r17			; 00C6 1F11
		brsh	avr00BC		; 00C7 F7A0
		sbi	DDRD, 5	; 00C8 9A8D
		ror	r19			; 00C9 9537
		brsh	avr00CD		; 00CA F410
		cbi	DDRD, 6	; 00CB 988E
		rjmp	avr00CE		; 00CC C001
avr00CD:	sbi	DDRD, 6	; 00CD 9A8E
avr00CE:	rcall	sub008D		; 00CE DFBE
		cbi	DDRD, 5	; 00CF 988D
avr00D0:	sbis	PIND, 5	; 00D0 9B85
		rjmp	avr00D0		; 00D1 CFFE
		rcall	sub008D		; 00D2 DFBA
		ret				; 00D3 9508

		.exit
 

I'm a really beginner in AVR programming, and I think I can't slove the problem!
Let's start from the begining! :)

I found this little circuit, and I tried to use it!

Is it a good schematic, or not?
 

It's a good schematics but you have to add the pull-up resistors at pins 9 and 11 of AVR toward +5v.
A value around 1k5 it's OK.
Next you must fit small capacitors from XTAL1 and XTAL2 to ground. Otherwise your micro wont start.
A value of 22-33pF it's OK.
Don't forget the 100nF capacitor fitted in parallel with pins 20 and 10 of AVR.

You can count on internal POR of AT90S2313 but if you don't, you must fit a 0,1uF capacitor from pin 1 (RESET) to ground and 10K resistor from pin 1 toward +5vcc
You can fit a reset switch in parallel with capacitor for manual reset.
 

I made the changes, what you said, so here is the new schematic! Is it finally good now?
 

0nf is 100 nf, between the gnd and the +5V!!!
 

Now should this code work???

$crystal = 4000000
Config Portb = Output
Config Sda = Portd.6
Config Scl = Portd.5
Config I2cdelay = 10
I2cinit

Dim B1 As Byte
Dim B2 As Byte
Portb = 0

I2cstart 'Write config byte
I2cwbyte &H90
I2cwbyte &HAC
I2cwbyte 8
I2cstop

I2cstart 'Start temperature conversion
I2cwbyte &H90
I2cwbyte &HEE
I2cstop

Do

I2cstart 'Read te temperature
I2cwbyte &H90
I2cwbyte &HAA
I2cstart
I2cwbyte &H91
I2crbyte B1 , Ack 'The temperature's first byte
I2crbyte B2 , Nack 'The temperature's second byte
I2cstop

Portb = B1 'Write the firs byte to the portB
Waitms 200
Portb = B2 'Write the second byte to the portB
Waitms 200
Loop
End
 

The schematics is OK now.

The only changes is inside loop.
Keep the 750ms delay at the loop beginning.
It's required in order to allow DS1621 to convert the temperature.
You don't need two 200ms delays, just because your LED display will flicker between the value ot temperature and 10000000 or 00000000 as 0.5 celsius degrees. Just keep the code above inside loop. The rest of the code seems OK.

pkedvessy said:
Now should this code work???

$crystal = 4000000
Config Portb = Output
Config Sda = Portd.6
Config Scl = Portd.5
Config I2cdelay = 10
I2cinit

Dim B1 As Byte
Dim B2 As Byte
Portb = 0

I2cstart 'Write config byte
I2cwbyte &H90
I2cwbyte &HAC
I2cwbyte 8
I2cstop

I2cstart 'Start temperature conversion
I2cwbyte &H90
I2cwbyte &HEE
I2cstop

Do

Waitms 750
I2cstart 'Read te temperature
I2cwbyte &H90
I2cwbyte &HAA
I2cstart
I2cwbyte &H91
I2crbyte B1 , Ack 'The temperature's first byte
I2crbyte B2 , Nack 'The temperature's second byte
I2cstop

Portb = B1 'Write the firs byte to the portB
'Waitms 200
'Portb = B2 Write the second byte to the portB
'Waitms 200
Loop
End
 

Is it a problemn, if I don't link the thermometer's adress pins to the GND?

Added after 30 minutes:

Heureca!!!
It's work!!!!! :-D Thanks for you very very lot help!!!
 

Hi Pkedvessy

Could you advise how to connect to 7-segments display,
So i can make one.
I am a newcomer
thx
 

this is the schematic how to connect to 7-segments display.



Added after 39 seconds:

this is the schematic how to connect to 7-segments display.






 

this is the schematic how to connect to 7-segments display.

92_1174358049.gif
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top