LetAllLinesOfCodeSpeak

本文介绍了一种使用DS18B20温度传感器进行编程的方法,包括初始化、读写操作等关键步骤,并提供了一个基于CC2530的串口通信实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/****************************************************************************************************************

*************************

*Author:JYW

*Time:2019_04_05

*Describe:LetAllLinesOfCodeSpeak

*****************************************************/

/****************************************
*Describe:Ds18b20代码练习
****************************/

#include"ds18b20.h"

#define Ds18b20IO P0_7

void Delay_us(unsigned int k)
{
    T1CC0L = 0x06;
    T1CC0H = 0x00;
    T1CTL = 0x02;
    while(k)
    {
        while(!(T1CNTL >= 0x04));
        k--;
    }
    T1CTL = 0x00;
}

void Delay_ms(unisgned int k)
{
    T1CC0L = 0xE8;
    T1CC0H = 0x03;
    T1CTL = 0x0A;
    while(k)
    {
        while(!((T1CNTL >= 0xE8)&&(T1CNTH >= 0x03)));
        k--;
    }
    T1CTL = 0x00;
}

void Delay_s(unsigned int k)
{
    while(k)
    {
        Delay_ms(1000);
        k--;
    }
}

//时钟频率为32M
void Ds18b20Delay(unsigned int k)
{
    unsigned int a,b;
    for(a=0; a<k;a++)
        for(b=0;b<2;b++);
}

void Ds18b20InputInitial(void)
{
    P0DIR &= ~0x80;
}

void Ds18b20OutputInitial(void)
{
    P0DIR |= 0x80;
}


unsigned char Ds18b20Initial(void)
{
    unsigned char Sta = 0x00;
    unsigned char Flag_1 = 0;
    unsigned int counnt = 0;
    Ds18b20OutputInitial();
    Ds18b20IO = 1;
    Ds18b20Delay(260);
    Ds18b20IO = 0;
    Ds18b20Delay(750);
    Ds18b20IO = 1;
    Ds18b20InputInitial();
    while((Ds18b20IO != 0)&&(Flag_1 == 0))
    {
        count++;
        if(count >8000)  Flag_1 = 1;    
        Sta = Ds18b20IO;
    }
    Ds18b20OutputInitial();
    Ds18b20IO = 1;
    Ds18b20Delay(100);
    return Sta;

}


void Ds18b20Write(unsigned char infor)
{
    unsigned char i;
    Ds18b20OutputInitial();
    for(i=0; i<8; i++)
    {
        if((infor & 0x01))
        {
        //    Ds18b20IO = 1;
            //Ds18b20Delay(5);
            Ds18b20IO = 0;
            Ds18b20Delay(5);
            Ds18b20IO = 1;
            Ds18b20Delay(50);
        }
        else
        {
            Ds18b20IO = 0;
            Ds18b20Delay(50);
            Ds18b20IO = 1;
            Ds18b20Delay(5);
        }
        infor >>= 1;
    }
}

unsigned char Ds18b20Read(void)
{
    unsigned char i= 0;
    unsigned char DataBuf = 0x00;
    Ds18b20OutputInitial();
    Ds18b20IO = 1;
    Ds18b20Delay(10);
    for(i=0; i<8; i++)
    {
        DataBuf >>= 1;
        Ds18b20OutputInitial();
        Ds18b20IO = 0;
        Ds18b20Delay(3);
        Ds18b20IO = 1;
        Ds18b20Delay(3);
        Ds18b20InputInitial();
        if(1 == Ds18b20IO) DataBuf |= 0x80;
        Ds18b20Delay(15);
    }
    return DataBuf;
}

 

 

 

/*板子与PC机收发练习自己写的*/

#include<ioCC2530.h>
unsigned  char*  DataBuf= "Hello Fly!";
unsigned char ReBuf[10];
unsigned char Flag = 1;

void ClockInit32M(void)
{
  CLKCONCMD &= ~0x40;
  while(CLKCONSTA & 0x40);
  CLKCONCMD &= ~0x47;
}

void UartInit(void)
{
  PERCFG &= 0x00;//0100 0000 
  P0SEL |= 0x0C;
  P2SEL &= ~0x40;
  P2DIR &= 0x3F;
  U0CSR |=0xC0; //1100 0000Uart模式,允许接收
  
  U0GCR |=11;
  U0BAUD |= 216;
  UTX0IF = 0;
  
  EA = 1;
  URX0IE = 1;
}

void PointInit()
{
  P1DIR |=0x03;
  P1_0 = 1;
  P1_1 = 1;
}

void Delay(unsigned int count)
{
  unsigned int a,b;
  for(a=0; a<count;  a++)
    for(b=0; b<1070;  b++);
}

void UartSendStr(unsigned char*str, unsigned char len)
{
  unsigned char i;
  for(i=0;i<len;i++)
  {
    U0DBUF = *(str++);
    while(UTX0IF == 0);
    UTX0IF = 0;
  
  }
}


void main(void)
{
  ClockInit32M();
  UartInit();
  PointInit();
  
  while(1)
  {
    UartSendStr(DataBuf, sizeof("Hello Fly!"));
    //UartSendStr(DataBuf, 10);
    Delay(3000);
    if(!Flag)
    {
      P1_0 = !P1_0;
      P1_1 = !P1_1;
    }
  }

}

#pragma vector = URX0_VECTOR
__interrupt void Uart0_ISR()
{
  URX0IF = 0;
  *ReBuf = U0DBUF;
  Flag = !Flag;

}

 

 

 

 


/*******************************************************
*Describe:03基础实验—10串口通讯收发字符练习
******************************/


void main()
{
    CLKCONCMD &= ~0x40;
    while(CLKCONSTA & 0x40);
    CLKCONCMD &= ~0x47;
    
    InitUart();
    UartState = UART0_RX;
    memset(RxData,0,SIZE);
    
    while(1)
    {
        if(UartState == UART0_RX)
        {
            if(RxBuf != 0)
            {
                if((RxBuf != '#')&&(count < 50))
                    RxData[count++]=RxBuf;
                else
                {
                    if(count >= 50)
                    {
                        count = 0;
                        memset(RxData,0,SIZE);
                    
                    }
                    else
                         UartState = UART0_TX;
                    
                
                }
                RxBuf = 0;
            
            }
        
        }
    
        if(UartState == UART0_TX)
        {
            U0CSR &= ~0x40;
            UartSendString(RxData,counnt);
            U0CSR |= 0x40;
            UartState = UART0_RX;
            count = 0;
            memset(RxData, 0, SIZE);
        
        }
    
    }

}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值