Easy Embedded Programming: How to Drive a Pin and Power an LED

发布人是

The first video in the series introduces the design programming software, the specifications of the CY8CKIT-042-BLE development board by Cypress Semiconductor, and how to drive a pin to power an LED without writing code.


Embedded programming is one of the most difficult tasks. Indeed, it requires several levels of knowledge. To name a few:

 

- C or C++ that are very type and not memory safe  languages. Extended usage of pointers can puzzle beginners
- Hardware understanding and architecture of the microcontroller
- To optimize code, assembly language can be incorporated in a C program
- Various levels of optimizations (trade-off between speed and space) for the compiler

Fortunately, microcontroller vendors have created a bunch of tools (automatic generation of header files, middle, GUI programming, middleware, APIs…) that greatly simplifies the way programs can be written.
The purpose of this tutorial is show only basic knowledge is required to create a first project. This project consists to spin a stepper motor at various speeds that depend on the distance between an object and a proximity sensor. The following components will be used:

- PSOC 4200 BLE from Cypress
- L6474 stepper motor driver from STMicroelectronics
- VL6180x : proximity sensor from STMicroelectronics

To write the program, the PSOC creator is a great tool that integrates a free of charge IDE and a GUI simplifies first steps in embedded programming world for beginners.

The evalboard is the CY8CKIT-BLE coming with PSOC BLE that integrates an ARM M0 core, very common on the market. The L6474 was chosen because it integrates an automatic calculation for decay timing, reducing dramatically code size. Finally, the VL6180x is based on time of flight technology, allowing distance calculation instead of a simple on/off proximity sensor.

4 major blocks are necessary for our project:

- How to drive a pin
- How to use a PWM (Pulse Width Modulation) and a timer
- How to handle interrupts
- How to communicate with I2C and SPI bus

After studying separately these 4 fundamental blocks, we’ll be ready for our final project.

How to drive a pin to turn on and off an LED
When learning a new programming language, the first program is always called "hello world" and it just prints this string on the monitor. For instance in Python, a popular programming language that many people appreciate for its simplicity, just writing print “hello world”, one line of code is enough to create a first program. The equivalent of hello World program in embedded programming is to turn on an LED.

A project in Cypress IDE has 3 main windows: window for c code, a window to show the pin out of the microcontroller or other internal configurations like clocks and finally a design/schematic window to show the components we want to program. A PSOC allows hardware connection between components, so no C code is required to turn on and off an LED based on push button entry. The evalboard comes with an RGB LED that is connected to the following pins: Red on port 2 pin 6, green on port 3 pin 6 and blue on port 3 pin 7. There is also a push button on port 2 pin 7. In this section, we use the RED LED and the push button.

Let’s build our first project. From the component catalog, a digital output pin is dragged to the schematic window. By double clicking on the component, a GUI pops up that allows configuring entirely the pin. For instance, the drive mode, the initial drive state, the name of the pin can be configured and the hardware connection must be check in our case as the push button will be connected to it. Click apply and OK. 
Similarly, for the push button, it’s necessary to drag an input button, configured as resistive pull-up drive mode and HW connection checked. In the pin out window, we program the red LED to be driven by the pin located on port 2 pin 6 and the input switch on port 2 pin 7. The program can then be built. 2 new folders are generated: RED file and SW file. That is very convenient, the code is automatically written for us.

The program can be flashed in the microcontroller. When the push button is pressed, the red LED is turned on. We made this first project without writing one line of code. If we want to turn on the green LED instead of the red LED, a quick change on the pin out is the only required action.  A LED can also be driven by firmware. Let’s write a basic program to turn on and off the red LED at a one Hertz frequency. For this second project, only the required component is an output pin driven in strong mode. The HW connection must be unchecked as the pin is driven by firmware and not by another component.

The component datasheet explains available APIs. Pin_Write function writes a value to the physical port to drive high or low an output pin. Let's go to the C main program, as the name of the pin is RED, the function is RED_Write. We pass 0 to this function to drive the pin low. The LED is then ON. CyDelay(500) gives a delay of 500 ms. Then RED_Write(1) drive the pin to high before a new delay of 500ms. CyDelay is part of the standard library generated by PSOC creator. Once this program is compiled and flashed to the microcontroller, the LED flashes at a one hertz. 

最新视频

抱歉,您所选择的筛选条件未返回任何结果。

请仔细阅读我们近期更改的隐私政策。当按下确认键时,您已了解并同意艾睿电子的隐私政策和用户协议。

本网站需使用cookies以改善用户您的体验并进一步改进我们的网站。此处阅读了解关于网站cookies的使用以及如何禁用cookies。网页cookies和追踪功能或許用于市场分析。当您按下同意按钮,您已经了解并同意在您的设备上接受cookies,并给予网站追踪权限。更多关于如何取消网站cookies及追踪的信息,请点击下方“阅读更多”。尽管同意启用cookies追踪与否取决用户意愿,取消网页cookies及追踪可能导致网站运作或显示异常,亦或导致相关推荐广告减少。

我们尊重您的隐私。请在此阅读我们的隐私政策。