Cov txheej txheem:

Arduino thiab PCF8591 ADC DAC IC: 7 Kauj Ruam
Arduino thiab PCF8591 ADC DAC IC: 7 Kauj Ruam

Video: Arduino thiab PCF8591 ADC DAC IC: 7 Kauj Ruam

Video: Arduino thiab PCF8591 ADC DAC IC: 7 Kauj Ruam
Video: Уроки Ардуино. Управление нагрузкой в сети переменного тока 2024, Hlis ntuj nqeg
Anonim
Arduino thiab PCF8591 ADC DAC IC
Arduino thiab PCF8591 ADC DAC IC

Koj puas tau xav tau ntau cov tswv yim sib txuas ntawm koj li Arduino txoj haujlwm, tab sis tsis xav tau rab rawg tawm rau Mega? Lossis koj puas xav tsim cov cim qhia analog? Tom qab ntawv txheeb xyuas cov ncauj lus ntawm peb kev qhia - NXP PCF8591 IC.

Nws daws ob qho teeb meem no vim nws muaj DAC ib leeg (digital rau analog) hloov pauv nrog rau plaub ADCs (sib piv rau cov hloov pauv digital) - txhua qhov nkag tau los ntawm I2C npav. PCF8591 muaj nyob hauv DIP, ntu saum npoo thiab daim ntawv qauv, uas ua rau nws yooj yim rau kev sim nrog.

Ua ntej txav mus, rub daim ntawv cov ntaub ntawv. PCF8591 tuaj yeem ua haujlwm ntawm ob qho 5V thiab 3.3V yog li yog tias koj siv Arduino Due, Raspberry Pi lossis lwm 3.3 V pawg thawj coj txhim kho koj zoo. Tam sim no peb yuav xub piav qhia DAC, tom qab ntawd ADCs.

Kauj Ruam 1: Siv DAC (digital-to-analogue Converter)

Siv DAC (digital-to-analogue Converter)
Siv DAC (digital-to-analogue Converter)

DAC ntawm PCF8591 muaj kev daws teeb meem ntawm 8-khoom-yog li nws tuaj yeem tsim lub cim theoretical ntawm nruab nrab ntawm cov xaim hluav taws xob thiab qhov ntsuas hluav taws xob siv (Vref) hauv 255 cov kauj ruam. Rau kev ua qauv qhia peb yuav siv Vref ntawm 5V, thiab koj tuaj yeem siv qis dua Vref xws li 3.3V lossis xijpeem koj xav tau tus nqi siab tshaj plaws … txawm li cas los xij nws yuav tsum tsawg dua li qhov hluav taws xob.

Nco ntsoov tias thaum muaj qhov thauj khoom ntawm cov khoom sib piv (qhov xwm txheej hauv ntiaj teb tiag), qhov siab tshaj qhov tso tawm hluav taws xob yuav poob-cov ntaub ntawv (uas koj rub tawm) qhia pom 10% poob rau 10kΩ load. Tam sim no rau peb qhov kev qhia ua ntu zus.

