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

📄 main.c

📁 TMS320LF2407A与TMP101A温度芯片的接口开发程序
💻 C
字号:


//TMP101测温+显示程序

#include "LF2407register.h"
#include "math.h"

#define COM1_ON  0X0101    ;
#define COM2_ON  0X0202    ;
#define COM3_ON  0X0404    ;
#define COM4_ON  0X0808    ;


unsigned digit[10]={0x0FFC0,0x0FFCF,0x0FFA4,0x0FFB0,0x0FF99,0x0FF92,0x0FF82,0x0FFF8,0x0FF80,0x0FF98} ;

void delay(unsigned j)
{
  while(j--)    ;
}

void delay_temp(void)
 {
   asm(" nop ")    ;
   asm(" nop ")    ;
   asm(" nop ")    ;
   asm(" nop ")    ;

 }

//系统初始化程序

void initial(void)
{   asm(" setc SXM") ;
    asm(" clrc OVM") ;
    asm(" clrc CNF") ;
    asm(" setc INTM");

  *SCSR1=0X8201      ;
  *WDCR=0XE8         ;
                      
  *IMR=0X0000        ;
  *IFR=0X0FFFF       ;
   WSGR=0X00         ;

  *MCRA=0X0000       ; 
  *MCRC=0X0000       ;
  
}

void display(int rst)
{  unsigned a,b,c,d        ;
   float    sigma          ;
   sigma=(float)rst*10/8   ;
   if(rst>=0)   
    { 
      a=(unsigned)sigma/1000          ;
    }
   else
    {
      a=0XFFBF                ;//SHOW THE SIGINAL OF "-"
    }
     b=(unsigned)sigma%1000             ;
     c=b%100                  ;
     b=b/100                  ;
     d=c%10                   ;
     c=c/10                   ;

   *PEDATDIR=digit[a]       ;
   *PBDATDIR=COM4_ON        ;
   delay(300)               ;
   *PBDATDIR=0xff00         ;
   delay(5)                ;
  
   *PEDATDIR=digit[b]       ;
   *PBDATDIR=COM3_ON        ;
   delay(300)               ;
   *PBDATDIR=0xff00         ;
   delay(5)                ;

   *PEDATDIR=(digit[c]&0xFF7F);//SET THE LED 2,S POINT 
   *PBDATDIR=COM2_ON        ; 
   delay(300)               ;
   *PBDATDIR=0xff00         ;
   delay(5)                ;

   *PEDATDIR=digit[d]       ;
   *PBDATDIR=COM1_ON        ;
   delay(300)               ;
   *PBDATDIR=0xff00         ;
    delay(5)               ;
}

void start(void)
 {
   *PADATDIR=0XFF01          ;//初始化状态,SDA=1,SCL=0
   delay_temp()                 ;
   *PADATDIR=0XFF03          ;//初始化状态,SDA=1,SCL=1
   delay_temp()                 ;
   *PADATDIR=0xFF02          ;//SDA从1跳变为0,形成起始状态
   delay_temp()                ;
   *PADATDIR=0XFF00          ;//将SCL置低,开始写数据到SDA
   delay_temp()                 ;
  
 } 
void stop(void)
 {
   *PADATDIR=0XFF02           ;//SDA=0
   delay_temp()                  ;
   *PADATDIR=0xFF03           ;//SDA从0跳变为1,形成stop
   delay_temp()                  ;
   *PADATDIR=0xFF01           ;//SCL=0, SET THE SCL=0 AS A USUAL STAT.
   delay_temp()                  ;
 }

void write(unsigned char dat)

{  unsigned char j,shift=0x80,temp ;
   unsigned APORT                  ;
   
CHA: for(j=0;j<8;j++)
    {
      if(dat&shift)
        APORT=0xff01            ;//将数据进行正确的配置,以适合A口
      else
        APORT=0XFF00            ;//以上将DAT的最高位放到了IOPA.0上,即SDA上
      *PADATDIR=APORT          ;
	  delay_temp()               ;
      *PADATDIR|=0XFF02        ;//将SCL拉高,并且保持SDA上的数据
      delay_temp()                ; 
	  *PADATDIR&=0XFF01        ;//SET SCL TO LOW WHICH IS A USUAL STAT
	  delay_temp()                ;
      shift>>=1                ;        
    } 
// 以下为ACK_CHECK (be aware that the SCL is low as a usual stat)
     
     *PADATDIR=0XFE01       ;//SETB SDA;
	   delay_temp()             ;

	 *PADATDIR|=0XFE02       ;//SETB SCL;
	   delay_temp()             ;

	  if(*PADATDIR&0X0001)
   	     goto CHA             ;

      else
       {
         *PADATDIR&=0XFE01       ;//CLR SCL;
         delay_temp()               ;	   
	     *PADATDIR=0XFF01       ;//CLR SCL;

       }
}


unsigned read(void)  // this part are properly work,

{ 
  int temp=0               ;
  unsigned char i,j        ;
  start()       ;//restart
  write(0x91)   ;//select the tmp101,and the R/W is 1 means to read
  
  for(i=0;i<2;i++)
   { 
    for(j=0;j<8;j++)
     {
        *PADATDIR=0XFE01       ;//set the SDA as the mode of input
        delay_temp()            ;
        *PADATDIR|=0XFE02      ;//SETB SCL ;BE NOTICED THAT THE DATA IS RIGHT WHEN SCL=1
	    delay_temp()            ;
	    if(*PADATDIR&0x0001)
	       { 
		     temp<<=1          ;
	         temp+=1           ;
		   }
         else
	       temp<<=1            ;

         *PADATDIR&=0XFE01     ;//SCL=0; this word can be ommitted because of scl=0 is the usual stat
         delay(30)            ;

    }
         
           //以下为产生ACK
         *PFDATDIR=0XFF00          ;//SDA=0,SCL=0
         delay_temp()                ;
         *PFDATDIR=0XFF02          ;//SDA=0,SCL=1
         delay_temp()                ;
         *PFDATDIR=0XFF00          ;//SCL=0,SDA=0
         delay(30)                   ;
   }    
          stop()                     ;
          return(temp)               ;  
}

void TMP_init(void)      
 { 
   //set the Thigh
   start()       ; 
   write(0x90)   ;//select the tmp101,address and mode is to write 
   write(0x03)   ;//通过POINT REG选中T high REGISTER.
   write(0x64)   ;
   write(0x00)   ;//0x6400 as the 100 degree
   stop()        ;
   //set the Tlow.
   start()       ; 
   write(0x90)   ;
   write(0x01)   ;
   write(0x00)   ;
   write(0x00)   ;//0x0000 as the 0 degree
   stop()        ;

   start()       ;//produce the suitable start condition. 
   write(0x90)   ;//select the tmp101,address and mode is to write 
   write(0x01)   ;//通过POINT REG选中CONFIG REGISTER.
   write(0x28)   ;//通过配置CONFIG REG,将精度设置为10位,连续转换模式。
   stop()        ;
   start()       ;//produce the suitable start condition. 
   write(0x90)   ;//select the tmp101,address and mode is to write 
   write(0x00)   ;//通过POINT REG选中temperature REGISTER.
   stop()        ;
}



void main(void)
{ int result               ; 
  initial()                ;
  TMP_init()               ;
  delay(320000)            ;
  while(1)
   {
     result=read()         ;
     display((result>>6))    ;
   }
}

void interrupt nothing()
 {
  return            ;
 }



⌨️ 快捷键说明

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