This application note will describe the implementation of an ADC user module in PSoC Designer which can be used to program Cypress’s line of PSoC microcontrollers.. Keywords: ADC: Analog
Trang 1How to Complete ADC Using PSoC
Designer Anthony Recca
11/05/06
Executive Summary: Analog to digital conversion is one way of communicating the
desired speed setting to the control circuit of a brushless DC motor This application note will describe the implementation of an ADC user module in PSoC Designer which can be used to program Cypress’s line of PSoC microcontrollers Further, code will be supplied which illustrates how to read the digital value in C language, and to choose a value which can be used to control the duty cycle of a pulse width modulated signal to ultimately control the speed of the motor
Keywords: ADC: Analog to Digital Conversion
PSoC: Programmable System on Chip PWM: Pulse Width Modulation ADCINCVR: Specific ADC User Module I/O: Input/Output
Trang 2Cypress has created many user modules for their Designer program which greatly simplifies programming their chips One of these modules provides capability for analog
to digital conversion, ADC There are more than ten ADC modules available within PSoC Designer, and which one the user chooses should be based upon their application and the particular chip they are using In the project the author is working on, the
CY8C29466 PSoC is used, and the ADCINCVR will be chosen to reduce current draw
Overview
In the following pages, a short description of the digital/analog blocks associated with the ADCINCVR and the method to add an ADCINCVR in PSoC Designer will be presented Following that will be a discussion on what values should be initially set for the digital and analog blocks used for implementation of this module, and how those values are found Next will be a discussion of the Application Programming Interface, API, functions that are built in to the module and how they are accessed in C code Finally, a short segment of code will be provided to illustrate correct use of the
ADCINCVR With the information provided, the reader should be able to implement their own ADCINCVR user module in PSoC Designer
ADCINCVR
The ADCINCVR user module requires three digital blocks and one analog block The three digital blocks are split into two counters, one is an 8-bit counter which
accumulates the number of cycles the output is positive, and the other two blocks are for
a 16-bit PWM that measures the integrate time, explained later, and gates the clock to the 8-bit counter The analog block requirement is for the ADC itself
Implementation
PSoC Designer has two main editors for the programmer, the Application Editor and the Device Editor The Device Editor gives a graphical view of the digital and analog blocks available, input/output ports, buses which can be used to interconnect modules and ports, and allows the user to add modules and modify the parameters for those modules Much more is available in the Device Editor, however those are the important functions pertinent to this discussion To view the Device Editor, click the button labeled A in Figure 1 below To add a user module to the project, click the pull down menu labeled C in Figure 1, choose ADC, and then choose the ADCINCVR from the pull down menu labeled D Clicking button E will add the user module to the list of modules in your project Now you must add the module to your analog and digital blocks, Figure 2, by clicking button G You may select which available digital blocks you wish to place the counter and PWM blocks in to by pressing button F to rotate
through the available blocks Interrupts shall not be discussed in this note, however it will be mentioned that Cypress recommends the counter be placed below the PWM digital blocks for interrupt priority as show in Figure 2 The input to the ADCINCVR can be routed to an external pin through the ports available, or to another analog block
To route the input to an external pin, the ADCINCVR analog block must be placed in the first column of analog blocks as seen in Figure 2 Looking closely, we can see that Port 2_1 is connected to the input of the analog block of the ADCINCVR Each port is routed
to one I/O pin on the chip, this port is routed to pin 8
Trang 3Figure 1: Toolbar in PSoC Designer
Figure 2: Digital Blocks in PSoC Designer Figure 3: Analog blocks and ports (left) in PSoC
Designer
Parameter Selection
Global Resource Parameters
When creating a project in PSoC Designer, there are parameters for global
resources, and parameters for specific user modules First the global resource parameters pertinent to this application will be discussed Figure 4 shows the global resource
parameters The first resource is the power setting and system clock This particular PSoC can be powered with 5V or 3.3V, and the system clock can be chosen to be either 24MHz or 6MHz for either power supply For this example, 5V and 24MHz has been chosen The next parameter is the CPU_Clock and this can be set to the system clock divided by 2^N, with 0<n<9 For this example, we will leave it equal to the system clock
at 24MHz The next parameters that must be considered for this example are VC1 and VC2 These can be used as the clock input to the digital and analog blocks in our project
We will set VC1=3MHz by setting it equal to system clock divided by 8, and
VC2=1.5MHz by dividing VC1 by 2 The reasons for this will be explained in the next section Lastly the Ref Mux parameter must be set This will determine our voltage range on the input to the ADC We will set it to (VDD/2)+/-(VDD/2) VDD=5V as set in the power setting parameter, so our range is from 0-5V This also sets our Analog
Ground (AGND) to 2.5V The rest of the global resource parameters will be left alone
as they do not impact the scope of this application
Trang 4
Figure 4: Global Resource Parameters Figure 5: User Module Parameters
User Module Parameters
The first parameter for the ADCINCVR user module, as seen in Figure 5, is the input This describes where the analog signal which we wish to convert to digital comes from We have the option of choosing the input to come from any one of the adjacent analog blocks or ports For this project, the input has been set to Port_2_1 as seen in Figure 5, and Figure 3 ClockPhase can be set to Norm or Swap This should be
considered when the input to the ADC is from another analog block The analog blocks
in this PSoC output their value on the second phase of a two-phase clock controlling those blocks If set to Norm, the analog block you are working with will read on phase one, however the output from the other analog block will be zero as their output is only valid during phase two In this application our input is from an external pin, so we will leave this as Norm The next three values will require some computing to find their appropriate values Table 1 below shows the three equations used to determine these values The time it takes the ADC to convert is broken in to two parts, the integrate time and the CalcTime The first value that must be computed is the integrate time We can filter out unwanted signals of a certain frequency by setting the integrate time equal to the period of that signal We will use 1.5 kHz for this example The clock is determined by the equation shown, and is found to be 1.536MHz We will round this down to 1.5MHz Using VC2 as set in the global resources will provide the correct clock frequency
Resolution refers to how many bits the digital result will be We are only concerned with finding digital value within three ranges for our project, therefore we are not too
concerned with accuracy and will use an 8-bit result The minimum time that the
calculation will take is 180 CPU clocks To ensure we provide enough time for the calculation to complete, we must set CalcTime greater than or equal to the equation below We will round up to 12 as 11.25 is not available DataFormat refers to whether the digital result is a signed or unsigned value For simplicity, we will set this to
unsigned
Trang 5Value Equation Result for this project
Clock 2^(Bits+2)/IntegrateTime 1.536MHz
CalcTime >=(Clock*180)/CPU_Clock 11.25
Table 1: Parameters, their equations, and results
Application Programming Interface Functions
Application Programming Interface, API, refers to the act of controlling the user modules in the C or Assembly language code For some modules, the parameters set when designing the project are accessible, and for most modules there are functions which must be called to initialize and start the module’s operation For the ADCINCVR there are 10 functions, as seen in the Table 2
_SetPower Power Setting: Int: 0-3 Nothing
_SetResolution Resolution(Bits): Int: 7-13 Nothing
_GetSamples Number of Samples: Int: 0-255
Table 2: API Functions
To call an API function, use the name of the module as defined by the user with the function name directly after as in ADCName_Start(Variable), with ADCName being the user-defined name of the module The _Start function may be called with or without a parameter If a parameter is passed for the power setting, _SetPower need not be called The power setting can be set with an integer taking the values 0-3, with 0 being the off-state, 1 being low power, 2 medium power, and 3 being high power It is recommended that high power is used unless the programmer is very experienced with PSoC Designer The number of bits that will be used to represent the digital result was set to 8 in the previous section This value may be modified inside the program, however the user must
be careful not to change this to a value that will effect the CalcTime parameter which can not be modified within the code The _Stop function and _StopAD functions are very similar Both stop the analog block from computing, however only the _StopAD
function will halt the counter and PWM digital blocks from running Power may be saved by halting the digital blocks from running _GetSamples determines how many samples you want the ADC to calculate A value of 0 will force continuous sampling and
is desirable for this application _fIsData will return either a 0 or a non-zero value This corresponds to a flag which is set when a new digital value has been calculated If a 0 is returned, this indicates that there is no new digital value, or that the value is corrupt This function should always be called before the following function, _iGetData This function returns the digital value that has been calculated The following function should be
Trang 6called after _iGetData _ClearFlag will reset the flag which indicates that a new digital value has been calculated Without resetting this, the user will have no way of
determining whether a new value has been calculated The last function combines the previous two together When called, _iGetDataClearFlag will return the digital value and reset the flag indicating that a new digital value has been calculated
Understanding the Calculated Digital Value
The equation for relating the digital value calculated to the analog value read in is quite simple In our example, we have set Vref=2.5, and our allowed voltage input range
of 0-5V The analog voltage in is related to the digital value out through the following
Bits
bits DigVal
) 1 (
^ 2
) 1 (
^ 2
This can easily be rearranged to
) 1 (
^ 2
* ) 1 (
^
Vref
Vin Bits
DigVal Vin is a measure of how far away from Vref the input voltage is For a voltage of 1.25V applied, this would relate to a Vin=-1.25V as
it is 1.5V below Vref Table 3 lists the analog voltage value and the corresponding digital value for our application
Analog Voltage Value Digital Value Speed
Table 3
Sample Application
In the above discussion, we described and set the values we would like to use for our sample application of an ADCINCVR with the Cypress CY8C29466 PSoC
Attached in Appendix A.1 is a segment of code that illustrates exactly how to call the API functions, and uses the digital result to select another variable which can be used to control the speed of a fan Appendix A.2 is a screenshot of the graphical view within PSoC with all the analog and digital blocks placed, as well as the global resource and user module parameters selected Also in that screenshot is a view of the chip being programmed Here, the user may define each pin as a global or analog I/O Attached as Appendix A.3 is a general flowchart the user should follow if they would like to
continuously read ADC values They may break out of the flowchart at any point to complete other operations, however upon returning to ADC control, manipulation, or retrieval, the programmer should follow from where they broke out of the flowchart
Trang 7A.1: Screenshot of the sample application
Trang 8A.2: Sample Code
Trang 9A.3 Flowchart
Trang 10ADCINCVR user module datasheet available at:
http://www.cypress.com/portal/server.pt?space=CommunityPage&control=SetCommunit y&CommunityID=285&PageID=552&shortlink=DA_560740&REF=SCH&CID=ILC-shortlinks&shk=DA_560740&REF=SCH
ADCINCVR tutorial program available at:
http://www.cypress.com/portal/server.pt?space=CommunityPage&control=SetCommunit y&CommunityID=285&PageID=552&shortlink=DA_795973&REF=SCH&CID=ILC-shortlinks&shk=DA_795973&REF=SCH