You are here

LED DIM MANUAL | Cypress Semiconductor

LED DIM MANUAL

Last Updated: 
Mar 18, 2012

Project Introduction:

For this example project, which we will call ‘LED DIM MANUAL’, the CY8CKIT-001 hardware will be used to control the brightness on an LED using a potentiometer and display that value on an LCD screen. This example will assume that you’ve completed the initial ‘PSoC Rocks!’ example project that instructed the user on creating new projects and discussed the basic layout of the software tool.

This example project focuses on merging two previous example projects ‘ADC LCD’ and ‘LED DIM’ and integrates a potentiometer, an analog to digital converter (ADC) user module, a pulse width modulator (PWM), and the LCD display user module. This example project will primarily focus on increasing project complexity, interfacing multiple user modules and displaying system values onto an LCD screen.

Software Used:

PSoC Designer: Click Here
PSoC Programmer: Click Here

Hardware Used:

  • This example project will utilize the following hardware.
  • CY8CKIT-001
  • CY8C28xxx Device Family Processor Module (CY8CKIT-020)
  • Miniprog3 (CY8CKIT-002)
  • LCD Screen
  • Connection Wires

All of the above hardware is included in the CY8CKIT-001 kit.

Steps to Set Up Hardware:

To set up this hardware please perform the following actions:

  • Insert the CY8CKIT-020 processor module into the CY8CKIT-001 socket.
  • Connect the LCD screen to the LCD header.
  • Connect a wire to the black bread board headers from VR to P0_3
  • Connect a wire to the black bread board headers from LED1 to P0_0

Please see the wire connection in the image below. The wire is represented by the red line and must be connected to the black header running next to white bread board. The CY8CKIT-001 is shipped with a set of wires. Each wire has an exposed end. This exposed end must be inserted into the header. Each header hole has silk screening on the board that indicates the routing. If the header hole indicates P0_0, then this means that the header is routed to the port 0 pin number 1 pin on the PSoC. The bottom black header indicates connections to components that are on the target board. For example, the VR header hole will connect directly to the Potentiometer. The wires allow us to physically connect a pin on the PSoC to a component on the board and use them in our embedded design.

Steps to Create Project

This example project assumes that you’ve completed the ‘PSoC Rocks’ introductory example project. The ‘PSoC Rocks’ example project walks the user through project creation and the software layout. It’s suggested to complete that example before completing this example project. To begin the example project create a new project using PSoC Designer.

  1. Open PSoC Designer by navigating Start -> All Programs -> Cypress -> PSoC Designer -> PSoC Designer. This will launch the PSoC Designer application.
  2. In the Recent Projects window at the top left please select the New Project… link.
  3. In the New Project window please enter in the name of the project “CY8C28445_LED_DIM_MANUAL”. Please select the project location by selecting the Browse button. Click OK
  4. The Select Project Type window will open. You can either click the drop down menu to select the target device or launch the device catalog by clicking the View Catalog button. We will be selecting the “CY8C28445-24PVXI” device.
  5. If you select the parts via the View Catalog option then please scroll down to the “CY8C28445-24PVXI” device and select the Select button.
  6. You will return to the Select Project Type window. Please select the “C” radio button for main.c file generation. This will mean that you will be developing a C-based project rather than an assembler based project. Select OK.

After you select the OK button PSoC Designer will open the Chip Editor view. In the following section we will cover the various options and actions available in the Chip Editor view. We will begin setting parameter values and designing our system.

User Module Placement and Configuration

