MikroBasic en de DS18B20

buckfast_beekeeper

Golden Member

@Bavelt: dat zijn de goedkope voetjes. De andere zijn er met gedraaide contactjes. Kosten net iets meer maar zijn een stuk betrouwbaarder.

Deze zijn ook handig. Bestaan ook in alle SOIC varianten en TQFP.

[Bericht gewijzigd door buckfast_beekeeper op 25 december 2019 12:53:30 (47%)]

Honing kan je importeren, bestuiving niet.

Die had ik inderdaad gepropbeerd (0x00).

Het loopt echter vast bij de I2C2_Wr (0x91) (zie onder).

Nu heb ik de routine wat aangepast, er werd gebruik gemaakt van de softwarematige I2C begreep ik. Ik heb dat aangepast naar gebruik van de MSSP (tweede I2C).
Maar ik doe hier dus iets niet goed.

pic basic code:

'========================================================================================
Sub Function MCP9800_Read(Dim pReg As Byte) As Word                 'Read
'========================================================================================
  I2C2_Start()                                                      'Start
  I2C2_Wr(0x90)                                                     'MCP write address
  I2C2_Wr(pReg)                                                     '
  I2C2_Repeated_start()                                             'Bus restart
  I2C2_Wr (0x91)                                                    'MCP read address
  If pReg = MCP9800_CONFIG Then                                     'Config reg. only
    Result = I2C2_Rd(1)                                             '1 byte long; rest
  Else                                                              'is 2 bytes
    Hi(Result) = I2C2_Rd(0)                                       '
    Lo(Result) = I2C2_Rd(1)                                       '
  End If                         '
  I2C2_Stop()
  result = 0                                                        'Stop
  delay_ms(5)                                                       'Wait to complete
End Sub                                                

Is gewoon hardwarematig, hoor... (softwarematig gebruik ik alleen in noodgevallen)
I2C1_xxx is de 1e i2c poort, I2C2_xxx de 2e,...
(I2C2 _xxx kan trouwens helemaal niet, de 887 heeft maar één i2c poort...)

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Ik gebruik hier een PIC16F1847, die heeft er twee.
De eerste I2c gebruik ik voor de OLED display

[Bericht gewijzigd door Bavelt op 25 december 2019 13:18:54 (32%)]

Maar hier twijfel ik over, of dat wel goed is hier

pic basic code:

I2C2_Repeated_start()                                             'Bus restart 

Bij een i2c read heb je altijd een restart nodig, om van write naar read om te schakelen.

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

En vwb de aansluitingen: het chipje kent een aantal address select pins: A0 t/m A2.

Die heb ik maar los laten hangen, die wordt toch niet gebruikt bij I2C?

Maar hier loop het dus op stuk:

pic basic code:

  I2C2_Wr (0x91)                                                    'MCP read address 

Het proces hangt; blijkbaar wacht de processor op 'iets'

Heb je het juiste slave adres? De MCP9800 bestaat in 8 uitvoeringen met verschillende adressen (om er meerdere aan te kunnen sluiten)

Waarom trouwens twee i2c bussen? Je kunt de MCP9800 toch gewoon bij het display knopen?

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Ach ja, 2 I2C bussen zou niet hoeven; de slave haeeft ieder zijn eigen adres (Ik zat nog in de flow van twee display;s met hetzelfde adres).

Het is een MCP9801 van Farnell. Ik zie dat je mbv A0, A1 en A2 je eigen slave asdres kan instellen. Ik heb ze alledrie op 0 gezet en nu wordt het adres dus 0x90. Het programma loopt nu verder.

ik krijg alleen als resultaat een temperatuur van 255 terug. Tamelijk warm... :D

Maar er gaat dus nog iets niet goed bij het tonen van de juiste waarde

pic basic code:

'==============================================================================
Program Oled
'------------------------------------------------------------------------------

