MSP430 and DS18B20 digital tube display (interrupt method)

Publisher:luanzgcLatest update time:2015-04-27 Source: 51heiKeywords:MSP430 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;
/*****18B20 interface definition********/
#define DQ1 P1OUT |= BIT6
#define DQ0 P1OUT &= ~BIT6
#define DQ_in P1DIR &= ~BIT6
#define DQ_out P1DIR |= BIT6
#define DQ_val (P1IN & BIT6)
/*****Digital tube interface definition********/
#define wei_h P5OUT|= BIT5
#define wei_l P5OUT&= ~BIT5
#define duan_l P6OUT &= ~BIT6
#define duan_h P6OUT |= BIT6
//Digital tube seven-segment code; 0--f
uchar table[16] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
                      0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
uchar table1[16] = {0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,
0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1}; // a bit
uchar tflag,num=0 ;
int tvalue;
uchar disdata[4];
/***********18B20 part of the program******************/
/************************************************
Function name: DelayNus
function Function: 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: Init_18B20
Function: Reset DS18B20
Parameter: 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; // If Error = 1 here, there will be an infinite loop later, indicating that 18B20 may be broken
}
/***********************************************
Function name: Write_18B20Function
: Write a byte of data to DS18B20
Parameter: 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();
}[page]
/*******************************************
Function name: Read_18B20Function
: Read one byte of data from DS18B20
Parameter: 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;
}
 
uint Do1Convert(void)
{
    uchar i;
    uchar temp_low;
    uint temp;
    do
    {
        i = Init_18B20();
    }
    while(i);
    //The i here is equal to the previous Error. If Error = 1, an infinite loop will occur, 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);
    //The i here is equal to the previous Error. If Error = 1, an infinite loop will occur, 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 the low bit
    temp = Read_18B20(); //Read the high bit
    temp = (temp<<8) | temp_low;
   
     if(temp<0x0fff)
   tflag=0;
    else
   { temp=~temp+1;
  tflag=1;
   }
tvalue=temp*(0.625); //Expand the temperature value by 10 times and be 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;
}
/****************Main function********************/
void main(void)
{
    /*The following six lines of program close all IO ports*/
    P5DIR = 0xff;
    =P5OUT = 0xff;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;
    
    /*------Select system master clock as 8MHz-------*/
    BCSCTL1 &= ~XT2OFF; //Turn on XT2 high frequency crystal oscillatordo
    {
    IFG1
        &= ~OFIFG; //Clear crystal failure flagfor
        (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
  
    /*P6DIR |= BIT6;P6OUT |= BIT6; //Disable level conversion
     P5DIR |= BIT5;P5OUT |= BIT5; //Disable level conversion
      P6DIR |= BIT7;P6OUT |= BIT7; //Disable buzzer*/
   // Set watchdog timer and initialize IO to control digital tube
    WDTCTL = WDT_ADLY_1_9;           
    IE1 |= WDTIE;
   
    //Count clock selects SMLK=8MHz, 1MHz after 1/8 division
    TACTL |= TASSEL_2 + ID_3;
    //Open global interrupt_EINT
    ();
    //Loop reading displaywhile
    (1)
    {
      display(Do1Convert());
    }
}
/*******************************************
Function name: watchdog_timer
Function: Watchdog timer interrupt service function,
          dynamic scanning of digital tube
Reference Number: None
Return value: None
********************************************/
#pragma vector = WDT_VECTOR
__interrupt void watchdog_timer(void)
{
    P4OUT = table[disdata[num]];
    if(num==2) P4OUT = table1[disdata[num]]; //Add decimal point  
    duan_h;
    duan_l;
    P4OUT = ~(1<     wei_h;
    wei_l; 
    num++;
    if(num == 4) num = 0;
}
 
Keywords:MSP430 Reference address:MSP430 and DS18B20 digital tube display (interrupt method)

Previous article:Clock written with 32768Hz crystal oscillator of MSP430
Next article:MSP430 and DS1302 1602 display

Recommended ReadingLatest update time:2024-11-16 13:54

DS18B20 Delay Function Version
Program running effect diagram: //Crystal oscillator 12MHZ, measured at room temperature 0--99.9 degrees #include reg51.h #include intrins.h #define uchar unsigned char #define uint unsigned int sfr wdtrst=0xa6; uchar code seg7 ={0x28, 0x7E, 0xA2, 0x62, 0x74, 0x61, 0x21, 0x7A, 0x20, 0x60}; //digital tube
[Microcontroller]
DS18B20 Delay Function Version
Design of intelligent reactive power compensation controller based on MSP430 single chip microcomputer
1 Introduction With the development of economy and the improvement of people's living standards, all walks of life have put forward higher requirements for power supply reliability and quality. Since the distribution network is at the end of the power grid, most users are low-voltage users. The power factor of
[Microcontroller]
Design of intelligent reactive power compensation controller based on MSP430 single chip microcomputer
MSP430F149 controls the LED light on and off C program
The first program of MSP430 microcontroller controls the on and off of LED light at P1.0 port. The C language program uses IAR 6.0 as programming environment; MCU: MSP430F149; the program has detailed comments and is very suitable for beginners. #include msp430x14x.h typedef unsigned int uint; typedef unsigned cha
[Microcontroller]
51 MCU drives DS18B20 temperature sensor program and experience
Regarding the DS18B20 temperature sensor, it is somewhat difficult to write the internal program without the assistance of hardware equipment, because the actual signal waveform cannot be seen. As for the single-chip microcomputer, I... gradually became a little discouraged. Although I mastered the 1_WIRE bus, I lost a
[Microcontroller]
Fruit battery drives LaunchPad (MSP430 G2553) + Nokia5110
  1 tomato, cut into 4 pieces, 4 iron nails (galvanized), 5 pieces of copper wire The wire has two functions: connection and anode. The four petals of the tomato should be separated from each other and cannot be directly in contact. Since each petal can only provide a voltage of about 0.8 to 0.9V, they should b
[Microcontroller]
Design of moving vehicle detector based on MSP430
A low-power vehicle detection system is composed of a toroidal coil, MSP430F112 1A microcontroller and an output interface. It can detect vehicles and output signals according to the sensitivity, working mode and output mode preset by the user. The software dynamic refresh benchmark method is also
[Industrial Control]
Design of moving vehicle detector based on MSP430
MSP430F413 uses Timer_A to make a virtual serial port
The HT824 keypad uses serial communication with the mainboard. When doing high and low temperature tests, communication problems often occur. Using an oscilloscope to track the communication waveform, it is found that the baud rate changes when the temperature changes. Theoretically, it should not change. The delay_us
[Microcontroller]
PIC16F877 MCU DS18B20 digital tube thermometer simulation program can display negative temperature
The circuit diagram is as follows: //************************************************************ // Function implemented: Digital tube displays real-time temperature, supports negative temperature // Chip PIC16F877 // XT:4MHZ //************************************************************ #include pic.h //Includes th
[Microcontroller]
PIC16F877 MCU DS18B20 digital tube thermometer simulation program can display negative temperature
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号