News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

TC74A7-5.0 I2C temperature sensor and 18F14k22

Started by HAL, Apr 17, 2023, 10:11 PM

Previous topic - Next topic

HAL

Hello

Here attached is code for a Microchip TC74A7-5.0 temperature sensor using I2C.   The original program has been modified a little here and there.  The code has been compiled and ported to a pic18F14k22 and is working with a 4x20 LCD display.  Hoping someone may find it helpful or useful. :)   



'******************************************************************
'*  Name   : Test_TC74_MOD.BAS USES A PIC18F14K22 AND TC74 I2C    *
'*  Author : BY [Alberto Freixanet] IN 2016   THIS VERSION HAS    *
'*         : A FEW CHANGES AND A DIFFERENT uC  *TESTED & WORKS!   *
'*  Notice : FOR BEGINNERS - DEMONSTRATES THE TC74 (MICROCHIP)    *
'*         : TEMPERATURE SENSOR -65C-->+127C  (0 TO +127 HERE)    *
'*         : TC74Ax IS AVAILABLE IN SOT23-5 AND TO220-5 PACKAGES  *
'*  Date   : FEB 2022 USING A PIC18F14K22 WITH Xtal=64            *
'*         : USES SOFTWARE_I2C.Inc / 1096 PGM BYTES 25 VAR BYTES  *
'*  Notes  : USING THE "POSITRON" COMPILER BY LES JOHNSON         *
'*         : POSITON: COULDN'T PROGRAM A PICuC WITHOUT IT!        *
'******************************************************************
'-------  EXTERNAL CONNECTIONS   ------------------------------
'   uc pin 13 (portB.4) to LCD pin 11 (data pin D4)
'   uc pin 12 (portB.5) to LCD pin 12 (data pin D5)
'   uc pin 11 (portB.6) to LCD pin 13 (data pin D6)
'   uc pin 10 (portB.7) to LCD pin 14 (data pin D7)
'   uc pin  9 (portC.7) to LCD pin 6  (EN pin)
'   uc pin  8 (portC.6) to LCD pin 4  (RS pin)
'   uc Pin 18 (PORTA.1) To I2C_CLK to TC74A5 pin 4
'   uc Pin 19 (PORTA.0) To I2C_SDA to TC74A5 Pin 2

;-------------------------------------------------------------------------------
'>>>>>>>> PLEASE NOTE
'>>>>>>>> PLEASE NOTE:  THE TC74 HAS AN ACCURACY OF 2 OR 3 DEGREES C <<<<<<<<<<<
'>>>>>>>> PLEASE NOTE
;-------------------------------------------------------------------------------

    Device = 18F14K22  'OR 18F13K22
   
    Declare Xtal=64    'USES A 16MHZ CERAMIC RESONATOR + 4X INTERNAL PLL           

    ' SEE THE POSITON MANUAL:
    ' RE: Alphanumeric (Hitachi HD44780) LCD Print Declares Page 427.
   
    Declare LCD_Type 0
    Declare LCD_DTPin PORTB.4   'Declare LCD_DTPin PortB.4 TO B7 -> LCD Pins 11 TO 14
    Declare LCD_RSPin PORTC.6   'Declare LCD_RSPin PortC.6 -> LCD Pin 4
    Declare LCD_ENPin PORTC.7   'Declare LCD_ENPin PortC.7 -> LCD Pin 6
    Declare LCD_Interface 4     '4 DATA LINES  B0-->B3
    Declare LCD_Lines 4         'LCD DISPLAY WITH 4 LINES
   
   'Declare LCD_CommandUs 2200  'default delay is 2000us
   'Declare LCD_DataUs 100      'default delay is 50us
   
    Declare Slow_Bus On ' KEEP CLOCK AT OR BELOW 100KHZ
   
   'Declare Bus_SCL On  ' DON'T NEED PULLUP R ON CLOCK PIN (IF USED)

    Include "SOFTWARE_I2C.inc"    ' SOFTWARE I2C INCLUDE
   
'===============================================================================   
    'Preprocessor directives   THESE VALUES DO NOT CHANGE FOR THE TC74 DEVICEES
   
    $define TC74_TEMPREG  $00 ' TEMP REG --> Read Temperature (TEMP REG)
    $define TC74_RWCONFGR $01 ' CONFIG REG --> Read/Write Config(CONFIG REG)
    $define TC74_STANDBY  $80 ' Setting --> Standby value (not used here)
    $define TC74_NORMAL   $00 ' Setting --> Normal value     
    '$define _Software_I2C_Fixed_Pins_inc_ '$define _SOFTWARE_I2C_INC_
   
    '$define I2C_bSDA_Pin As Pin PORTA.0
    '$define I2C_bSCL_Pin As Pin PORTA.1 
