Single chip microcomputer + PCF8591 to realize digital voltmeter

Publisher:创意旅程Latest update time:2020-09-14 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Making a digital voltmeter (2 credit hours)

1. Experimental purpose:

1. Understand the working principle and communication protocol of I2C serial bus

2. Understand the interface design of ADC0804, DAC0832, PCF8591 and AT89S51

3. Master the programming methods of ADC0804, DAC0832, and PCF8591

4. Be able to skillfully use the digital-to-analog conversion module


2. Experimental requirements:

1. Digital voltmeter: PCF8591 chip is used in AT89C52 system to measure DC voltage in the range of 0-5V and display the voltage value on a 2-digit digital tube.


3. Experimental equipment: (PROTEUS component list)


4. Experimental report:

1. Describe the experimental procedure (with screenshots of important steps).

2. Give the circuit diagram of the single-chip microcomputer system designed in PROTEUS

3. Draw a program flow chart

4. Give the source program written in KEIL.

5. Describe the experimental phenomena of simulating the program in Proteus


5. Experimental Summary


The microcontroller source program is as follows:

/**********************BST-M51 experimental development board routine************************

* Platform: BST-M51 + Keil U4 + STC89C52

* Name: AD serial port reading experiment

* Company: Shenzhen Yabo Software Development Co., Ltd.      

* Date: 2015-6

* Crystal oscillator: 11.0592MHZ

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

#include //Header file containing microcontroller registers

#include


#define AddWr 0x90 //PCF8591 address


// Variable definitions

unsigned char AD_CHANNEL=0;

unsigned char  D[32];

unsigned char code table[10]={0xC0,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //numeric table of common anode digital tubes 0~9

unsigned int data dis[3]={0x00,0x00,0x00}; //3-element array used to calculate and store output voltage


sbit scl=P2^0; //I2C clock

sbit sda=P2^1; //I2C data

bit ack; /*Response flag*/

sbit C1=P2^6; //digital tube bit selection

sbit C2=P2^7; //digital tube bit selection

sbit Dp=P2^5; //decimal point


unsigned char date;


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

                     Start bus function              

Function prototype: void Start_I2c();

Function: Start the I2C bus, that is, send the I2C start condition.

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

void Start_I2c()

{

  sda=1; /*Send the data signal of the start condition*/

  _nop_();

  scl=1;

  _nop_(); /*Start condition establishment time is greater than 4.7us, delay*/

  _nop_();

  _nop_();

  _nop_();

  _nop_();   

  sda=0; /*Send start signal*/

  _nop_(); /* Start condition lock time is greater than 4μs*/

  _nop_();

  _nop_();

  _nop_();

  _nop_();      

  scl=0; /*Clamp the I2C bus and prepare to send or receive data*/

  _nop_();

  _nop_();

}


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

                      End bus function              

Function prototype: void Stop_I2c();

Function: End the I2C bus, that is, send the I2C end condition.

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

void Stop_I2c()

{

  sda=0; /*Send data signal of end condition*/

  _nop_(); /*Send the clock signal of the end condition*/

  scl=1; /*End condition establishment time is greater than 4μs*/

  _nop_();

  _nop_();

  _nop_();

  _nop_();

  _nop_();

  sda=1; /*Send I2C bus end signal*/

  _nop_();

  _nop_();

  _nop_();

  _nop_();

}


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

                 Byte data sending function              

Function prototype: void I2C_SendByte(UCHAR c);

Function: Send data c, which can be an address or data, wait for a response after sending, and

          This status bit operates. (No response or non-response makes ack=0)   

           If data is sent normally, ack=1; ack=0 means the controlled device has no response or is damaged.

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

void  I2C_SendByte(unsigned char  c)

