Subscribe

Programming in pictures

Posted by Circuit Labs on Thursday, November 13, 2008

Coding without keywords

Author: Burkhard Kainka

e05b044

Microcontrollers are particularly hard for beginners to program. Graphical user interfaces
help to show interdependencies, but they have their limitations. We look at a couple of systems and some simple application examples.

<< Figure 1.Schematic diagram of a PSoC and its functional blocks

      Everyone has had the experience of recognising a face, but being unable to put a name to it. It is just the same in programming: commands and keywords, register names, the names of all the various control flags: it is a lot to hold in the mind at once. It is often easier to think in a ‘graphical’ way, for example describing the operation of a microcontroller in terms of a flow chart or an equivalent circuit diagram, before embarking on the work proper.

     This is almost the same as using a graphical interface: instead of typing in program text, you can simply click on logic functions and connections, draw lines between objects with a sweep of the mouse, and set properties of various elements by simply selecting from a list.

    Some manufacturers of microcontrollers have recognised this demand. In the struggle for market share it is no longer enough just to compete on performance figures; the product must also be easy for potential customers to use. The value of making a user’s first experience with a new chip a success makes the (high) development cost of graphical programming tools entirely justifiable.

Download PDF version for larger diagram

Figure 2. The analogue blocks that are in use are highlighted in colour in PSoC Designer’s Device Editor.

     We frequently have the choice between a graphical and a text-oriented programming environment. In general the graphical environment will seem simpler since in many cases decisions have already been made behind the scenes to make it harder for us to make mistakes. This can, however, be a limitation in more complex projects. The immediate experience with graphical programming environments and the ready-made projects supplied with them is indeed positive: it looks so easy that anyone could use it. However, this overlooks the fact that the interface is hiding many decisions. It is possible, for example, that objects have been given various properties that can only be revealed by clicking on them with the mouse. If you take a working example project, it is not always immediately obvious how many properties and decisions are hidden away in the background. Perhaps only when you try to build your own project from the ground up will it become apparent that you are standing before as great a mountain of information as if you had used a text-based interface.
     

      In other cases you might embark confidently on an ambitious programming task, seemingly made child’s play by the graphical interface. Then suddenly an insurmountable obstacle crops up. The problem could be solved with three lines of program code; but unfortunately the whole project would then have to be restarted from scratch in text form. Whether a graphical programming interface is worth using, and whether it makes work any easier, can only be decided easily in a handful of cases. Nevertheless, it is worth familiarising oneself with this style of programming. The reports of our experiences below will give an overview of the possibilities. In this article we will not be covering the microcontroller building-block system EBlocks, whose Flowcode programming interface (shortly to become available at http://www.elektor-electronics.co.uk) is suitable for use even by beginners: more on this system will be found elsewhere in this issue.

PSoC Designer
     The PSoC family from Cypress is a series of microcontrollers equipped with an extra mixed-signal array. Each PSoC controller includes highly-reconfigurable analogue and digital blocks (see Figure 1) with programmable interconnections. This architecture allows the user to match the external interfaces of the device to the requirements of the application. The device also includes a fast CPU with in-circuit programmable Flash ROM, RAM, and configurable ports. Programming and connecting the various blocks is very complex, since there are countless configuration possibilities, as well as many constraints. Finally, hidden behind the configuration of each PSoC block and the connections that form a circuit is a large number of registers and corresponding switches. Programming all these bits by hand is a job beyond mere mortals, and so the only practical approach is to develop the circuit graphically.

Download PDF version for larger diagram

Figure 3. The sine wave generator project also uses the digital block Counter8_1.

       The user is assisted in this by ‘PSoC Designer’, a complex programming environment which even includes an assembler and a C compiler. Generally, most of the time is taken in constructing the circuit. With small projects at least, the amount of text-oriented programming is relatively small.

        The quickest way to start using the system is to take a ready-made PSoC project and expand and alter it. In this way all the necessary settings for a project will have already been made correctly. In our case, we took the example project asm_example_dac_adc.SOC configured for the CY8C27443. The program turns the device into an adjustable sine wave generator, taking data from a lookup table of sine values and passing them to a D/A converter. An A/D converter measures the voltage at an analogue input and sets a timer appropriately so that a potentiometer can be used to control the frequency of the output sine wave. To expand this project, create a new project with its own name, making a clone of the example project.

       Once the project has been loaded it is possible to select between the Application Editor, which shows the assembler source code, and the graphical Device Editor. Figure2 shows a section of the Device Editor screen (here showing only the analogue PSoC blocks and their connections). Four configured blocks can be seen: PGA_1 is a programmable amplifier, here used as an input buffer with gain set to 1. The input is connected to pin Port_0_1. The output of the buffer amplifier is connected to the input of the ADC block DELSIG8_1, a delta-sigma
converter with 8-bit resolution. The third PSoC block is the 6-bit D/A converter DAC6_1, whose output is connected to pin Port_0_5. In addition to the analogue blocks, this project also uses the digital block Counter8_1 (see Figure 3). The timer sets the rate at which the sine values are output, but is not itself connected to an output pin. The digital part of the delta-sigma converter can also be found in the digital section.

Pin overview
         The complete circuit as seen from the outside has one input, where a potentiometer is connected (VR), and one output for the sine wave signal (AnalogOutBuf_1). The selected pins can be seen in the Device Editor (see Figure 4). This is practically the complete circuit: of course VSS and VDD need to be connected, but there is no crystal and no reset circuit. The microcontroller operates from an internal 24 MHz RC oscillator with an accuracy of +/–2.5%. Each PSoC block has a large number of configurable parameters, which can be set at the design stage. The parameters can also be changed under program control. Whenever a new block is added to the circuit, PSoC Designer adds the necessary code to the project in the form of a library module. The main program main.asm then remains very high-level and can be examined in the Application Editor view. The blocks which are used must first be enabled, which is done using standard calls. The program then runs in a simple loop (see listing).

         The project can now be built using ‘Build’. The result is the file ‘Sine.hex’ in the ‘Output’ subdirectory of the main project directory. This file can now be transferred to the target system, for example using MiniProg (see box). MiniProg is controlled by its accompanying software ‘PSoC Programmer’ (see Figure 5). It is important to select the correct device family (27x43-*XI) and device type (CY8C27443). Set the acquire mode to ‘Reset’ and then apply power to the device using ‘Power Device’. When programming has been successfully completed, set ‘Power Device’ to off and then on again. The necessary potentiometer is already present on the CY3210 MiniEval evaluation board (see box), connected to the correct input. The sine wave signal will now be present on pin P05, and its frequency can be set as desired between approximately 15 Hz and 260 Hz.

PSoC Express
       Using the PSoC Express graphical development tool is is possible to develop applications for the Cypress PSoC mixed-signal devices without writing a single line of C or assembler code: the complete program is created automatically. One reason that PSoC Express was brought out by the manufacturer was perhaps that using PSoC Designer is very efficient, but not always completely straightforward. Presumably it was not at the right level for some potential users. The purely graphical interface of PSoC Express can fill that gap, since it is very simple and motivating to use, and results can be achieved with very little effort and in a very short time.

e05b044[7] 
Figure 4. Device Editor gives an overview of the selected pins.
 e05b044[11]

Figure 7. The desired threshold values can be entered into the ‘valuator’.

e05b044[9]

Figure 5. MiniProg (see box) is controlled by the PSoC Programmer software.

 e05b044[13]

Figure 6. The work area of PSoC Express shows the input and outputs as well as a ‘valuator’.

       There are just four steps to using this new tool:

1. Select the input and output blocks from a catalogue. For inputs this includes such things as temperature sensors, analogue voltage inputs, switches and buttons as well as I/O pins; for outputs it includes MOSFET switches, relays, LEDs and fans.

2. Configure the function of each block using its parameters.

3. A simulation mode allows the design to be verified. Input values can be set using sliders or knobs and the behaviour of the outputs can be observed.

4. In the final step a suitable PSoC device is selected and the hex file containing the programming data is generated.

       PSoC Express is of course a specialised tool, which cannot cover all the potential uses of the PSoC family. Rather, it is oriented towards constructing analogue and digital controllers and displays. If, for example, it is desired to indicate a given voltage range using LEDs or turn on a fan at a specified temperature, PSoC is the right tool for the job. In our example we will build a display for monitoring a battery, indicating voltage over a set range using LEDs.

      The ‘undervoltage’ range is below 2.2 V, and is to be indicated by a red LED. The range up to 2.9 V is to be indicated by a yellow/green LED, and the range above 2.9 V by a flashing green LED. The first step is to create an input suitable for measuring from 0 V e05b044[15]to 5 V in the work area (see Figure 6). The second step requires a ‘valuator’ of type ‘Setpoint Region’, in which the necessary threshold values are entered (Figure 7). Finally, the outputs are dragged onto the work area. A transfer function must be defined for each output. In each case a ‘TableLookup’ type is suitable. You can then decide on which input or valuator the output state is to depend, and then define the individual states (Figure 8).

Figure 8. The transfer function
for the green LED is set using a lookup table.

No soldering

Programming for the project is now complete, and it can be simulated on the screen, as shown in Figure 9. A slider is used to adjust the input voltage and the state of the outputs can be observed.

Figure 9. The completed project can be
simulated on the screen.e05b044[17]

           When the simulation has completed satisfactorily, the project can be built using ‘Build’. At this point the controller type needs to be selected. Building the project takes a little time: behind the scenes, PSoC Express is creating a complete PSoC project in C, launching the C compiler in PSoC Designer, and creating an executable program. A circuit diagram and a data sheet with the pinout and a tabular functional description are also produced.

        All that is left to do is transfer the code to the target system. We can use the MiniEval board, and control the input using the potentiometer. Output 2 and the flashing output 3 are suitably connected to LEDs on the board, so that operation can be checked without any extra soldering.

           The curious user can examine the C project that has been created using PSoC Designer, if for no other reason than to learn a little about C programming and program structure. The kernel of the program is an infinite loop which reads the inputs and drives the outputs.

       The program created is always built around an infinite loop which includes all the decision and control processing. It is not possible to add processes that run at specified times or to use special-purpose analogue blocks such as filters. If all that is wanted is a simple permanent linkage between input and output, this interface is, however, entirely adequate. PSoC Express is available for free download from www.cypress.com/psocexpress.

MiniProg and MiniEval

e05b044[19]

The low-cost CY3210 starter kit includes: the MiniProg1 programming device, which has a USB interface; a small miniEval printed circuit board for the processor, with a 28-pin IC socket; and two PSoC sample devices, a CY8C27443 and a CY8C29466. A manual, a CD containing the complete PSoC Designer and PSoC Programmer software, and a USB cable are also included. It is possible to get started immediately with this kit. The printed circuit board includes four LEDs, a pushbutton and a potentiometer. Power can be taken from the USB port via MiniProg1, or the controller (once programmed) can be powered from a separate 5 V supply.

The PSoC family
e05b044[21]The figure shows the PSoC architecture divided into four main blocks: the processor core, the digital peripherals, the analogue peripherals, and system resources. Configurable global buses allow these to be connected to form a complete system.

     One member of the PSoC family is the CY8C27443, which can use up to five I/O ports as analogue and digital inputs and outputs. There are eight digital blocks (8 to 32-bit timer, counters, PWM generators, up to two full-duplex UARTs, SPI masters and slaves etc.) and twelve analogue blocks (A/D converters up to 14 bits, D/A converters up to 9 bits, programmable amplifiers, filters and comparators etc.). The device has an M8C processor core running at up to 24 MHz and has an internal 24 MHz/48 MHz oscillator, 16 kbytes of Flash program memory and 256 bytes of data RAM. The PSoC Designer software is free.

Sine wave generator output loop

loop:
lcall DELSIG8_1_fIsDataAvailable // if ADC conversion complete then.....
jz loop
lcall DELSIG8_1_cGetDataClearFlag // get ADC result and convert to offset binary
add A, 0x80
mov [bADCvalue], A // store value for debug watch variable
cmp A, 0x0F // counter period less then 0x03 is invalid due to
jnc LoadCounter // excessive interrupt servicing
mov A, 0x0F
LoadCounter:
lcall Counter8_1_WritePeriod // update DAC update rate
jmp loop
area lit
SINtable: // 64 entry SIN lookup table
db 31, 33, 36, 39, 41, 44, 46, 49, 51, 53, 55, 56, 58, 59, 59
db 60, 60, 60, 59, 59, 58, 56, 55, 53, 51, 49, 47, 44, 42, 39
db 36, 33, 31, 28, 25, 22, 19, 16, 13, 11, 9, 7, 5, 3, 2, 1, 0
db 0, 0, 0, 1, 2, 3, 4, 6, 7, 10, 12, 14, 17, 20, 23, 26, 29
area text

(Elektor Electronics Magazine - 11/2005)



Download this article #e05b044.pdf  >> or SkyDrive link.

0 comments:

Subscribe to: Post Comments (Atom)