AD conversion photoresistor AIN2 ---- learning notes

Publisher:大树下的大白菜yLatest update time:2021-11-02 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

1. Conversion principle of successive approximation ADC

The successive approximation AD converter is similar to the counting A/D converter, except that the digital quantity is generated by the "successive approximation register SAR". SAR uses the "bisection search method" to generate digital quantities. Taking the 8-bit digital quantity as an example, SAR first generates half of the 8-bit digital quantity, that is, 10000000B, to test the size of the analog quantity Vi. If Vn>Vi, clear the highest bit. If Vn

2. Timing diagram and pins

 

3. Code

3.1, main

/*

 Experimental phenomenon: After downloading the program, the first 4 digits of the digital tube display the AD value detected by the photosensitive sensor

1. MCU-->AD/DAC module pin interpretation

    P34-->DI DIN   serial data input terminal, when CS is low, data is latched in on the rising edge of DCLK

P35-->CS CS   chip select signal, controls the conversion timing and enables the serial input and output registers. When the high level is high, the ADC is powered off.

P36-->CL DCLK   clock, external clock signal input

P37-->DO DOUT   serial data output port. Data is shifted out on the falling edge of DCLK and is in high impedance state when cs is high.

           BUSY busy signal, when cs is high level, it is in high impedance state

   LOVDD Digital power input terminal

   AUX ADC auxiliary input channel

     2. Single chip microcomputer --> dynamic digital tube module

    J22-->J6

P22-->J9(A)

P23-->J9(B)

P24-->J9(C)

*/

#include "reg52.h" // This file defines some special function registers of the microcontroller

#include "XPT2046.h"

 

typedef unsigned int u16;

typedef unsigned char u8; // data type definition

 

sbit LSA = P2^2; //These three ports jointly control the bit selection of the digital tube

sbit LSB = P2^3;

sbit LSC = P2^4;

 

u8 disp[4]; // The character array is used to store the data of the first four digits of the digital tube

u8 code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; // 0~9 digital display

 

/*Delay function*/

void delay(u16 i)

{

   while(i--);

}

 

/*Data processing module*/

void datapros()

{

u16 temp; // integer variable

/*The static local variable modified by static is only executed once, and the life cycle of the local variable is extended until it is released after the program ends.

When a global variable is modified with static, the global variable can only be accessed in this file and cannot be accessed in other files, even if it is declared as extern.

Static modifies a function, and this function can only be called in this file, and cannot be called by other files. Local variables modified by Static are stored in the static variable area of ​​the global data area. */

static u8 i; 

if(i==50)

{

i=0;

/*

 AIN0 potentiometer: If you want to detect the conversion potentiometer analog signal, the control word command register value is 0X94 or 0XB4.

 If you want to detect and convert thermistor analog signal, the control word command register value is 0XD4.

  AIN2 photoresistor: If you want to detect and convert the photoresistor analog signal, the control word command register value is 0XA4.

 If you want to detect and convert the analog signal on the AIN3 channel, the control word command register value is 0XE4.

*/

temp = Read_AD_Data(0xA4); // AIN2 photoresistor

}

 i++;

 disp[0] = smgduan[temp/1000]; // Thousands

 disp[1] = smgduan[temp%1000/100];   // hundreds place

 disp[2] = smgduan[temp%1000%100/10]; //ten digit

 disp[3] = smgduan[temp%1000%100%10]; // units

 

}

 

/*Digital tube display module*/

void DigDisplay()

{

u8 i;

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

{

switch(i) // bit selection

{

           case(0):

            LSA=1;LSB=1;LSC=1; break;//display bit 0

        case(1):

LSA=0;LSB=1;LSC=1; break;//display the first bit

case(2):

LSA=1;LSB=0;LSC=1; break;//display the second digit

case(3):

LSA=0;LSB=0;LSC=1; break;//display the third bit

}

           P0=disp[i]; // Send data

   delay(100); // Delay for a while

   P0 = 0x00; // erase

}

}

 

/* Main function */

void main()

  {

  while(1)

    {

      datapros();     // data processing function

      DigDisplay(); // Digital tube display function

   }

 }

 

 

 