const Dos8x16ext As Byte [1520]=   (
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  '
  0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x33,0x00,0x00,0x00,  ' !
  0x00,0xF3,0xF3,0x00,0x00,0xF3,0xF3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' "
  0x30,0xF3,0xF3,0x30,0x30,0xF3,0xF3,0x30,0x03,0x0F,0x0F,0x03,0x03,0x0F,0x0F,0x03,  ' #
  0x00,0x78,0xF3,0xCF,0xCF,0xCC,0x8C,0x00,0x00,0x0C,0x0C,0x3C,0x3C,0x0F,0x07,0x00,  ' $
  0x00,0x1C,0x1C,0xC0,0xF0,0x3C,0x0C,0x00,0x00,0x0C,0x0F,0x03,0x00,0x0E,0x0E,0x00,  ' %
  0x00,0xD8,0xFC,0xCC,0x7C,0x18,0x00,0x00,0x1F,0x3F,0x31,0x33,0x1F,0x3F,0x33,0x00,  ' &
  0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' '
  0x00,0x00,0x00,0xF0,0xF8,0x1C,0x04,0x00,0x00,0x00,0x00,0x0F,0x1F,0x38,0x20,0x00,  ' (
  0x00,0x04,0x1C,0xF8,0xF0,0x00,0x00,0x00,0x00,0x20,0x38,0x1F,0x0F,0x00,0x00,0x00,  ' )
  0xC0,0xCC,0xFC,0xF0,0xF0,0xFC,0xCC,0xC0,0x00,0x0C,0x0F,0x03,0x03,0x0F,0x0C,0x00,  ' *
  0x00,0xC0,0xC0,0xF8,0xF8,0xC0,0xC0,0x00,0x00,0x00,0x00,0x07,0x07,0x00,0x00,0x00,  ' +
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x7C,0x3C,0x00,0x00,0x00,  ' ,
  0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' -
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x00,0x00,0x00,  ' .
  0x00,0x00,0x00,0x80,0xE0,0x7C,0x1C,0x00,0x00,0x38,0x3E,0x07,0x01,0x00,0x00,0x00,  ' /
  0x00,0xF8,0xFC,0x0C,0x8C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x31,0x30,0x3F,0x1F,0x00,  ' 0
  0x00,0x00,0x30,0xFC,0xFC,0x00,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x30,0x30,0x00,  ' 1
  0x00,0x38,0x3C,0x0C,0xCC,0xFC,0x38,0x00,0x00,0x30,0x3C,0x3F,0x33,0x30,0x30,0x00,  ' 2
  0x00,0x0C,0x0C,0xCC,0xFC,0x3C,0x0C,0x00,0x00,0x1C,0x3C,0x30,0x33,0x3F,0x1C,0x00,  ' 3
  0x00,0x00,0xC0,0xF0,0xFC,0xFC,0x00,0x00,0x00,0x0F,0x0F,0x0C,0x3F,0x3F,0x0C,0x00,  ' 4
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x8C,0x00,0x00,0x18,0x38,0x30,0x30,0x3F,0x1F,0x00,  ' 5
  0x00,0xF0,0xF8,0x9C,0x8C,0x8C,0x00,0x00,0x00,0x1F,0x3F,0x31,0x31,0x3F,0x1F,0x00,  ' 6
  0x00,0x0C,0x0C,0x0C,0xCC,0xFC,0x3C,0x00,0x00,0x00,0x3C,0x3F,0x03,0x00,0x00,0x00,  ' 7
  0x00,0x38,0xFC,0xCC,0xCC,0xFC,0x38,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' 8
  0x00,0x78,0xFC,0xCC,0xCC,0xFC,0xF8,0x00,0x00,0x00,0x30,0x30,0x38,0x1F,0x0F,0x00,  ' 9
  0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x00,0x00,0x00,  ' :
  0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,0xC0,0x7C,0x3C,0x00,0x00,0x00,  ' ;
  0x80,0xC0,0xE0,0x70,0x38,0x18,0x08,0x00,0x00,0x01,0x03,0x07,0x0E,0x0C,0x08,0x00,  ' <
  0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x00,  ' =
  0x08,0x18,0x38,0x70,0xE0,0xC0,0x80,0x00,0x08,0x0C,0x0E,0x07,0x03,0x01,0x00,0x00,  ' >
  0x00,0x38,0x3C,0x0C,0xCC,0xFC,0x38,0x00,0x00,0x00,0x00,0x37,0x37,0x00,0x00,0x00,  ' ?
  0xF0,0xF8,0x1C,0xCC,0x4C,0x98,0xF0,0x00,0x0F,0x1F,0x38,0x33,0x32,0x33,0x19,0x00,  ' @
  0x00,0xF0,0xF8,0x1C,0x1C,0xF8,0xF0,0x00,0x00,0x3F,0x3F,0x03,0x03,0x3F,0x3F,0x00,  ' A
  0x00,0xFC,0xFC,0xCC,0xCC,0xFC,0x78,0x00,0x00,0x3F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' B
  0x00,0xF8,0xFC,0x0C,0x0C,0x3C,0x38,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3C,0x1C,0x00,  ' C
  0x00,0xFC,0xFC,0x0C,0x1C,0xF8,0xF0,0x00,0x00,0x3F,0x3F,0x30,0x38,0x1F,0x0F,0x00,  ' D
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x0C,0x00,0x00,0x3F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' E
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x0C,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,  ' F
  0x00,0xF8,0xFC,0x0C,0xCC,0xCC,0xCC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' G
  0x00,0xFC,0xFC,0xC0,0xC0,0xFC,0xFC,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' H
  0x00,0x0C,0x0C,0xFC,0xFC,0x0C,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' I
  0x00,0x00,0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x1C,0x3C,0x30,0x30,0x3F,0x1F,0x00,  ' J
  0xFC,0xFC,0xC0,0xF0,0x3C,0x0C,0x00,0x00,0x3F,0x3F,0x00,0x03,0x0F,0x3C,0x30,0x00,  ' K
  0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' L
  0xFC,0xFC,0x70,0xC0,0x70,0xFC,0xFC,0x00,0x3F,0x3F,0x00,0x01,0x00,0x3F,0x3F,0x00,  ' M
  0x00,0xFC,0xFC,0xE0,0x80,0xFC,0xFC,0x00,0x00,0x3F,0x3F,0x01,0x07,0x3F,0x3F,0x00,  ' N
  0x00,0xF8,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' O
  0x00,0xFC,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x3F,0x3F,0x03,0x03,0x03,0x01,0x00,  ' P
  0x00,0xF8,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x38,0x1E,0x3F,0x37,0x00,  ' Q
  0xFC,0xFC,0x8C,0x8C,0xFC,0xF8,0x00,0x00,0x3F,0x3F,0x01,0x03,0x0F,0x3C,0x30,0x00,  ' R
  0x00,0x78,0xFC,0xCC,0x8C,0x8C,0x18,0x00,0x00,0x18,0x31,0x31,0x33,0x3F,0x1E,0x00,  ' S
  0x00,0x0C,0x0C,0xFC,0xFC,0x0C,0x0C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' T
  0x00,0xFC,0xFC,0x00,0x00,0xFC,0xFC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' U
  0x00,0xFC,0xFC,0x00,0x00,0xFC,0xFC,0x00,0x00,0x03,0x0F,0x3C,0x3C,0x0F,0x03,0x00,  ' V
  0xFC,0xFC,0x00,0x80,0x00,0xFC,0xFC,0x00,0x3F,0x1F,0x0E,0x07,0x0E,0x1F,0x3F,0x00,  ' W
  0x00,0x1C,0x7C,0xE0,0xE0,0x7C,0x1C,0x00,0x00,0x38,0x3E,0x07,0x07,0x3E,0x38,0x00,  ' X
  0x00,0x3C,0xFC,0xC0,0xC0,0xFC,0x3C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' Y
  0x00,0x0C,0x0C,0xCC,0xFC,0x3C,0x0C,0x00,0x00,0x3C,0x3F,0x33,0x30,0x30,0x30,0x00,  ' Z
  0x00,0x00,0x00,0xFC,0xFC,0x0C,0x0C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x30,0x30,0x00,  ' [
  0x00,0x0C,0x0C,0xFC,0xFC,0x00,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x00,0x00,0x00,  ' \
  0x80,0xE0,0x78,0x1E,0x78,0xE0,0xE0,0x00,0x01,0x01,0x00,0x00,0x00,0x01,0x01,0x00,  ' ]
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' ^
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x00,  ' _
  0x00,0x07,0x0E,0x1C,0x38,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' `
  0x00,0x00,0x60,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1E,0x3F,0x33,0x33,0x3F,0x3F,0x00,  ' a
  0x00,0xFC,0xFC,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' b
  0x00,0xC0,0xE0,0x60,0x60,0x60,0x00,0x00,0x00,0x1F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' c
  0x00,0xC0,0xE0,0x60,0x60,0xFC,0xFC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x3F,0x00,  ' d
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1F,0x3F,0x32,0x32,0x33,0x33,0x00,  ' e
  0x00,0xC0,0xC0,0xF8,0xFC,0xCC,0xCC,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' f
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xE0,0x00,0x00,0xCF,0xDF,0xD8,0xD8,0xFF,0x7F,0x00,  ' g
  0x00,0xFC,0xFC,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' h
  0x00,0x00,0x60,0xEC,0xEC,0x00,0x00,0x00,0x00,0x00,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' i
  0x00,0x00,0x00,0x00,0xEC,0xEC,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xFF,0x7F,0x00,0x00,  ' j
  0xFC,0xFC,0x80,0xC0,0xE0,0x60,0x00,0x00,0x3F,0x3F,0x03,0x07,0x1E,0x38,0x30,0x00,  ' k
  0x00,0x00,0x0C,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' l
  0xC0,0xE0,0xE0,0xC0,0xE0,0xE0,0xC0,0x00,0x3F,0x3F,0x00,0x07,0x00,0x3F,0x3F,0x00,  ' m
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' n
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' o
  0x00,0xE0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0xFF,0xFF,0x30,0x30,0x3F,0x1F,0x00,  ' p
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xE0,0x00,0x00,0x1F,0x3F,0x30,0x30,0xFF,0xFF,0x00,  ' q
  0x00,0xE0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,  ' r
  0x00,0xC0,0xE0,0x60,0x60,0x60,0x60,0x00,0x00,0x31,0x33,0x33,0x36,0x3E,0x3C,0x00,  ' s
  0x00,0x60,0x60,0xF8,0xF8,0x60,0x60,0x00,0x00,0x00,0x00,0x1F,0x3F,0x30,0x30,0x00,  ' t
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x3F,0x00,  ' u
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0x03,0x0F,0x3C,0x3C,0x0F,0x03,0x00,  ' v
  0xE0,0xE0,0x00,0x80,0x00,0xE0,0xE0,0x00,0x3F,0x1F,0x0E,0x07,0x0E,0x1F,0x3F,0x00,  ' w
  0x00,0x60,0xE0,0x80,0x80,0xE0,0x60,0x00,0x00,0x30,0x3D,0x0F,0x0F,0x3D,0x30,0x00,  ' x
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0xCF,0xDF,0xD8,0xD8,0xFF,0x7F,0x00,  ' y
  0x00,0x60,0x60,0x60,0xE0,0xE0,0x60,0x00,0x00,0x30,0x3C,0x3F,0x33,0x30,0x30,0x00,  ' z
  0x80,0x80,0xC0,0xFC,0x7E,0x02,0x02,0x00,0x01,0x01,0x03,0x3F,0x7E,0x40,0x40,0x00,  ' {
  0x00,0x00,0x00,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x00,0x00,0x00,  ' |
  0x02,0x02,0x7E,0x7C,0xC0,0x80,0x80,0x00,0x40,0x40,0x7E,0x3F,0x03,0x01,0x01,0x00,  ' }
  0xC0,0x60,0xE0,0xC0,0x80,0x80,0xE0,0x00,0x01,0x00,0x00,0x01,0x01,0x01,0x00,0x00)  ' ~
