//************************************** *************
// To measure and control the temperature, pressure, flow, speed, displacement, and other physical quantities
// The sensor converts the above physical quantity into an electrical signal that simulates the physical quantity, that is, a simulated electrical signal, and then passes through the module
// The converter is converted to a digital volume and sent to the processor for processing.
// Objective: To understand the use of ADC
//************************************** *************
// Designed by: http://blog.ednchina.com/Manymany/
// 2009-07-10
/*************************************** *************************
Function:
The AD function converts analog channels by adjusting the adjustable resistance connected to the channels.
Changes the analog input and displays the echo data to the terminal through the serial port.
**************************************** *************************/
# Include "2440addr. H"
# Include "2440lib. H"
Void test_adc (void );
Void xmain (void)
{
Changeclockdivider (); //
Changempllvalue (405, 2, 1); // MHz
Port_init ();
Isr_init ();
Uart_init (0, 115200 );
Uart_select (0 );
Uart_printf ("the main is running \ n ");
Test_adc (); // ad test function
}
// ================================================ ========================================================== ====
Int readadc (INT ch) // read the ADC function. ch determines the channel to be selected. The number of channels (CH is 0-7)
{
Int I;
Static int prevch = "-1 ";
// Enable ADC settings
// Set the number division value to 49.
// Select the channel bit. the voltage on the selected channel is connected to the ad
Radccon = (1 <14) | (49 <6) | (CH <3 );
If (prevch! = CH)
{
For (I = 0; I <1000; I ++); // delay to set up the next channel
Prevch = "ch ";
}
Radccon | = 0x1; // enables the ADC.
While (! (Radccon & 0x8000); // determines whether the conversion is complete. If the conversion is completed, skip to the next statement,
// Otherwise, an endless loop is generated until it is completed.
// Adccon [15] bit: the flag of the conversion result, which is a read-only bit. If the value is 0, it indicates
// If the value is 1, the modulus conversion is completed.
Return (INT) radcdat0 & 0x3ff); // The number returned is the number converted by the ADC.
}
// ================================================ ========================================================== ====
Void test_adc (void)
{
Int adcdata = "0"; // defines a variable to store the converted number.
Uart_printf ("\ NADC input test, Press ESC key to exit! \ N ");
While (uart_getkey ()! = Esc_key)
{
Adcdata = "readadc" (adc_ch );
Uart_printf ("Ain % d: % 04d \ n", adc_ch, adcdata); // terminal display
Delay (2000 );
}
}