/**************************************************************/
/**/
/**/
/**************************************************************/
#include <at89lp52.h>
#include <stdio.h>
#include "sensor.h"
#define uint unsigned int
#define uchar unsigned char
/*-------------------------------------------------------------*/
/****************Ports and variations dellarations**************/
/*-------------------------------------------------------------*/
sbit LED_PORT = P3^6;
uchar ADS1115_BUF[9];
uchar Tick5ms_Num;
uchar channel_1, channel_2, channel_3;
unsigned long temperature, windspeed, humidity;
float WS, RH, Pr, Te;
idata uchar Result1[17] = " C m/s";
idata uchar Result2[17] = " % hP";
bit k = 0;
extern bit flag_100ms;
extern bit flag_500ms;
extern bit flag_1s;
extern bit RS485LSTN;
extern bit RS485TALK;
extern bit RS485DGrecv(void);
extern bit RS485DGsend(void);
extern bit RS485CheckDG(void);
extern uchar RS485BUFidx;
extern void RS485ParseDG(uchar *);
extern uint ADS1115_RD(uchar);
extern ADS1115_Scan(char *);
extern uchar ADS1115_chn;
extern void MS5607_get_cal_params();
extern void MS5607_calTP(void);
extern int T100; //Temperature[0.01C]
extern long Pa; //Pressure[Pa]
extern void System_Init(void);
extern void Wait_Ticks(void);
extern void Send2OLED(uchar *);
/*-------------------------------------------------------------*/
/***************************The End*****************************/
/*-------------------------------------------------------------*/
/*-------------------------------------------------------------*/
/****************Functions declarations of main.c***************/
/*-------------------------------------------------------------*/
void FloatToStr(float, uchar *, uchar);
void Get_Data(void);
void Data_Prepare(void);
void RS484_Check(void);
void Delay_5ms(uint);
void Time_Event(void);
/*-------------------------------------------------------------*/
/***************************The End*****************************/
/*-------------------------------------------------------------*/
/*-------------------------------------------------------------*/
/*************************Main function*************************/
/*-------------------------------------------------------------*/
void main()
{
uchar j;
System_Init();
LED_PORT = 0;
Delay_5ms(200);
LED_PORT = 1;
MS5607_get_cal_params();
while(1)
{
Time_Event();
}
}
/*-------------------------------------------------------------*/
/******************************The End**************************/
/*-------------------------------------------------------------*/
/*-------------------------------------------------------------*/
/******************Functions Definitions of main.c**************/
/*-------------------------------------------------------------*/
//*************************************************************/
//*Function Name:FloatToStr(float num, uchar *str, uchar p)
//*Function :change a float number into a string
//*Parameter :float number,char string,start point
//*Return Value :None
//*************************************************************/
void FloatToStr(float num, uchar *str, uchar p)
{
str[p+0] = (uint)num/10+0x30;
str[p+1] = (uint)num%10+0x30;
str[p+2] = '.';
str[p+3] = (uint)(num*10)%10+0x30;
str[p+4] = (uint)(num*100)%10+0x30;
}
//*************************************************************/
//*Function Name:Get_Data(void)
//*Function :Get the Data from ADS1115
//*Parameter :None
//*Return Value :None
//*************************************************************/
void Get_Data(void)
{
if((ADS1115_RD(0X01)&0X8000)) //Abort if OS=0[in conversion]
{
ADS1115_Scan(ADS1115_BUF);
if((ADS1115_chn & 0X03) == 0) //The last conversion is channel 1
{
channel_1++;
temperature += (ADS1115_BUF[0]*256 + ADS1115_BUF[1]);
}
if((ADS1115_chn & 0X03) == 1) //The last conversion is channel 2
{
channel_2++;
humidity += (ADS1115_BUF[2]*256 + ADS1115_BUF[3]);
}
if((ADS1115_chn & 0X03) == 2) //The last conversion is channel 3
{
channel_2++;
windspeed += (ADS1115_BUF[4]*256 + ADS1115_BUF[5]);
}
}
}
//*************************************************************/
//*Function Name:Data_Prepare(void)
//*Function :Prepare the Data to be send
//*Parameter :None
//*Return Value :None
//*************************************************************/
void Data_Prepare(void)
{
Te = Temperature(temperature/50.0);
RH = Humidity(humidity/50.0);
WS = Wind_Speed(windspeed/50.0);
Pr = Pa;
temperature = 0;
humidity = 0;
windspeed = 0;
channel_1 = 0;
channel_2 = 0;
channel_3 = 0;
FloatToStr(Te, Result1, 0);
FloatToStr(WS, Result1, 7);
FloatToStr(RH, Result2, 0);
Result2[7] = (uint)Pr/1000+0x30;
Result2[8] = (uint)Pr%1000/100+0x30;
Result2[9] = (uint)Pr%100/10+0x30;
Result2[10] = (uint)Pr%10+0x30;
Result2[11] = '.';
Result2[12] = (uint)(Pr*10)%10+0x30;
Result1[15] = '\n';
Result2[15] = '\n';
}
//*************************************************************/
//*Function Name:RS484_Check(void)
//*Function :Do the RS485 task
//*Parameter :None
//*Return Value :None
//*************************************************************/
void RS484_Check(void)
{
if(RS485LSTN) //Client in LISTEN mode?
{
if(!RS485DGrecv()) //Receive one datagram
{
LED_PORT = !LED_PORT;
if(!RS485CheckDG()) //Check IDN and CKS...
{
if(k == 0) //Send the first datagram(Temperature & Wind speed)
{
k = 1;
RS485ParseDG(Result1); //Processing the datagram and prepare the firt 16 bytes data to response
RS485LSTN = 0; //Pause datagram listen
RS485TALK = 1; //Ready to send the response datagram
}
else //Send the second datagram(Humidity & Presure)
{
k = 0;
RS485ParseDG(Result2); //Processing the datagram and prepare the firt 16 bytes data to response
RS485LSTN = 0; //Pause datagram listen
RS485TALK = 1; //Ready to send the response datagram
}
}
RS485BUFidx = 0; //Reset datagram buffer
}
}
if(RS485TALK)
{
if(!RS485DGsend())
{
LED_PORT = !LED_PORT;
}
RS485LSTN = 1;
RS485TALK = 0;
}
}
//*************************************************************/
//*Function Name:void Time_Event(void)
//*Function :execute task of different time interval
//*Parameter :None
//*Return Value :None
//*************************************************************/
void Time_Event(void)
{
//Set CPU into IDLE mode when no task to do
//it will be woke up by interrupt every 5ms
/***********Task in each 5ms interval************/
PCON |= 0X01;
Get_Data();
RS484_Check();
/************************************************/
if(flag_500ms == 1)
{
/********Task in each 500ms interval*********/
flag_500ms = 0;
MS5607_calTP();
Data_Prepare();
// j++;
// if(j < 5)
// {
// Send2OLED(Result1);
// }
// else
// {
// Send2OLED(Result2);
// if(j == 10)
// j = 0;
/********************************************/
}
if(flag_1s == 1)
{
/*********Task in each 1s interval***********/
flag_1s = 0;
LED_PORT =! LED_PORT; //working station indicator
/********************************************/
}
}
//*************************************************************/
//*Function Name:void Delay_5ms(uint delay4msval)
//*Function :Delay 0~65536*5ms
//*Parameter :The number of 5ms
//*Return Value :None
//*************************************************************/
void Delay_5ms(uint delay4msval)
{
while(delay4msval)
{
PCON |= 0X01;
--delay4msval;
Time_Event();
}
}
/*-------------------------------------------------------------*/
/******************************The End**************************/
/*-------------------------------------------------------------*/
|