/* ========================================
*
* 程序名称: PSOC3与LM92之间的数据通讯
* 功能描述: 用PSOC3通过I2C总线实现温度采集并显示在1602液晶显示器上
* 使用模块; I2C主机模块
* 作 者: 万选明
* 完成时间: 2011年3月28日
* 备 注: 无
* ========================================
*/
#include
/*以下是用到的函数声明*/
float Tempture(uint8 slaveAddr); //函数声明
void UpdateDisplay (float voltageRawCount,uint16 Channel); //显示函数
void Delay(uint16 Time); //延时函数
void UART_TX_float(float data1);
#define LM92_Addr (0x48u) //定义LM82从机地址
float Temp = 0.0; //存温度变量
void main()
{
uint8 Data = 0;
CYGlobalIntEnable;
I2C_1_Start(); //I2C总线启动开始
LCD_Char_1_Start(); //1602启动开始
UART_1_Start();
LCD_Char_1_Position(0,0);
/*移动光标到(0,0)处 */
LCD_Char_1_PrintString("Tempture: "); /*显示Tempture: */
UART_1_ClearRxBuffer();
// UART_1_EnableRxInt();
// UART_1_EnableRxInt();
// UART_1_SetRxInterruptMode(UART_RX_STS_FIFO_NOTEMPTY) ;
for(;;)
{
//{
Temp = Tempture(LM92_Addr); /* 读取温度值 */
UpdateDisplay (Temp,1); /*显示温度数据 */
Delay(500); /*延时 */
if ( UART_1_ReadRxData()== 0x48) //是本机地址,发送温度数据
{
UART_TX_float(Temp); //发送温度数据
}
// }
}
}
/* ========================================
*
* 函数名称:float Tempture(uint8 slaveAddr)
* 功能描述:从从机中读取数字温度值并转换成实际温度
* 参 数; slaveAddr--从机地址
* 返 回:TEMP -- 实际温度值
* 完成时间:2011年3月28日
* 备 注: 无
* ========================================*/
float Tempture(uint8 slaveAddr)
{
uint8 Status;
int Data[4];
float TEMP;
I2C_1_MasterClearStatus(); //清除I2C状态数据
Status =I2C_1_MasterSendStart(slaveAddr, I2C_1_READ_XFER_MODE); //发送读数据命令
if(Status == I2C_1_MSTR_NO_ERROR) /* 判断状态 */
{
/* 读回温度数据 */
Data[0] = I2C_1_MasterReadByte(I2C_1_ACK_DATA); //读取温度数据高8位
Data[1] = I2C_1_MasterReadByte(I2C_1_ACK_DATA); //读取温度数据低8位
Data[2] = I2C_1_MasterReadByte(I2C_1_NAK_DATA); //温度读完读NCK信号
}
I2C_1_MasterSendStop(); /* 发送停止 */
Data[3] = (Data[0] <<8)| (Data[1] & 0x00ff) ; //温度数据合并
Data[3] >>= 3 ; //温度从中高12位,为转换方便移到第十二位
if (Data[3] & 0x1000) // 是负数
{
Data[3] &= 0x0fff;
Data[3] = ~Data[3] + 1; // 补码运算
TEMP = -0.0625 * Data[3]; // 求出温度值
}
else
{
TEMP = 0.0625 * Data[3];
}
return (TEMP);
}
/* ========================================
*
* 函数名称:void UpdateDisplay (float voltageRawCount,uint16 Channel)
* 功能描述:显示数据
* 参 数; Data--显示的数据
* 返 回:无
* 完成时间:2011年3月28日
* 备 注: 无
* ========================================*/
void UpdateDisplay (float Data,uint16 Channel)
{
int Integer,Decimal; /*定义两个变量分别存整数部分和小数部分*/
Integer = (int) Data;
Decimal = (int) ((Data - Integer)*1000)+1; /*分别取出小数部分和小数部分,保留3位小数*1000,两位*100*/
LCD_Char_1_Position(0,9); /* 移动光标到(0,9) */
LCD_Char_1_PrintNumber(Integer); /* 输出整数部分 */
LCD_Char_1_PrintString("."); /* 输出小数点*/
LCD_Char_1_PrintNumber(Decimal); /*输出小数部分*/
LCD_Char_1_Position(1,0);
LCD_Char_1_PrintString("Channel: ");
LCD_Char_1_PrintNumber(Channel);
}
/* [] END OF FILE */
/* ========================================
*
* 函数名称:void Delay(int8 Time)
* 功能描述:延时
* 参 数; time--yan时时间
* 返 回:无
* 完成时间:2011年3月27日
* 备 注: 无
* ========================================*/
void Delay(uint16 Time)
{
int i=0,j=0 ;
for(j=0;j
{
for (i=0;i<1000;i++);
}
}
//****************************************************************************
//函数名:UART_TX_float(float data)
//功能:通过串口发送浮点数
//参数: data;要发送的浮点数
//返回: 无
//****************************************************************************
void UART_TX_float(float data1)
{
int High_data,Low_data,Mid_data;
Mid_data =(int)(data1*100); //取小数点后两位数字上传
Low_data = Mid_data & 0x00ff;
High_data = (Mid_data & 0xff00)>>8;
UART_1_WriteTxData(0x48); //回送本机地址
UART_1_WriteTxData(High_data); //发送温度数据的高8位数据 UART_1_WriteTxData(Low_data); //发送温度数据的低8位数据
}
当然,这个程序最好用中断的方式来实现,但由于时间关系,我没有测试,感兴趣的朋友可以测试后,把你的代码与我共享,在此声明,本程序仅是用来学习,与实际工程还有很大差异,有很多BUG,下位机的现象与上讲一样,在此不在重述。
|