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.