Application Note

Total Page:16

File Type:pdf, Size:1020Kb

Application Note

Application Note ANxxxx

Hello Lights

By: Lynn Nguyen Associated Part Family: CY8C24894 PSoC Designer Version: 4.4 References: www.easypsoc.com/book Summary This application note is aimed to provide a jump start on learning about the PsoC microcontroller. A common practice when learning a new programming language is to learn how to print “Hello World.” Similarly, the “Hello World” program counterpart on embedded systems is to learn how to blink the LEDs.

Introduction If you’re using this application note, then you are probably also new to electronics. So, let’s go over a little electronics 101 just to get a broad understanding of electronic circuits/embedded systems in general. Electricity is a phenomenon that represents movement of electron (hence, electronics). Since electrons are negatively charged particles, such a movement of charge over time represents current. Current flows from a high electric potential (voltage) to a low potential (opposite the direction of electrons). As these charges move, they face resistance from the atoms in the medium which causes them to generate heat or otherwise reduce the amount of current flowing through. Ohm’s Law defines the relationship between voltage, current, and resistance.

Resistance = Voltage/Current  Voltage – Voltage refers to an electric potential at a point. The difference in voltage (potential) between two points sets up an electric field between the two points. Electric field produces force on electrons (as charge carriers) that makes them move. (Once again, electrons, because they are negatively charged, move from low voltage to high voltage. Current, by convention, flows in the opposite direction.) Measured in Volts ‘V’.  Current – The flow of charge (carried on electrons as carriers). Measured in Amperes or Amps ‘I’.  Resistance – Any resistance posed to the movement of electrons. Will reduce the voltage and limit the current. Constant resistance is independent of the voltage or current. Resistance can also vary with supplied voltage. Measured in Ohms ‘R’. Now that we’ve covered a little of the basics, we have a little background about the project and the components it uses. This project will use a PWM to control the blink rate of an LED onboard the controller. What exactly does that mean? PWM stands for Pulse Width Modulation and is used in this project to control the rate that an LED blinks. A

PWM pulse is a pulse going from 0 to Vcc and back. The duration of the pulse (i.e., its high time) is proportional to the numerical value written by the microcontroller. What is an LED? LED stands for Light Emitting Diode and that’s just what it is. A diode is a device that only allows current to flow in one direction. It is a form of (unidirectional) resistance to the current flow. When that resistance is faced, the electron energy is converted into light (instead of heat), thus emitting light. Basically, an LED turns a current into light.

Figure 1 – LED Schematic Symbol

Creating the Project

Page | 1 Open PsoC Designer and under the File menu, expand the New Project option and click on Create New… Upon completion of the New Project Wizard, PsoC will open up to the Device Editor window. This is where the user can select programmable components (also known as user modules) for the project. For this project, we will be using the PWM16 component found in the PWMs section. Double click or Right Click and choose Select to add the PWM to the Selected User Modules Slot. It is automatically named PWM16_1 but we can change this. However, if you do change the name, make sure to remember the name as this is important when you make function calls to the module (more on this later). Note in the resource window that this component takes 2 digital blocks. When designing projects in the future, it is important to watch the resources. Figure 2 – Creating The Project

This will open the New Project Wizard. In the New project name textbox, type “Hello_Lights” or any other name that you want to name your first PsoC project! Then click the Next button.

Figure 5.1 – Selecting PWM16

Figure 5.2 – Selected Component

Next we’ll place the component by going to the Figure 3 – New Project Wizard Interconnect View of the Device Editor (found on top righthand side of the toolbar under the Menu bar) and In the next window, select Part CY8C24894-24LFXI right clicking the component and selecting Place. through the View Catalog… button and choose to Generate the Main file using C. Click Finish when done.

Figure 6.1 – Going to Interconnect View

Figure 6.2 – Placing Component The PWM16 component creates a digital signal that is sent to the LED. So, in order to control the rate of the LED we must configure the parameters of PWM16_1. The period and width of PWM impulse is set in the User Module Parameters window (found in the middle of the toolbar on the left). Copy the parameters of Figure 4 – Figure 7 into your own project. Selecting Base Part

