PDA

View Full Version : I2C problem (16F877A with DS1844)



lissajous
- 7th June 2007, 12:37
Hi!

I am working on a digital amplifier project with DS1844 digipot but the I2C communication don't work.
I use PIC16F877A, SCL is PORTC.3 and SDA PORTC.4 with 10k pull-up resistors. (also I tried with 4,7k)
The control byte is 01010000, A0, A1, A2=0; PS=1

The program:

TRISC = %11100111 'bemenetek beallitasa
TRISD = %00001111
TRISA = 0
TRISB = 0
SDA var PORTC.4
SCL var PORTC.3

DEFINE OSC 4
DEFINE I2C_HOLD 1

ADCON0=0
ADCON1=7 'ADC kikapcsolva
CMCON=7 'komparator ki
SSPCON=0

Define LCD_DREG PORTB 'LCD definialasa
.
.
Define LCD_LINES 2 '2 soros
pause 500

I2CWRITE SDA, SCL, %01010000, 0, [%00011111, %01011111, %10011111, %11011111]
pause 100

I was tried it in many ways, but not work.

excuse for my english
thanks
lissajous from Hungary

breesy
- 7th June 2007, 12:42
Im not very familiar with I2C, however I remember seeing this:

http://www.picbasic.co.uk/forum/showthread.php?t=587&highlight=i2cwrite

See the first point, 'Do you have any CONSTANTS in the device address?'

lissajous
- 7th June 2007, 13:36
Thanks for the post

I correct them, but it still doesn't work.

skimask
- 7th June 2007, 15:00
Thanks for the post

I correct them, but it still doesn't work.

I don't read your language, but I get the idea of the program (I think)...a few problems noted, no enable in the program for the interrupts, moved some blocks around, did some re-writing of the code...check it out.



DEFINE OSC 4
DEFINE I2C_HOLD 1
Define LCD_DREG PORTB 'LCD definialasa
Define LCD_DBIT 4 'kezdo adatbit
Define LCD_RSREG PORTB 'PORTB.3:RSbit
Define LCD_RSBIT 3
Define LCD_EREG PORTB 'Ebit:PORTB.2
Define LCD_EBIT 2
Define LCD_BITS 4 'LCD bus meret
Define LCD_LINES 2 '2 soros

Buttontemp var Byte:h var Byte:b var byte:c var byte:t var byte:m1 var byte:m2 var byte:m3 var byte:m4 var byte:mb1 var byte:mb2 var byte:mt1 var byte
mt2 var byte:d1 var byte:I2Cdev var byte:SDA var PORTC.4:SCL var PORTC.3:I2Caddr var word

goto skip_on_int

On interrupt goto delay_5s 'megszakitasi rutin

disable 'megszakitas felfüggesztese
delay_5s: d1=d1+1:INTCON.2=0
if d1>77 then '5s=5000ms=65ms*77
d1=0:INTCON=0 : goto kiirhe 'hangero kiirasa
endif 'ha nem,vissza
resume

enable

skip_on_int:

m1=0:m2=$40:m3=$80:m4=$c0:TRISC=$e7:TRISD=$f:TRISA =0:TRISB=0:I2Cdev=$50:I2Caddr=0:h=0:b=0:c=31:t=31: PORTA=63:high portd.4:ADCON0=0:ADCON1=7:CMCON=7:SSPCON=0
pause 500:lcdout $fe,1:pause 100:lcdout " Udvozollek":pause 200:I2CWRITE SDA,SCL,I2Cdev,I2Caddr,[$1f,$5f,$9f,$df]:pause 100:OPTION_REG=7:INTCON=$a0

'*************************** A foprogram *****************************

main: button PORTC.1,0,0,0,buttontemp,1,novelhe:button PORTC.6,0,0,0,buttontemp,1,csokkenthe:button PortC.5,0,0,0,buttontemp,1,bemnov
button PORTd.3,0,0,0,buttontemp,1,bemcsokk:button PORTC.0,0,0,0,buttontemp,1,bassn:button PORTD.1,0,0,0,buttontemp,1,basscs
button PORTc.7,0,0,0,buttontemp,1,tren:button PORTd.2,0,0,0,buttontemp,1,trecs:Goto main

'****************** A hangero-szabalyozo rutinjai ********************

novelhe: if h<63 THEN h = h + 1

if ( h < 16 ) OR ( h > 31 and h < 48 ) then
portA = h + 16 : goto kiirhe
endif
if ( h > 15 and h < 32 ) OR ( h > 47 and h < 64) then
porta = h - 16 : goto kiirhe
endif