Nco tseg kev siv 10kΩ rub-up resistors ntawm I2C npav, thiab 10μF capacitor ntawm 5V thiab GND. Qhov chaw nyob npav I2C tau teeb tsa los ntawm kev sib txuas ntawm tus pin A0 ~ A2, thiab nrog lawv txhua tus mus rau GND qhov chaw nyob yog 0x90. Cov zis sib txawv tuaj yeem raug coj los ntawm tus pin 15 (thiab muaj qhov sib cais sib txawv GND ntawm tus pin 13. Tsis tas li ntawd, txuas tus pin 13 rau GND, thiab Circuit GND rau Arduino GND.

Txhawm rau tswj DAC peb yuav tsum xa ob bytes ntawm cov ntaub ntawv. Thawj yog kev tswj hwm byte, uas yooj yim qhib DAC thiab yog 1000000 (lossis 0x40) thiab tom ntej byte yog tus nqi nruab nrab ntawm 0 thiab 255 (qib tso tawm). Qhov no tau qhia hauv cov duab kos hauv qab no:

// Piv txwv 52.1 PCF8591 DAC demo

#include "Wire.h" #define PCF8591 (0x90 >> 1) // I2C npav chaw nyob tsis muaj chaw teeb tsa () {Wire.begin (); } void loop () {rau (int i = 0; i <256; i ++) {Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (0x40); // tswj byte - qhib DAC (binary 1000000) Wire.write (i); // tus nqi xa mus rau DAC Wire.endTransmission (); // xaus kev xa tawm}

rau (int i = 255; i> = 0; --i)

{Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (0x40); // tswj byte - qhib DAC (binary 1000000) Wire.write (i); // tus nqi xa mus rau DAC Wire.endTransmission (); // xaus kev xa tawm}}

Koj puas tau pom qhov hloov pauv me ntsis ntawm qhov chaw nyob npav hauv #define nqe lus? Arduino xa 7-ntsis chaw nyob tab sis PCF8591 xav tau 8-ntsis, yog li peb hloov pauv byte dhau los ntawm ib ntsis.

Kauj ruam 2:

Duab
Duab

Cov txiaj ntsig ntawm kev kos duab tau qhia hauv daim duab, peb tau txuas nrog Vref rau 5V thiab oscilloscope sojntsuam thiab GND rau qhov tso tawm analog thiab GND feem.

Kauj ruam 3:

Duab
Duab

Yog tias koj nyiam txoj kab nkhaus koj tuaj yeem tsim hluav taws xob sine nrog cov duab hauv qab no. Nws siv lub rooj saib hauv kab ntawv uas muaj cov ntsiab lus xav tau ua ntej suav cov ntsiab lus:

// Piv txwv 52.2 PCF8591 DAC demo - sine wave

#include "Wire.h" #define PCF8591 (0x90 >> 1) // I2C npav chaw nyob uint8_t sine_wave [256] = {0x80, 0x83, 0x86, 0x89, 0x8C, 0x90, 0x93, 0x96, 0x99, 0x9C, 0x9F, 0xA2, 0xA5, 0xA8, 0xAB, 0xAE, 0xB1, 0xB3, 0xB6, 0xB9, 0xB9, 0xBC, 0xBF, 0xC1, 0xC4, 0xC7, 0xC9, 0xCC, 0xCE, 0xD1, 0xD5, 0xD8, 0xD8, 0xDA, 0xDC, 0xDE, 0xE0, 0xE2, 0xE4, 0xE6, 0xE8, 0xEA, 0xEB, 0xED, 0xEF, 0xF0, 0xF1, 0xF3, 0xF4, 0xF4, 0xF5, 0xF6, 0xF8, 0xF9, 0xFA, 0xFA, 0xFB, 0xFD, 0xFD, 0xFD, 0xFE, 0xFE, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFD, 0xFD, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE, 0xFE, 0xFE, 0xFD, 0xFD, 0xFD, 0xFD, 0xFF, 0xFB, 0xFA, 0xFA, 0xF8, 0xF6, 0xF5, 0xF4, 0xF3, 0xF1, 0xF0, 0xEF, 0xED, 0xEB, 0xEA, 0xE8, 0xE6, 0xE4, 0xE2, 0xE0, 0xDE, 0xDC, 0xDA, 0xD8, 0xD8, 0xD5, 0xD3, 0xD1, 0xC1, 0xC9, 0xC7, 0xC4, 0xC1, 0xBF, 0xBC, 0xB9, 0xB6, 0xB3, 0xB1, 0xAE, 0xAB, 0xA8, 0xA5, 0xA2, 0x9F, 0x9C, 0x99, 0x99, 0x96, 0x93, 0x93, 0x90, 0x90, 0x8C, 0x89, 0x86, 0x83, 0x80, 0x7D, 0x7A, 0x77, 0x74, 0x70, 0x70, 0x6D, 0x6A, 0x67, 0x64, 0x61, 0x5E, 0x5B, 0x58, 0x55, 0x52, 0x4F, 0x4D, 0x4A, 0x4A, 0x47, 0x44, 0x41, 0x3F, 0x 3C, 0x39, 0x37, 0x34, 0x32, 0x2F, 0x2D, 0x2B, 0x28, 0x26, 0x24, 0x22, 0x20, 0x1, 0x1E, 0x1C, 0x1A, 0x18, 0x16, 0x13, 0x11, 0x10, 0x0F, 0x0D, 0x0C, 0x0B, 0x0A, 0x08, 0x07, 0x06, 0x06, 0x05, 0x04, 0x03, 0x03, 0x02, 0x02, 0x02, 0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x03, 0x04, 0x05, 0x06, 0x06, 0x07, 0x08, 0x0A, 0x0B, 0x0B, 0x0C, 0x0D, 0x0F, 0x10, 0x10, 0x11, 0x13, 0x16, 0x18, 0x1A, 0x1C, 0x1E, 0x20, 0x22, 0x24, 0x26, 0x28, 0x2B, 0x2D, 0x2F, 0x32, 0x34, 0x37, 0x39, 0x3C, 0x3F, 0x41, 0x44, 0x47, 0x4A, 0x4A, 0x4D, 0x4F, 0x52, 0x55, 0x5B, 0x5E, 0x61, 0x64, 0x67, 0x6A, 0x6D, 0x70, 0x74, 0x77, 0x7A, 0x7D}; teeb tsa tsis muaj dabtsis () {Wire.begin (); } void loop () {rau (int i = 0; i <256; i ++) {Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (0x40); // tswj byte - qhib DAC (binary 1000000) Wire.write (sine_wave ); // tus nqi xa mus rau DAC Wire.endTransmission (); // xaus kev xa tawm}}

Kauj ruam 4:

Duab
Duab

Rau cov duab DSO hauv qab no, peb hloov Vref rau 3.3V - nco qhov kev hloov pauv hauv maxima ntawm sine wave.

Tam sim no koj tuaj yeem sim nrog DAC los ua suab nrov, teeb liab lossis tswj hwm lwm cov kev sib piv sib piv.

Kauj Ruam 5: Siv ADCs (sib piv-rau-digital hloov pauv)

Yog tias koj tau siv cov analogRead () ua haujlwm ntawm koj Arduino (txoj kev rov qab hauv Tshooj Ib) ces koj twb tau paub nrog ADC lawm. Nrog tawm PCF8591 peb tuaj yeem nyeem qhov hluav taws xob nruab nrab ntawm xoom thiab Vref thiab nws yuav rov qab tus nqi nruab nrab ntawm xoom thiab 255 uas yog ncaj qha sib piv rau xoom thiab Vref.

Piv txwv li, ntsuas 3.3V yuav tsum rov qab 168. Qhov kev daws teeb meem (8-ntsis) ntawm ADC qis dua ntawm onboard Arduino (10-ntsis) txawm li cas los PCF8591 tuaj yeem ua qee yam Arduino's ADC ua tsis tau. Tab sis peb yuav tau txais qhov ntawd hauv ib pliag. Ua ntej, kom yooj yim nyeem qhov tseem ceeb ntawm txhua tus lej ADC peb xa tus tswj byte los qhia rau PCF8591 uas ADC peb xav nyeem. Rau ADCs xoom rau peb qhov kev tswj hwm byte yog 0x00, 0x01, ox02 thiab 0x03 feem.

Tom qab ntawd peb thov kom ob lub bytes ntawm cov ntaub ntawv rov qab los ntawm ADC, thiab khaws ob byte rau siv. Vim li cas ob bytes? PCF8591 xa rov qab tus nqi ntsuas yav dhau los ua ntej - tom qab ntawv tam sim no byte. (Saib Daim Duab 8 hauv daim ntawv cov ntaub ntawv). Thaum kawg, yog tias koj tsis siv txhua tus ADC pins, txuas qhov tsis siv rau GND. Cov piv txwv hauv qab no kos duab yooj yim khaws cov txiaj ntsig los ntawm txhua tus ADC tus pin ib zaug, tom qab ntawd qhia lawv hauv kab ntawv saib:

#suav nrog "Wire.h"

#define PCF8591 (0x90 >> 1) // I2C chaw nyob npav #define ADC0 0x00 // tswj bytes rau nyeem ib leeg ADCs #define ADC1 0x01 #define ADC2 0x02 #define ADC3 0x03 byte value0, value1, value2, value3; teeb tsa tsis muaj dabtsis () {Wire.begin (); Serial.begin (9600); } void loop () {Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (ADC0); // tswj byte - nyeem ADC0 Wire.endTransmission (); // kawg kev xa xov Wire.requestFrom (PCF8591, 2); value0 = Hlau.read (); value0 = Hlau.read (); Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (ADC1); // tswj byte - nyeem ADC1 Wire.endTransmission (); // kawg kev xa xov Wire.requestFrom (PCF8591, 2); value1 = Hlau.read (); value1 = Hlau.read (); Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (ADC2); // tswj byte - nyeem ADC2 Wire.endTransmission (); // kawg kev xa xov Wire.requestFrom (PCF8591, 2); value2 = Hlau.read (); value2 = Hlau.read (); Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (ADC3); // tswj byte - nyeem ADC3 Wire.endTransmission (); // kawg kev xa xov Wire.requestFrom (PCF8591, 2); value3 = Hlau.read (); value3 = Hlau.read (); Serial.print (tus nqi 0); Serial.print (""); Serial.print (tus nqi 1); Serial.print (""); Serial.print (tus nqi 2); Serial.print (""); Serial.print (tus nqi 3); Serial.print (""); Serial.println (); }

Thaum khiav cov duab kos koj yuav raug nthuav qhia nrog qhov muaj txiaj ntsig ntawm txhua ADC hauv kev saib xyuas ntu. Txawm hais tias nws yog qhov ua piv txwv yooj yim los qhia koj yuav ua li cas nyeem ib leeg zuj zus txhua tus ADC, nws yog txoj hauv kev nyuaj kom tau txais ntau dua ib byte ntawm ib lub sijhawm los ntawm ADC tshwj xeeb.

Kauj Ruam 6:

Txhawm rau ua qhov no, hloov qhov kev tswj hwm byte kom thov pib nce ntxiv, uas ua tiav los ntawm teeb tsa me ntsis 2 ntawm kev tswj hwm byte rau 1. Yog li pib los ntawm ADC0 peb siv kev tswj hwm tshiab byte ntawm binary 00000100 lossis hexadecimal 0x04. Tom qab ntawv thov tsib bytes ntawm cov ntaub ntawv (ib zaug ntxiv peb tsis quav ntsej thawj byte) uas yuav ua rau PCF8591 xa rov qab txhua qhov txiaj ntsig hauv ib saw ntawm bytes. Cov txheej txheem no tau qhia hauv cov duab kos hauv qab no:

#suav nrog "Wire.h"

#define PCF8591 (0x90 >> 1) // I2C npav chaw nyob byte tus nqi 0, tus nqi 1, tus nqi 2, tus nqi 3; teeb tsa tsis muaj dabtsis () {Wire.begin (); Serial.begin (9600); } void loop () {Wire.beginTransmission (PCF8591); // sawv PCF8591 Wire.write (0x04); // tswj byte - nyeem ADC0 tom qab ntawd nws pib -nce Wire.endTransmission (); // kawg kev xa xov Wire.requestFrom (PCF8591, 5); value0 = Hlau.read (); value0 = Hlau.read (); value1 = Hlau.read (); value2 = Hlau.read (); value3 = Hlau.read (); Serial.print (tus nqi 0); Serial.print (""); Serial.print (tus nqi 1); Serial.print (""); Serial.print (tus nqi 2); Serial.print (""); Serial.print (tus nqi 3); Serial.print (""); Serial.println (); }

Yav dhau los peb tau hais tias PCF8591 tuaj yeem ua qee yam uas Arduino's ADC ua tsis tau, thiab qhov no yog muab qhov sib txawv ADC. Raws li kev tawm tsam rau Arduino qhov ib leeg -tau (piv txwv li nws rov qab sib txawv ntawm qhov teeb liab zoo thiab GND, qhov sib txawv ADC lees txais ob lub cim (uas tsis tas yuav tsum tau hais txog hauv av), thiab rov qab sib txawv ntawm ob lub cim Qhov no tuaj yeem yooj yim rau ntsuas qhov hloov pauv me me hauv qhov hluav taws xob rau thauj cov cell thiab ntxiv rau.

Kauj ruam 7:

Duab
Duab

Teeb tsa PCF8591 rau qhov sib txawv ADC yog qhov teeb meem yooj yim ntawm kev hloov pauv kev tswj hwm byte. Yog tias koj tig mus rau nplooj ntawv xya ntawm cov ntaub ntawv, tom qab ntawd txiav txim siab ntau hom sib txawv ntawm cov tswv yim sib tham. Yav dhau los peb tau siv hom '00' rau plaub lub tswv yim, txawm li cas los xij koj tuaj yeem xaiv lwm tus uas tau qhia meej, piv txwv li daim duab.

Yog li txhawm rau teeb tsa kev tswj hwm byte rau ob qhov kev sib txawv sib txawv, siv binary 00110000 lossis 0x30. Tom qab ntawd nws yog qhov teeb meem yooj yim ntawm kev thov cov bytes ntawm cov ntaub ntawv thiab ua haujlwm nrog lawv. Raws li koj tuaj yeem pom tseem muaj kev sib xyaw ua ke ib leeg/sib txawv thiab cov lus sib cais peb-sib txawv. Txawm li cas los xij peb yuav tso lawv rau lub sijhawm tam sim no.

Vam tias koj pom qhov kev txaus siab no, txawm tias ntxiv DAC rau koj qhov kev sim lossis kawm paub me ntsis ntxiv txog ADCs. Thov txiav txim siab xaj koj li PCF8591 los ntawm PMD Txoj Kev.

Cov ntawv tshaj tawm no tau coj tuaj rau koj los ntawm pmdway.com - txhua yam rau cov kws tsim khoom thiab cov khoom siv hluav taws xob, nrog kev xa khoom dawb thoob ntiaj teb.

Pom zoo: