QC

ADC 8051

 ADC 8051

Trong hướng dẫn này, chúng ta sẽ thảo luận về giao tiếp của ADC0808 / 9 bên ngoài với 8051.

Chúng tôi sẽ đọc các giá trị ADC từ kênh Zero và được truyền trên UART ở tốc độ 9600 baudrate.

ADC-0808/9

8051 không có ADC sẵn có và chúng ta sẽ sử dụng ADC 8-bit bên ngoài tức là. ADC0808 / ADC0809. ADC0809 là bộ ADC xấp xỉ kế tiếp 8 bit được ghép kênh giữa 8 chân đầu vào.
Mô-đun A / D có đầu vào tham chiếu điện áp cao và thấp có thể được thiết lập bằng cách sử dụng các chân Vref + và Vref- như thể hiện trong hình dưới đây. Với 5v là Vref + và 0v là Vref- độ phân giải của ADC0809 có thể được xác định như sau: $$ độ phân giải của ADC = ((Vref +) - (Vref -)) / (2 ^ {8} -1) = 5/255 = 0,0196 = 19,6mv 


Ghim ADC
Cho phép xem các chân của ADC0809
Addres Lines (A, B, C): Như đã đề cập trước đó, ADC0809 có 8 kênh ADC đa kênh. Một kênh đầu vào cụ thể được chọn bằng cách sử dụng các bit địa chỉ này như hình dưới đây.

CBASelected ADC channel
000INO
001IN1
010IN2
011IN3
100IN4
101IN5
110IN6
111IN7

-Address Latch Enable (ALE): Tín hiệu từ thấp đến cao tại chân này sẽ chốt địa chỉ đã chọn ở trên và chọn kênh tương ứng để chuyển đổi ADC.
-Chuyển đổi BẮT ĐẦU (SOC): Thanh ghi xấp xỉ kế tiếp (SAR) của bộ chuyển đổi A / D được đặt lại trên cạnh dương của xung chuyển đổi bắt đầu (SC). Do đó, chúng ta cần tạo xung THẤP-CAO-THẤP để bắt đầu chuyển đổi ADC.
-Kết thúc chuyển đổi (EOC): Khi quá trình chuyển đổi kết thúc, chân này được kéo CAO bởi ADC0809. Pin này cần được theo dõi để quá trình chuyển đổi hoàn tất và sau đó đọc dữ liệu.
-Kích hoạt đầu ra (OE): ADC0809 thực hiện chuyển đổi adc và giữ dữ liệu trong các thanh ghi bên trong. Tín hiệu CAO trên chân này sẽ đưa dữ liệu trên các đường đầu ra.


Pin Connections

/* ADC DataBus is connected to P2 and Control lines are connected to P1*/
#define adc_DATABUS  P2

sbit adc_A = P1^0;
sbit adc_B = P1^1;
sbit adc_C = P1^2;
sbit adc_ALE = P1^3;
sbit adc_START = P1^4;
sbit adc_EOC = P1^5;
sbit adc_OE = P1^6;

Steps for ADC Conversion

  1. Select the required ADC channel using A,B,C pins.
  2. Send a HIGH pulse on ALE for latching the above selected channel.
  3. Send a HIGH pulse on Start pin to reset the SAR(Successive Approximation Register).
  4. Pull the ALE line low as the address is lateched and futher changes on A,B,C should not change the channel.
  5. Pull Start line LOW to start the conversion.
  6. Wait for the EOC line go HIGH.
  7. Pull the OE line HIGH to bring the conversion data on DataBus.
  8. Read the ADC data and pull OE line back to LOW.
  9. Use the ADC data for further processing

uint8_t ADC_Read(uint8_t v_adcChannel_u8)
{
    uint8_t adc_result;

    adc_A=((v_adcChannel_u8>>0) & 0x01);   //Select the channel
    adc_B=((v_adcChannel_u8>>1) & 0x01);   //for which the conversion needs to be done
    adc_C=((v_adcChannel_u8>>2) & 0x01);

    adc_ALE=1;         // Latch the address by making the ALE high.
    delay_us(50);
    adc_Start=1;       //Start the conversion after latching the channel address
    delay_us(25);

    adc_ALE=0;         //Pull ALE line to zero after starting the conversion.
    delay_us(50);
    adc_START=0;       //Pull Start line to zero after starting the conversion.

    while(adc_EOC==0); // Wait till the ADC conversion is completed,
                       // EOC will be pulled to HIGH by ADC0809 once conversion is completed.

    adc_OE=1;          //Make the Output Enable high to bring the ADC data to port pins
    DELAY_us(25);
    adc_result=adc_DATABUS;  //Read the ADC data from ADC bus
    adc_OE=0;                //After reading the data, disable the ADC output line.

    return(adc_result) ;
}

Hardware Connection



Code


#include<reg51.h>
/* ADC DataBus is connected to P2 and Control lines are connected to P1*/
#define adc_DATABUS  P2
sbit adc_A = P1^0;
sbit adc_B = P1^1;
sbit adc_C = P1^2;
sbit adc_ALE = P1^3;
sbit adc_START = P1^4;
sbit adc_EOC = P1^5;
sbit adc_OE = P1^6;

void delay_us(int count)
{
    while(count--);
}

void ADC_Init(void)
{
    adc_START=0;        //Initialize all the control lines to zero.
    adc_ALE=0;
    adc_OE=0;
    adc_EOC=1;          //Configure the EOC pin as I/P
    adc_DATABUS=0xff;   //configure adc_databus as input
}

uint8_t ADC_Read(uint8_t v_adcChannel_u8)
{
    uint8_t adc_result;

    adc_A=((v_adcChannel_u8>>0) & 0x01);   //Select the channel
    adc_B=((v_adcChannel_u8>>1) & 0x01);   //for which the conversion needs to be done
    adc_C=((v_adcChannel_u8>>2) & 0x01);

    adc_ALE=1;         // Latch the address by making the ALE high.
    DELAY_us(50);
    adc_Start=1;       //Start the conversion after latching the channel address
    delay_us(25);

    adc_ALE=0;         //Pull ALE line to zero after starting the conversion.
    delay_us(50);
    adc_START=0;       //Pull Start line to zero after starting the conversion.

    while(adc_EOC==0); // Wait till the ADC conversion is completed,
                       // EOC will be pulled to HIGH by ADC0809 once conversion is completed.

    adc_OE=1;          //Make the Output Enable high to bring the ADC data to port pins
    delay_us(25);
    adc_result=adc_DATABUS;  //Read the ADC data from ADC bus
    adc_OE=0;                //After reading the data, disable the ADC output line.

    return(adc_result) ;
}

void main()
{
    ADC_Init();             //Initialize the ADC module

    while(1)
    {
        P0 = ADC_Read(0);       // Read the ADC value of channel zero and display on LED's Connected to PO
    }
}

Sử Dụng Thư Viện.


#include "adc.h"
#include "uart.h"

void main(void) 
{
    int adcValue;

    ADC_Init();       /* Initialize the ADC module */
    UART_Init(9600);  /* Initialize UART at 9600 baud rate */

    while(1)
    {
        adcValue = ADC_GetAdcValue(0); // Read the ADC value of channel zero
        UART_Printf("ADC0 Value:%3d \n\r",adcValue);     // Send the value on UART
    }

    return (0);
}

Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...