Goto kiirhe

csokkenthe: if h>0 then h= h - 1

if ( h < 16 ) OR ( h > 31 and h < 48 ) then
portA = h + 16 : goto kiirhe
endif
if ( h > 15 and h < 32 ) OR ( h > 47 and h < 64) then
porta = h - 16 : goto kiirhe
endif

Goto kiirhe

'******************** A hangszinszabalyozo rutinjai ******************

bassn: if c<63 THEN c = c + 2
mb1=c & m1:mb2=c & m4:I2CWRITE SDA,SCL,I2Cdev,I2Caddr,[mb1,mb2]:pause 200:goto kiirmely

basscs: if c>1 then c= c - 2
mb1 = c & m1:mb2 = c & m4:I2CWRITE SDA,SCL,I2Cdev,I2Caddr,[mb1,mb2]:pause 200:goto kiirmely

tren: if t<63 THEN t = t + 2
mt1 = t & m2:mt2 = t & m3:I2CWRITE SDA,SCL,I2Cdev,I2Caddr,[mt1,mt2]:Pause 200:goto kiirmely

trecs: if t>1 THEN t = t - 2
mt1 = t & m2:mt2 = t & m3:I2CWRITE SDA,SCL,I2Cdev,I2Caddr,[mt1,mt2]:Pause 200:goto kiirmely

'************************ A hangero cimkei ***************************
'replaced as shown above

'************************ A hangero kiiratasa ************************

kiirhe: lcdout $fe,1:pause 100:lcdout " Hangero: ",#h:pause 250:b=b+1:lcdout $fe,$c0:pause 100:lcdout " Bemenet: ",#b:pause 250:b=b-1:goto main

'*********************** A dB-ertekek kiiratasa **********************

kiirmely: mb1=0:mb2=0

LOOKUP c,[0,20,0,15,0,9,0,8,0,7,0,6,0,5,0,4,0,4,0,3,0,2,0,2, 0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,2,0,2,0,3,0,4,0,4,0, 5,0,6,0,7,0,8,0,9,0,15,0,20,0,20],mb1

LOOKUP c,[0,0,0,0,0,8,0,4,0,3,0,3,0,5,0,6,0,0,0,4,0,8,0,2,0, 6,0,0,0,5,0,0,0,5,0,0,0,6,0,2,0,8,0,4,0,0,0,6,0,5, 0,3,0,3,0,4,0,8,0,0,0,0,0,0],mb2

if c<32 then kiirmep 'pozitiv ertek eseten
if c>32 then kiirmen 'negativ eseten

kiirmen: lcdout $fe,1:pause 100:lcdout " Mely: ","-",#mb1,",",#mb2,"dB":goto kiirmag

kiirmep: lcdout $fe,1:pause 100:lcdout " Mely: "," ",#mb1,",",#mb2,"dB":goto kiirmag

kiirmag: pause 250 : mb1=0:mb2=0

LOOKUP t,[0,20,0,15,0,9,0,8,0,7,0,6,0,5,0,4,0,4,0,3,0,2,0,2, 0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,2,0,2,0,3,0,4,0,4,0, 5,0,6,0,7,0,8,0,9,0,15,0,20,0,20],mb1

LOOKUP t,[0,0,0,0,0,8,0,4,0,3,0,3,0,5,0,6,0,0,0,4,0,8,0,2,0, 6,0,0,0,5,0,0,0,5,0,0,0,6,0,2,0,8,0,4,0,0,0,6,0,5, 0,3,0,3,0,4,0,8,0,0,0,0,0,0],mb2

if t<32 then kiirmap
if t>32 then kiirman

kiirman: lcdout $fe,$c0:pause 100:lcdout " Magas: ","-",#mb1,",",#mb2,"dB":pause 250:TMR0=0:d1=0:INTCON=%10100000:goto main

kiirmap: lcdout $fe,$c0:pause 100:lcdout " Magas: "," ",#mb1,",",#mb2,"dB":pause 250:TMR0=0:d1=0:INTCON=%10100000:goto main

'********************* A bemenetvalaszto rutinjai ********************

bemnov: if b<3 then b=b+1
goto new1

bemcsokk: if b>0 then b=b-1
goto new1

new1: low portd.7:low portd.6:low portd.5:low portd.4 : high portd.0[ bem + 4 ] : goto kirrhe

'********************** 5 masodperces delay rutin *******************
'moved to near start of program
End