'
Dim i1 as byte
Dim Teller as byte
Dim Getal as string[4]
DIm temp as byte
Dim str as string[1]

Dim MCP9800_TEMP   as byte
Dim MCP9800_CONFIG as byte
Dim MCP9800_HYST   as byte
Dim MCP9800_ALERT  as byte

'
'==================================================================================================
Sub Procedure Lcd_WrCmd(Dim pCmd As Byte)                       'Write data to display
'==================================================================================================
  while Not (i2c1_Is_Idle) wend
  i2c1_Start()
  i2c1_Wr(0x78)
  i2c1_Wr(0x80)
  i2c1_Wr(pCmd)
  i2c1_Stop()
End Sub

'==================================================================================================
Sub Procedure Lcd_WrDat(Dim pDat As Byte)                       'Write data to display
'==================================================================================================
  while Not (i2c1_Is_Idle) wend
  i2c1_Start()
  i2c1_Wr(0x78)
  i2c1_Wr(0x40)
  i2c1_Wr(pDat)
  Delay_ms(1)
  i2c1_Stop()
End Sub

'==================================================================================================
Sub Procedure Lcd_SetPos(Dim px, py as Byte)                    'Set write position on display
'==================================================================================================
  Lcd_WrCmd(0xB0 + py)
  Lcd_WrCmd(0xB0 + py)
  Lcd_WrCmd((px  >> 4) Or 0x10)
  Lcd_WrCmd(px And 0x0F)
End Sub
'
'===============================================================================
Sub Procedure Lcd_Char_8x16(Dim px, py, pchar As Byte)
'===============================================================================
  Dim lIndex As Word
  '----------------------------------------------------------------------------
  lIndex = pChar - 0x20
  lIndex = (lIndex << 4)

  Lcd_SetPos(px, py)
  Lcd_WrDat(Dos8x16ext[lIndex+0])
  Lcd_WrDat(Dos8x16ext[lIndex+1])
  Lcd_WrDat(Dos8x16ext[lIndex+2])
  Lcd_WrDat(Dos8x16ext[lIndex+3])
  Lcd_WrDat(Dos8x16ext[lIndex+4])
  Lcd_WrDat(Dos8x16ext[lIndex+5])
  Lcd_WrDat(Dos8x16ext[lIndex+6])
  Lcd_WrDat(Dos8x16ext[lIndex+7])

  Lcd_SetPos(px, py+1)
  Lcd_WrDat(Dos8x16ext[lIndex+8])
  Lcd_WrDat(Dos8x16ext[lIndex+9])
  Lcd_WrDat(Dos8x16ext[lIndex+10])
  Lcd_WrDat(Dos8x16ext[lIndex+11])
  Lcd_WrDat(Dos8x16ext[lIndex+12])
  Lcd_WrDat(Dos8x16ext[lIndex+13])
  Lcd_WrDat(Dos8x16ext[lIndex+14])
  Lcd_WrDat(Dos8x16ext[lIndex+15])
End Sub
'
'===============================================================================
Sub Procedure LCD_Fill(Dim pFill As Byte)     'Fill display with character pFill
'===============================================================================
  Dim lx, ly As Byte
  For ly = 0 To 8
  LCD_wrCmd(0xB0 + ly)
  LCD_wrCmd(0x01)
  LCD_wrCmd(0x10)
    For lx = 0 to 131
      Lcd_wrDat(pFill)
    Next lx
  Next ly
End Sub