In this section we will discuss how to design the embedded system by setting global parameters for the PSoC, adding User Modules, and making pin selections.

  1. Before we add any user modules and select parameters it is important for us to configure our global parameters. For this example project we will use the following global parameters. Please navigate to the Global Parameters window to add these selections.


     

  2. Please navigate to the User Module window and select Amplifiers>PGA. Right click on PGA user module and select Place. You will notice that the Workspace Explorer window was updated and now lists the PGA user module, “PGA_1”.
  3. Select the PGA user module in the Workspace Explorer. You will notice that the Parameters window updates and now offers selections pertaining to the PGA user module. Please set the PGA parameters to match the image below.


     

  4. Please navigate to the User Module window and select ADCs>ADCINC. Right click on ADCINC user module and select Place. You will be presented with a window asking the user to select a multiple user module(MUM) configuration. For this example we will use the Single Stage Modulator configuration. Please select the radio button and then click OK.


     

  5. You will notice that the Workspace Explorer window was updated and now lists the ADCINC user module, “ADCINC_1”.
  6. Select the ADCINC user module in the Workspace Explorer. You will notice that the Parameters window updates and now offers selections pertaining to the ADCINC user module.
  7. Under the Parameters window please rename the user module from “ADCINC_1” to “ADCINC”. Please set the remaining ADCINC user module parameters to the values displayed in the image below.


     

  8. The ADCINC user module supplies the user with a built in PWM. We will use this PWM to control our LED. To zoom in the user must select Ctrl+Click, to zoom out Shift+Ctrl+Click. To pan the user must hold Alt and then click and drag. Please navigate to the PWM in the digital user module array. We must route out the PWM Output connection from the PWM to the Row Output bus. To do this we can simply click on the PWM Output text on the ADCINC PWM. This will present us with five options. We will want to select Row_0_Output_0. Once selected we will see a line drawn from the PWM output to the Row Output. This connection can also be selected from the user module parameters window.


     

  9. From the Row Output bus we will need to connect to the vertical Global Output Even bus. To do this we will click on the blue box at the right end of the Row Output bus. This will launch a small window that will present us with a number of options. Click on the top triangle and enable the GlobalOutputEven_0 value. This will connect the Row Output to the Global Output Even bus. Please select Close when the selection has been made.


     

  10. Finally we will want to connect the Global Out Even bus to our P0(0) pin. This is the pin we have connected on our board to the LED1 component. By making this connection we will have connected the output of the PWM to the LED. To make this connection pan up to the Port_0_0 icon. Simply click on the icon and a configuration window will appear. We want to click on the Select row and then on the drop down menu to view the available connections. We will want to select GlobalOutEven_0 from the available selections. When complete please click OK.

  11. Please navigate to the User Module window and select Misc Digital>LCD. Right click on LCD user module and select Place. You will notice that the Workspace Explorer window was updated and now lists the LCD user module, “LCD_1”.
  12. Select the LCD user module in the Workspace Explorer. You will notice that the Parameters window updates and now offers selections pertaining to the LCD user module. Please make the following changes to the parameters window.


     

  13. Next we will begin routing the PGA and ADC connections. Often when you are working on the chip interconnect it is helpful to zoom in and pan.
  14. Click on the AnalogColumn_Clock_0, when the selection window opens please select VC2.


     

  15. Click on the AnalogColumn_InputMUX_0 and select Port_0_1. This connects our potentiometer to our PGA which feeds into the ACD. 


     

  16. Now that all of the user module placement and configuration is complete we need to generate the project source files based on our selections. Please navigate to the menu panel and select the Generate Configuration Files button. The output window will display the actions when generating the configuration files and report any design rule check (DRC) warnings. DRC warnings are used to warn the user if they have selections in their project that are outside of the best practices.

Now that we have completed the user module selection and configuration we will proceed to add source code to call the user module code we generated.

Adding Source Code

When generating the project we selected that the project be generated for C language development. We will now add our C code to the main.c file.

  1. Navigate to the Workspace Explorer window, expand the source files directory and double-click the main.c file.
  2. The main.c file is now open in the editor tab window. Users will notice that the Workspace Explorer is still available on the right side and can scroll between the text editor and the Chip Editor. Users can open additional files by double-clicking files in the Workspace Explorer.
  3. We will now add source code to our project to enable the PGA and ADC to read in the value from the potentiometer and then write that value to the PWM and LCD screen. The code is linked below and should be added into the main function.

    void main(void)
    {
         unsigned char bADCData; /*Define the LCD display variable*/
         int bADCDataPWM; /*Define the PWM output variable*/
          /* Enable Interrupt for ADC */
          M8C_EnableGInt ;

          /* Initialize all User Modules */
          LCD_Start();
          PGA_Start(PGA_HIGHPOWER);
          ADCINC_Start(ADCINC_HIGHPOWER);

           /* Run ADC continuously */
           ADCINC_GetSamples(0);

           while(1) /* Loop forever */
           {
                  /* If data is valid display on LCD */
                  if(ADCINC_fIsDataAvailable() != 0)
                  {
                           /* Read ADC Result and assign to PWM output */
                           bADCDataPWM = ADCINC_bGetData();
                           /* Assign PWM output to LCD Display variable */
                           bADCData = bADCDataPWM;

                           /* Display on LCD */
                           LCD_Position(0,0);
                           /* Write LCD display value to LCD screen */
                           LCD_PrHexByte(bADCData);
                           /* Write PWM output variable to control PWM pulse width */
                           ADCINC_WritePulseWidth(bADCDataPWM);

                           /* Clear ADC flag for next reading */
                           ADCINC_fClearFlag();
                  }
           }
    }

  4. Once the code has been entered into the main.c file we will then build the project by selecting Build>Generate/Build, pressing F6, or by selecting the Build button.
  5. The build status will be displayed in the Output window. The output window will display either a successful build or a list of build errors associated with the project. In this case we will have no build errors.
  6. Make sure that the Miniprog3, included in the CY8CKIT-001 kit, is connected to the white 5-pin programming connector to the USB port on the PC.
  7. To program our target device please select Program>Program Part. This will launch the PSoC Designer embedded programmer. Please choose the Power Cycle radio button and then select the Program button, which is located at the bottom right of the embedded programmer.

      
     

  8. Now that the device is programmed we want to power the CY8CKIT-001 and see the ADC value displayed on the LCD screen. To do this keep the Embedded Programmer open and select the Toggle Power button.
  9. Once power is applied the LCD will display the current ADC value. Begin twisting the potentiometer to see the brightness on the LED vary and the value on the LCD screen change from 0x00 to 0xFF. The value displayed is in HEX. We configured the ADC to read at an 8 bit resolution, therefore our highest value is 0xFF.


