MikroBasic en de DS18B20

Je kunt dat checken door het CONFIG register uit te lezen, moet dan de nieuwe waarde hebben...
Pull-ups ook niet te groot? (2k2 werkt altijd goed)
I2c pinnen als inputs gezet in TRISx register?

[Bericht gewijzigd door Arco op 26 december 2019 23:25:11 (14%)]

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

Nu wel (Heel PORTB stond op Output)
(NB Maar waarom is de SCL een input? Je zendt toch klokpulsen uit?)

Ik had als pullup weerstanden 5K6, en heb dat ook dus maar naar 2K2 teruggebracht. (ik dacht dat alles tot 10K goed was)
Geeft allemaal hetzelfde resultaat.
Voor de volledigheid het programma zoals dat nu is:

pic basic code:

'==============================================================================
Program Oled_I2C_096
'------------------------------------------------------------------------------

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[5]

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

'==================================================================================================
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  = %00010010
  OSCCON = %01110000                             '8Mhz internal
  Delay_ms(50)
  i2c1_init(100000)                              'Init i2c port 100kHz

  Display_Init()
  LCD_wrCmd(0xAF)                                'Turn Oled Panel on
  
  MCP9800_Write(MCP9800_CONFIG, %01100000)

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

  Lcd_String_8x16(0,2,"Temperatuur:")
 
  While True
    temp = (MCP9800_Read(MCP9800_TEMP))
    ByteToStr(Hi(temp),  str)
    Lcd_String_8x16(0,4,str)
    ByteToStr(Lo(temp), str)
    Lcd_String_8x16(50,4, str)
    delay_ms(250)
 Wend

end. 

Maar waarom is de SCL een input?

TRISx heeft te maken met de reguliere in/outputs, de i2c module is een op zich staande module.
(als je in TRISx outputs specificeert, gaan de 'normale' output buffers touwtrekken met de i2c in/outputs... ;)

Als je CONFIG terugleest, klopt die waarde dan?

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

Als je CONFIG terugleest, klopt die waarde dan?

Ja zojuist getest met

pic basic code:

 i1 =    (MCP9800_Read(MCP9800_CONFIG))

(i1 is testbyte, die ik toon op de LCD)

Hij geeft dan keurig de waarde 96 die is weggeschreven in het CONF register. Bij een andere waarde leest hij hem ook goed uit.

Wel iets raars: Als ik het programma vertaal en de HEX importeer, moet ik wel eerst de spanning van de PIC halen om het programma draaiend te krijgen.
(Zelfs MCLR laag helpt dan niet).
Bij power-up gaat alles goed.

Bij 'normale', andere wijzigingen zie je na laden meteen het resultaat.
Beetje vreemd, maar ala.

Ik vroeg me af of het misschien niet zo kan zijn dat door al dat gesoldeerderij direct aan de chip (het is echt een thermo-plak verbinding geworden :r ), wellicht de sensor te heet is geworden?

Dat kan, ik heb er veel gebruikt en nooit problemen mee gehad...
Beste is wel om altijd de Power-up timer in de 1847 aan te zetten, dat voorkomt veel problemen met opstarten.

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

Ik bestel er gewoon nog twee bij. Ondertussen heb ik de adaptertjes om ze op te monteren ook besteld.
Dan doe ik het gewoon over met nieuw spul.

Ik bestel het bij Farnell, ze zijn leverbaar in SOIC en MSOP. De MSOP lijkt iets groter op het plaatje, maar dat kan gezichtsbedrog zijn.

MSOP is ongeveer 2x zo klein als SOIC... ;)
(pitch MSOP is 0.65mm, bij SOIC is dat 1.27mm. Is eigenlijk een SSOP die ook in de breedte nog is gehalveerd)

[Bericht gewijzigd door Arco op 27 december 2019 11:26:34 (56%)]

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

Inmiddels heb ik nieuwe MCP9801 binnen. En de adapter printjes om ze op te solderen. Nu ziet het er fatsoenlijk uit.
En het werkt ook nog meteen, nu met processor PIC16F887.

Enig puntje nog is dat het getal achter de komma op '255' blijft staan.