lissajous
- 7th June 2007, 15:47
Thank you very much

But the "I2CWRITE" still don't work, the potentiometer value not change.

skimask
- 7th June 2007, 15:57
Thank you very much

But the "I2CWRITE" still don't work, the potentiometer value not change.

Is this the first time you've tried using this particular IC?
I've said it before, I'll say it again, start simple, work your way up...
How do you really know that the value is supposed to change? How do you really know that your interrupts are being handled the way you want them to be handled?

Replace the I2CWRITE commands with writes to the LCD, make sure you're logic is working, don't worry about what the chip does, just watch your numbers, see what they do first. Rewrite your program to write various values to the chip and the LCD and make sure the chip follows what you really want to do. Then integrate everything together. For all you know, there's something fundamentally wrong with the way you wrote the program.

lissajous
- 7th June 2007, 16:42
Is this the first time you've tried using this particular IC?
I've said it before, I'll say it again, start simple, work your way up...
How do you really know that the value is supposed to change? How do you really know that your interrupts are being handled the way you want them to be handled?

Replace the I2CWRITE commands with writes to the LCD, make sure you're logic is working, don't worry about what the chip does, just watch your numbers, see what they do first. Rewrite your program to write various values to the chip and the LCD and make sure the chip follows what you really want to do. Then integrate everything together. For all you know, there's something fundamentally wrong with the way you wrote the program.

Yes, it is the first time with this IC.

The hardware is ready , everything function functionate , excepting the I2C.
The LCD writes the right value, the interrupt also well functionate.

The trouble is that between the potentiometer outputs not mensurable the tuned worth. When there is off , here mensurable the 100k, run during wrongly the value.

excuse for my english
thanks

skimask
- 7th June 2007, 17:37
Try this out. If it works, it's up to you to figure out what's different...

(and your English is fine, not perfect, but fine...and certainly a lot better than my ___________(<-insert language here).)

DEFINE OSC 4
DEFINE I2C_HOLD 1
Define LCD_DREG PORTB 'LCD definialasa
Define LCD_DBIT 4 'kezdo adatbit
Define LCD_RSREG PORTB 'PORTB.3:RSbit
Define LCD_RSBIT 3
Define LCD_EREG PORTB 'Ebit:PORTB.2
Define LCD_EBIT 2
Define LCD_BITS 4 'LCD bus meret
Define LCD_LINES 2 '2 soros
buttontemp var byte : h var byte : b var byte : c var byte : t var byte : m1 var byte : m2 var byte : m3 var byte : m4 var byte : mb1 var byte : mb2 var byte
mt1 var byte : mt2 var byte : d1 var byte : i2cdev var byte : sda var portc.4 : scl var portc.3 : goto skip_on_int
On interrupt goto delay_5s 'megszakitasi rutin
disable 'megszakitas felfüggesztese
delay_5s: d1=d1+1 : INTCON.2=0
if d1>77 then '5s=5000ms=65ms*77
d1=0 : INTCON=0 : goto kiirhe 'hangero kiirasa
endif 'ha nem,vissza
resume
enable
skip_on_int:
m1=0 : m2=$40 : m3=$80 : m4=$c0 : trisc=$e7 : trisd=$f : trisa=0 : trisb=0 : i2cdev=$50 : h=0 : b=0 : c=31 : t=31 : porta=63 : high portd.4 : adcon0=0
adcon1=7 : cmcon=7 : sspcon=0 : pause 500 : lcdout $fe,1," Udvozollek" : pause 200 : i2cwrite sda,scl,i2cdev,[$1f,$5f,$9f,$df] : pause 100 : option_reg=7
intcon=$a0
main: button PORTC.1,0,0,0,buttontemp,1,novelhe : button PORTC.6,0,0,0,buttontemp,1,csokkenthe : button PortC.5,0,0,0,buttontemp,1,bemnov
button PORTd.3,0,0,0,buttontemp,1,bemcsokk : button PORTC.0,0,0,0,buttontemp,1,bassn : button PORTD.1,0,0,0,buttontemp,1,basscs
button PORTc.7,0,0,0,buttontemp,1,tren : button PORTd.2,0,0,0,buttontemp,1,trecs : Goto main
novelhe: if h<63 THEN h = h + 1
if ( h < 16 ) OR ( h > 31 and h < 48 ) then
portA = h + 16 : goto kiirhe
endif
if ( h > 15 and h < 32 ) OR ( h > 47 and h < 64) then
porta = h - 16 : goto kiirhe
endif
Goto kiirhe
csokkenthe: if h>0 then h= h - 1
if ( h < 16 ) OR ( h > 31 and h < 48 ) then
portA = h + 16 : goto kiirhe
endif
if ( h > 15 and h < 32 ) OR ( h > 47 and h < 64) then
porta = h - 16 : goto kiirhe
endif
Goto kiirhe
bassn: if c<63 THEN c = c + 2
mb1=c & m1 : mb2=c & m4 : i2cwrite sda,scl,,i2cdev,[mb1,mb2] : pause 200 : goto kiirmely
basscs: if c>1 then c= c - 2
mb1=c & m1 : mb2=c & m4 : i2cwrite sda,scl,i2cdev,[mb1,mb2] : pause 200 : goto kiirmely
tren: if t<63 THEN t = t + 2
mt1=t & m2 : mt2=t & m3 : i2cwrite sda,scl,i2cdev,[mt1,mt2] : Pause 200 : goto kiirmely
trecs: if t>1 THEN t = t - 2
mt1=t & m2 : mt2=t & m3 : i2cwrite sda,scl,i2cdev,[mt1,mt2] : Pause 200 : goto kiirmely
kiirhe: lcdout $fe,1," Hangero: ",#h : pause 250 : b=b+1 : lcdout $fe,$c0," Bemenet: ",#b : pause 250 : b=b-1 : goto main
kiirmely: mb1=0 : mb2=0
LOOKUP c,[0,20,0,15,0,9,0,8,0,7,0,6,0,5,0,4,0,4,0,3,0,2,0,2, 0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,2,0,2,0,3,0,4,0,4,0, 5,0,6,0,7,0,8,0,9,0,15,0,20,0,20],mb1
LOOKUP c,[0,0,0,0,0,8,0,4,0,3,0,3,0,5,0,6,0,0,0,4,0,8,0,2,0, 6,0,0,0,5,0,0,0,5,0,0,0,6,0,2,0,8,0,4,0,0,0,6,0,5, 0,3,0,3,0,4,0,8,0,0,0,0,0,0],mb2
if c<32 then kiirmep 'pozitiv ertek eseten
if c>32 then kiirmen 'negativ eseten
kiirmen: lcdout $fe,1," Mely: ","-",#mb1,",",#mb2,"dB":goto kiirmag
kiirmep: lcdout $fe,1," Mely: "," ",#mb1,",",#mb2,"dB":goto kiirmag
kiirmag: pause 250 : mb1=0 : mb2=0
LOOKUP t,[0,20,0,15,0,9,0,8,0,7,0,6,0,5,0,4,0,4,0,3,0,2,0,2, 0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,2,0,2,0,3,0,4,0,4,0, 5,0,6,0,7,0,8,0,9,0,15,0,20,0,20],mb1
LOOKUP t,[0,0,0,0,0,8,0,4,0,3,0,3,0,5,0,6,0,0,0,4,0,8,0,2,0, 6,0,0,0,5,0,0,0,5,0,0,0,6,0,2,0,8,0,4,0,0,0,6,0,5, 0,3,0,3,0,4,0,8,0,0,0,0,0,0],mb2
if t<32 then kiirmap
if t>32 then kiirman
kiirman: lcdout $fe,$c0," Magas: ","-",#mb1,",",#mb2,"dB" : pause 250 : TMR0=0 : d1=0 : INTCON=%10100000 : goto main
kiirmap: lcdout $fe,$c0," Magas: "," ",#mb1,",",#mb2,"dB" : pause 250 : TMR0=0 : d1=0 : INTCON=%10100000 : goto main
bemnov: if b<3 then b=b+1
goto new1
bemcsokk: if b>0 then b=b-1
goto new1
new1: low portd.7 : low portd.6 : low portd.5 : low portd.4 : high portd.0[bem+4] : goto kirrhe
End

lissajous
- 7th June 2007, 19:56
Thanks for the post

Unfortunately that way nor functionate. Already I've tried with one other DS1844 also , but nothing change. Already everything run-through on several occasions , but nothing make mistake not I had found.

BobK
- 9th June 2007, 11:40
Hello,

Do you have 4.7K pull up resistors on SCL and SDA?

BobK

T_J_W
- 9th June 2007, 12:59
You may have a trace problem or breadboard problem. Check with a meter.

Tim

lissajous
- 9th June 2007, 15:18
Thanks

Yes, I have 4,7k pull-up resistors

Already I've tried check, but I hadn't found nothing mistake.
Yet I will to try