Analysis of the Code:

In this section we will go into more depth on the source code we added to the example project.

The sample codes is broken into two halves, first the initialization code and second the read, control, and display loop.

The initialization code focuses on defining critical variables and starting the user modules. We have defined a variable bADCDATA that will be used to store the ADC data and will be written to the LCD screen to display the current value. The second variable will store the ADC value and be written to the PWM to control the pulse width. This control will control the brightness of the LED.

unsigned char bADCData; /*Define the LCD display variable*/
int bADCDataPWM; /*Define the PWM output variable*/

After defining our variable we will now initialize interrupts for our ADC and start all of our user module functions.

/* Enable Interrupt for ADC */
M8C_EnableGInt ;
/* Initialize all User Modules */
LCD_Start();
PGA_Start(PGA_HIGHPOWER);
ADCINC_Start(ADCINC_HIGHPOWER);
/* Run ADC continuously */
ADCINC_GetSamples(0);

After we initializing the system we dive into a continuous while loop to read the value from the ADC and then write that value to the LCD screen. Inside of the while loop our logic is very simple, we only want to read from the ADC if the ADC is ready to be sampled. In our if statement we call the ADCINC_fIsDataAvailable() function and only enter additional code if the function returns 1.

while(1) /* Loop forever */
{
      if(ADCINC_fIsDataAvailable() != 0)

The if loop contains all of the code to read the value from the ADC and then write that value to the PWM output and LCD display. The first action is to read from the ADC and store that value in our defined variable. This is completed by calling the ADCINC_bGetData() function. The second action is to assign the PWM data to the LCD display variable.

/* Read ADC Result and assign to PWM output */
bADCDataPWM = ADCINC_bGetData();
/* Assign PWM output to LCD Display variable */
bADCData = bADCDataPWM;

As a note all of the user module functions were generated when we completed our user module placement and configuration. All of the functions or APIs are detailed in the respective user module datasheets.

Since we have stored our ADC value into our variable we will then configure the LCD screen by selecting the positioning and then writing the ADC value to the screen. This is completed by calling the following two functions. The first function LCD_Position includes the static value of 0,0 as the row and column positions. The section function LCD_PrHexByte is passed our ADC variable data to display on the LCD screen. Every time the loop executes this value may be different due to its dependence on the value read from the potentiometer. If the user twists the potentiometer then the ADC value will change.

/* Display on LCD */
LCD_Position(0,0);
/* Write LCD display value to LCD screen */
LCD_PrHexByte(bADCData);

After we’ve written the ADC value to the LCD screen we will need to write the ADC value to the PWM output variable. This will allow us to control the length of the PWM pulse. If the ADC value is large then the LED will be bright and if the value is small then the LED will be dim. We accomplish this by passing our PWM value into the ADCINC_WritePulseWidth function. Each time the While loop executes this value will be updated. Therefore as the user twists the potentiometer they will be able to control the LED brightness in real-time.

/* Write PWM output variable to control PWM pulse width */
ADCINC_WritePulseWidth(bADCDataPWM);

Finally we close the code by clearing the ADC flags. This lets the ADC know that we’ve read the value and that the ADC is free to read a new value.

/* Clear ADC flag for next reading */
ADCINC_fClearFlag();

Summary of the Project:

The goal of this project was to introduce the user to analog user modules and interface those user modules with external components. We then used the LCD screen to display the value of our measurement and the PWM to control the brightness of an LED. This example project builds on the themes discussed in earlier example projects by using external inputs, in this case a potentiometer, to drive external outputs, an LCD and LED. This example project is a merger of the ‘DIM LED’ and ‘ADC LCD’ example projects.

For more information on the PGA, LCD and the ADCINC user modules please the respective user module datasheets. Users can access these datasheets by right clicking on the user modules in the Workspace Explorer window. The datasheet details parameter selections, API descriptions, and example code.

Related Projects:

PSoC Rocks!: The user will interface an LCD with the PSoC to display a statement.
LED DIM: The user will utilize a pulse width modulator (PWM) to dim an LED
ADC LCD: Display a potentiometer output on an LCD screen.