312KB

The following document contains information on Cypress products.
Colophon
The products described in this document are designed, developed and manufactured as contemplated for general use,
including without limitation, ordinary industrial use, general office use, personal use, and household use, but are not
designed, developed and manufactured as contemplated (1) for any use that includes fatal risks or dangers that, unless
extremely high safety is secured, could have a serious effect to the public, and could lead directly to death, personal injury,
severe physical damage or other loss (i.e., nuclear reaction control in nuclear facility, aircraft flight control, air traffic control,
mass transport control, medical life support system, missile launch control in weapon system), or (2) for any use where
chance of failure is intolerable (i.e., submersible repeater and artificial satellite). Please note that Spansion will not be liable
to you and/or any third party for any claims or damages arising in connection with above-mentioned uses of the products.
Any semiconductor devices have an inherent chance of failure. You must protect against injury, damage or loss from such
failures by incorporating safety design measures into your facility and equipment such as redundancy, fire protection, and
prevention of over-current levels and other abnormal operating conditions. If any products described in this document
represent goods or technologies subject to certain restrictions on export under the Foreign Exchange and Foreign Trade Law
of Japan, the US Export Administration Regulations or the applicable laws of any other country, the prior authorization by the
respective government entity will be required for export of those products.
Trademarks and Notice
The contents of this document are subject to change without notice. This document may contain information on a Spansion
product under development by Spansion. Spansion reserves the right to change or discontinue work on any product without
notice. The information in this document is provided as is without warranty or guarantee of any kind as to its accuracy,
completeness, operability, fitness for particular purpose, merchantability, non-infringement of third-party rights, or any other
warranty, express, implied, or statutory. Spansion assumes no liability for any damages of any kind arising out of the use of
the information in this document.
®
®
®
TM
Copyright © 2013 Spansion Inc. All rights reserved. Spansion , the Spansion logo, MirrorBit , MirrorBit Eclipse ,
TM
ORNAND and combinations thereof, are trademarks and registered trademarks of Spansion LLC in the United States and
other countries. Other names used are for informational purposes only and may be trademarks of their respective owners.
Fujitsu Semiconductor (Shanghai) Co., Ltd.
Application Note
MCU-AN- 500005-E-11
F²MC-8FX FAMILY
8-BIT MICROCONTROLLER
MB95200H/210H SERIES
A/D CONVERTER
APPLICATION NOTE
A/D Converter V1.1
Revision History
Revision History
Date
2008-03-20
2008-07-15
Author
Benjamin. Yang
Benjamin. Yang
Change Record
V1.0, First draft
V1.1, Modification
This manual contains 30 pages.
1. The products described in this manual and the specifications thereof may be changed without prior
notice. To obtain up-to-date information and/or specifications, contact your Fujitsu sales representative or
Fujitsu authorized dealer.
2. Fujitsu will not be liable for infringement of copyright, industrial property right, or other rights of a third
party caused by the use of information or drawings described in this manual.
3. The contents of this manual may not be transferred or copied without the express permission of Fujitsu.
4. The products contained in this manual are not intended for use with equipment which require extremely
high reliability such as aerospace equipment, undersea repeaters, nuclear control systems or medical
equipment for life support.
5. Some of the products described in this manual may be strategic materials (or special technology) as
defined by the Foreign Exchange and Foreign Trade Control Law. In such cases, the products or portions
thereof must not be exported without permission as defined under the law.
© 2008 Fujitsu Semiconductor (Shanghai) Co., Ltd.
MCU-AN- 500005-E-11 – Page 2
A/D Converter V1.1
CONTENTS
CONTENTS
REVISION HISTORY .............................................................................................................. 2 CONTENTS ............................................................................................................................ 3 1 INTRODUCTION ................................................................................................................ 5 2 FEATURE .......................................................................................................................... 6 2.1 Key Features ............................................................................................................. 6 2.2 Block Diagram ........................................................................................................... 7 2.3 Registers ................................................................................................................... 8 2.3.1 8/10-bit A/D Converter Control Register 1 (ADC1) ..................................... 8 2.3.2 8/10-bit A/D Converter Control Register 2 (ADC2) ..................................... 9 2.3.3 8/10-bit A/D Converter Data Registers Upper/Lower (ADDH, ADDL) ....... 10 2.4 Interrupts of 8/10-bit A/D Converter ........................................................................ 11 2.4.1 Interrupts in 8/10-bit A/D Converter Operation .......................................... 11 2.4.2 Register and Vector Table Related to 8/10-bit A/D Converter Interrupts .. 11 3 ANALOG INPUT AND RELATED EXTERNAL CIRCUITS ............................................. 12 3.1 Electrical Characteristics ......................................................................................... 12 3.2 External Circuits for Analog Input............................................................................ 13 4 CONSIDERATION FOR SAMPLING TIME ..................................................................... 15 5 EXAMPLES FOR ADC .................................................................................................... 16 5.1 A/D General Procedure ........................................................................................... 16 5.1.1 Initialization ............................................................................................... 16 5.1.2 Interrupt Processing .................................................................................. 16 5.2 How to Start A/D ..................................................................................................... 17 5.2.1 By Setting Start-bit to Start A/D Converter ................................................ 17 5.2.2 By 8/16-bit Composite Timer (TO00) Output ............................................ 18 5.3 How to Judge if A/D Converter has Finished .......................................................... 19 5.3.1 Checking with the Conversion Flag Bit ..................................................... 19 5.3.2 Checking with Interrupt Request Flag Bit .................................................. 20 6 NOTES ON USING 8/10-BIT A/D CONVERTER ............................................................ 22 6.1 Notes on Programming Setup ................................................................................. 22 6.2 Note on Interrupt Requests ..................................................................................... 22 6.3 Error ........................................................................................................................ 22 6.4 8/10-bit A/D Converter Analog Input Sequences .................................................... 22 7 ADDITIONAL INFORMATION ......................................................................................... 23 MCU-AN- 500001-E-11 – Page 3
A/D Converter V1.1
CONTENTS
8 APPENDIX ....................................................................................................................... 24 8.1 Index of Figures ...................................................................................................... 24 8.2 Index of Tables........................................................................................................ 24 8.3 Sample Code .......................................................................................................... 25 8.3.1 Project 1 .................................................................................................... 25 8.3.2 Project 2 .................................................................................................... 27 8.3.3 Project 3 .................................................................................................... 29 MCU-AN- 500005-E-11 – Page 4
A/D Converter V1.1
Chapter 1 Introduction
1 Introduction
This application note describes how to use 8/10-bit A/D converter in each mode.
The following application note is intended to describe the functions of the Analog/Digital
Converter (ADC) and gives some examples.
MCU-AN- 500001-E-11 – Page 5
A/D Converter V1.1
Chapter 2 Feature
2 Feature
This chapter introduces the basic functions of 8/10-bit A/D converter.
2.1
Key Features
The A/D converter converts analog voltage (input voltage) input to an analog input pin to 10bit digital value.
One of multiple analog input pins can be selected.
The conversion speed is programmable to be configured (selected according to
operating voltage and frequency).
An interrupt is generated when A/D conversion completes. The completion state of
conversion can also be checked with ADI bit in the ADC1 register.
To activate A/D conversion function, two methods there are given below.
a. Activation using the A/D bit in the ADC1 register
b. Continuous activation using the 8/16-bit composite timer output TO00
MCU-AN- 500005-E-11 – Page 6
A/D Converter V1.1
Chapter 2 Feature
2.2
Block Diagram
Figure 2-1 shows the internal block diagram of ADC.
Figure 2-1: Block Diagram of ADC
The analog input signal (AN00 ~ AN05) is passed by an analog channel selector to a sample
and hold stage and finally to a control circuit. The conversion results from analog to digital is
conserved in A/D converter data register (ADDH, ADDL). Once a conversion is started or
triggered, the control block will first sample the selected input channel and keep the voltage
level stable in the sample and hold stage.
MCU-AN- 500001-E-11 – Page 7
A/D Converter V1.1
Chapter 2 Feature
2.3
Registers
Please refer to Chapter 17 of the MB95200H/210H Series Hardware Manual for detailed
register setting.
2.3.1 8/10-bit A/D Converter Control Register 1 (ADC1)
This register is used to enable and disable individual function of 8/10-bit A/D converter,
select an analog input pin and check the status of A/D conversion.
Table 2-1: 8/10-bit A/D Converter Control Register 1 (ADC1)
Note that the number of analog input pins varies depending on the series. MB95200H series
has 6 channels of analog input pins (AN00~AN05). But MB95210H series has only 2
channels of analog input pins (AN05 ~ AN04). Analog input pins also serve as generalpurpose I/O ports.
MCU-AN- 500005-E-11 – Page 8
A/D Converter V1.1
Chapter 2 Feature
2.3.2 8/10-bit A/D Converter Control Register 2 (ADC2)
This register is used to select 8/10-bit A/D converter function, select input clock and perform
an interrupt and check the status of A/D converter.
Figure 2-2: 8/10-bit A/D Converter Control Register 2 (ADC2)
MCU-AN- 500001-E-11 – Page 9
A/D Converter V1.1
Chapter 2 Feature
2.3.3 8/10-bit A/D Converter Data Registers Upper/Lower (ADDH, ADDL)
The 8/16-bit A/D converter data registers upper/lower (ADDH, ADDL) contain the results of
10-bit A/D conversion. The upper two bits of 10-bit data correspond to ADDH register; the
lower eight bits correspond to ADDL register. Set ADC2:AD8=1, in which case eight bits of
data can be read from ADDL register, set ADC2:AD8=0, the bit selects 10-bit precision.
Figure 2-3: 8/10-bit A/D Converter Data Registers Upper/Lower (ADDH, ADDL)
MCU-AN- 500005-E-11 – Page 10
A/D Converter V1.1
Chapter 2 Feature
2.4
Interrupts of 8/10-bit A/D Converter
2.4.1 Interrupts in 8/10-bit A/D Converter Operation
When A/D conversion is completed, the interrupt request flag bit (ADC1: ADI) is set to "1". If
interrupt request enable bit is enabled (ADC2: ADIE = 1), an interrupt request is generated
to the interrupt controller. Writing "0" to the ADI bit using the interrupt service routine to clear
the interrupt request. The ADI bit is set when A/D conversion is completed, regardless of the
value of ADIE bit. CPU cannot return from interrupt processing if interrupt request flag bit
(ADC1: ADI) is set to “1” with interrupt requests enabled (ADC2: ADIE = 1). Be sure to clear
ADI bit within the interrupt service routine.
2.4.2 Register and Vector Table Related to 8/10-bit A/D Converter Interrupts
Table 2-2: Register and Vector Table Related to 8/10-bit A/D Converter Interrupts
Please refer to APPENDIX B Table of Interrupt Sources of the MB95200H/210H Series
Hardware Manual for interrupt request numbers and vector tables of all peripheral resources.
MCU-AN- 500001-E-11 – Page 11
A/D Converter V1.1
Chapter 3 Analog Input and Related External Circuits
3 Analog Input and Related External Circuits
This chapter explains electrical characteristics and basic external circuits
3.1
Electrical Characteristics
This table shows the main electrical characteristics of the 8FX Family.
Table 3-1: Electrical Characteristics Related to A/D converter
MCU-AN- 500005-E-11 – Page 12
A/D Converter V1.1
Chapter 3 Analog Input and Related External Circuits
3.2
External Circuits for Analog Input
Figure 3-1: A Suggested Connection for A/D Conversion
To protect the analog pins from an over-voltage, a so-called “clamping resistor” is usually
added to the input pins. The minimum value of the resistor can be chosen as
Rclamp = Uovervoltage / Iclamp
Where, Iclamp is the specified maximum clamping current in the Data Sheet.
For some applications, a large clamping resistor is sometimes unacceptable. As a
compromise, an external clamping diode with low leakage current could be added between
the input pin and VCC pin.
In some cases, the sensor has been biased with a voltage supply higher than the maximum
guaranteed voltage for the microcontroller. For example, in the automotive applications, the
sensors could be biased directly with the car battery, which exhibits a voltage of 12V/24V. A
resistor divider consisting of R1/R2 is commonly used to tail the sensor voltage signal “seen”
on the pin down to the value which is equal to or smaller than VCC (see Figure 3-1). The
ratio between R1 and R2 should satisfy the following constraint:
Other factor which affects the size dimension of R1, R2 and Rclamp, is related to current
consumption budget and the input signal noise suppressing. The second factor will be
discussed here with more details. The signal from the sensors could be also noisy. The
noise, which has a time constant smaller than the sampling time Tsampling, is transparent to
the ADC, resulting in distorted output. In this case, an additional dedicated bypass capacitor
the clamping resistor or resistor divider, works as a low pass filter. A larger capacitor will
lower the AC impedance and will be more effective at shunt away the noise signal.
Generally, the time constant of this low pass filter (Rclamp + R1 || R2) x Cnoise should be
chosen considerably larger than the sampling time (5 to 10 times larger with a rule of
thumb).
However, at the same time this time constant should be also considerably smaller than the
one of the sensor signal, depending on the applications. In this way, the analog pin is able to
MCU-AN- 500001-E-11 – Page 13
A/D Converter V1.1
Chapter 3 Analog Input and Related External Circuits
follow the dynamic changes, which the ADC is being used to track. These, along with the
dimension of R1/R2 or Rclamp must be considered when choosing the capacitor dimension
to avoid rolling off any high frequency signal components of interest.
MCU-AN- 500005-E-11 – Page 14
A/D Converter V1.1
Chapter 4 Consideration for Sampling Time
4 Consideration for Sampling Time
This chapter introduces the relationship between variables.
This section introduces the sampling time in MCU (MB95200H/210H). First, we introduce the
relationship between variables.
1 MCLK= 1/ PLL clock
The speed of A/D conversion is affected by clock mode, main clock oscillation frequency and
main clock speed switching (gear function).
Example: Sampling time= CKIN× (ADC2: TIM1/TIM0 setting)
Compare time= CKIN×10 (fixed value) +MCLK
Conversion time= A/D start processing time + sampling time +compare time
The error max.1CKIN-1MCLK may occur depending on the startup timing of AD
converter.
Program the software satisfied with "sampling time" and "compare time" in A/D
Converter. Refer to the section” Electrical Characteristics” in Data Sheet of F2MC8FX MB95200H/210H Series.
MCU-AN- 500001-E-11 – Page 15
A/D Converter V1.1
Chapter 5 Examples for ADC
5 Examples for ADC
This chapter gives examples on 8/10-bit A/D converter
Generally, there are two methods to select 8/10-bit A/D conversion activation. One is to start
by 8/16-bit composite timer (TO00) output, and the other is to set A/D conversion start bit
(ADC1: AD=1). Also two methods there can be used to check whether the conversion has
been completed or not.
5.1
A/D General Procedure
The following procedure is used to set up 8/10-bit A/D converter:
5.1.1 Initialization
Set the port for input (DDR1).
Set the interrupt level (ILR4).
Enable A/D input (ADC1:ANS0 to ANS3).
Set the sampling time (ADC2:TIM1, TIM0).
Select the clock (ADC2:CKDIV1, CKDIV0).
Set A/D conversion precision (ADC2:AD8).
Select the operation mode (ADC2: EXT).
Select the startup trigger (ADC2: ADCK).
Enable interrupts (ADC2:ADIE=1).
Activate A/D converter (ADC1:AD=1).
5.1.2 Interrupt Processing
Clear the interrupt request flag (ADC1: ADI=0).
Read convertsion values (ADDH, ADDL).
Activate AD converter (ADC1:AD=1).
MCU-AN- 500005-E-11 – Page 16
A/D Converter V1.1
Chapter 5 Examples for ADC
5.2
How to Start A/D
There are two methods to start A/D converter. One method is to set the start-bit to generate
a software trigger. The other method is to start A/D converter by 8/10-bit composite timer
(TO00) output. Examples for above are gave as follow.
5.2.1 By Setting Start-bit to Start A/D Converter
Use the A/D conversion start bit (ADC1: AD = 1) to generate a software trigger.
ADC2
= 0x81;
//8-bit resolution,2×MCLK
ADC1
= 0x00;
//AN00 as input
ADC1_AD
= 1;
//start AD converter
Refer to appendix for sample code project1”AD_BASIC”
MCU-AN- 500001-E-11 – Page 17
A/D Converter V1.1
Chapter 5 Examples for ADC
5.2.2 By 8/16-bit Composite Timer (TO00) Output
In this case, the timer0 has been used. For details on Timer0 operation, please refer to
Chapter 14 in Hardware Manual of of MB95200H/210H Series.
The following example shows how to set up the 8/16-bit composite timer 0.
/**********************************************************
Name:
InitCompTimer
Function:
TO00 control AD converter
**********************************************************/
void InitCompTimer(void)
{
T00DR = 0xE0;
// set count value (low 8 bit)
TMCR0 = 0x40;
// 8-bit, no filtering
T00CR0 = 0x01;
// interval timer with continuous mode
T00CR1 = 0x81;
// enable output, start timer
}
Also the example for A/D conversion shows as follows
/****************************************************
Name:
MCU_Initialization
Function:
Initialization MCU
*****************************************************/
void MCU_initialization()
{
//.....
ADC1=0x00;
//AN00 as input
ADC2=0x81;
//8-bit resolution, 2×MCLK
ADC1_AD = 0;
//Don’t to start AD conversion.
ADC2_ADCK =1;
//Start via 8/16-bit composite timer (TO00) output
ADC2_EXT =1;
//Continuous activation with the clock selected by
the ADCK bit in the ADC2 register
//.....
}
Refer to appendix for sample code project2”AD-TIMER”
MCU-AN- 500005-E-11 – Page 18
A/D Converter V1.1
Chapter 5 Examples for ADC
5.3
How to Judge if A/D Converter has Finished
5.3.1 Checking with the Conversion Flag Bit
In this case, we should pay attention to the conversion flag bit (ADC1: ADMV). If the read
value is "1", the A/D conversion is in progress.
/*************************************************
Name:
AD_Sample
Function:
Use AN00 as the sample voltage channel
**************************************************/
void AD_Sample(void)
{
Distem = ADDL/50;
//Read AD Sample Value
ADC1_AD=1;
//Start AD again
}
/***********************************************************************
Name:
syinit
Function:
Initial AD
***********************************************************************/
void syinit(void)
{
ADC1
= 0x01;
// start AD converter
ADC2
= 0x82;
//8-bit precision, 4 MCLK
}
/********************************************************************
Name:
main
Function:
Main Routine
***********************************************************************/
void main(void)
{
syinit();
while (1)
{
while(ADC1_ADMV);
//Check converter finish flag,
//ADC1_ADMV = 0 means AD Sample finished
AD_Sample();
}
}
Refer appendix sample code project1”AD_BASIC”
MCU-AN- 500001-E-11 – Page 19
A/D Converter V1.1
Chapter 5 Examples for ADC
5.3.2 Checking with Interrupt Request Flag Bit
In this case, we should pay attention to check with interrupt request flag bit, if the interrupt
request flag (ADC1: ADI=1), A/D conversion are completed with interrupt request generated.
/*********************************************************************
Name:
InitADC
Function:
initialize AD
**********************************************************************/
void InitADC(void)
{
ADC2 = 0x89;
// 8-bit resolution, enable interrupts
ADC1 = 0x01;
// AN0 pin as input
}
/********************************************************************
Name:
Function:
main
Main Routine
***********************************************************************/
void main(void)
{
while(1)
// waiting for interrupt ( no operation )
__asm("nop");
}
/********************************************************************
AD Interrupt process
***********************************************************************/
__interrupt void ISR_ADC (void)
{
Temp=ADDL;
// read the AD sample value
ADC1 = 0x00;
// clear interrupt flag
}
MCU-AN- 500005-E-11 – Page 20
A/D Converter V1.1
Chapter 5 Examples for ADC
Please note, that the corresponding interrupt vector and level shall be defined in the
Vectors.c module of our standard template project.
void InitIrqLevels(void)
{
ILR4 = 0xDF;
// IRQ18: 10-bit AD-converter
// IRQ19: Timebase timer
}
..............
__interrupt void ISR_ADC (void);
..............
#pragma intvect ISR_ADC
18 // IRQ18: 10-bit AD-converter
Refer to appendix for sample code project3”AD_INT”
MCU-AN- 500001-E-11 – Page 21
A/D Converter V1.1
Chapter 6 Notes on Using 8/10-bit A/D Converter
6 Notes on Using 8/10-bit A/D Converter
This section summarizes notes on using 8/10-bit A/D converter.
6.1
Notes on Programming Setup
When A/D conversion function is used, the contents of ADDH and ADDL registers
are retained upon the Completion of A/D conversion. During A/D conversion, the
values resulting from last conversion are loaded.
Do not re-select the analog input pin (ADC1: ANS3 to ANS0) while A/D conversion
function is running, especially, during continuous activation. Disable the continuous
activation (ADC2: EXT = 0) before re-selecting the analog input channel.
Starting the reset, stop, or watch mode stops 8/10-bit A/D converter and initializes
each register.
CPU cannot return from interrupt processing routine if the interrupt request flag bit
(ADC1: ADI) is set to”1” with interrupt requests enabled (ADC2: ADIE = 1). Be sure
to clear the ADI bit within interrupt service routine.
6.2
Note on Interrupt Requests
If A/D conversion is reactivated (ADC1: AD = 1) and terminated simultaneously, the interrupt
request flag bit (ADC1: ADI) is set.
6.3
Error
As | Vcc - Vss | decreases, an error increases relatively.
6.4
8/10-bit A/D Converter Analog Input Sequences
Turn on analog input (AN00 to AN05) at the same as or after turning on the digital power
supply (VCC). In addition, turn off digital power supply (VCC) either at the same time as or
after turning off analog input (AN00 to AN05).
Be careful not to let analog input exceed the voltage of digital power supply when turning
8/10-bit A/D converter on and off.
MCU-AN- 500005-E-11 – Page 22
A/D Converter V1.1
Chapter 7 Additional Information
7 Additional Information
For more information on FUJITSU SEMICONDUCTOR products, please visit the following
websites:
English version:
http://www.fujitsu.com/cn/fsp/services/mcu/mb95/application_notes.html
Simplified Chinese Version:
http://www.fujitsu.com/cn/fss/services/mcu/mb95/application_notes.html
MCU-AN- 500001-E-11 – Page 23
A/D Converter V1.1
Chapter 8 Appendix
8 Appendix
8.1
Index of Figures
Figure 2-1: Block Diagram of ADC ........................................................................................... 7 Figure 2-2: 8/10-bit A/D Converter Control Register 2 (ADC2) ................................................ 9 Figure 2-3: 8/10-bit A/D Converter Data Registers Upper/Lower (ADDH, ADDL) ................. 10 Figure 3-1: A Suggested Connection for A/D Conversion ..................................................... 13 8.2
Index of Tables
Table 2-1: 8/10-bit A/D Converter Control Register 1 (ADC1) ................................................. 8 Table 2-2: Register and Vector Table Related to 8/10-bit A/D Converter Interrupts .............. 11 Table 3-1: Electrical Characteristics Related to A/D converter .............................................. 12 MCU-AN- 500005-E-11 – Page 24
A/D Converter V1.1
Chapter 8 Appendix
8.3
Sample Code
8.3.1 Project 1
Name: AD_BASIC
Function: By setting start-bit and Check with the conversion flag bit
/******************************************************************************
Name:
MAIN.C
******************************************************************************/
#include "mb95200.h"
unsigned char Distem;
/******************************************************************************
Name:
AD_Sample
Function:
Use AN00 as the sample voltage channel
******************************************************************************/
void AD_Sample(void)
{
Distem = ADDL;
// Read AD Sample value
ADC1_AD=1;
// Start AD again
}
/******************************************************************************
Name:
syinit
Function:
Initial AD, IO-PORT
******************************************************************************/
void syinit(void)
{
ADC1=00;
//AN00 as input
ADC2 = 0x81;
//8-bit resolution£¬2¡ÁMCLK
ADC1_AD = 1;
//start A/D converter
DDR0 = 0x00;
//use P00-P05 input
PDR0 = 0x00;
}
/******************************************************************************
Name:
main
Function:
Main Routine
******************************************************************************/
void main(void)
{
syinit();
MCU-AN- 500001-E-11 – Page 25
A/D Converter V1.1
Chapter 8 Appendix
InitIrqLevels();
// initialize Interrupt level register and IRQ vector table
__EI();
// global interrupt enable
__set_il(3);
// set global interrupt mask to allow all IRQ levels
while(1)
{
while(ADC1_ADMV);
//Check converter finish flag
AD_Sample();
}
}
MCU-AN- 500005-E-11 – Page 26
A/D Converter V1.1
Chapter 8 Appendix
8.3.2
Project 2
Name: AD-TIMER
Function: Start A/D by 8/16 bit compound timer and checking with the conversion flag bit
/******************************************************************************
Name:
MAIN.C
Function:
Start AD By 8/16-bit composite timer (TO00) output.
******************************************************************************/
#include "mb95200.h"
unsigned char ad_data;
/******************************************************************************
Name:
MCU_Initialization
Function:
Initialization MCU
******************************************************************************/
void MCU_initialization()
{
__DI();
SYCC=0x00;
//MCLK = source clock = 8 MHz (Main CR)
DDR0_P05 = 1;
AIDRL=0xfc;
//IO port
WDTC=0x35;
//Clear watch dog timer
ADC1=0x00 ;
//AN00 as input
ADC2=0x81;
//8-bit precision,disable interrupt, 2¡ÁMCLK
ADC1_AD = 0;
//No start A/D conversion.
ADC2_ADCK =1;
//Start AD by 8/16-bit composite timer (TO00) output
// Continuous activation with the clock selected by the ADCK bit in the ADC2 register
ADC2_EXT =1;
}
/******************************************************************************
Name:
InitCompTimer
Function:
TO00 control AD converter
******************************************************************************/
void InitCompTimer(void)
{
T00DR = 0xE0;
// set count value (low 8 bit)
TMCR0 = 0x40;
// 8-bit, no filtering
T00CR0 = 0x01;
// interval timer with continuous mode
T00CR1 = 0x81;
// enable output, start timer
MCU-AN- 500001-E-11 – Page 27
A/D Converter V1.1
Chapter 8 Appendix
}
/******************************************************************************
Name:
ad_sample
Function:
AD sample code
******************************************************************************/
void ad_sample()
{
while (ADC1_ADMV);
// If AD Complete?
ad_data=ADDL;
//Read AD Sample value
}
/******************************************************************************
Name:
main
Function:
Main Loop
******************************************************************************/
void main()
{
MCU_initialization();
InitCompTimer();
while(1)
{
ad_sample();
WDTC=0x35;
//Clear watch dog timer
}
}
MCU-AN- 500005-E-11 – Page 28
A/D Converter V1.1
Chapter 8 Appendix
8.3.3
Project 3
Name: AD_INT
Function: Start A/D by setting start-bit and checking with interrupt request flag bit
/******************************************************************************
Name:
MAIN.C
Function:
Checking with interrupt request flag bit
******************************************************************************/
#include "mb95200.h"
/******************************************************************************
Name:
InitADC
Function:
initialize AD
******************************************************************************/
int tmp;
void InitADC(void)
{
ADC2 = 0x89;
// 8-bit resolution, enable interrupts
ADC1 = 0x01;
// AN0 pin as input, start AD
}
/******************************************************************************
Name:
main
Function:
Main Routine
******************************************************************************/
void main(void)
{
PDR0 = 0x00;
// Port 0:
AIDRL = 0xfe;
// used as I/O-port (no analog inputs), P00 as AN-input
DDR0 = 0x00;
// Set to input
InitIrqLevels();
// initialise Interrupt level register and IRQ vector table
__EI();
// global interrupt enable
__set_il(3);
// set global interrupt mask to allow all IRQ levels
InitADC();
// init AD - converter
while(1)
// waiting for interrupt ( no operation )
{
__asm("nop");
}
}
MCU-AN- 500001-E-11 – Page 29
A/D Converter V1.1
Chapter 8 Appendix
/*****************************************************************************
Interrupt process
******************************************************************************/
#include “mb95200.h”
__interrupt void ISR_ADC (void)
{
tmp = ADDL;
// voltage calculating
ADC1 = 0x01;
// clear interrupt flag, start A/D again
}
/*****************************************************************************
VECTORS.C
******************************************************************************/
void InitIrqLevels(void)
{
ILR4 = 0xDF;
// IRQ18: 10-bit AD-converter
// IRQ19: Timebase timer
}
__interrupt void DefaultIRQHandler (void);
__interrupt void ISR_ADC (void);
#pragma intvect ISR_ADC 18
// IRQ18: 10-bit AD-converter
__interrupt void DefaultIRQHandler (void)
{
__DI();
// disable interrupts
while(1)
__wait_nop();
// halt system
}
MCU-AN- 500005-E-11 – Page 30