#include "stdio.h"
#include "SH79F081.h"
#include "string.h"
#include "EUART.h"
#include <intrins.h>
//Pin defination
////////////////Gloable Variable defination/////////////////////////////////
//If this counter is more than one number, it means that alarm states.
unsigned int Timer0_Counter;
unsigned char SPI_Command_Data[5];
volatile unsigned char Flag;
sbit IR_CTRL = P3^0;
sbit IR_REC = P2^0;
unsigned char NEC_IR_ADDR1;
unsigned char NEC_IR_ADDR2;
#define RC5_Protocol 1
#define NEC_Protocol 2
volatile unsigned char Protocol_Type;
volatile unsigned int endcount;
unsigned char iPreBit,iIrMode;
unsigned int iHight,iLow;
unsigned char iBitCount,iIrData[4];
unsigned char IR_Data_Received;
unsigned char CheckIR();
sbit d_18b20=P3^1;//18b20器件引脚
unsigned char temp_int;//整数部分
unsigned char temp_point;//小数部分
unsigned int temph;
unsigned int templ;
////////////////End of Gloable Variable defination//////////////////////////
void Delayms(unsigned int ms)
{
int i,j;
for(j = 0; j < ms; j++)
{
for (i = 0; i < 820; i++); //1ms
}
}
void Init_Timer0_ISR(void)
{
TMOD &= 0xF0;
TMOD |= 0x01;
TL0 = 0x84;
TH0 = 0xFF;
ET0 = 1;
TR0 = 1;
EA = 1;
}
//External 0 interrupt service routine
void INT0_isr() interrupt 0 using 2
{
EA=0;
EA=1;
}
//Timer 0 interrupt service routine
void Timer0_ISR()interrupt 1
{
// EA=0;
if(1 == Protocol_Type)
{
TL0 = 0x85;
TH0 = 0xFF;
}
else if(2 == Protocol_Type)
{
TL0 = 0x83;
TH0 = 0xFF;
}
else if(3 == Protocol_Type)
{
TL0 = 0x85;
TH0 = 0xFF;
CheckIR();
}
if(1 == Flag)
{
IR_CTRL = !IR_CTRL;
}
else
{
IR_CTRL = 0;
}
Timer0_Counter++;
// EA=1;
}
void RC5_Send_IF_One()
{
}
void RC5_Send_IF_Zero()
{
}
void RC5_Send_IR_Data(unsigned char RC5_Address, unsigned char RC5_Command)
{
}
void NEC_Send_IR_Data(unsigned char p_irdata)
{
}
void NEC_Send_IR_Data_SAMSUNG(unsigned char p_irdata)
{
}
unsigned char CheckIR()
{
}
void delay_18b20(unsigned int i)//18b20延时程序
{
while(--i);
}
void initial_18b20()//初始化
{
unsigned char x = 0;
d_18b20=1;
P3CR = P3CR | 0x02;
delay_18b20(10);
d_18b20=0;
delay_18b20(600);
P3CR = P3CR & 0xFD;
P3PCR = P3PCR & 0xFD;
delay_18b20(55);
x = d_18b20;
delay_18b20(10);
while(x){;}
delay_18b20(150);
}
void ww_byte_18b20(unsigned char _data)//向18b20写入数据
{
unsigned char i;
d_18b20=1;
P3CR = P3CR | 0x02;
delay_18b20(5);
for(i=0;i<8;i++)
{
d_18b20=0;
delay_18b20(1);
d_18b20=_data&0x01;
delay_18b20(90);
d_18b20=1;
_data>>=1;
}
delay_18b20(10);
}
unsigned int rebyte(void)//读取温度子程序
{
unsigned char i,dat=0;
unsigned char x = 0;
for(i=0;i<8;i++)
{
dat>>=1;
d_18b20=1;
P3CR = P3CR | 0x02;
delay_18b20(1);
d_18b20=0;
delay_18b20(1);
P3CR = P3CR & 0xFD;
P3PCR = P3PCR & 0xFD;
delay_18b20(8);
x = d_18b20;
if(x)dat|=0x80;
delay_18b20(40);
}
delay_18b20(40);
return(dat);
}
unsigned int readtemp()//读取温度
{
initial_18b20();//先初始化18b20
ww_byte_18b20(0xcc);//跳过ROM匹配
ww_byte_18b20(0x44);//启动温度转换
delay_18b20(1200);
delay_18b20(1200);
delay_18b20(1200);
delay_18b20(1200);
delay_18b20(1200);
delay_18b20(1200);
initial_18b20();
ww_byte_18b20(0xcc);//跳过ROM匹配
ww_byte_18b20(0xbe);//读取温度寄存器等
templ=rebyte();
temph=rebyte();
temp_int=((temph<<4)&0x70)|((templ>>4)&0x0f);//整数部分
temp_point=(templ&0x0f)*5;//小数部分
return(temp_int*100+temp_point);
}
////////////////////////////////////////////////////////////////////////////
///////////////Main Function, the program starts from here///////////////////
void main()
{
unsigned char Receive_Data;
unsigned char String_Length;
unsigned int i,j;
unsigned char Parity,temp;
unsigned int Temperature;
unsigned char string[8];
unsigned char IR_Flag = 0;
// P3CR = P3CR | 0x02;
P2CR = P2CR & 0xFE;
Flag = 1;
// Init_Timer0_ISR();
Init_EUART_ISR();
uiGRcvNew = 0;
uiGNum = 0;
// NEC_IR_ADDR1 = 3;
// NEC_IR_ADDR2 = ~NEC_IR_ADDR1;
NEC_IR_ADDR1 = 0x07;
NEC_IR_ADDR2 = 0x07;
PCON = PCON |0x80;
Flag = 1; ////////////////////////////*************/////////
iIrMode = 0;
iPreBit = 0;
Protocol_Type = 3;
IR_Data_Received = 0;
EUART_SendnStr("The system is running ...\r\n",27);
EUART_SendnStr("\r\n",2);
P3CR = P3CR & 0xFD;
P3PCR = P3PCR & 0xFD;
while(1)
{
// IR_CTRL=1;
// delay_18b20(600);
/// IR_CTRL=0;
// delay_18b20(600);
//
//1 --> 2.84uS
//10 --> 14.4uS
//20 --> 27.2uS
//50 --> 64.8uS
//100 --> 128uS
//160 --> 204uS
//600 --> 748uS
// initial_18b20();
// initial_18b20();
Temperature = readtemp();
EUART_SendnStr("The temperature is \r\n",20);
string[0] = ((Temperature/100)/10) + '0';
string[1] = ((Temperature/100)%10) + '0';
string[2] = '.';
string[3] = ((Temperature%100)/10) + '0';
string[4] = ((Temperature%100)%10) + '0';
string[5] = '\r';
string[6] = '\n';
EUART_SendnStr(string,7);
#if 0
if(1 == uiGRcvNew)
{
EUART_SendnStr("This is a demo program for UART speed test\r\n",44);
uiGRcvNew = 0;
if((uiGRcvBuf[0] == 0x55))
{
Parity = uiGRcvBuf[0] ^ uiGRcvBuf[1] ^ uiGRcvBuf[2] ^ uiGRcvBuf[3] ^ uiGRcvBuf[4];
if(Parity == uiGRcvBuf[5])
{
if(0x01 == uiGRcvBuf[1]) //NEC Protocol
{
Protocol_Type = 1;
NEC_IR_ADDR1 = uiGRcvBuf[2];
NEC_IR_ADDR2 = uiGRcvBuf[3];
NEC_Send_IR_Data(uiGRcvBuf[4]);
}
else if(0x02 == uiGRcvBuf[1])
{
Protocol_Type = 2;
RC5_Send_IR_Data(uiGRcvBuf[2], uiGRcvBuf[3]);
}
}
}
}
if(1 == IR_Data_Received)
{
IR_Data_Received = 0;
}
#endif
}// end while(1)
}
- 1
- 2
前往页