w_arm
Joined: 10 Apr 2007 Posts: 4 Location: Desenvolvedor
|
Conversion 80C51 family for PIC16F877 |
Posted: Thu May 10, 2007 10:47 am |
|
|
Which are the necessary modifications below for the code to work with PIC16F877??
//**********************************************************************************
//Project: SHT11 demo program (V2.0)
//Filename: SHT11.c
//Prozessor: 80C51 family
//Compiler: Keil Version 6.14
//Autor: MST
//Copyrigth: (c) Sensirion AG
//***********************************************************************************
#include "AT89s53.h" //Microcontroller specific library, e.g. port definitions
#include "intrins.h" //Keil library (is used for _nop()_ operation)
#include "math.h" //Keil library
#include "stdio.h" //Keil library
typedef union
{
unsigned int i;
float f;
}value;
//----------------------------------------------------------------------------------
// modul-var
//----------------------------------------------------------------------------------
enum {TEMP,HUMI};
#define DATA P1_1
#define SCK P1_0
#define noACK 0
#define ACK 1
//adr command r/w
#define STATUS_REG_W 0x06 //000 0011 0
#define STATUS_REG_R 0x07 //000 0011 1
#define MEASURE_TEMP 0x03 //000 0001 1
#define MEASURE_HUMI 0x05 //000 0010 1
#define RESET 0x1e //000 1111 0
//----------------------------------------------------------------------------------
// writes a byte on the Sensibus and checks the acknowledge
//----------------------------------------------------------------------------------
char s_write_byte(unsigned char value)
{
unsigned char i,error=0;
for(i=0x80; i>0; i/=2) //shift bit for masking
{
if(i & value)
DATA=1; //masking value with i , write to SENSI-BUS
else
DATA=0;
SCK=1; //clk for SENSI-BUS
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
}
DATA=1; //release DATA-line
SCK=1; //clk #9 for ack
error=DATA; //check ack (DATA will be pulled down by SHT11)
SCK=0;
return error; //error=1 in case of no acknowledge
}
//----------------------------------------------------------------------------------
// reads a byte form the Sensibus and gives an acknowledge in case of "ack=1"
//----------------------------------------------------------------------------------
char s_read_byte(unsigned char ack)
{
unsigned char i,val=0;
DATA=1; //release DATA-line
for(i=0x80; i>0; i/=2) //shift bit for masking
{
SCK=1; //clk for SENSI-BUS
if(DATA)
val=(val | i); //read bit
SCK=0;
}
DATA = !ack; //in case of "ack==1" pull down DATA-Line
SCK=1; //clk #9 for ack
_nop_();_nop_();_nop_(); //pulswith approx. 5 us
SCK=0;
DATA=1; //release DATA-line
return val;
}
//----------------------------------------------------------------------------------
// generates a transmission start
//----------------------------------------------------------------------------------
void s_transstart(void)
// _____ ________
// DATA: |_______|
// ___ ___
// SCK : ___| |___| |______
{
DATA=1;
SCK=0; //Initial state
_nop_();
SCK=1;
_nop_();
DATA=0;
_nop_();
SCK=0;
_nop_();_nop_();_nop_();
SCK=1;
_nop_();
DATA=1;
_nop_();
SCK=0;
}
//----------------------------------------------------------------------------------
// communication reset: DATA-line=1 and at least 9 SCK cycles followed by transstart
//----------------------------------------------------------------------------------
void s_connectionreset(void)
// _____________________________________________________ ________
// DATA: |_______|
// _ _ _ _ _ _ _ _ _ ___ ___
// SCK : __| |__| |__| |__| |__| |__| |__| |__| |__| |______| |___| |______
{
unsigned char i;
DATA=1;
SCK=0; //Initial state
for(i=0; i<9>=1 in case of no response form the sensor
}
//------------------------------------------------------------------------------
// makes a measurement (humidity/temperature) with checksum
//------------------------------------------------------------------------------
char s_measure(unsigned char *p_value, unsigned char *p_checksum, unsigned char mode)
{
unsigned error=0;
unsigned int i;
s_transstart(); //transmission start
switch(mode)
{ //send command to sensor
case TEMP : error+=s_write_byte(MEASURE_TEMP); break;
case HUMI : error+=s_write_byte(MEASURE_HUMI); break;
default : break;
}
for(i=0;i<65535>100)
rh_true=100; //cut if the value is outside of
if(rh_true<0.1)
rh_true=0.1; //the physical possible range
*p_temperature = t_C; //return temperature [C]
*p_humidity = rh_true; //return humidity[%RH]
}
//------------------------------------------------------------------------------
// calculates dew point
// input: humidity [%RH], temperature [C]
// output: dew point [C]
//------------------------------------------------------------------------------
float calc_dewpoint(float h,float t)
{
float k,dew_point ;
k = (log10(h)-2)/0.4343 + (17.62*t)/(243.12+t);
dew_point = 243.12*k/(17.62-k);
return dew_point;
}
//----------------------------------------------------------------------------------
// sample program that shows how to use SHT11 functions
// 1. connection reset
// 2. measure humidity [ticks](12 bit) and temperature [ticks](14 bit)
// 3. calculate humidity [%RH] and temperature [C]
// 4. calculate dew point [C]
// 5. print temperature, humidity, dew point
//----------------------------------------------------------------------------------
void main()
{
value humi_val,temp_val;
float dew_point;
unsigned char error,checksum;
unsigned int i;
s_connectionreset();
while(1)
{
error=0;
error+=s_measure((unsigned char*) &humi_val.i,&checksum,HUMI); //measure humidity
error+=s_measure((unsigned char*) &temp_val.i,&checksum,TEMP); //measure temperature
if(error!= 0)
s_connectionreset(); //in case of an error: connection reset
else
{
humi_val.f = (float)humi_val.i; //converts integer to float
temp_val.f = (float)temp_val.i; //converts integer to float
calc_sth11(&humi_val.f,&temp_val.f); //calculate humidity, temperature
dew_point = calc_dewpoint(humi_val.f,temp_val.f); //calculate dew point
//send final data to serial interface (UART)
printf("temp:%5.1fC humi:%5.1f%% dew point:%5.1fC\n",temp_val.f,humi_val.f,dew_point);
}
//----------wait approx. 0.8s to avoid heating up SHTxx------------------------------
for(i=0; i<40000; i++); //(be sure that the compiler doesn’t eliminate this line!)
//-----------------------------------------------------------------------------------
}
} |
|