QC

ADC Đọc Nhiệt Độ AVR

Atmega32 đi kèm với bộ chuyển đổi Ananlog sang kỹ thuật số (ADC) 10-bit, 8 kênh. Chúng tôi sẽ thiết lập nó và đọc nhiệt độ từ LM35 và ánh sáng bằng một LDR đơn giản (phụ thuộc vào ánh sáng). Vậy hãy bắt đầu!

Basics

   Thanh ghi ADCSRA cho phép chúng tôi bật adc và đặt tốc độ lấy mẫu. Thanh ghi trạng thái và điều khiển ADC A (ADCSRA):
76543210
ADENADSCADATEADIFADIEADPS2ADPS1ADPS0
10000001

♦ Bit 7 - ADEN: ADC Enable

Ghi bit này vào một sẽ kích hoạt ADC. Bằng cách ghi nó thành 0, ADC sẽ bị tắt.\

♦ Bit 2:0 - ADPS2:0: ADC Prescaler Select Bits

Các bit này xác định hệ số phân chia giữa tần số XTAL và xung nhịp đầu vào ADC.

ADC Prescaler Selection


ADPS2ADPS1ADPS0Division Factor
0002
0012
0104
0118
10016
10132
11064
111128


Đầu tiên chúng ta sẽ khởi tạo ADC bằng đoạn mã sau

Bật ADC bằng cách Đặt bit ADEN của ADCSRA và đặt tần số lấy mẫu thành một nửa tần số bộ dao động (tức là 8 MHz)

Chọn kênh 0 theo mặc định bằng cách sử dụng thanh ghi ADMUX được hiển thị bên dưới.

ADC Multiplexer Select (ADMUX)

76543210
REFS1REFS0ADLARMUX4MUX3MUX2MUX1MUX0
00000000

♦ Bit 7:6 - REFS1:0: Reference Selection Bits

These bits select the voltage reference for the ADC, as shown in below table.

REFS1REFS0Voltage Reference Selection
00AREF,internal Vref turned off
01AVCC with external Capacitor at AREF pin
10Reserved
11Internal 2.56V Voltage Reference with external Capacitor at AREF pin.

♦ Bits 4:0 - MUX 4:0 : Analog Channel and Gain Selection Bits

Giá trị của các bit này chọn tổ hợp đầu vào tương tự nào được kết nối với ADC.

  1. void ADC_Init()
  2. {
  3. ADCSRA=(1<<ADEN) | (1<<ADPS0); /* Enable ADC , sampling freq=osc_freq/2 */
  4. ADMUX=0x00; /* Result right justified, select channel zero */
  5. }


Hãy để chúng tôi viết một hàm khác để chọn kênh cần thiết và đọc dữ liệu tương tự như sau:

  1. Select the required channel passed as input to the function
  2. Start the ADC conversion by setting ADSC bit in ADCSRA
  3. Wait till the conversion is over by checking the ADIF bit in ADCSRA
  4. Read the ADCW register and return the result of conversion
  1. uint16_t ADC_GetAdcValue(uint8_t v_adcChannel_u8)
  2. {
  3.  
  4. ADMUX = v_adcChannel_u8; /* Select the required channel */
  5. DELAY_us(10); /* Wait for some time for the channel to get selected */
  6. util_BitSet(ADCSRA,ADSC); /* Start the ADC conversion by setting ADSC bit */
  7.  
  8. while(util_IsBitCleared(ADCSRA,ADIF)); /* Wait till the conversion is over */
  9. /* ADIF will be set once ADC conversion is complete */
  10. return(ADCW); /* Return the 10-bit result */
  11. }


KẾT NỐI PHẦN CỨNG : PA0- VỚI  POT LÀ CHÂN ĐỌC ADC ( ĐỌC ĐIỆN ÁP TỪ BIẾN TRỞ NHÌN KỸ HÌNH SẼ THẤY).

Code to Read Voltage

#include "adc.h"
#include "uart.h"
int main() 
{
    int adcValue;
    float volt;
    
    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
        volt = (adcValue*5.00)/1023;   //10bit resolution, 5vReference
		
        UART_Printf("ADC0 Value:%4d Equivalent Voltage:%f\n\r",adcValue,volt);     // Send the value on UART
    }
    
    return (0);
}

Code to Read Temperature // CODE đọc nhiệt độ.


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

int main() 
{
    int adcValue;
    int temp;
    
    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 where the temperature sensor(LM35) is connected
        
        /* Convert the raw ADC value to equivalent temperature with 5v as ADC reference
		 Step size of AdC= (5v/1023)=4.887mv = 5mv.
		 for every degree celcius the Lm35 provides 10mv voltage change.
	     1 step of ADC=5mv=0.5'c, hence the Raw ADC value can be divided by 2 to get equivalent temp*/
        
        temp = adcValue/2.0; // Divide by 2 to get the temp value.
        UART_Printf("ADC0 Value:%4d  Equivalent Temperature:%dC\n\r",adcValue,temp);     // 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/...