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

📄 main.c

📁 DS1722 accessed via SPI on HCS12
💻 C
字号:
/*
Travis Lytle - ECET483
SPI Tempature Sensor code
Maxim IC temp sensor DS1722
http://datasheets.maxim-ic.com/en/ds/DS1722.pdf

Using HCS12 S12UB board 

HC12				DS1722					DS1722 PINS 
SCLK----------------SCLK					3
SS----5V			Chip Select---PortA0	2
MOSI----------------SDI						6
MISO----------------SDO						5
*/

#include <hidef.h>      // common defines and macros
#include <mc9s12dg128.h>     // derivative information
#pragma LINK_INFO DERIVATIVE "mc9s12dg128b"
#include <stdio.h>

int tmp, tmp_msb, tmp_lsb, tmph, tmpc, tmpf;
char tempu[25];	

void initSCI0(void);
char getChar(void);
void putChar(char), putString(char *);

void initSPI0(void);
void spiput1722(int);		
int spiget1722(int);	

void main(void) 
{
  uchar ch;
  
  DDRA = 0xFF;				//Port A 0x01 used for chip select

  initSCI0();
  initSPI0();
  spiput1722(0xE8);     	//configure temp sensor

   while(1)	
   {
    ch = getChar();	
      if(ch == 0x20){					//checks for space key input
        
      tmp_lsb = spiget1722(0x01);		
      tmp_msb = spiget1722(0x02);		
      
      tmp = tmp_msb << 8; 
      tmp = tmp | tmp_lsb;		
      tmph = tmp;
      
      tmpc = tmp/255;
      tmpf = ((tmp/255) *9/5) +32;	//convert to Fahrenheit

      sprintf(tempu, "Hex:%4X Temp F:%d C:%d", tmph, tmpf, tmpc);
      putString(tempu);
      putString("\n\r");
      }
   }
}

void initSPI0(void) 
{
 SPI0CR1 = 0x5E;  			//SPI on, set to master, and clock phase bit
 SPI0CR2 = 0x00;  
 SPI0BR = 0x77;   			//SPI baud rates: 0x77 fastest, 0x00 slowest
}

void spiput1722(int cx)
{  
 int temp;					

 PORTA = 0x01;				//chip select
 while (!(SPI0SR & 0x20));		
 SPI0DR = 0x80;				//address for writing
 while (!(SPI0SR & 0x80));		
 temp = SPI0DR;
 while (!(SPI0SR & 0x20));		
 SPI0DR = cx;				//config data							  
 while (!(SPI0SR & 0x80));		
 PORTA = 0x00;								
}

int spiget1722(int cx)
{  
 int temp;						        

 PORTA = 0x01;				//chip select
 while (!(SPI0SR & 0x20));		
 SPI0DR = cx;				//address for reading  usually 0x01 or 0x02 for this chip		  
 while (!(SPI0SR & 0x80));		
 temp = SPI0DR;								
 while (!(SPI0SR & 0x20));		
 SPI0DR = 0x00;				//dummy data				
 while (!(SPI0SR & 0x80));		
 PORTA = 0x00;								
 return(SPI0DR);						  
}

/********************** SCI **************************************************/
void initSCI0() {
  char x;
  SCI0BDH = 0x00; 	//Baud Rate H Byte
  SCI0BDL = 26;  	//Baud Rate L Byte  9600 Baud
  SCI0CR1 = 0x00;  	
  SCI0CR2 = 0x0C; 	//TE and RE enabled
  x = SCI0SR1;     	//status reg    clears RDRF flag
  x = SCI0DRL;     	//data reg
}

void putString(char *cx)
{
  while((*cx)) 
  {
    putChar(*cx);
    cx++;
  }
}

/* gets  a char from the serial input.
waits for RDRF flag, which is recieve data registry full*/
char getChar(void) 
{
  while((SCI0SR1 & 0x20) == 0x00);
  return (SCI0DRL);
}

/* puts  a char to the serial input.
waits for TDRE flag, which is transmit data registry empty*/
void putChar(char cx)
{ 
  while(!(SCI0SR1 & 0x80));      
  SCI0DRL = cx; 
}

⌨️ 快捷键说明

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