'===============================================================================                                         
    Dim TC74_AddressWrite As Byte ' HOLDS I2C WRITE ADDRESS
    Dim TC74_AddressRead As Byte  ' HOLDS I2C READ ADDRESS
    Dim Temperature As Byte
    Dim TempF As Byte
   
'===============================================================================
          'see address tables --> at the end of the code
'===============================================================================
    Temperature = 0  ' SET THE VARIABLE TO ZERO
   
   '  A TC74A7 Sensor is used here...       'TC74A7 7 BIT ADDRESS %1001111 = $4F
   '  SHIFT ADDRESS LEFT BY 1 BIT POSITION GIVES Write Address = %10011110 = $9E
   '  SO THE TC74_AddressWrite = SensorAddress << 1
     
    TC74_AddressWrite = $9E 'WE HAVE THE I2C WRITE ADDRESS
   
   ' NEXT ADD 1 TO THE WRITE ADDRESS MAKING IT READ AddressRead = %10011111 = $9F
     
    TC74_AddressRead = TC74_AddressWrite + 1
'===============================================================================
    GoTo MAIN:
   
 ' SUBS WOULD/COULD GO HERE BUT WE DON'T HAVE ANY
'===============================================================================
MAIN:
    Symbol I2C_SDA_Pin = PORTA.0      ' Holds the pin used for the SDA line
    Symbol I2C_SCL_Pin = PORTA.1      ' Holds the pin used for the SCL line     
'===============================================================================     
' SET UP THE TC74A7 TO A KNOWN STATE FOR MEASUREMENT   
' NOTE:  THE TC74 POWERS On IN THE ACTIVE MODE (NORMAL) BUT LET'S MAKE SURE!

    BStart    'THIS BIT OF CODE WILL PLACE THE TC74 IN THE ACTIVE (NORMAL) MODE
    BusOut TC74_AddressWrite   ' 9E HEX =    %10011110     $9E  TC74A7     
    BusOut TC74_RWCONFGR       ' TC74_RWCONFGR REG $01 -> SELECT CONFIG REGISTER   
    BusOut TC74_NORMAL         ' SET THE CONFIG REGISTER TO $00 (NOT IN STANDBY)   
    BStop                      ' THE TC74A7 IS CONFIGURED
'===============================================================================         

       Cls           ' LCD START UP MESSAGE
       Print At 1, 1, "TEST OF TC74 SENSOR"
       Print At 2, 1, " USING I2C FOR TEMP"
       Print At 3, 1, " BUT WILL IT WORK??"
       DelayMS 3000
 
'-------------------------------------------------------------------------------   
    Do       ' SET UP A LOOP TO MEASURE TEMPERATURE
    BStart   'Let'S GET THE TEMPERATURE FROM THE TC74 (IT'S 2'S COMPLEMENT FOR TEMPS BELOW 0C)
    BusOut TC74_AddressWrite   '%10011110 = $9E TC74A7 AddressWrite
   
    BusOut TC74_TEMPREG        'SELECT TC74_TEMPREG  $00 (HOLDS TEMPERATURE C)
   
    BReStart
    BusOut TC74_AddressRead    '%10011111 = $9F TC74A7 AddressRead
   
    BusIn Temperature          ' READ DATA FROM TC74_TEMPREG SHOULD BE NEAR 21 INSIDE
    BusNack                    ' TEMP IS 2'S COMPLEMENT BUT WE'RE STAYING POSITIVE
    BStop
    TempF = 32 + Temperature * 9/5 
'===============================================================================   
    'DISPLAY TEMPERATURE DATA
       
    Cls
    Print At 1, 1, "TESTING TC74A SENSOR"
    Print At 2, 1, "TEMP C = ", Dec, Temperature
    Print At 3, 1, "TEMP F = ", Dec, TempF

    DelayMS 3000  'DISPLAY FOR 3 SECONDS
   
    Print At 4, 1, "GETTING A NEW VALUE"
    DelayMS 800   'LET THE WIEWER KNOW THAT IT IS ACTIVE
    Loop          'DO IT AGAIN
    End
       
