MSP430 and DS18B20 1602 display

Publisher:大头玩家Latest update time:2015-04-28 Source: 51heiKeywords:MSP430  DS18B20 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
#include

typedef unsigned char uchar;
typedef unsigned int uint;
/**************Macro definition****************/
#define DataDir P4DIR
#define DataPort P4OUT    
#define Busy 0x80
#define CtrlDir P3DIR
#define CLR_RS P3OUT&=~BIT0; //RS = P3.0 
#define SET_RS P3OUT|=BIT0; 
#define CLR_RW P3OUT&=~BIT1; //RW = P3.1
#define SET_RW P3OUT|=BIT1; 
#define CLR_EN P3OUT&=~BIT2; //EN = P3.2 
#define SET_EN P3OUT|=BIT2;

#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
uint tvalue;
uchar tflag;
uchar disdata[4];
                      
/*******************************************
Function name: Delay5msFunction
: Delay about 5msParameter
: None
Return value: None
********************************************/
void Delay5ms(void)
{
    uint i=40000;
    while (i != 0)
    {
        i--;
    }
}
/*******************************************
Function name: DelayNusFunction
: Achieve N microseconds of delay
Parameter: n--delay length
Return value: None
Description :The counting clock of timer A is 1MHz, and the CPU main frequency is 8MHz
          , so extremely accurate
          us-level delay can be obtained through timer delay
********************************************/
void DelayNus(uint n)
{
    CCR0 = n;
    TACTL |= MC_1; //Increase count to CCR0
    while(!(TACTL & BIT0)); //Wait for
    TACTL &= ~MC_1; //Stop counting
    TACTL &= ~BIT0; //Clear interrupt flag
}
/*******************************************
Function name: WaitForEnableFunction
: Wait for 1602 LCD to complete internal operation
Parameter: None
Return value: None
********************************************/
void WaitForEnable(void)
{
    P4DIR &= 0x00; //Switch P4 port to input
    stateCLR_RS;
    SET_RW;
    _NOP();
    SET_EN;
    _NOP();
    _NOP();
 
    while((P4IN & Busy)!=0); //Detect busy flag
    CLR_EN;
    P4DIR |= 0xFF; //Switch P4 port to output state

/*******************************************
Function name: write_comFunction
: Write command to LCD module
Parameter: cmd--command,
          chk--flag of whether to judge busy, 1: judge busy, 0: not judge
Return value: None
********************************************/
void write_com(uchar cmd)
{
    WaitForEnable(); //Detect busy signal?
   
    CLR_RS; 
    CLR_RW;
    _NOP();
    DataPort = cmd; //Write command word to data port_NOP
    ();     
   
    SET_EN; //Generate enable pulse signal_NOP
    ();
    _NOP();
    CLR_EN;   
}
 
/*******************************************
Function name: write_dataFunction
: Write display data to the current address of LCD display
Parameter Number: data--display character data
Return value: None
********************************************/
void write_data( uchar data )
{
    WaitForEnable(); //Wait for the LCD to be unbusy
    SET_RS;
    CLR_RW;
    _NOP();
    DataPort = data; //Write display data to data port_NOP
    ();
    SET_EN; //Generate enable pulse signal_NOP
    ();
    _NOP();
    CLR_EN;  
}

void zifuchuan(uchar *ch)
{
  while(*ch!=0)
  write_data(*ch++);
  Delay5ms(); 
}

/*******************************************
Function name: LcdResetFunction
: Reset the 1602 LCD module
Parameter: None
Return value: None
********************************************/
void LcdReset(void)
{
    CtrlDir |= 0x07; //Control line port is set to output
    stateDataDir = 0xFF; //Data port is set to output
 
    statewrite_com(0x38); //Specified reset operationDelay5ms
    ();
    write_com(0x38);  
    Delay5ms();
    write_com(0x38);
    Delay5ms();
    write_com(0x38); //Display mode settingwrite_com
    (0x08); //Display offwrite_com
    (0x01); //Display clear screenwrite_com
    (0x06); //Write characters without moving the whole
    write_com(0x0c); //Display on, cursor off, no flashing
}

/*******************************************
Function name: Init_18B20Function
: Reset DS18B20
Parameters: None
Return value: Initialization status flag: 1-failure, 0-success
****************************************/
uchar Init_18B20(void)
{
    uchar Error;
   
    DQ_out;
    _DINT();
    DQ0;
    DelayNus(500);
    DQ1;
    DelayNus(55);
    DQ_in;
    _NOP();
    if(DQ_val)     
    {
        Error = 1; //Initialization failed
    }
    else
    {
        Error = 0; //Initialization successful
    }
    DQ_out;
    DQ1;
    _EINT();
   
    DelayNus(400);
   
    return Error;
}[page]
/***********************************************
Function name: Write_18B20Function
: Write one byte of data to DS18B20
Parameters Data: wdata--Written data
Return value: None
********************************************/
void Write_18B20(uchar wdata)
{
    uchar i;
   
    _DINT();
    for(i = 0; i < 8;i++)
    {
        DQ0;
        DelayNus(6); //Delay 6us
        if(wdata & 0X01) DQ1;
        else DQ0;
        wdata >>= 1;
        DelayNus(50); //Delay 50us
        DQ1;
        DelayNus(10); //Delay 10us
    }
    _EINT();
}
/***********************************************
Function name: Read_18B20Function
: Read one byte of data from DS18B20
Parameters: None
Return value: One byte of data read out
********************************************/
uchar Read_18B20(void)
{
    uchar i;
    uchar temp = 0;
   
    _DINT();
    for(i = 0;i < 8;i++)
    {
        temp >>= 1;
        DQ0;
        DelayNus(6); //Delay 6us
        DQ1;
        DelayNus(8); //Delay 9us
        DQ_in;
        _NOP();
        if(DQ_val) temp |= 0x80;
        DelayNus(45); //Delay 45us
        DQ_out;
        DQ1;
        DelayNus(10); //Delay 10us
    }
    _EINT();
   
    return temp;
}
/***********************************************
Function name: Skip
Function: Send skip reading product ID command
Parameter: None
Return value: None
********************************************/
void Skip(void)
{
    Write_18B20(0xcc);
}
/*******************************************
Function name: ConvertFunction
: Send temperature conversion command
Parameter: None
Return value: None
********************************************/
void Convert(void)
{
    Write_18B20(0x44);
}
/*******************************************
Function name: Read_SPFunction
: Send read ScratchPad command
Parameter: None
Return value: None
********************************************/
void Read_SP(void)
{
    Write_18B20(0xbe);
}
/***********************************************
Function name: ReadTempFunction
: Read temperature conversion result from DS18B20 ScratchPad
Parameter: None
Return value: Read temperature value
********************************************/
uint ReadTemp(void)
{
    uchar temp_low;
    uint temp;
   
    temp_low = Read_18B20(); //Read low bit
    temp = Read_18B20(); //Read high bit
    temp = (temp<<8) | temp_low;
   
    return temp;
}

uint Do1Convert(void)
{
    uchar i;
    uchar temp_low;
    uint temp;
    do
    {
        i = Init_18B20();
    }
    while(i);
    //i here is equal to the previous Error. If Error = 1, an infinite loop will appear, indicating that 18B20 may be broken.
    Write_18B20(0xcc); //Send the command to skip reading the product ID number
 
     . Write_18B20(0x44); //Send the temperature conversion command
    for(i = 20;i > 0;i--) 
        DelayNus(60000); //Delay for more than 800ms
    do
    {
        i = Init_18B20();
    }
    while(i);
    //i here is equal to the previous Error. If Error = 1, an infinite loop will appear, indicating that 18B20 may be broken.
   
    Write_18B20(0xcc); //Send the command to skip reading the product ID number.
   
     Write_18B20(0xbe); //Send the command to read ROM.
      
    temp_low = Read_18B20(); //Read low bit
    temp = Read_18B20(); //Read high bit
    temp = (temp<<8) | temp_low;
   
     if(temp<0x0fff)
   tflag=0;
    else
   {temp=~temp+1;
  tflag=1;
   }
tvalue=temp*(0.625);//Temperature value is enlarged 10 times, accurate to 1 decimal place
  return tvalue;
}
void display(int dat)
{
disdata[0]=dat/1000;
disdata[1]=dat%1000/100;
disdata[2]=dat%100/10;
disdata[3]=dat%10;
write_com(0x80+0x40);
write_data(disdata[0]+0x30);
write_data(disdata[1]+0x30);
write_data(disdata[2]+0x30);
write_com(0x80+0x43);
write_data(0x2e);
write_data(disdata[3]+0x30);
write_data(0xdf); //Write the small circle of Celsius
write_data('C');
}
 

/*************************Main function****************************/
void main( void )
{
    /*The following six lines of program close all IO ports*/
    P1DIR = 0XFF;P1OUT = 0XFF;
    P2DIR = 0XFF;P2OUT = 0XFF;
    P3DIR = 0XFF;P3OUT = 0XFF; P4DIR = 0XFF;
    P4OUT = 0XFF;
    P5DIR = 0XFF;P5OUT = 0XFF;
    P6DIR = 0XFF;P6OUT = 0XFF;
 
    uchar i;
   
    WDTCTL = WDTPW + WDTHOLD; //Turn off the dog
    P6DIR |= BIT2;P6OUT |= BIT2; //Turn off the level conversion
    /*------Select the system main clock as 8MHz-------*/
    BCSCTL1 &= ~XT2OFF; //Turn on the XT2 high frequency crystal oscillatordo
    {
    IFG1
        &= ~OFIFG; //Clear crystal failure flag
        for (i = 0xFF; i > 0; i--); //Wait for 8MHz crystal to start oscillating
    }
    while ((IFG1 & OFIFG)); //Does the crystal failure flag still exist?
    BCSCTL2 |= SELM_2 + SELS; //MCLK and SMCLK select high-frequency crystal oscillator
  
    //Counting clock selects SMLK=8MHz, which is 1MHz after 1/8 division
    TACTL |= TASSEL_2 + ID_3;
    //Open global interrupt
    _EINT();
   
    LcdReset();
 zifuchuan("Temperature is:");
    //Loop reading display
    while(1)
    {
      display(Do1Convert());
    }         
}

Keywords:MSP430  DS18B20 Reference address:MSP430 and DS18B20 1602 display

Previous article:IIC bus protocol written in 430: 24C02 power-off memory program (digital tube display)
Next article:MSP430 ADC12 1602 display

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号