|
#include
#define HIGH P2OUT|=BIT1;
#define LOW P2OUT&=~BIT1;
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
int i;
char temph,templ,humdh,humdl,check,cal;
void Init_Clk(void)
{
P7SEL |= 0x03; // 设置DCO范围
UCSCTL1 = DCORSEL_4; // 选择FLL参考源自REFO
UCSCTL2 |= 0x79; // Set DCO Multiplier for 8MHz
UCSCTL4 = SELM_3 + SELA_0 + SELS_3; // 配置 MCLK = DCOC,SMCLK =DCOC,ACLK=XT1
UCSCTL6 |= SMCLKOFF;
while (SFRIFG1 & OFIFG) //清除OFIFG,and XT1OFFG ,DCOFFG
{
UCSCTL7 &= ~( XT1LFOFFG + DCOFFG);
SFRIFG1 &= ~OFIFG;
}
}
char receive(void) //接受函数
{
char tem,cnt=0; //临时变量用于存储接受数据
while(cnt<8)
{
while(!(P2IN&BIT1)); //等待50us的低电平结束
delay_us(45);
if(P2IN&BIT1) //长于30us定义为1
{
tem++;
tem<<=1;
while(P2IN&BIT1); //结束高电平
}
else
{
tem<<=1;
}
if(cnt!=7)
while(!(P2IN&BIT1)); //最后一次给函数返回留下时间
cnt++;
}
return tem;
}
void main( void )
{
WDTCTL=WDTPW+WDTHOLD;
//SMCLK 8分频
Init_Clk();
P8DIR|=BIT1;
P8OUT|=BIT1;
//inited
LOW;
delay_ms(18);
HIGH;
delay_us(30);
P2DIR&=~BIT1;
while(P8IN&BIT1);
while(!(P8IN&BIT1));
while(P8IN&BIT1);
//Data comes
humdh=receive();
humdl=receive();
temph=receive();
templ=receive();
check=receive();
cal=humdh+humdl+temph+templ;
}
|
|