'===============================================================================
 ' ADDITIONAL DATA
     ' TC74 Temperature measurement is in °C
     ' TC74 Temperature OUTPUT resolution is 1°C
     ' TC74 Temperature ACCURACY is 2 or 3°C
     ' TC74 Conversion rate is a nominal 8 samples/sec OR 125 mS/SAMPLE

'===============================================================================
' THE i2c VALUES CHANGE For VARIOUS TC74 DEVICEES AVAILABLE FOR 5v And 3.3v
' TYPES TC74A0--->THERE ARE TO220-5, also available in surface mount SOT-23-5
     
     'I2C Addresses of standard TC74 Temperature sensors
     
 '  The TC74  device i2c address is specified as follows....                         
'   ADDRESS FROM DATA SHEET      ADDRESS  FOR I2C WRITE                 READ ADDS 1 TO WRITE address
'   ------------------------   -----------------------------            ----------------------------
'   A6 A5 A4 A3 A2 A1 A0 HEX   A7 A6 A5 A4 A3 A2 A1 A0   HEX            Hex
'   1  0  0  1  0  0  0  $48   1  0  0  1  0  0  0  R/W  $90 (WRITE) or $91 (READ) REGISTER  TC74-A0                     
'   1  0  0  1  0  0  1  $49   1  0  0  1  0  0  1  R/W  $92 (WRITE) or $93 (READ) REGISTER  TC74-A1                       
'   1  0  0  1  0  1  0  $4A   1  0  0  1  0  1  0  R/W  $94 (WRITE) or $95 (READ) REGISTER  TC74-A2                     
'   1  0  0  1  0  1  1  $4B   1  0  0  1  0  1  1  R/W  $96 (WRITE) or $97 (READ) REGISTER  TC74-A3     
'   1  0  0  1  1  0  0  $4C   1  0  0  1  1  0  0  R/W  $98 (WRITE) or $99 (READ) REGISTER  TC74-A4   
'   1  0  0  1  1  0  1  $4D   1  0  0  1  1  0  1  R/W  $9A (WRITE) or $9B (READ) REGISTER  TC74-A5
'   1  0  0  1  1  1  0  $4E   1  0  0  1  1  1  0  R/W  $9C (WRITE) or $9D (READ) REGISTER  TC74-A6
'   1  0  0  1  1  1  1  $4F   1  0  0  1  1  1  1  R/W  $9E (WRITE) or $9F (READ) REGISTER  TC74-A7     
'

;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

  'Device = 18F14K22

Config_Start
  FOSC = HS        ;HS oscillator
  PLLEN = On    ;Oscillator multiplied by 4
  PCLKEN = On    ;Primary clock enabled
  FCMEN = OFF    ;Fail-Safe Clock Monitor disabled
  IESO = OFF    ;Oscillator Switchover mode disabled
  PWRTEN = OFF    ;PWRT disabled
  BOREN = SBORDIS    ;Brown-out Reset enabled in hardware only (SBOREN is disabled)
  BORV = 19    ;VBOR set to 1.9 V nominal
  WDTEN = OFF    ;WDT is controlled by SWDTEN bit of the WDTCON register
  WDTPS = 32768    ;1:32768
  HFOFST = On    ;HFINTOSC starts clocking the CPU without waiting for the oscillator to stablize.
  MCLRE = OFF    ;RA3 input pin enabled; MCLR disabled
  STVREN = On    ;Stack full/underflow will cause Reset
  LVP = OFF    ;Single-Supply ICSP disabled
  BBSIZ = OFF    ;1kW boot block size
  XINST = OFF    ;Instruction set extension and Indexed Addressing mode disabled (Legacy mode)
  Debug = OFF    ;Background debugger disabled, RA0 and RA1 configured as general purpose I/O pins
  Cp0 = OFF    ;Block 0 not code-protected
  CP1 = OFF    ;Block 1 not code-protected
  CPB = OFF    ;Boot block not code-protected
  CPD = OFF    ;Data EEPROM not code-protected
  WRT0 = OFF    ;Block 0 not write-protected
  WRT1 = OFF    ;Block 1 not write-protected
  WRTC = OFF    ;Configuration registers not write-protected
  WRTB = OFF    ;Boot block not write-protected
  WRTD = OFF    ;Data EEPROM not write-protected
  EBTR0 = OFF    ;Block 0 not protected from table reads executed in other blocks
  EBTR1 = OFF    ;Block 1 not protected from table reads executed in other blocks
  EBTRB = OFF    ;Boot block not protected from table reads executed in other blocks
Config_End

;**** End of Fuse Configurator Settings ****
;-------------------------------------------------------------------------------