'===============================================================================
Sub Procedure Lcd_String_8x16(Dim px, py As Byte, Dim ByRef pDisp As String)
'===============================================================================
  Dim lLen, lCnt As Byte
  '----------------------------------------------------------------------------
  lLen = Length(pDisp)
  For lCnt = 0 To (lLen-1)
    Lcd_Char_8x16(px+1, py, pDisp[lCnt])
    px = px + 8
  Next lCnt
End Sub
'===============================================================================
Sub Procedure Display_8x16()
'===============================================================================
  LCD_Fill(0x00)
  LCD_Setpos(0,0)
  Lcd_String_8x16(0,0,"First Line")
  Lcd_String_8x16(0,2,"Second Line")
  Lcd_String_8x16(0,4,"Third Line")
  Lcd_String_8x16(0,6,"Fourth Line")
  End Sub

'===============================================================================
Sub Procedure Display_Init()
'===============================================================================
  Lcd_WrCmd(0xA8)                                 'Multiplex ratio set to
  Lcd_WrCmd(0x3F)                                 '63
  Lcd_WrCmd(0xD3)                                 'Display offset RAM counter
  Lcd_WrCmd(0x00)                                 'none
  Lcd_WrCmd(0x40)                                 'Start line address
  Lcd_WrCmd(0xA1)                                 'Set segment remap rotation to left
  Lcd_WrCmd(0xC8)                                 'Common output scan direction
  Lcd_WrCmd(0xDA)                                 'Common signals pad
  Lcd_WrCmd(0x12)                                 'value
  Lcd_WrCmd(0x81)                                 'Contrast control
  Lcd_WrCmd(0xFF)                                 'value
  Lcd_WrCmd(0xA4)
  Lcd_WrCmd(0xA6)                                 'Normal display
  Lcd_WrCmd(0xD5)                                 'Clock ratio:oscillator frequency
  Lcd_WrCmd(0x80)                                 'oooo:rrrr
  Lcd_WrCmd(0x8D)
  Lcd_WrCmd(0x14)
  Lcd_WrCmd(0x00)                                 'Set lower column address
  Lcd_WrCmd(0x10)                                 'Set higher column address
  Lcd_WrCmd(0xD9)                                 '15 clocks, discharge 1 clock
  Lcd_WrCmd(0xF1)                                 'dddd:pppp
  Lcd_WrCmd(0xDB)                                 'Common output voltage
  Lcd_WrCmd(0x40)                                 'level
  Lcd_WrCmd(0x20)                                 'Addressing mode
  Lcd_WrCmd(0x02)                                 'value
  Lcd_WrCmd(0xAE)                                 'Turn OLED panel off
End Sub

'========================================================================================
Sub Function MCP9800_Read(Dim pReg As Byte) As Word                 'Read
'========================================================================================
  I2C2_Start()                                                      'Start
  I2C2_Wr(0x90)                                                     'MCP write address
  I2C2_Wr(pReg)                                                     '
  I2C2_Repeated_start()                                             'Bus restart
  I2C2_Wr (0x90)                                                    'MCP read address
  If pReg = MCP9800_CONFIG Then                                     'Config reg. only
    Result = I2C2_Rd(1)                                             '1 byte long; rest
  Else                                                              'is 2 bytes
    Hi(Result) = I2C2_Rd(0)                                       '
    Lo(Result) = I2C2_Rd(1)                                       '
  End If                         '
  I2C2_Stop()
  'result = 0                                                        'Stop
delay_ms(5)                                                       'Wait to complete
End Sub                                                             '

'========================================================================================
Sub Procedure MCP9800_Write(Dim pReg As Byte, dim lData As Word)    'Write
'========================================================================================
  I2C2_Start()                                                      'Start
  I2C2_Wr(0x90)                                                     'MCP write address
  I2C2_Wr(pReg)                                                  '
  If pReg = MCP9800_CONFIG Then                                     'Config re. only
    I2C2_Wr(lData)                                                  '1 byte long; rest
  Else                                                              'is 2 bytes
    I2C2_Wr(Hi(lData))                                           '
    I2C2_Wr(Lo(lData))                                           '
  End If                                                            '
  I2C2_Stop()                                                       'Stop
  delay_ms(5)                                                       'Wait to complete
End Sub                                                             '

'========================================================================================
Sub Procedure MCP9800_SetAlert(Dim pMin, pMax As Byte)
'========================================================================================
  MCP9800_Write(MCP9800_HYST, pMin<<8)                              'Sets min/max temp
  MCP9800_Write(MCP9800_ALERT, pMax<<8)                             '
End Sub                                                             '

'==============================================================================
main:

''==============================================================================
  ANSELB = 0                                     'All output/digital
  TRISB  = 0
  OSCCON = %01110000                             '8Mhz internal
  Delay_ms(50)
  i2c1_init(100000)                              'Init i2c port 100kHz
  i2c2_init(100000)                              'Init i2c port 100kHz
  
  MCP9800_TEMP     = 0x00                        'MCP9800 registers
  MCP9800_CONFIG   = 0x01                        '  "
  MCP9800_HYST     = 0x02                        '  "
  MCP9800_ALERT    = 0x03                        '  "

  Display_Init()
  LCD_wrCmd(0xAF)                                'Turn Oled Panel on

 LCD_Fill(0x00)                                  'Clear Screen
 LCD_Setpos(0,0)
 Lcd_String_8x16(0,0,"Start")
 
 MCP9800_Read(temp)
 Lcd_String_8x16(0,2,"Temperatuur:")
 temp = MCP9800_Read(MCP9800_TEMP)
 ByteToStr(temp, str)
 Lcd_String_8x16(0,4,str)

 While True
 Wend

end. 

Nee, toch niet juist van me.

pic basic code:

I2C2_Wr (0x91)                                                    'MCP read address 

Ik had deze in 0x90 veranderd, maar is niet juist (de laatste 1 betekent read)
En nu loop het [programma dus weer vast)

Display_Init()
LCD_wrCmd(0xAF) 'Turn Oled Panel on

LCD_Fill(0x00) 'Clear Screen

MCP9800_Read(temp) <- Wat is dat???
Lcd_String_8x16(0,2,"Temperatuur:")
temp = MCP9800_Read(MCP9800_TEMP)
ByteToStr(temp, str)
Lcd_String_8x16(0,4,str)

Ook: je hebt temp als byte gedefinieerd, dat kan niet. De temperatuur is een word...

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

MCP9800_Read(temp) <- Wat is dat???

Dat is een dikke fout, niet-verwijderd overblijfsel van een eerste poging