Page | 2 output line with index 0 which happens to be GOO. So, let’s connect the LED (you can choose from LED{1,2,3,4} to P1[0]. Because of the flexibility of this controller, we can route the pin directly from LED to P1[0], or if you don’t have a long enough wire, we may route it through the breadboard.

Figure 7 – PWM User Module Parameters Figure 9.1 – Direct Pin Connection

Setting the Cock to CPU_32_KHz and Period to 32000 results in a one second period. To get an equal length of signal and pause, Pulse Width is set to half of period, or 16000. From this, we get a 50% duty cycle (32000/16000). A duty cycle is the proportion Figure 9.2 – Pin of time in which a device is operated. The following Through Breadboard figure depicts a PWM signal at a 10%, 50%, and 90% duty cycle. Now we will connect the output of the PWM (which is to control the LED) to P1[0] (which is, coincidentally, where the LED is connected). This will be achieved in two steps.

First, we can see that CompareOut of PWM is already connected to R0[0] because we had set the User Module Parameters. So, click on the R0[0] line to open up the digital multiplexer settings window. Click on the third triangle from the top and choose GlobalOutOdd_0. As mentioned before, the LED is connected to P1[0] which can only be connected to an output line with index 0, or in this case, GOO_0 because of the connection rules explained previously. Figure 8 – Example Duty Cycles Thus by routing R0[0] to G00_0 we will eventually be able to connect the output of the PWM to the Also,we connect CompareOut to R0[0] (row 0, LED. output 0). As a side note, logic circuit output can be set to R0[0], R0[1], R0[2], or R0[3] which can make connections to one or more global output lines. These lines, R0[1-4] can be connected to GOO or GOE lines with the same or four higher index. In turn, the global output lines can be used to connect to pins as we shall now see.

Figure 9 – Digital Multiplexer Window Global output lines (as well as global input lines for Now, we will connect GOO_0 to P1[0] to finish that matter) are divided into two groups, GOO establishing the connection between the PWM and (global output odd) or GOE (global output even). the LED. To do this, click on the GOO_0 line, Select This indicates whether they can be connected to ports Pin… and choose Port_1_0. This will send the digital with an odd or even index. Also, only lines and pins signal created by the PWM to P1[0] which will of the same index can be connected. For example, control the rate at which the LED is to blink. lets connect the LED to P1[0] (port 1, pin 0). The index is 0 so it can only be connected to a digital

Page | 3 This API can be found under the Library Source folder in pwm16_1.asm. Look through the code in the file just to see the other calls we could’ve made to the PWM. Remember, if we had named our PWM16 something else, say dogeater, we would start the module by calling dogeater_Start() instead.

Now build the project. Build->Rebuild All. Then to program it into your microcontroller, Program->Program Part. Figure 10 – Pin Out Figure 13 – Rebuild all Now that we have set up all the connections we can write source code! But first we have to generate the This will open up PSoC source code to make calls to the PWM component. Programmer. Plug your To do so, click on the Generate Application icon MiniProg into the device and connect it to the PC. located on the right side of the toolbar beneath the Everything should already by automatically set. Menu bar. When ready, click the Program icon and wait for the program to load.

Figure 11 – Generating Files For PWM16

Then go to the Application Editor to actually write some code. We can go here by clicking on the ApplicationEditor icon, located somewhere near the Generate Application icon. Figure 14 – Programming Controller

Now the program should be loaded into the controller! Click Toggle Device Power button and Figure 12 – Switching to the Application Editor watch the LED blink! (Make sure the LED is connected to P1[0]!! Also, if there is a problem In the Application Editor view of PSoC Designer, we loading the hex file, I find that it is often better to can edit source code for the program. On the left make the connections AFTER the program has hand side, you can see a list of all the files automated already been loaded). generated by clicking the Generate Application icon. For future reference, make sure you click that icon Congratulations! You just finished programming every time you add or remove user modules so the your first microcontroller! correct files are created in the Application Editor. Conclusion Finally, click on the file main.c in the left hand The PSoC controller is a flexible piece of hardware window. To start the PWM module, we will include with many capabilities.We can extend this project the following line in the main method: and learn about the PWM by trying to get the LED blinking at a different rate. Or, be daring and connect PWM16_1_Start(); multiple LEDs and have them all blink at different rates!

Page | 4

Recommended publications