キーワード: MAXQ2010, analog to digital, ADC, temperature, thermistor, microcontroller
関連製品 |
void initADC ( ) { ADCN = 0x00; // Single conversion, external AVDD reference, divide-by-1 clock frequency. ADST = 0x10; //Select configuration register - read/write access on ADDATA. ADDATA = 0x00; //Channel 0 for AN0. ADDATA = 0x01; //Channel 0 for AN1. ADDATA = 0x02; //Channel 0 for AN2. ADDATA = 0x03; //Channel 0 for AN3. ADDATA = 0x04; //Channel 0 for AN4. ADDATA = 0x05; //Channel 0 for AN5. ADDATA = 0x06; //Channel 0 for AN6. ADDATA = 0x07; //Channel 0 for AN7, thermistor is attached on this channel. ADADDR = 0x07; //Selecting 0x07 as the last conversion configuration register. }After initialization, setting the ADCONV bit of the ADST register initiates a conversion. The hardware indicates that the conversion is complete by clearing that same bit. To read the results, set the ADIDX[3:0] bits of the ADST register to the input channel of interest. The ADDATA register will then hold the digital data of the corresponding channel. In this sample application, AN7 is assigned to ADC data buffer 7. Therefore to read thermistor data, set ADST = 0x07 and read ADDATA.
unsigned int getADCReading() { unsigned short data = 0; ADST_bit.ADCONV = 1; //Enable conversion. while( ADST_bit.ADCONV == 0x01 ); //ADST.ADCONV bit indicates conversion in progress. ADST = 0x07; //We are interested in reading only the AN7 input, that is thermistor data. data = ADDATA; //Read AN7 data. return data; }The following steps are performed on the digital value read to arrive at the temperature:
void initSerial() { SCON0_bit.SM1 = 1; // Set to Mode 1. SCON0_bit.REN = 1; // Enable receives. SMD0_bit.SMOD = 1; // Set baud rate to 16 times the baud clock. PR0 = 0x75F7; // Set phase for 115200 with an 8MHz crystal. SCON0_bit.TI = 0; // Clear the transmit flag. SBUF0 = 0x0D; // Send carriage return to start communication. }In the UART, a single register sends and receives serial data. Writing to the SBUF0 register initiates a transfer. When data becomes available on the serial port, reading the SBUF0 register retrieves the input. The example program uses the following function to output data to the serial port.
int putchar(int ch) { while(SCON0_bit.TI == 0); // Wait until we can send. SCON0_bit.TI = 0; // Clear the sent flag. SBUF0 = ch; // Send the char. return ch; }
TB0V = 0x00000; // Set current timer value. TB0R = 0x0F42; // Set reload value. TB0CN = 0x0506; // Set Timer Clock = SysClk/1024, Reload Timer Mode, Interrupt Enabled.Using this timer as an interrupt source for this example requires a few more steps. Interrupts for the MAXQ architecture must be enabled on three levels: globally, for each module, and locally. Using the IAR™ compiler, enable global interrupts by calling the
__enable_interrupt()
function. This function effectively sets the Interrupt Global Enable (IGE) bit of the Interrupt and Control (IC) register. Since timer B0 is located in module 4, set bit 4 of the Interrupt Mask Register (IMR) to enable interrupts for the module. Enable the local interrupt by setting the Enable Timer B Interrupts (ETB) bit in Timer B Control Register (TB0CN). These steps are shown below.
__enable_interrupt() TB0CN = 0x506; // Enable interrupts along with setting Timer Clock and run Timer. IMR |= 0x10; // Enable the interrupts for module 4.Finally, using an interrupt requires initializing the interrupt vector. IAR's compiler allows a different interrupt handling function for each module. Setting the interrupt handler for a particular module requires using the #pragma vector directive. The interrupt-handling function declaration should also be preceded by the __interrupt keyword. The example application declares an interrupt handler for module 4 in the following way.
#pragma vector = 4 __interrupt void timerInterrupt() { // Add interrupt handler here. }
a. JU14 and JU22 shorted to enable thermistor reading on channel AN7.
b. JU9 pin 2 and 3 shorted to enable UART0.
c. JU8 open to let 8MHz crystal source the controller.
a. lnkmaxq2010.xcl as the Linker command file (select Options from the Project menu, then select Linker from the Category list, then select the Config tab).
b. maxq2010.ddf as the Device description file (select Options from the Project menu, then select Debugger from the Category list, then select the Setup tab).
c. JTAG as the Driver (select Options from the Project menu, then select Debugger from the Category list, then select the Setup tab).
d. COMx port setting to communicate with JTAG (select Options from the Project menu, then select JTAG from the Category list).
a. PC COMx port, to which the UART0 of MAXQ2010 is connected.
b. Configuration properties as 115200 baud rate, 8 data bits, 1-STOP bit, and no parity and flow control.