{

unsigned char  i;


for(i=0;i<8;i++) /*The length of the data to be transmitted is 8 bits*/

    {

     if((c<       else sda=0;               

     _nop_();

     scl=1; /*Set the clock line high to notify the controlled device to start receiving data bits*/

      _nop_();

      _nop_(); /*Ensure that the clock high level period is greater than 4μs*/

      _nop_();

      _nop_();

      _nop_();        

     scl=0;

    }


    _nop_();

    _nop_();

    sda=1; /*Release the data line after sending 8 bits and prepare to receive the response bit*/

    _nop_();

    _nop_();  

    scl=1;

    _nop_();

    _nop_();

    _nop_();

    if(sda==1)ack=0;   

       else ack=1; /*Judge whether the response signal is received*/

    scl=0;

    _nop_();

    _nop_();

}


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

                 Byte data receiving function              

Function prototype: UCHAR I2C_RcvByte();

Function: Used to receive data from the device and determine bus errors (no response signal is sent).

          After sending, please use the response function to respond to the slave.

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

unsigned char   I2C_RcvByte()

{

  unsigned char  retc=0,i;

  sda=1; /*Set the data line to input mode*/

  for(i=0;i<8;i++)

      {

        _nop_();         

        scl=0; /*Set the clock line low and prepare to receive data bits*/

        _nop_();

        _nop_(); /*Clock low level period is greater than 4.7μs*/

        _nop_();

        _nop_();

        _nop_();

        scl=1; /*Set the clock line high to make the data on the data line valid*/

        _nop_();

        _nop_();

        retc=retc<<1;

        if(sda==1)retc=retc+1; /*Read data bit, and put the received data bit into retc*/

        _nop_();

        _nop_();

      }

  scl=0;   

  _nop_();

  _nop_();

  return(retc);

}


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

                     Response subfunction

Function prototype: void Ack_I2c(bit a);

Function: The master controller responds to the signal (can be a response or non-response signal, determined by bit parameter a)

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

void Ack_I2c(bit a)

{

  if(a==0)sda=0; /*Send a response or non-response signal here*/

  else sda=1; /*0 is a response, 1 is a non-response signal*/

  _nop_();

  _nop_();

  _nop_();     

  scl=1;

  _nop_();

  _nop_(); /*Clock low level period is greater than 4μs*/

  _nop_();

  _nop_();

  _nop_();

  scl=0; /*Clear the clock line and hold the I2C bus to continue receiving*/

  _nop_();

  _nop_();   

}


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

* Function name: Pcf8591_DaConversion

* Function: Output analog value of PCF8591

* Input: addr (device address), channel (conversion channel), value (conversion value)

* Output: None

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

bit Pcf8591_DaConversion(unsigned char addr,unsigned char channel,  unsigned char Val)

{

   Start_I2c(); //Start the bus

   I2C_SendByte(addr); //Send device address

   if(ack==0)return(0);

   I2C_SendByte(0x40|channel); //Send control byte

   if(ack==0)return(0);

   I2C_SendByte(Val); //Send DAC value

   if(ack==0)return(0);

   Stop_I2c(); //End the bus

   return(1);

}


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

* Function name: Pcf8591_SendByte

* Function: Write a control command

* Input: addr (device address), channel (conversion channel)

* Output: None

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

bit PCF8591_SendByte(unsigned char addr,unsigned char channel)

{

   Start_I2c(); //Start the bus

   I2C_SendByte(addr); //Send device address

   if(ack==0)return(0);

   I2C_SendByte(0x40|channel); //Send control byte

   if(ack==0)return(0);

   Stop_I2c(); //End the bus

   return(1);

}


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

* Function name: PCF8591_RcvByte

* Function: Read a conversion value

* Input :

* Output: dat

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

unsigned char PCF8591_RcvByte(unsigned char addr)

{

   unsigned char dat;


   Start_I2c(); //Start the bus

   I2C_SendByte(addr+1); //Send device address

   if(ack==0)return(0);

   dat=I2C_RcvByte(); //Read data 0


   Ack_I2c(1); //Send non-acknowledge signal

   Stop_I2c(); //End the bus

   return(that);

}

/*------------------------------------------------

                 Serial port initialization function

------------------------------------------------*/

void init_com(void)

{

EA=1; //Open the general interrupt

ES=1; //Enable serial port interrupt

ET1=1; //Enable timer T1 interrupt

TMOD=0x20; //Timer T1, generates baud rate in mode 2 interrupt

PCON=0x00; //SMOD=0

SCON=0x50; // Mode 1 controlled by timer

TH1=0xfd; //Set the baud rate to 9600

TL1=0xfd;

TR1=1; //Start timer T1 running control bit


}

/*------------------------------------------------

                  Delay function

------------------------------------------------*/

void delay(unsigned char i)

{

  unsigned char j,k;

  for(j=i;j>0;j--)

    for(k=125;k>0;k--);

}

/*------------------------------------------------

Convert the read value into characters one by one and display them on the serial port

------------------------------------------------*/

void To_ascii(unsigned char num)

{            

              SBUF=num/100+'0';                                               

              delay(200);                             

              SBUF=num/10%10+'0';                                            

              delay(200);            

              SBUF=num%10+'0';

              delay(200);

}

/*------------------------------------------------

                    Main function

------------------------------------------------*/

main()

{


              init_com();

              while(1)

              {

              /********The following AD-DA processing*************/

              PCF8591_SendByte(AddWr,0); //Start conversion

              D[0]=PCF8591_RcvByte(AddWr); //Read the converted digital signal, ADC0 analog-to-digital conversion 1 photoresistor            

              /********The following will send the AD value through the serial port*************/

[1] [2]
Keywords:MCU Reference address:Single chip microcomputer + PCF8591 to realize digital voltmeter

Previous article:51 single chip simple electronic scale program
Next article:Electronic password lock based on AT89S52 single chip microcomputer

Recommended ReadingLatest update time:2024-11-15 15:12

[51 MCU Quick Start Guide] 9: Power saving mode (low power consumption)
Puzhong 51-Single-core-A2 STC89C52 Keil uVision V5.29.0.0 PK51 Prof. Developers Kit Version:9.60.0.0 Hard Knowledge        Excerpted from "STC89C52 Series Microcontroller Device Manual"        Only supports power-down mode, not idle mode        The STC89C52 series microcontrollers can run in two power saving modes t
[Microcontroller]
[51 MCU Quick Start Guide] 9: Power saving mode (low power consumption)
MCU C language application fixed address variables
int8 *pRStut1 = ADDR1; //Apply for an 8-bit pointer variable named pRStut1 int16 *pRStut2 = ADDR2; //Apply for a 16-bit pointer variable named pRStut2 uint8 RStu1 _at_ ADDR1 ; //Apply for an 8-bit variable named RStu1 uint16 RStu2 _at_ ADDR2 ; //Apply for a 16-bit variable named RStu2 DEF_8BIT_REG_AT(RStu1,ADD
[Microcontroller]
Lingdong MM32 promotes the localization of automotive electronics MCU
The world situation in 2020 has caused the semiconductor electronics industry to be profoundly affected by the external environment such as the epidemic and Sino-US trade. Although the overall Chinese market has recovered, the automotive electronic main control chips, which are at the core competitiveness of the indus
[Automotive Electronics]
51 MCU special function register and bit definition
In the next few sections, we will show you how to write your first MCU program. Before that, let's first learn about some of the 51 MCU's unique programming syntax and the basic operating steps of Keil software. Please be patient. We mainly use C language to program MCUs, and some MCUs have a few very special and uni
[Microcontroller]
51 MCU special function register and bit definition
51 MCU Introduction - Basic Knowledge Summary
Part 1 Introduction to MCU MSC-51 microcontroller refers to a microcontroller with 8051 as the core, which was launched by Intel Corporation of the United States in 1980. 80C51 is a typical variety in the MCS-51 series; CMOS process microcontroller products developed by other manufacturers with 8051 as the base core a
[Microcontroller]
51 MCU Introduction - Basic Knowledge Summary
Common hardware circuits of 51 microcontrollers
It is very difficult to make your own boards without knowledge of hardware circuits. Let's talk about several common circuits in 51 microcontrollers. You can also evaluate your potential as a hardware engineer by looking at the circuits. Our common circuits in the 51 microcontroller include crystal oscillator circuits
[Microcontroller]
Common hardware circuits of 51 microcontrollers
Detailed explanation of the classic experimental examples of single-chip microcomputers (with source code) (I)
     Self-study experience of single chip microcomputer   Whether you are an amateur electronics enthusiast or a practitioner in the electronics industry, mastering microcontroller technology will undoubtedly give you an extra advantage and open the door to convenience for your electronic gadgets or the developmen
[Analog Electronics]
Detailed explanation of the classic experimental examples of single-chip microcomputers (with source code) (I)
Hardware design of greenhouse intelligent controller based on single chip microcomputer
  In recent years, greenhouse environmental control has been studied and applied accordingly at home and abroad. Most of the existing intelligent greenhouse system hardware in China is imported from abroad. The foreign system has undergone years of development and improvement and is relatively mature and advanced in t
[Microcontroller]
Hardware design of greenhouse intelligent controller based on single chip microcomputer
Latest Microcontroller Articles
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号