|:(

Heb het verwijderd, programma (nu met een I2C bus) loopt door. Temp blijft 255 geven.

pic basic code:

'==============================================================================
Program Oled
'------------------------------------------------------------------------------

const Dos8x16ext As Byte [1520]=   (
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  '
  0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x33,0x00,0x00,0x00,  ' !
  0x00,0xF3,0xF3,0x00,0x00,0xF3,0xF3,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' "
  0x30,0xF3,0xF3,0x30,0x30,0xF3,0xF3,0x30,0x03,0x0F,0x0F,0x03,0x03,0x0F,0x0F,0x03,  ' #
  0x00,0x78,0xF3,0xCF,0xCF,0xCC,0x8C,0x00,0x00,0x0C,0x0C,0x3C,0x3C,0x0F,0x07,0x00,  ' $
  0x00,0x1C,0x1C,0xC0,0xF0,0x3C,0x0C,0x00,0x00,0x0C,0x0F,0x03,0x00,0x0E,0x0E,0x00,  ' %
  0x00,0xD8,0xFC,0xCC,0x7C,0x18,0x00,0x00,0x1F,0x3F,0x31,0x33,0x1F,0x3F,0x33,0x00,  ' &
  0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' '
  0x00,0x00,0x00,0xF0,0xF8,0x1C,0x04,0x00,0x00,0x00,0x00,0x0F,0x1F,0x38,0x20,0x00,  ' (
  0x00,0x04,0x1C,0xF8,0xF0,0x00,0x00,0x00,0x00,0x20,0x38,0x1F,0x0F,0x00,0x00,0x00,  ' )
  0xC0,0xCC,0xFC,0xF0,0xF0,0xFC,0xCC,0xC0,0x00,0x0C,0x0F,0x03,0x03,0x0F,0x0C,0x00,  ' *
  0x00,0xC0,0xC0,0xF8,0xF8,0xC0,0xC0,0x00,0x00,0x00,0x00,0x07,0x07,0x00,0x00,0x00,  ' +
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x7C,0x3C,0x00,0x00,0x00,  ' ,
  0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' -
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x00,0x00,0x00,  ' .
  0x00,0x00,0x00,0x80,0xE0,0x7C,0x1C,0x00,0x00,0x38,0x3E,0x07,0x01,0x00,0x00,0x00,  ' /
  0x00,0xF8,0xFC,0x0C,0x8C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x31,0x30,0x3F,0x1F,0x00,  ' 0
  0x00,0x00,0x30,0xFC,0xFC,0x00,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x30,0x30,0x00,  ' 1
  0x00,0x38,0x3C,0x0C,0xCC,0xFC,0x38,0x00,0x00,0x30,0x3C,0x3F,0x33,0x30,0x30,0x00,  ' 2
  0x00,0x0C,0x0C,0xCC,0xFC,0x3C,0x0C,0x00,0x00,0x1C,0x3C,0x30,0x33,0x3F,0x1C,0x00,  ' 3
  0x00,0x00,0xC0,0xF0,0xFC,0xFC,0x00,0x00,0x00,0x0F,0x0F,0x0C,0x3F,0x3F,0x0C,0x00,  ' 4
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x8C,0x00,0x00,0x18,0x38,0x30,0x30,0x3F,0x1F,0x00,  ' 5
  0x00,0xF0,0xF8,0x9C,0x8C,0x8C,0x00,0x00,0x00,0x1F,0x3F,0x31,0x31,0x3F,0x1F,0x00,  ' 6
  0x00,0x0C,0x0C,0x0C,0xCC,0xFC,0x3C,0x00,0x00,0x00,0x3C,0x3F,0x03,0x00,0x00,0x00,  ' 7
  0x00,0x38,0xFC,0xCC,0xCC,0xFC,0x38,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' 8
  0x00,0x78,0xFC,0xCC,0xCC,0xFC,0xF8,0x00,0x00,0x00,0x30,0x30,0x38,0x1F,0x0F,0x00,  ' 9
  0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x3C,0x3C,0x00,0x00,0x00,  ' :
  0x00,0x00,0x00,0xF0,0xF0,0x00,0x00,0x00,0x00,0x00,0xC0,0x7C,0x3C,0x00,0x00,0x00,  ' ;
  0x80,0xC0,0xE0,0x70,0x38,0x18,0x08,0x00,0x00,0x01,0x03,0x07,0x0E,0x0C,0x08,0x00,  ' <
  0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x00,0x00,0x03,0x03,0x03,0x03,0x03,0x03,0x00,  ' =
  0x08,0x18,0x38,0x70,0xE0,0xC0,0x80,0x00,0x08,0x0C,0x0E,0x07,0x03,0x01,0x00,0x00,  ' >
  0x00,0x38,0x3C,0x0C,0xCC,0xFC,0x38,0x00,0x00,0x00,0x00,0x37,0x37,0x00,0x00,0x00,  ' ?
  0xF0,0xF8,0x1C,0xCC,0x4C,0x98,0xF0,0x00,0x0F,0x1F,0x38,0x33,0x32,0x33,0x19,0x00,  ' @
  0x00,0xF0,0xF8,0x1C,0x1C,0xF8,0xF0,0x00,0x00,0x3F,0x3F,0x03,0x03,0x3F,0x3F,0x00,  ' A
  0x00,0xFC,0xFC,0xCC,0xCC,0xFC,0x78,0x00,0x00,0x3F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' B
  0x00,0xF8,0xFC,0x0C,0x0C,0x3C,0x38,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3C,0x1C,0x00,  ' C
  0x00,0xFC,0xFC,0x0C,0x1C,0xF8,0xF0,0x00,0x00,0x3F,0x3F,0x30,0x38,0x1F,0x0F,0x00,  ' D
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x0C,0x00,0x00,0x3F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' E
  0x00,0xFC,0xFC,0xCC,0xCC,0xCC,0x0C,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,  ' F
  0x00,0xF8,0xFC,0x0C,0xCC,0xCC,0xCC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' G
  0x00,0xFC,0xFC,0xC0,0xC0,0xFC,0xFC,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' H
  0x00,0x0C,0x0C,0xFC,0xFC,0x0C,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' I
  0x00,0x00,0x00,0x00,0x00,0xFC,0xFC,0x00,0x00,0x1C,0x3C,0x30,0x30,0x3F,0x1F,0x00,  ' J
  0xFC,0xFC,0xC0,0xF0,0x3C,0x0C,0x00,0x00,0x3F,0x3F,0x00,0x03,0x0F,0x3C,0x30,0x00,  ' K
  0x00,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x3F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' L
  0xFC,0xFC,0x70,0xC0,0x70,0xFC,0xFC,0x00,0x3F,0x3F,0x00,0x01,0x00,0x3F,0x3F,0x00,  ' M
  0x00,0xFC,0xFC,0xE0,0x80,0xFC,0xFC,0x00,0x00,0x3F,0x3F,0x01,0x07,0x3F,0x3F,0x00,  ' N
  0x00,0xF8,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' O
  0x00,0xFC,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x3F,0x3F,0x03,0x03,0x03,0x01,0x00,  ' P
  0x00,0xF8,0xFC,0x0C,0x0C,0xFC,0xF8,0x00,0x00,0x1F,0x3F,0x38,0x1E,0x3F,0x37,0x00,  ' Q
  0xFC,0xFC,0x8C,0x8C,0xFC,0xF8,0x00,0x00,0x3F,0x3F,0x01,0x03,0x0F,0x3C,0x30,0x00,  ' R
  0x00,0x78,0xFC,0xCC,0x8C,0x8C,0x18,0x00,0x00,0x18,0x31,0x31,0x33,0x3F,0x1E,0x00,  ' S
  0x00,0x0C,0x0C,0xFC,0xFC,0x0C,0x0C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' T
  0x00,0xFC,0xFC,0x00,0x00,0xFC,0xFC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' U
  0x00,0xFC,0xFC,0x00,0x00,0xFC,0xFC,0x00,0x00,0x03,0x0F,0x3C,0x3C,0x0F,0x03,0x00,  ' V
  0xFC,0xFC,0x00,0x80,0x00,0xFC,0xFC,0x00,0x3F,0x1F,0x0E,0x07,0x0E,0x1F,0x3F,0x00,  ' W
  0x00,0x1C,0x7C,0xE0,0xE0,0x7C,0x1C,0x00,0x00,0x38,0x3E,0x07,0x07,0x3E,0x38,0x00,  ' X
  0x00,0x3C,0xFC,0xC0,0xC0,0xFC,0x3C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' Y
  0x00,0x0C,0x0C,0xCC,0xFC,0x3C,0x0C,0x00,0x00,0x3C,0x3F,0x33,0x30,0x30,0x30,0x00,  ' Z
  0x00,0x00,0x00,0xFC,0xFC,0x0C,0x0C,0x00,0x00,0x00,0x00,0x3F,0x3F,0x30,0x30,0x00,  ' [
  0x00,0x0C,0x0C,0xFC,0xFC,0x00,0x00,0x00,0x00,0x30,0x30,0x3F,0x3F,0x00,0x00,0x00,  ' \
  0x80,0xE0,0x78,0x1E,0x78,0xE0,0xE0,0x00,0x01,0x01,0x00,0x00,0x00,0x01,0x01,0x00,  ' ]
  0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' ^
  0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x30,0x30,0x30,0x30,0x30,0x00,  ' _
  0x00,0x07,0x0E,0x1C,0x38,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,  ' `
  0x00,0x00,0x60,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1E,0x3F,0x33,0x33,0x3F,0x3F,0x00,  ' a
  0x00,0xFC,0xFC,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' b
  0x00,0xC0,0xE0,0x60,0x60,0x60,0x00,0x00,0x00,0x1F,0x3F,0x30,0x30,0x30,0x30,0x00,  ' c
  0x00,0xC0,0xE0,0x60,0x60,0xFC,0xFC,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x3F,0x00,  ' d
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1F,0x3F,0x32,0x32,0x33,0x33,0x00,  ' e
  0x00,0xC0,0xC0,0xF8,0xFC,0xCC,0xCC,0x00,0x00,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,  ' f
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xE0,0x00,0x00,0xCF,0xDF,0xD8,0xD8,0xFF,0x7F,0x00,  ' g
  0x00,0xFC,0xFC,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' h
  0x00,0x00,0x60,0xEC,0xEC,0x00,0x00,0x00,0x00,0x00,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' i
  0x00,0x00,0x00,0x00,0xEC,0xEC,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xFF,0x7F,0x00,0x00,  ' j
  0xFC,0xFC,0x80,0xC0,0xE0,0x60,0x00,0x00,0x3F,0x3F,0x03,0x07,0x1E,0x38,0x30,0x00,  ' k
  0x00,0x00,0x0C,0xFC,0xFC,0x00,0x00,0x00,0x00,0x00,0x30,0x3F,0x3F,0x30,0x00,0x00,  ' l
  0xC0,0xE0,0xE0,0xC0,0xE0,0xE0,0xC0,0x00,0x3F,0x3F,0x00,0x07,0x00,0x3F,0x3F,0x00,  ' m
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x3F,0x3F,0x00,  ' n
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x1F,0x00,  ' o
  0x00,0xE0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0xFF,0xFF,0x30,0x30,0x3F,0x1F,0x00,  ' p
  0x00,0xC0,0xE0,0x60,0x60,0xE0,0xE0,0x00,0x00,0x1F,0x3F,0x30,0x30,0xFF,0xFF,0x00,  ' q
  0x00,0xE0,0xE0,0x60,0x60,0xE0,0xC0,0x00,0x00,0x3F,0x3F,0x00,0x00,0x00,0x00,0x00,  ' r
  0x00,0xC0,0xE0,0x60,0x60,0x60,0x60,0x00,0x00,0x31,0x33,0x33,0x36,0x3E,0x3C,0x00,  ' s
  0x00,0x60,0x60,0xF8,0xF8,0x60,0x60,0x00,0x00,0x00,0x00,0x1F,0x3F,0x30,0x30,0x00,  ' t
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0x1F,0x3F,0x30,0x30,0x3F,0x3F,0x00,  ' u
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0x03,0x0F,0x3C,0x3C,0x0F,0x03,0x00,  ' v
  0xE0,0xE0,0x00,0x80,0x00,0xE0,0xE0,0x00,0x3F,0x1F,0x0E,0x07,0x0E,0x1F,0x3F,0x00,  ' w
  0x00,0x60,0xE0,0x80,0x80,0xE0,0x60,0x00,0x00,0x30,0x3D,0x0F,0x0F,0x3D,0x30,0x00,  ' x
  0x00,0xE0,0xE0,0x00,0x00,0xE0,0xE0,0x00,0x00,0xCF,0xDF,0xD8,0xD8,0xFF,0x7F,0x00,  ' y
  0x00,0x60,0x60,0x60,0xE0,0xE0,0x60,0x00,0x00,0x30,0x3C,0x3F,0x33,0x30,0x30,0x00,  ' z
  0x80,0x80,0xC0,0xFC,0x7E,0x02,0x02,0x00,0x01,0x01,0x03,0x3F,0x7E,0x40,0x40,0x00,  ' {
  0x00,0x00,0x00,0xFE,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x7F,0x00,0x00,0x00,  ' |
  0x02,0x02,0x7E,0x7C,0xC0,0x80,0x80,0x00,0x40,0x40,0x7E,0x3F,0x03,0x01,0x01,0x00,  ' }
  0xC0,0x60,0xE0,0xC0,0x80,0x80,0xE0,0x00,0x01,0x00,0x00,0x01,0x01,0x01,0x00,0x00)  ' ~