pic basic code:

'==============================================================================
 program Temp_MCP9801
'Temp sensor: MCP9801
'MicroController: PIC16F887
'------------------------------------------------------------------------------

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[5]

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

'==================================================================================================
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_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:


''==============================================================================
  ANSELH = 0                                     'All output/digital
  TRISB  = %00010010
  Delay_ms(50)
  i2c1_init(100000)                              'Init i2c port 100kHz

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

  MCP9800_Write(MCP9800_CONFIG, %01100000)

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

  Lcd_String_8x16(0,2,"Temperatuur:")

  While True
    temp = (MCP9800_Read(MCP9800_TEMP))
    ByteToStr(Hi(temp),  str)
    Lcd_String_8x16(0,4,str)
    ByteToStr(Lo(temp), str)
    Lcd_String_8x16(50,4, str)
    delay_ms(250)
 Wend
end. 

Hmm, wat ik ook probeer;

de 'Lower Half' van het temperatuur register van de MCP9801 blijft op 11111111 staan.
De 'higher half' geeft wel de juiste waarde weer en reageert op de temperatuur .

Ergens moet iets dus niet goed zijn ingesteld.

(Ik heb de SCL en SDA (Poort C.3 en C.4 van de 887) op input gezet
met TRISC = %00011000

PullUp weerstanden - 2k2.)

Probeer eens een integer van de temperatuur te maken. (de waardes zijn 2's complement omdat ze ook negatief kunnen worden)

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

Eerst moet ik even ander problemen oplossen: ;(

Ik heb de migratie van Windows 7 naar Windows 10 gedaan.
(Windows 7 werkte prima, maar ja, beveiliging, geen updates meer, etc)

Bosjes ellende. Krijg geen filmpje meer afgespeeld in geen enkele browser, maar nog vervelender: allerlei Errors op Pickit v3.10 omdat de map Program Files(x86)Microchip en zijn submappen "alleen lezen" zijn na de migratie en geen toegang bieden voor de applicatie.
"geen 'administrator rechten' terwijl ik de enige gebruiker ben en dat soort ellende.)
Zie dat er maar eens uit te krijgen. Alles wat ik tot nu toe op internet heb gevonden strandt bij verfoeijlijke Windows meldingen zoals "er is een onbekende fout opgetreden".
:(

Ik vrees dat dit iets is voor een ander forum...

Probeer eens een integer van de temperatuur te maken. (de waardes zijn 2's complement omdat ze ook negatief kunnen worden)

Integer maken geeft geen verschil.
Ik heb de inhoud wat terugkomt van het lezen op het display getoond met:

pic basic code:



While True
    temp = (MCP9800_Read(MCP9800_TEMP))
    WordToStr(temp,  str)
    Lcd_String_8x16(0,4,str)
    delay_ms(250)
 Wend 

Daar komt uit 6399, dat is binair 00011000 11111111
M.a.w. laatste 8 bits een 1.

Die 24 Graden (1e 8 bits) klopt zo ongeveer wel.

Wat verder ook een punt is: als ik een waarde naar het CONFIG register schrijf en ik lees dat terug, dan komt daar steevast 0 uit.
Daar kan dus ook wellicht iets niet in orde zijn.

Het schrijven en lezen naar het CONFIG Register gaat mu goed.
Ik kan de ingebrachte waarden reproduceren.

Daarnaast wat gespeeld met de resolutie van 9 (default) tot 12.

In alle gevallen blijft de 2e byte op 255 staan. Alle bits dus hoog.
Nu twijfel ik of deze chip soms stuk is; leek me sterk, nieuw besteld bij Farnell...

Wat - waarschijnlijk los van het '255' probleem - , is dat na een aantal cycli de zaak vastloopt.

Ik heb er een tellertje ingebouwd en toon deze op het display.
Na een keer of 6 tot 10 stopt de zaak.

De maximale Stack diepte is 6 heb ik gezien in de View Statistics.

pic basic code:

Teller = 0
  While True
    Teller = Teller + 1
    temp = (MCP9800_Read(MCP9800_TEMP))
    ByteToStr(Hi(temp),  str)
    Lcd_String_8x16(0,2,str)
    Lcd_String_8x16(25,2,",")
    ByteToStr(Lo(temp), str)
    Lcd_String_8x16(35,2, str)
    ByteToStr(Teller, str)
    Lcd_String_8x16(00,4, str)
    delay_ms(1000)
 Wend

Ik weet ook niet wat er dan mis is: ik heb er nog nooit problemen mee gehad. (ik gebruik wel de pic24 compiler)
In 12 bits resolutie geeft het hoge byte de temperatuur, en de hoge 4 bits van de lage byte de waarde achter de komma in 0.0625 graden stapjes...

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

Ik heb de MCP9801 vervangen; zelfde resultaat.

Als het programma na een aantal cycli ( < 10) stopt, dan zit alles ook echt vast. De MCLR (pin 1) laag brengen helpt dan ook niet, wat rest is de spanning eraf en er weer op.. Dan loopt hij weer even.

Ik ga het nu maar eens met een andere processor proberen, bv de PIC16F1847 die ik hier ook heb.

Je hebt bij de 887 wel beide Vdd en Vss aansluitingen gebruikt? En LVP uitgezet in de config?

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

Je hebt bij de 887 wel beide Vdd en Vss aansluitingen gebruikt? En LVP uitgezet in de config?

Ja, had ik.

Ondertussen nieuw breadboardje en alles opgezet met een PIC16F1827.

(De ALERT, pin 3 heb ik loshangen. Weet niet of dat kan/mag).
Zelfde resultaat.
Klein verschil: de uC houdt het wat langer vol, ergens bij de 40e keer stopt het programma ook en zit de boel vast.
Ik kan het nu wel resetten met de MCLR naar laag. Dan start het weer.
De bits van de higher byte werken; als je bv je vinger op de chip legt zie je de temperatuurwaarde meteen reageren.
Maar alle bits van de lower byte blijven op 1.

De Chip is voluit MCP9801M (weet niet of die laatste letter M iets toevoegt).

Zou het toch iets met compiler te maken kunnen hebben? Kan ik dit MikroBasic programma ook vertalen met iets anders?

Het chipje MCP9801 is goed.

Ik heb even een Arduino (UNO) aangesloten, sketch gedownload en het werkt meteen..

Dan wordt 't zoeken waar het zit. (zou in de i2c kunnen zijn, of een stack overflow)
Met een led kun je veel testen. Doe 'm aan voor de i2c, en daarna weer uit. Als 'ie dan blijft branden, hang je ergens in de i2c...

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

Ik was daar inderdaad al aan het zoeken.
Ik had al

pic basic code:

 while Not (i2c1_Is_Idle) wend 

bij de I2c routines ingebouwd voor alle zekerheid.

Het hangen zit inderdaad ergens in de I2c. Misschien wel in combinatie met het tonen van de waardes op de LCD, die parallel staat aan de MBP9801.
Ieder heeft immers zijn eigen slave address.

Wanneer ik enkel een teller toon op de LCD dan loopt het door.

In combinatie met het lezen van de MCP9801 (zelfs zonder de waarden te tonen) slaat alles na enkele cycli vast.

Ik kan ook eens testen om voor de MCP9801 een 'eigen' (tweede) I2C te gebruiken.
De 1827 heeft er twee.
Zou niet mogen uitmaken volgens mij, maar toch maar proberen.

Pull-ups voldoende? (iets van 2k2)
De i2c pinnen moeten ook wel als input gezet worden...

[Bericht gewijzigd door Arco op 16 januari 2020 14:13:26 (45%)]

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

Met een led kun je veel testen. Doe 'm aan voor de i2c, en daarna weer uit. Als 'ie dan blijft branden, hang je ergens in de i2c...

Zojuist gedaan.
Een ledje voor de I2c van de LCD en eentje voor de MCP9801.

De boosdoener is de LCD. Die blijft hangen.

De pullups heb ik op 4k7. Als ik 2k2 neem, dan wil de display niet meer; die blijft halverwege hangen bij het leegmaken.