Distributors

U.K

www.byvac.com

C Programming

** This text does not apply to BV500 MX1 type processors as they do not have a boot loader **

The range of controller boards and IC's are intended for use with ByPic, however they can be programmed with C or any other language provided you have the means of creating a Hex file.

This guide will take the reader step by step through a few simple projects using C and the boot loader facility. It is important to do project 1 no matter how trivial as it forms the foundation of all of the other projects. In other words there is vital information in project 1 that does not exist in any other project.

It is possible to use a PICKit or similar Microchip programmer, all of the PCB's have a row of 5 holes, the square one being pin 1 which is pin for pin the same as a PICKit 2 or 3. It is stongly reccomended that the boot loader be used. See warning.

Warning
The boot loader software is not publicly available so if you use a PIC programmer instead of using the boot loader and overwrite the boot loader you will no longer be able to download ByPic firmware.

Resources

There are significant differences between C32 compiler version 1 and version 2. The original files were compiled and linked under version 1, using version 2 will compile okay but will not link to the correct address for the boot loader. This is explained here and the files marked in green below have the appropriate linker script to enable them to be compiled with version 2 of the compiler.

Use project 1 above for the BV523 and follow the instructions below. The BV513 zip file is designed for the BV513_V1 but is still relevant. However to get you going there is a new file above specifically for the BV513_V2. It has just one project but the from there it can be expanded.

The documentation is in the zip file. The main file to change is the procdefs.ld which is different for the BV513_V1 and BV523 so replace the procdefs.ld in projects 2,3 and 4 with the procdefs.ld this project 1 file. The only other difference is that the BV523 does not have an LED connected to port RC15.

The Boot Loader

The BV5xx is based around a PIC32 and like any other PIC it can be programmed with one of the PIC programmers. However to make life really simple and quicker a free boot loader (also referred to as the Application Loader) has been provided.

What is a boot loader? At some point you have to get your C program (which will be in the form of a hex file) on the PIC’s flash memory. One way is by using a programmer as mentioned above. The other way is to use a boot loader. A boot loader resides permanently on the PIC’s flash and will accept your hex file and load it into the flash for running.

The operation is actually quicker than programming with a PIC programmer. In order to use a boot loader the user C program needs so be compiled or more accurately linked to a slightly different place in memory.

BV5xx Boot loader

Normally a C program would start where the boot loader starts and so the user program must be moved up a bit in order to make room for the loader. This is all taken care of by the “procdefs.ld” file.

The boot loader starts at 0x1d000000 and the procdefs.ld file that tells the linker where to put the code has been designed for this. This boot loader takes up a bit of room but on this version it is all in the protected area and so does not impact on the space available for C programs.

The older version(1) of the boot loader started at 0x1d008000 and the procdef.lf file for that is in the BV513 zip file. It is unlikely that this will now be used as boot loader version 2 has been supplied since 2012.

Getting Started

This guide is in the form of a tutorial and so you will need a BV5xx to follow it properly. You will also need BV-COMM2 Version 0.9 or later, this has built in tools that will allow hex files to be directly programmed into the PIC. In case you are wondering, yes it is possible to go back to using ByPic simply by loading the latest ByPic release instead of your C program.

Compiler & IDE

To make things simple we will be using the Microchip compiler, and the MPLAB IDE, you may already be familiar with these. The compiler and IDE are free for personal use. The project files already have the MPLAB project ‘.mcp’ file for example “proj1.mcp” so just double clicking on that will invoke the IDE.

If you are using the later MPLABX then you will need to convert the project files.

It is beyond the scope of this text to explain the details of installing the IDE (Integrated Development Environment) but this is fully covered on the Microchip site. It is assumed therefore that this is already installed but will need some ‘setting up’. This is explained in the first project. The IDE version used for the projects was 8.40, there is no reason to suspect that a later version will not work.

Project 1

It is important to do this project as although very simple it does contain information that will be useful for all of the other projects. You will also need an LED, this is connected to RD0 which is pin 2 of the display connector of the BV523, for other boards see the pin out diagrams. The long lead [anode] goes into pin 2. You could modify the code for a BV513 as this already has an LED connected to RC15. This project will simply flash that LED.

Setting up the MPLAB IDE

The target device should already be set in the project file, the compiler may not be, so these two items are given here. Set the target as below, this dialogue is in Configure>Select Device..

Set the target to PIC32MX340512H
 

Now select Project>Select Language Tool Suite and from the drop down menu select as Figure 3.

Selecting PIC32 Tool Suite
 

The dialog will not be exactly the same and you may need to download the free version of the PIC32 compiler form Microchip. Once these items have been set up we can now build project 1 by selecting BV523 build icon.jpg this icon. You will now see in the output window at the bottom “BUILD SUCCEEDED”.

Loading Into BV5xx

The IDE will have generated a project HEX file, in this case 'proj1.hex'. Use BV_COMM2 to load this into the BV523 by selecting the boot loader BV COMM2 boot icon.jpg icon. This will produce the boot loader dialog.

Boot Loader Dialog
 

Select the project hex file as shown in figure 4 and then press send. After a second or two the LED will start flashing.

Other Projects

From here you should be able to see the technique that is used to compile and download programs to the BV5xx boards.

For the other projects follow the tutorial for the BV513 which is above. The basic difference between the two (BV513 and BV513_V2, BV523) is that they use different boot loaders and there is no LED connected to RC15 on the BV523 as there is in the BV513 and BV513_V2, hence the different project 1 here.

To successfully use the tutorial for the BV513_V2 & BV523, replace the procdefs.ld file in all of the projects that are designed for the BV513 and use BV_COMM2. The procdefs.ld file for BV523 is in project 1. Also any mention of RC15 should be deleted or replaces with some other port if using BV523.

The Screen BV523 Only

This is an example of how to use the TFT320.o library. All of the files are in the zip file in the project 2 directory. Also included is the mcp project file for use with Microchip IDE. This was created using version 8. The main() is in "proj2" and has examples that have been commented out, uncomment them and load to the BV523 to see what they do.

Colours

The display mode uses RGB565 which means that red and blue are specified as 5 bits and green as 6 bits in a 16 bit word. There is a macro in the head "RGB565" that takes care of this so any colour is specified as a 16 bit word. NOTE that each colour is specified as 6 bites when using RGB565 so the range for each is 0-63, 0 is black and 63 is full on.

Fonts

This is complex because apart form one built in font the font information is obtained externally. It is initialised by:


hx_setFont(BYTE *font, BYTE*(*get_char_data_function)(BYTE)) [1]
or
hx_setUserFont(BYTE *fontData, BYTE*(*get_char_data_function)(BYTE)) [2]

Both point to a function that will obtain a charter to be rendered to the screen. *font [1] points to a font table and the user supplied routine gets the character data for that font. In this way the font information can be supplied from various sources, Flash, SD Card etc. because it is a user supplied routine that determines where it comes from. The internal font is compiled into the library file but to supply an external font as a head file the easiest way is to use the internal 'get_char_data_function' as shown in the proj2 example.

Using the second [2] format the "get_char_data_function" supplies a pointer to data which is in the form of a byte array, i.e. a string of bytes. The *fontData should point to the start of the data where the font size information can be found. Have a look using a hex editor at the file "Arial13B.bf" to see the format required. Note that CBFG.exe (see elswhare on this site) will produce this format.

Touch Screen

Example 3 is a simple demonstration of how to use the touch screen, it will simply draw a pixel under the area being touched. For graphics the cursor does not want to be visible, use crSus(1) to turn it off and crSus(0) to turn it back on, these must be balanced for the cursor to work correctly.