'
Dim i1 as byte
Dim Teller as byte
Dim Getal as string[4]
DIm temp as word
Dim str as string[1]

Dim MCP9800_TEMP   as byte
Dim MCP9800_CONFIG as byte
Dim MCP9800_HYST   as byte
Dim MCP9800_ALERT  as byte

'
'==================================================================================================
Sub Procedure Lcd_WrCmd(Dim pCmd As Byte)                       'Write data to display
'==================================================================================================
  while Not (i2c1_Is_Idle) wend
  i2c1_Start()
  i2c1_Wr(0x78)
  i2c1_Wr(0x80)
  i2c1_Wr(pCmd)
  i2c1_Stop()
End Sub

'==================================================================================================
Sub Procedure Lcd_WrDat(Dim pDat As Byte)                       'Write data to display
'==================================================================================================
  while Not (i2c1_Is_Idle) wend
  i2c1_Start()
  i2c1_Wr(0x78)
  i2c1_Wr(0x40)
  i2c1_Wr(pDat)
  Delay_ms(1)
  i2c1_Stop()
End Sub

'==================================================================================================
Sub Procedure Lcd_SetPos(Dim px, py as Byte)                    'Set write position on display
'==================================================================================================
  Lcd_WrCmd(0xB0 + py)
  Lcd_WrCmd(0xB0 + py)
  Lcd_WrCmd((px  >> 4) Or 0x10)
  Lcd_WrCmd(px And 0x0F)
