Flykar ATD Module

Source: Internet
Author: User

The atd module of the s12xs family MCU has 27 registers, six conversion control registers, two conversion status registers, one comparison enable register, one comparison mode register, and 16 Conversion Result registers, attach a routine

Void Atd_init (Void) 

  Atd0ctl1 = 0x0f;  // Select the 8-bit conversion precision
  

  Atd0ctl2 = 0x40; // Enable the CCF quick reset bit, disable the External Trigger input, and disable the interrupt   

 Atd0ctl3 = 0x08;  // Data is left aligned, non-FIFO, and the conversion sequence length is 1
  

 Atd0ctl4 = 0xe3; // The sampling time is 24 ATD clock periods,               Atdclk = 8 Mb/8 = 1 MHz
  

}

Generally, several registers are used in this routine.

//-----------------------------------------------------------//
// Function Description: MC9S12XS128--ATD routines
// Instructions for use: Multi-Channel A/D conversion is performed by the channel atd0. The conversion value is displayed at Port B.
//ProgramDESIGN: demok Studio (demok.taobao.com)
// Design time: 2010.03.12
//------------------------------------------------------------//

# Include # Include "derivative. H"

Word ad_wvalue; // adconversion result


// -------------- Initialization function ----------------//
// ----- Clock initialization program --------//

Void pll_init (void) // pllclk = 2 * oscclk * (synr + 1)/(refdv + 1)
{// PLL clock = 2*16*(2 + 1)/(1 + 1) = 48 MHz
Refdv = 1; // bus clock = 48/2 = 24 MHz
Synr = 2;
While (! (Crgflg & 0x08 ));
Clksel = 0x80;
}

// ----- ATD initialization program --------//

Void ad_init (void)
{
Atd0ctl1 = 0x00; // select ad channel as External Trigger, 8-bit precision, no discharge before sampling
Atd0ctl2 = 0x40; // The flag is automatically cleared. External triggering and interruption are prohibited.
Atd0ctl3 = 0xa0; // The right alignment is unsigned. Four sequences are converted each time. The conversion continues in the no-FIFO and freeze modes.
Atd0ctl4 = 0x01; // the sampling time is 4 ad clock cycles, PRS = 1, atdclock = 6 MHz
Atd0ctl5 = 0x30; // special channel prohibited, continuous conversion, multi-channel conversion, starting channel 0 conversion 4 channels
Atd0dien = 0x00; // enter a number.
}

// ----- Read the adconversion result --------//

Void ad_getvalue (word * ad_wvalue)
{
* Ad_wvalue = atd0dr0; // read the result register value
}

// ----- Main function --------//

void main (void)
{< br> pll_init ();
ad_init ();
ddrb = 0xff;
portb = 0x00;
enableinterrupts;
for (;)
{< br> while (! Atd0stat2_ccf0); // wait until the conversion ends while (atdostat2_ccf0 = 1)
ad_getvalue (& ad_wvalue); // read the Conversion Result
portb = (byte) ad_wvalue; // display the conversion value on port B
}< BR >}< span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.