3.3, XPT2046 chip control pin definition, function, variable declaration file

#ifndef _XPT2046_H_

#define _XPT2046_H_

 

// Include header file

#include

#include

 

// Redefine keywords

#ifndef uchar

#define uchar unsigned char

#endif

 

#ifndef uint

#define uint unsigned int 

#endif

 

#ifndef ulong

#define ulong unsigned long

#endif

 

//IO definition

sbit DOUT = P3^7;

sbit CLK = P3^6;

sbit CS = P3^5;  

sbit DIN = P3^4;

 

// Function definition

uint Read_AD_Data(uchar cmd);

uint SPI_Read(void);

void SPI_Write(uchar dat);

 


3.3, XPT2046.c chip control

 #include "XPT2046.h"

 

// Initialize touch function

void SPI_Start(void)

{

  CLK=0; // Clock, external clock signal input

  CS=1;     // Chip select signal, controls conversion timing and enables serial input and output registers. ADC powers down when high

 

  DIN = 1; // Serial data input terminal, when CS is low, data is latched in on the rising edge of DCLK

  CLK=1;

  CS=0;

}

 

/*Write data

When the CLK external clock rises, data is input from the DIN port and latched

*/ 

void SPI_Write(uchar dat)

{

 uchar i; // character type variable

 CLK=0; //External clock low level

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

 {

 /* Successive approximation AD conversion principle

  The successive approximation AD converter is similar to the counting A/D converter, except that the digital quantity is generated by the "successive approximation register SAR".

SAR uses the "bisection search method" to generate digital quantities. Taking an 8-bit digital quantity as an example, SAR first generates half of the 8-bit digital quantity.

That is, 10000000B, test the size of the analog quantity Vi, if Vn>Vi, clear the highest bit, if Vn SAR uses the bisection search method to determine the next highest bit, that is, using half of the lower 7 bits y1000000B (y is the determined bit) to test the size of the analog quantity Vi.

After bit6 is determined, SAR uses the bisection search method to determine bit5, that is, using half of the lower 6 bits yy100000B (y is the determined bit) to test the size of the analog quantity.

Repeat this process until the lowest bit 0 is determined and the conversion is completed.

 */

 DIN = dat >> 7; // Shift right by 7 bits and assign the highest bit of dat to DIN

 dat<<=1; // dat=dat<<1 Move the highest bit stored in dat out for the next loop

 CLK = 0;

   // Place data on the rising edge, such as transferring data to the data line

 CLK = 1;

 }

}

 

  /*Read data function

  CLK Clock signal input port Data is shifted out at the falling edge (CLK=1 changes to CLK=0)

  */

uint SPI_Read(void)

{

uint i, dat = 0; // define integer variables

CLK = 0;

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

{

 dat <<=1; // dat = dat << 1; Shift left by 1 bit

 CLK=1;

               // Falling edge data shift out

 CLK = 0;

 /*  

 and&& and a && b a and b    

 or|| or a || b a or b

 Not! !1=0 

 Bitwise AND (&)          a & b a and b 

 Bitwise OR (|) a | b a or b

 

 */

 dat |= DOUT; // dat = dat | DOUT    

}

 return dat;

}


/*cmd: read X or Y*/ 

uint Read_AD_Data(uchar cmd)

{

uchar i; // unsigned character type variable

uint AD_Value; // integer variable

CLK =0; // External clock signal input

CS = 0;         //Chip select signal, controls conversion timing and enables serial input and output registers. ADC powers down when high (does not work when high, works when low)

SPI_Write(cmd);

for(i=6;i>0;i--); // Send a clock cycle and clear BUSY

CLK = 1; // Falling edge shifts out data

_nop_(); // Delay 1us

_nop_(); // Delay 1us

CLK = 0;

_nop_(); // Delay 1us

_nop_(); // Delay 1us

AD_Value = SPI_Read();

CS = 1;

return AD_Value;

}

Reference address:AD conversion photoresistor AIN2 ---- learning notes

Previous article:AD analog-to-digital conversion thermistor
Next article:STC89C51 AD analog-to-digital conversion ---- self-study notes

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号