End Sub
'
'===============================================================================
Sub Procedure Lcd_Char_8x16(Dim px, py, pchar As Byte)
'===============================================================================
  Dim lIndex As Word
  '----------------------------------------------------------------------------
  lIndex = pChar - 0x20
  lIndex = (lIndex << 4)

  Lcd_SetPos(px, py)
  Lcd_WrDat(Dos8x16ext[lIndex+0])
  Lcd_WrDat(Dos8x16ext[lIndex+1])
  Lcd_WrDat(Dos8x16ext[lIndex+2])
  Lcd_WrDat(Dos8x16ext[lIndex+3])
  Lcd_WrDat(Dos8x16ext[lIndex+4])
  Lcd_WrDat(Dos8x16ext[lIndex+5])
  Lcd_WrDat(Dos8x16ext[lIndex+6])
  Lcd_WrDat(Dos8x16ext[lIndex+7])

  Lcd_SetPos(px, py+1)
  Lcd_WrDat(Dos8x16ext[lIndex+8])
  Lcd_WrDat(Dos8x16ext[lIndex+9])
  Lcd_WrDat(Dos8x16ext[lIndex+10])
  Lcd_WrDat(Dos8x16ext[lIndex+11])
  Lcd_WrDat(Dos8x16ext[lIndex+12])
  Lcd_WrDat(Dos8x16ext[lIndex+13])
  Lcd_WrDat(Dos8x16ext[lIndex+14])
  Lcd_WrDat(Dos8x16ext[lIndex+15])
End Sub
'
'===============================================================================
Sub Procedure LCD_Fill(Dim pFill As Byte)     'Fill display with character pFill
'===============================================================================
  Dim lx, ly As Byte
  For ly = 0 To 8
  LCD_wrCmd(0xB0 + ly)
  LCD_wrCmd(0x01)
  LCD_wrCmd(0x10)
    For lx = 0 to 131
      Lcd_wrDat(pFill)
    Next lx
  Next ly
End Sub

'===============================================================================
Sub Procedure Lcd_String_8x16(Dim px, py As Byte, Dim ByRef pDisp As String)
'===============================================================================
  Dim lLen, lCnt As Byte
  '----------------------------------------------------------------------------
  lLen = Length(pDisp)
  For lCnt = 0 To (lLen-1)
    Lcd_Char_8x16(px+1, py, pDisp[lCnt])
    px = px + 8
  Next lCnt
End Sub
'===============================================================================
Sub Procedure Display_8x16()
'===============================================================================
  LCD_Fill(0x00)
  LCD_Setpos(0,0)
  Lcd_String_8x16(0,0,"First Line")
  Lcd_String_8x16(0,2,"Second Line")
  Lcd_String_8x16(0,4,"Third Line")
  Lcd_String_8x16(0,6,"Fourth Line")
  End Sub

'===============================================================================
Sub Procedure Display_Init()
'===============================================================================
  Lcd_WrCmd(0xA8)                                 'Multiplex ratio set to
  Lcd_WrCmd(0x3F)                                 '63
  Lcd_WrCmd(0xD3)                                 'Display offset RAM counter
  Lcd_WrCmd(0x00)                                 'none
  Lcd_WrCmd(0x40)                                 'Start line address
  Lcd_WrCmd(0xA1)                                 'Set segment remap rotation to left
  Lcd_WrCmd(0xC8)                                 'Common output scan direction
  Lcd_WrCmd(0xDA)                                 'Common signals pad
  Lcd_WrCmd(0x12)                                 'value
  Lcd_WrCmd(0x81)                                 'Contrast control
  Lcd_WrCmd(0xFF)                                 'value
  Lcd_WrCmd(0xA4)
  Lcd_WrCmd(0xA6)                                 'Normal display
  Lcd_WrCmd(0xD5)                                 'Clock ratio:oscillator frequency
  Lcd_WrCmd(0x80)                                 'oooo:rrrr
  Lcd_WrCmd(0x8D)
  Lcd_WrCmd(0x14)
  Lcd_WrCmd(0x00)                                 'Set lower column address
  Lcd_WrCmd(0x10)                                 'Set higher column address
  Lcd_WrCmd(0xD9)                                 '15 clocks, discharge 1 clock
  Lcd_WrCmd(0xF1)                                 'dddd:pppp
  Lcd_WrCmd(0xDB)                                 'Common output voltage
  Lcd_WrCmd(0x40)                                 'level
  Lcd_WrCmd(0x20)                                 'Addressing mode
  Lcd_WrCmd(0x02)                                 'value
  Lcd_WrCmd(0xAE)                                 'Turn OLED panel off
