Tento příklad slouží pro základní ovládání AD převodníku v dsPICovi. K dsPICovi je připojen D/A převodník stejně jako u minulých příkladů ( DA_prevod , hlas..,...) . Vstupní napětí v rozsaho 0 - 5V přivádíme na pin 4 - AN2. Napětí je kopírováno na výstup DA převodníku MCP 4921 Vzorkovací kmitočet je zhruba 1 Hz, takže ke změně výstupního napětí dojde po 1 sec. Vlastně jsme z dsPICa a DA převodníku udělali drát. Na piník 4 přivést vstupní napětí. Řádově po 1 sec by to mělo kopírovat napětí ze vstupu na výstupní DA přenodník přes SPI interface DA převodník je připojen stejně jako u programů hlas.s napěťová reference je vnitřní, takže 0V a +5V. Vref- nastavit programově na GND analog. Vref+ nastavit programově na +5V analog pin 4 - AN2 - vstup takže RB2 nebudeme používat jako digitální - v TRIS se tento bit MUSÍ nastavit do 1 registr ADPCFG nastavit tak, aby AN2 byl použity pro převodník, tedy 0011 1111 1011 čas konverse se nesmí nastavit kratší než 333.33 nsec ADON bit nastavit do 1 až po úplném nastavení všeho nejdříve se tedy nastaví spousta registrů pro převodník, a až úplně nakonec se dá ADCON1<15> do 1 Vlastní převod: Nejdříve se to musí navzorkovat, buď automaticky nebo ručně setting the SAMP bit ADCON1<1> a potom teprve začne vlastní převod - nastavili jsme to tak, aby převod začal sám How to Stop Sampling and Start Conversions The conversion trigger source will terminate sampling and start a selected sequence of conversions. The SSRC<2:0> bits (ADCON1<7:5>) select the source of the conversion trigger. Jsou tam tedy dvě věci: Sampling a Conversion A generuje to přerušení, dá se nastavit, po kolika převedených hodnotách výsledek převodu je v registrech ADCBUF0 - ADCBUFF - můžeme tedy nechat převěst 16 vzorků, a potom teprve generovat přerušení. To ovšem není náš případ, my generujeme přerušení hned po převodu jednoho vzorku. Hodnota je tedy uložena v jediném registru ADCBUF0 Výsledek chceme číst jako Integer - to je prostě 12 bitů bez jakýchkoli opiček Přečtěte si k tomu Vojtovu příručku, a je to. Když jsem to zkoušel, lezlo mi z DA převodníku zhruba poloviční napští, než bylo na vstupu. Asi je ještě něco špatně nastaveno, ale už to nebudu hledat. Generating an A/D Interrupt The SMPI<3:0> bits control the generation of interrupts. The interrupt will occur some number of sample/conversion sequences after starting sampling and re-occur on each equivalent number of samples. The value specified by the SMPI bits will correspond to the number of data samples in the buffer, up to the maximum of 16. When the ADON bit (ADCON1<15>) is ‘1’, the module is in Active mode and is fully powered and functional. When ADON is ‘0’, the module is disabled. The digital and analog portions of the circuit are turned off for maximum current savings. A/D Result Buffer The module contains a 16-word dual port RAM, called ADCBUF, to buffer the A/D results. The 16 buffer locations are referred to as ADCBUF0, ADCBUF1, ADCBUF2, ..., ADCBUFE, ADCBUFF. Note: The A/D result buffer is a read only buffer. Control Registers The A/D module has six Control and Status registers. These registers are: • ADCON1: A/D Control Register 1 • ADCON2: A/D Control Register 2 • ADCON3: A/D Control Register 3 • ADCHS: A/D Input Channel Select Register • ADPCFG: A/D Port Configuration Register • ADCSSL: A/D Input Scan Selection Register The ADCON1, ADCON2 and ADCON3 registers control the operation of the A/D module. The ADCHS register selects the input pins to be connected to the S/H amplifiers. The ADPCFG register configures the analog input pins as analog inputs or as digital I/O. The ADCSSL register selects inputs to be sequentially scanned. How to Start Sampling 18.10.1 Manual - to děláme my ! Setting the SAMP bit (ADCON1<1>) causes the A/D to begin sampling. One of several options can be used to end sampling and complete the conversions. The sample time may be started manually by setting the SAMP bit (ADCON1<1>) or started automatically by the A/D converter hardware. The sample time is ended manually by clearing the SAMP control bit in the user software or automatically by a conversion trigger source. Konec vzorku máme nastaven na automatický, takže dsPIC po určitém čase sám skončí vzorek a začne převod. Tady se bouřil překladač, bylo nutno upravit čas vzorkování, ale stejně je tam jenom plácnutá nějaká hodnota, o které moc nevím, co vlastně znamená. Conversion time is the time required for the A/D converter to convert the voltage held by the S/H amplifier. The A/D is disconnected from the analog input pin at the end of the sample time. The A/D converter requires one A/D clock cycle (TAD) to convert each bit of the result plus one additional clock cycle. A total of 14 TAD cycles are required to perform the complete conversion. When the conversion time is complete, the result is loaded into one of 16 A/D result registers (ADCBUF0...ADCBUFF), the S/H can be reconnected to the input pin, and a CPU interrupt may A/D Module Configuration The following steps should be followed for performing an A/D conversion: 1. Configure the A/D module • Select voltage reference source to match expected range on analog inputs • Select the analog conversion clock to match desired data rate with processor clock • Determine how sampling will occur • Determine how inputs will be allocated to the S/H channel • Select how conversion results are presented in the buffer • Select interrupt rate • Turn on A/D module 2. Configure A/D interrupt (if required) • Clear ADIF bit • Select A/D interrupt priority The options for each configuration step are described in the subsequent sections. 18.6 Selecting the Voltage Reference Source The voltage references for A/D conversions are selected using the VCFG<2:0> control bits (ADCON2<15:13>). The upper voltage reference (VREFH) and the lower voltage reference (VREFL) may be the internal AVDD and AVSS voltage rails or the VREF+ and VREF- input pins. The external voltage reference pins may be shared with the AN0 and AN1 inputs on low pin count devices. The A/D converter can still perform conversions on these pins when they are shared with the VREF+ and VREF- input pins. The voltages applied to the external reference pins must meet certain specifications. Refer to the “Electrical Specifications” section of the device data sheet for further details.. Note: The SSRC<2:0>, SIMSAM, ASAM, CHPS<1:0>, SMPI<3:0>, BUFM and ALTS bits, as well as the ADCON3 and ADCSSL registers, should not be written to while ADON = 1. This would lead to indeterminate results. Note: External VREF+ and VREF- pins must be selected for the conversion rates above 100 ksps. See Section 18.21 “A/D Conversion Speeds” for further details. Selecting the A/D Conversion Clock The A/D converter has a maximum rate at which conversions may be completed. An analog module clock, TAD, controls the conversion timing. The A/D conversion requires 14 clock periods (14 TAD). The A/D clock is derived from the device instruction clock. The period of the A/D conversion clock is software selected using a six-bit counter. There are 64 possible options for TAD, specified by the ADCS<5:0> bits (ADCON3<5:0>). Equation 18-1 gives the TAD value as a function of the ADCS control bits and the device instruction cycle clock period, TCY. Equation 18-1: A/D Conversion Clock Period For correct A/D conversions, the A/D conversion clock (TAD) must be selected to ensure a minimum TAD time of 333.33 nsec Linker script Sekce pro nadefinovéní přerušení vypadá takto: LONG(DEFINED(__ADCInterrupt ) ? ABSOLUTE(__ADCInterrupt ) : ABSOLUTE(odAD)); - změnili jsme návěští odAD, to musí být samozřejmě v source file jako .global