[TI首届低功耗设计大赛]+环境温,湿度采集显示
1、硬件
2、实物布局
3、程序
直接采用TI的EUSCI库函数
//*************************************************************
//slave_address:器件地址
//reg :寄存器
//lenght :读数据长度
//*data :读寄存器地址
//*************************************************************
void I2C(unsigned char slave_address,unsigned char reg,unsigned char lenght,unsigned char *data)
{
unsigned char i=0;
//Initialize Master
EUSCI_B_I2C_masterInit(EUSCI_B0_BASE,
EUSCI_B_I2C_CLOCKSOURCE_SMCLK,
CS_getSMCLK(__MSP430_BASEADDRESS_PMM_FRAM__),
EUSCI_B_I2C_SET_DATA_RATE_100KBPS,
1,
EUSCI_B_I2C_NO_AUTO_STOP
);
//Specify slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
slave_address
);
//Set in transmit mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
EUSCI_B_I2C_TRANSMIT_MODE
);
//Enable I2C Module to start operations
EUSCI_B_I2C_enable(EUSCI_B0_BASE);
//Send single byte data.
EUSCI_B_I2C_masterSendSingleByte(EUSCI_B0_BASE,reg);
//Delay until transmission completes
while (EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE)) ;
__delay_cycles(70000);
//Send single byte data.
//EUSCI_B_I2C_masterSendSingleByte(EUSCI_B0_BASE,address);
//Delay until transmission completes
//while (EUSCI_B_I2C_isBusBusy(EUSCI_B0_BASE)) ;
// __delay_cycles(70000);
//接收
//如果lenght>0,有读模式
if(lenght>0)
{
EUSCI_B_I2C_masterInit(EUSCI_B0_BASE,
EUSCI_B_I2C_CLOCKSOURCE_SMCLK,
CS_getSMCLK(__MSP430_BASEADDRESS_PMM_FRAM__),
EUSCI_B_I2C_SET_DATA_RATE_100KBPS,
1,
EUSCI_B_I2C_SEND_STOP_AUTOMATICALLY_ON_BYTECOUNT_THRESHOLD
);
//Specify slave address
EUSCI_B_I2C_setSlaveAddress(EUSCI_B0_BASE,
slave_address
);
//Set Master in receive mode
EUSCI_B_I2C_setMode(EUSCI_B0_BASE,
EUSCI_B_I2C_RECEIVE_MODE
);
//Enable I2C Module to start operations
EUSCI_B_I2C_enable(EUSCI_B0_BASE);
EUSCI_B_I2C_clearInterruptFlag(EUSCI_B0_BASE,
EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
);
//Enable master Receive interrupt
EUSCI_B_I2C_enableInterrupt(EUSCI_B0_BASE,
EUSCI_B_I2C_RECEIVE_INTERRUPT0 +
EUSCI_B_I2C_BYTE_COUNTER_INTERRUPT
);
while(i
{
// I2C start condition
data
= EUSCI_B_I2C_masterReceiveSingleByte(EUSCI_B0_BASE);
i++;
__delay_cycles(70000);
}
}
}
float read_temp(void)
{
unsigned char tempdata[3];
unsigned int uitemp=0;
float ftemp=0.0;
I2C(HAL_SHT21_I2C_ADDRESS,SHT21_CMD_TEMP_T_NH,2,&tempdata[0]);
uitemp=tempdata[0];
uitemp=(uitemp<<8)+tempdata[1];
uitemp= uitemp & 0xfffc;
ftemp= (float)uitemp* 0.002681-46.85;
return ftemp;
}
float read_humi(void)
{
unsigned char tempdata[3];
unsigned int uitemp=0;
float ftemp=0.0;
I2C(HAL_SHT21_I2C_ADDRESS,SHT21_CMD_HUMI_T_NH,2,&tempdata[0]);
uitemp=tempdata[0];
uitemp=(uitemp<<8)+tempdata[1];
uitemp= uitemp & 0xfff0;
ftemp= (float)uitemp* 0.001907 -6;
return ftemp;
}
4、SPI和I2C复用,要不断的初始化端口
5、操作效果