End Sub

'========================================================================================
Sub Function MCP9800_Read(Dim pReg As Byte) As Word                 'Read
'========================================================================================
  I2C1_Start()                                                      'Start
  I2C1_Wr(0x90)                                                     'MCP write address
  I2C1_Wr(pReg)                                                     '
  I2C1_Repeated_start()                                             'Bus restart
  I2C1_Wr (0x91)                                                    'MCP read address
  If pReg = MCP9800_CONFIG Then                                     'Config reg. only
    Result = I2C1_Rd(1)                                             '1 byte long; rest
  Else                                                              'is 2 bytes
    Hi(Result) = I2C1_Rd(0)                                       '
    Lo(Result) = I2C1_Rd(1)                                       '
  End If                         '
  I2C1_Stop()
delay_ms(5)                                                       'Wait to complete
End Sub                                                             '

'========================================================================================
Sub Procedure MCP9800_Write(Dim pReg As Byte, dim lData As Word)    'Write
'========================================================================================
  I2C1_Start()                                                      'Start
  I2C1_Wr(0x90)                                                     'MCP write address
  I2C1_Wr(pReg)                                                  '
  If pReg = MCP9800_CONFIG Then                                     'Config re. only
    I2C1_Wr(lData)                                                  '1 byte long; rest
  Else                                                              'is 2 bytes
    I2C1_Wr(Hi(lData))                                           '
    I2C1_Wr(Lo(lData))                                           '
  End If                                                            '
  I2C1_Stop()                                                       'Stop
  delay_ms(5)                                                       'Wait to complete
End Sub                                                             '

'========================================================================================
Sub Procedure MCP9800_SetAlert(Dim pMin, pMax As Byte)
'========================================================================================
  MCP9800_Write(MCP9800_HYST, pMin<<8)                              'Sets min/max temp
  MCP9800_Write(MCP9800_ALERT, pMax<<8)                             '
End Sub                                                             '

'==============================================================================
main:

''==============================================================================
  ANSELB = 0                                     'All output/digital
  TRISB  = 0
  OSCCON = %01110000                             '8Mhz internal
  Delay_ms(50)
  i2c1_init(100000)                              'Init i2c port 100kHz

  MCP9800_TEMP     = 0x00                        'MCP9800 registers
  MCP9800_CONFIG   = 0x01                        '  "
  MCP9800_HYST     = 0x02                        '  "
  MCP9800_ALERT    = 0x03                        '  "

  Display_Init()
  LCD_wrCmd(0xAF)                                'Turn Oled Panel on

 LCD_Fill(0x00)                                  'Clear Screen
 LCD_Setpos(0,0)
 Lcd_String_8x16(0,0,"Start")

 Lcd_String_8x16(0,2,"Temperatuur:")
 temp = MCP9800_Read(MCP9800_TEMP)
 ByteToStr(temp, str)
 Lcd_String_8x16(0,4,str)

 While True
 Wend

end.

Je kunt MCP9800_TEMP enz... direct als constantes declareren, eerst een byte kost vier geheugenplaatsen en heeft geen nut...

pic basic code:


Const  MCP9800_TEMP     = 0x00                    'MCP9800 registers
       MCP9800_CONFIG   = 0x01                    '  "
       MCP9800_HYST     = 0x02                    '  "
       MCP9800_ALERT    = 0x03                    '  "

Je hebt ook Str[] gedefinieerd als een 1 byte string, dat klopt niet. ByteToStr heeft een string van minimaal 3 bytes nodig (zie helpfile)
Nu loopt het array "out of bounds", valt mee dat het programma niet crasht...

Edit:
Je moet trouwens WordToStr[] gebruiken, want Temp is een word. (Str wordt dan 5 bytes)

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Je kunt MCP9800_TEMP enz... direct als constantes declareren, eerst een byte kost vier geheugenplaatsen en heeft geen nut...

Dat is weer een goed leermoment. Ik dacht dat het niet uitmaakte, maar scheelt dus idd weer geheugenruimte.

Ook een WordtToStr opgenomen (ik was begonnen met temp als byte, maar is later naar Word gegaan).

Ik krijg nu evenwel 11775 als resultaat.

Is dat constant, of verandert die waarde (spontaan of met de temperatuur)

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Hij las het maar 1 keer uit, zat niet in de While- Wend loop.

Nu verandert de waarde continue, maar van de waardes kan ik nog geen chocola maken (tussen de 11000 - 13000)

pic basic code:

If pReg = MCP9800_CONFIG Then                                     'Config reg. only
    Result = I2C1_Rd(1)                                             '1 byte long; rest
  Else                                                              'is 2 bytes
    Hi(Result) = I2C1_Rd(0)                                       '
    Lo(Result) = I2C1_Rd(1)             

Wat gebeurt hier nu eigenlijk?

Het CONFIG register is maar 1 byte lang, dus 1 byte lezen. De rest is een word lang dus 2 bytes lezen...
Ik zet 'm altijd direct op 12 bits resolutie(default is 9) met:

pic basic code:

  MCP9800_Write(MCP9800_CONFIG, %01100000)
Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Ok, thx

Nu nog uitpluizen hoe de temperatuur in graden Celcius kan worden getoond (met bv 1 decimaal achter de komma) ipv een hoge waarde boven de 11000

Temperatuur staat in hi byte (hi(temp)), alles achter de komma in lo byte (lo(temp))van temp.

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

pic basic code:

WordToStr(Hi(temp), str)
 Lcd_String_8x16(0,4,str)
 WordToStr(Lo(temp), str)
 Lcd_String_8x16(0,6, str)

Voor Hi krijg ik nu de waarde schommelend tussen 48 - 51

De Lo geeft continu 255

Zal wel niks uitmaken, maar lo(temp) en hi(temp) zijn eigenlijk bytes, geen words (dus weer bytetostr)

Arco - "Simplicity is a prerequisite for reliability" - www.arcovox.com

Nee,maakt niet uit. Blijft 47 - 255 geven.

De sensor reageert wel op temperatuurschommelingen, maar geeft dus een dikke factor 2 teveel aan.

[Bericht gewijzigd door Bavelt op 26 december 2019 22:04:46 (55%)]

pic basic code:

MCP9800_Write(MCP9800_CONFIG, %01100000) 

De instelling van het CONF register geeft geen effect op de waarde; welke waarde ik daar ook ingeef.