⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mspf413-tmp100 interface.s43

📁 关于msp430f413用TMP100传感器测量温度的源码
💻 S43
📖 第 1 页 / 共 2 页
字号:
;******************************************************************************
;   MSP430F413 & TMP100 Software Interface
; 
;   Description: I2C communication with a TMP100 in one-shot conversion mode 
;   is demonstrated. Temperature is read from the TMP100 and displayed to LCD.
;   Only upper 8-bits from TMP100 temperature register is read, representing 
;   7Fh = 128oC to 80h = -128oC in 1oC steps. 
;   MSP430 is I2C master with TMP100 as the slave. Ack error checking on WR
;   not implemented. DCO/FLL generated MCLK = 1MHz. LMP3 implemented with
;   wake-up every 2 seconds as defined by Basic Timer interrupt from
;   watch crystal.
;
;          /|\            /|\ /|\
;           |    TMP100   10k 10k  MSP430F413           
;           |    -------   |   |  ---------- 
;           +---|Vcc SDA|<-|---+>|P2.0      |    +----------------------+
;            +--|A0     |  |     |    S0-S23|--->| Varitronix 3.5 digit |
;            |--|A1     |  |     |      COM0|--->|  Static LCD Display  |
;            |--|Vss SCL|<-+-----|P2.1      |    +----------------------+                        
;           \|/  -------         |          |
;                                |       XIN|<---32.768KHz Watch Crystal     
;
;
;            CPU registers used
#define      DIGITS   R6                    
#define      RXTXI2C  R7                    
#define      ADDRI2C  R8                    
#define      DATAI2C  R9                    
#define      BITI2C   R10                   
;
;            Definitions for I2C bus
TMPADDR      equ   090h                     ; TMP100 HW Address (A0=A1=0+WR)
SDA          equ   001h                     ; P2.0 controls SDA line (pull-up)
SCL          equ   002h                     ; P2.1 controls SCL line (pull-up)
;
;   Z. Albus
;   Texas Instruments, Inc
;   August 2002
;******************************************************************************

#include  "msp430x41x.h"
                                            ;
;------------------------------------------------------------------------------ 
; word & byte variables definition
            ORG     0200h
;------------------------------------------------------------------------------ 
Neg_Sign    DB      0                       ; Negative Temperature Flag
                                            ;
;------------------------------------------------------------------------------ 
            ORG     0F000h
;------------------------------------------------------------------------------ 
RESET       mov.w   #300h,SP                ; Initialize stack-pointer
            call    #Init_Sys               ; Initialize system
                                            ;
One_Shot_1  mov.b   #001h,ADDRI2C           ; ADDRI2C = TMP100 Pointer = Config    
            mov.b   #081h,DATAI2C           ; One-Shot Instruction Data
            call    #Write_I2C              ; Write to TMP100
                                            ;             
Mainloop    bis.w   #LPM3,SR                ; Enter LPM3
                                            ;
Read_Temp   mov.b   #000h,ADDRI2C           ; ADDRI2C = TMP100 Pointer = Data
            call    #Read_I2C               ; Get Upper 8-bit Conversion Result
            and.w   #000FFh,DATAI2C         ; Clear Upper Byte
                                            ;
            call    #Calc_Temp              ; Handle "-" Temperature Data
            call    #Disp_Signed_3_5        ; Update LCD with BCD Data
                                            ;
One_Shot    mov.b   #001h,ADDRI2C           ; ADDRI2C = TMP100 Pointer = Config
            mov.b   #081h,DATAI2C           ; One-Shot Instruction Data
            call    #Write_I2C              ; Write to TMP100
                                            ;
Main_End    jmp     Mainloop                ; Loop Again
                                            ;
;------------------------------------------------------------------------------ 
Init_Sys;   Subroutine sets up  Modules and Control Registers
;------------------------------------------------------------------------------ 
            mov.w   #WDTPW+WDTHOLD,&WDTCTL  ; Stop WDT
SetupFLL2   bis.b   #XCAP18PF,&FLL_CTL0     ; Set load capacitance for xtal
SetupLCD    mov.b   #0C5h,&LCDCTL           ; Static LCD, segments = 0 - 23
SetupBT     mov.b   #BTFRFQ1+BTFRFQ0+BTDIV+BTIP2+BTIP1+BTIP0,&BTCTL;
                                            ; 2s BT Int, Set LCD freq
            bis.b   #BTIE,&IE2              ; Enable Basic Timer interrupt      
