Forum: PC-Programmierung Probleme mit i2c


von Programist (Gast)


Lesenswert?

Hat jemand Erfahrung mit JAL compiler?
Ich habe ein Programm damit gemacht, mit DS1307, aber die zeigt nur 00 
an.
Mit software i2c geht's aber.Schon seltsam.

Das ist die library die nicht geht, obwohl sie eigenlich müsste.
Ich habe sie aus i2c_hardware für Jalv2.4 adaptiert für JAL 0.4.60
1
-- I2C hardware implementation  pic18f
2
-- at 20 Mhz crystal and 100kHz bus speed  master mode
3
4
procedure _i2c_init is
5
6
   SSPADD =  0x31 -- 0b_0011_0001        -- clock = 20/(4*(12+1)) = 385kHz   --  49 for 100kHz
7
   SSPCON1 = 0b_0000_1000       -- master mode, clock=Fosc/(4*(SSPADD+1))
8
   SSPCON1_SSPEN = high        -- MSSP-Modul enable
9
   SSPCON2 = 0b_0010_0000       -- sets default acknowledge bit value
10
--   SSPSTAT =  0b_0000_0000     -- enable slew rate control
11
end procedure
12
13
14
-- output a start condition  in Master-Mode
15
procedure i2c_start is
16
17
--    PIR1_SSPIF = low     -- SSPIF Bit löschen
18
    SSPCON2_SEN = high   -- Bus Übernahme anweisen
19
    while  SSPCON2_SEN == high  loop
20
    end loop
21
end procedure
22
23
-- i2c_restart - Sends Restart bit and waits untill finished
24
procedure i2c_restart is
25
   SSPCON2_RSEN = high
26
   while SSPCON2_RSEN == high loop
27
   end loop
28
end procedure
29
30
31
-- output stop condition
32
procedure i2c_stop is
33
34
  SSPCON2_PEN = high    -- initiate I2C bus stop condition
35
  while SSPCON2_PEN == high loop
36
  end loop
37
38
end procedure  
39
40
-- init the I2C
41
_i2c_init
42
i2c_stop
43
44
45
-- output an ack condition
46
procedure i2c_put_ack is
47
  SSPCON2_ACKDT = low
48
  SSPCON2_ACKEN = high     -- ACK senden
49
  while  SSPCON2_ACKEN == high  loop
50
    end loop
51
  SSPCON2_RCEN  = high     -- generate receive condition
52
end procedure
53
54
-- output a nack condition
55
procedure i2c_put_nack is
56
    SSPCON2_ACKDT = high
57
    SSPCON2_ACKEN = high
58
end procedure  
59
60
-- output a nack condition and stop
61
procedure i2c_put_nack_stop is
62
   i2c_put_nack
63
   i2c_stop
64
end procedure  
65
66
-- output an ack condition and stop
67
procedure i2c_put_ack_stop is
68
   i2c_put_ack
69
   i2c_stop
70
end procedure  
71
72
73
-- i2c_transmit_byte
74
function i2c_send_byte(byte in data) return bit is
75
76
   PIR1_SSPIF = low    -- clear pending flag
77
   SSPBUF = data       -- write data
78
79
   -- wait untill write is finished
80
   while PIR1_SSPIF == low loop
81
   end loop
82
83
   -- get Acknowledge Status Bit _ACKSTAT
84
   -- wich indicates that the slave has responded (or not)
85
   -- if i2c device send an Aknowledge, then ready and OK
86
   if SSPCON2_ACKSTAT == low  then
87
      return true   -- okay
88
   else
89
      SSPCON1_SSPEN = low
90
      SSPCON1_SSPEN = high
91
92
      return false  -- no response
93
   end if
94
end function
95
96
97
-- i2c_receive_byte
98
--
99
-- The ACK flag indicated if the byte read must be acked.
100
-- In general, all bytes read by the master are acked, except the last one.
101
-- Failing to NACK the last byte read will give unpredictable results.
102
--
103
-- start receiving of a byte and waits till finished
104
-- if param ACK is true, the byte is acknowledged and next bytes can be received.
105
-- if param ACK is false, the byte is nacked and a stop *should be* sent.
106
-- note: this behavior is inverted from the line level and orignal library, but
107
--       consistent with the i2c_software library.
108
--
109
function i2c_get_byte(bit in ACK ) return byte is
110
   var byte data
111
   -- start receive cyclus and wait till full byte received
112
   SSPCON2_RCEN = high
113
   -- delay_100us(1)
114
   while SSPSTAT_BF == low loop
115
   end loop
116
117
   -- send Acknowledge (=low) if STOP=true=high
118
   -- and wait till finishd
119
--   SSPCON2_ACKDT = ACK
120
   SSPCON2_ACKDT = ! ACK
121
   SSPCON2_ACKEN = high
122
   while SSPCON2_ACKEN == high loop
123
   end loop
124
125
   -- get read data
126
   data = SSPBUF
127
128
   return data
129
end function
130
131
-- -----------------------------------------------------------------------------
132
-- start receiving of a byte and waits till finished
133
-- if stop is set, an ACK and STOP wil be generated
134
-- -----------------------------------------------------------------------------
135
procedure I2C_get_raw_byte (byte out data) is
136
-- procedure _I2C_receive_byte (byte out data) is
137
  -- start receive cyclus and wait till full byte received
138
  SSPCON2_RCEN = high
139
  while SSPSTAT_BF == low loop
140
  end loop
141
142
  -- send Acknowledge (=low) if STOP=true=high
143
  -- and wait till finishd
144
  SSPCON2_ACKDT = low
145
  SSPCON2_ACKEN = high
146
  while SSPCON2_ACKEN == high loop
147
  end loop
148
  -- get read data
149
  data = SSPBUF
150
end procedure

Und das ist die implementation
1
Var bit r
2
3
procedure ds1307_read ( byte in ad , byte out d )is
4
5
   i2c_start
6
   r = i2c_send_byte ( 0xD0 )   -- Chip ID in write mode
7
   r = r & i2c_send_byte ( ad )    -- put register address  
8
--   i2c_restart
9
   i2c_stop
10
   i2c_start
11
   r = r & i2c_send_byte ( 0xD1 )    -- Chip ID in read mode
12
   d = i2c_get_byte (false)        -- get data
13
   i2c_stop
14
-- bcd conversion
15
   var byte loc
16
   loc = d
17
   loc = loc & 0xF0
18
   loc = loc / 16
19
   d = d & 0x0F
20
   loc = loc * 10 + d
21
   d = loc
22
end procedure
23
24
procedure ds1307_write ( byte in ad , byte in d ) is
25
26
-- bcd conversion
27
   var byte loc
28
   loc = d
29
   loc = loc / 10
30
   loc = loc * 16
31
   d = d % 10
32
   loc = loc + d
33
   d = loc
34
35
   i2c_start
36
   r = i2c_send_byte ( 0xD0 )    -- Chip ID in write mode
37
   r = r & i2c_send_byte ( ad )    -- put register address  
38
   r = r & i2c_send_byte ( d )     -- put data in the register
39
   i2c_stop
40
end procedure
41
42
procedure display_time is
43
    var byte hr, min, sec, day
44
45
    hd44780_position1 ( 3 )
46
    ds1307_read ( 0x02 , hr )
47
    print_decimal_2 ( hd44780 , hr , "0" )
48
    hd44780 = ":"
49
    ds1307_read ( 0x01 , min )
50
    print_decimal_2 ( hd44780 , min , "0" )
51
    hd44780 = ":"
52
    ds1307_read ( 0x00 , sec )
53
    print_decimal_2 ( hd44780 , sec , "0" )
54
    hd44780 = "/"
55
    ds1307_read ( 0x03 , day )
56
    print_decimal_1 ( hd44780 , day , "0" )
57
end procedure

von Programist (Gast)


Angehängte Dateien:

Lesenswert?

Hier habe Ich noch die Library mit der es geht.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.