SetupPorts  mov.b   #0FFh,&P1DIR            ; Set port to outputs
            clr.b   &P1OUT                  ; P1OUTs = 0
            mov.b   #0FFh,&P2DIR            ; Set port to outputs
            clr.b   &P2OUT                  ; P2OUTs = 0
            mov.b   #0FFh,&P3DIR            ; Set port to outputs
            clr.b   &P3OUT                  ; P3OUTs = 0
            mov.b   #0FFh,&P4DIR            ; Set port to outputs
            clr.b   &P4OUT                  ; P4OUTs = 0
            mov.b   #0FFh,&P5DIR            ; Set port to outputs
            clr.b   &P5OUT                  ; P5OUTs = 0
            mov.b   #0FFh,&P6DIR            ; Set port to outputs
            clr.b   &P6OUT                  ; P6OUTs = 0
                                            ;
            eint                            ; Enable Interrupts
                                            ;
            ret                             ; Return to Main
                                            ;            
;///////////I2C Subroutines start////////////////////////////////////////////// 
;------------------------------------------------------------------------------ 
Read_I2C   ; Reads two bytes of data transmitted from slave
;            enter ADDRI2C=00 - FF I2C device address to read
;                  RXTXI2C=x
;                  DATAI2C=x
;            exit  ADDRI2C=x
;                  RXTXI2C=x
;                  DATAI2C=0000 - FFFF I2C device data
;------------------------------------------------------------------------------ 
            mov.b   #TMPADDR,RXTXI2C        ; Load HW Address
            call    #I2C_Start              ; Send Start, Address and Ack
            mov.b   ADDRI2C,RXTXI2C         ; Load Pointer Address
            call    #I2C_TX                 ; Send Pointer and Ack
            mov.b   #TMPADDR,RXTXI2C        ; Load HW Address
            bis.b   #01h,RXTXI2C            ; Set LSB for "READ"
            call    #I2C_Start              ; Send Start, Address+RD and Ack
            call    #I2C_RX                 ; Read Data and Ack
                                            ;
;***** Used for 2-Byte transfer only *****  ;
;            call    #I2C_ACKn               ; Acknowledge Byte Rcv'd
;            call    #I2C_RX                 ; Read Data and Ack
                                            ;
            call    #I2C_NACKn              ; NOT Acknowledge Byte Rcv'd
            call    #I2C_Stop               ; Send Stop
            ret                             ; Return from subroutine
                                            ;
;------------------------------------------------------------------------------ 
Write_I2C;  enter ADDRI2C=00 - FF I2C device address to write to
;                 RXTXI2C=x
;                 DATAI2C=00 - FF I2C device data to write
;           exit  ADDRI2C=x
;                 RXTXI2C=x
;                 DATAI2C=x
;------------------------------------------------------------------------------ 
            mov.b   #TMPADDR,RXTXI2C        ; Load HW Address
            call    #I2C_Start              ; Send Start, Address and Ack
            mov.b   ADDRI2C,RXTXI2C         ; Load Pointer Address
            call    #I2C_TX                 ; Send Pointer and Ack
            mov.b   DATAI2C,RXTXI2C         ; Load Out-Going Data
            call    #I2C_TX                 ; Send Data and Ack
            call    #I2C_Stop               ; Send Stop
            ret                             ; Return from subroutine
                                            ;
;------------------------------------------------------------------------------ 
I2C_Start;  enter SDA=1, SCL=x
;           exit  SDA=1, SCL=0
;------------------------------------------------------------------------------ 
            bic.b   #SCL+SDA,&P2DIR         ; SCL and SDA to input direction
            bic.b   #SCL+SDA,&P2OUT         ; SCL=1, SDA=1
            bis.b   #SDA,&P2DIR             ; SDA=0
            bis.b   #SCL,&P2DIR             ; SCL=0
;------------------------------------------------------------------------------ 
I2C_TX;     enter SDA=x, SCL=0
;           exit  SDA=1, SCL=0
;------------------------------------------------------------------------------ 
            mov     #08,BITI2C              ; number of bits to xfer
I2C_TX_Bit  rla.b   RXTXI2C                 ; data bit -> carry
            jc      I2C_TX1                 ; test carry for 1 or 0
I2C_TX0     bis.b   #SDA,&P2DIR             ; SDA=0
            jmp     I2C_TXx                 ; Toggle SCL
I2C_TX1     bic.b   #SDA,&P2DIR             ; SDA=1
I2C_TXx     bic.b   #SCL,&P2DIR             ; SCL=1
            nop                             ; delay to meet I2C spec
            nop                             ;
            bis.b   #SCL,&P2DIR             ; SCL=0
            dec     BITI2C                  ; all bits read?

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -