Anda di halaman 1dari 14

Microtronics Pakistan

PIC Lab-III USB


Quick Start Manual

Amer Iqbal Qureshi

Learn The Quick and Easy Way

Introduction

elcome to the exciting world of Microcontroller programming. A few years ago, using and programming microcontrollers was a daunting task, primarily because of their little availability and secondly
due to non-availability of programming tools and tool-chains. Today situation is quite different, not
only that very powerful microcontrollers are available
at reasonably low price, but they can be programmed and reprogrammed millions of times. This is a heaven for beginner and hobbyist as he has to spend on microcontroller only once, and program it
many times.
The issue of tool-chain has also been facilitated by many flavors available, from Microchip to third party tools. We shall be using Mikroelectrinicas MikroC V 5.6.1. You may find a newer version to download
from their site www.mikroe.com. This tool has everything to start
with. A very powerful compiler, with extensive library of precompiled routines, a very interactive and user-friendly IDE and above
all a USB boot-loader tool for microcontrollers that support USB connectivity. We ship this board with PIC18F4550 running at 20MHz
Crystal oscillator. The controller is pre-programmed with Mikroelectronica USB HID bootloader, therefore you do
not need a dedicated programmer (as required for other microcontrollers) to use this board.

MikroC Integrated Development Environment


First iof all you need to download and install MikroC compiler. The demo version can be downloaded free from
Mikroelectronica web site. This version supports all microcontrollers including 18F4550. However there is a demo
limit of code size less than 2K. This is sufficient for most cases, to test the microcontroller and to develop simple
applications. For larger code memory hungry applications you will however need to get full version of the compiler.
Installation is simple, just follow the screens with default options. Windows 7 users will need to provide administrative rights to the IDE so that it can save its last settings, otherwise the IDE will give saving errors. Just right click
over the MikroC icon on desktop, in the context menu click properties and then compatibility tab. Select the Run-as
Administrator option and click OK.

Now run the MikroC and you should see a welcome screen. MikroC will organize your application as a project. A
project consists of many files required for the proper compilation. These are your source files that have an extension
*.c or header files like *.h and a project file that contains all the settings about your application like choice of microcontroller, the oscillator settings and settings for various fuses to configure the controller. So before writing your
code you will need to make a Project. It is better to make a folder somewhere on your computer, and then make subfolders for every Project. The project must have a name, it should not have space or special characters like #,$ etc
and it should not begin with a number. Thus 7segments is not a valid project name.
To make a new project click over the project  New Project and follow the screens. When asked to choose a microcontroller choose P18F4550. Name the project as Blink and chose the project Folder. Notice the device clock speed
has been selected to 48.0 MHz whereas the development board PIC lab-III has 20 MHz crystal. 18f4550 has capability to multiply its clock frequency to generate this 48MHz speed. We have to use this speed because the microcontroller already has a USB bootloader into it that runs the controller at 48MHz. Therefore all your applications with

USB bootloader should follow this approach, otherwise there will be issues about timing. Click Next, and you will be
asked if you already have a .c file that you want to add to the project, leave this empty as we do not have any source
file yet, click Next, accept the defaults and proceed to finish. Since we want the PLL clock system to be enabled, we
have to set a few configuration bit settings. To select the configuration bits you can click Project  Edit Project or
just press Shift+Ctrl+E.
You will see a dialog box with lots of options, set following as shown in image below.

Additionally PIC 18F4550 can have PORTB configured as analog. In most cases we want it to be digital, therefore
its a good practice to disable the analog on PORTB. To do this scroll down the configuration options and find
PORTB A/D setting and chose disable in drop list.

Thats it, you have set the most common settings as required to work on this board, with USB features enabled. It
would be advantageous to save these settings so that you do not have to set these again every time you start a new
project. Notice you have Load scheme and Save Scheme buttons (Blue Circle) Click Save Scheme and there is a list
of number of pre-saved selections, make a new one by naming it PIC Lab-III USB., and save it. Next time when you
make a new project open this dialog box Edit project, and Load Scheme PIC Lab-III USB and click OK.
Now you have the MikroC integrated development environment ready to accept your code. The IDE has rich features
o help you writing your code, a detailed discussion on these features is beyond scope of this manual. You can however download the various documents from compiler page on Mikroelectronica site that give you a detailed view of
the various components of your IDE. For now you have the central part with an editor and a bare-bone C program
structure to help you getting started. Notice the source file will be named as Blink.c. Now enter this code to Blink the
LEDs on PORTD.

void main() {
TRISD=0;
while(1)
{
LATD=255;
delay_ms(500);
LATD=0;
delay_ms(500);
}
}

Now you have to build the project to generate the .hex file. Click the Build on top menu and then select Build, or
press Ctrl+F9. this will invoke compiler and if you have entered the code correctly should compile successfully as
shown in the messages panel below.
Now you have to set your board. First of all notice the LED block on top left side close to power supply and the configuration header. Place jumpers over the headers to connect the LEDs to PORTD. Notice the PORTD is 8 bit wide,
whereas there are 10 LEDs. The last two will be connected to PORTC.B6 and PORTC.B7 so not applicable in this
example.

Place Jumpers here

Notice the USB connector, it has 4 jumpers. One of the jumpers is labeled as 5V and VDD. If you want to power the
board from USB then place the jumper here, and if want to power the board using an external adapter open this
jumper. Note as a precaution always remove this jumper when powering from external source to avoid mixing
of USB and external powers.

USB Power Jumper

Place all other three jumpers to allow USB communication. Now insert the USB cable and power ON the board your
PC should recognize the board and install the device driver automatically. The device driver is supplied as part of
windows therefore you do not need to provide a CD with driver.
Now you have to run the USB bootloader utility provided with MikroC. Click over Tools in the top menu and then
USB HID Bootloader. This will bring the bootloader client application.

If the board is already powered press the reset button, to force the controller to re-boot. This will start the bootloader

inside the controller and you should see the device and MCU type on the bootloader application. This will wait for
about 5 seconds, during this 5 second period press the connect button to connect the Bootloader application with
bootloader inside the controller.

Notice the connected message in the history. Now click the browse button and locate your project folder and the generated .hex file Click OK and then Click Begin Uploading Button. This will show a progress bar and your hex file
will be uploaded into the microcontroller. The bootloader application will automatically disconnect itself and your
Blink Led application will start executing. You should see the LEDs blinking turning ON for 500ms seconds and

OFF for 500ms. Now try to change the program by changing the On and Off duration, lets say change delay_ms(500)
to delay_ms(1000), build the program again, launch HID Bootloader application, press reset button on PIC Lab-III
and click connect when the device appears on bootloader application, Browse the .hex file again, and click Begin
Uploading. Thats it you should now see the new program in action. 1 second ON and 1 second OFF.

Reading Push Buttons


There are four user programmable push buttons on PIC Lab-III they can be connected to RB4, RB5, RB6 and RB7
using jumpers. Note in PIC18F4550 PORTB is by default analog and it must be declared digital in your program using ADCON1 register. Here is a simple program to check the status of push switches and toggle the LEDs.
Make sure you have jumpers over the switches configuration header as well as LEDs configuration header.

#define
#define
#define
#define

SW1
SW2
SW3
SW4

PORTB.B4
PORTB.B5
PORTB.B6
PORTB.B7

#define
#define
#define
#define

LED1
LED2
LED3
LED4

PORTD.B0
PORTD.B1
PORTD.B2
PORTD.B3

void main() {
ADCON1=0x0f; // Set all ports digital Note PORTB is Analog in 18f4550
TRISD=0;
TRISB= 0b11110000;
PORTD=0;
while (1)
{
if(SW1 == 0)
LED1 = ~ LED1;
if (SW2==0)
LED2=~LED2;
if (SW3==0)
LED3=~LED3;
if (SW4==0)
LED4= ~LED4;
delay_ms(200);
}
}

Displaying Data on Seven Segment Displays


PIC Lab-III has 4 seven segment display digits, multiplexed together. This means the LED segments anodes of all
digits are connected together to eight I-O lines of microcontroller. You can connect them to PORTD when jumpers
are placed. The cathodes of each digit are combined to be connected to a transistor. Thus when you set the PORTD,
same data is passed to all digits, but only the digit whose transistor is given logic 1 turns on and shows the number.
In this way all digits are turned On and OFF one by one every time changing the display data and entire number is
shown.
You will need to read some proper texts on how to manipulate multiplexed displays. Here in this example, we have
simply set a number to 1234, and then extract each digit one by one and display on seven segment display.
The transistors are connected to PORTB.B0 .. PORTB.B3. RB0 will drive the thousands digit, and RB3 units digit.

The image above shows a basic schematic of how multiplexed digits are connected. In our case A-G segments are
connected to PORTD and transistors connected to RB0..RB3
const mask[]={63, 6,91,79, 102, 109, 125, 7, 127, 111};
unsigned short portd_array[4];
int number=1234;
unsigned int digit;
void main() {
int i;
ADCON1=0x0f; // Make All PORTS Digital
TRISD=0;
TRISB=0;
PORTB=0;
while(1)
{
digit = number / 1000u ;
// extract thousands digit
portd_array[3] = mask[digit];
// and store it to PORTD array
digit = (number / 100u) % 10u;
// extract hundreds digit
portd_array[2] = mask[digit];
// and store it to PORTD array
digit = (number / 10u) % 10u;
// extract tens digit
portd_array[1] = mask[digit];
// and store it to PORTD array
digit = number % 10u;
// extract ones digit
portd_array[0] = mask[digit];
// and store it to PORTD array
PORTB.B0=1;
for(i=3;i>=0;i--)
{
PORTD=portd_array[i];
delay_ms(5);
PORTB <<= 1;
}
}
}

The image above shows result of the seven segments display program.

Character LCD
Character LCDs are intelligent devices having their own processor and memory. Once the data to be displayed has
been sent to it the microcontroller is free to carry out other actions, while LCD controller will keep the data displayed
on device.
HD44780 based LCDs are most commonly used. This board has interface for this LCD. It can be configured to use 4
bit or 8 bit data. Most programmers and applications use 4 bit mode to conserver microcontroller pins for other uses. Apart from data the LCD
need RS, RW and E pins to be controlled by
microcontroller. The RW pin is usually configured permanently as write, therefore for character LCD this pin is permanently connected to
GND. The RW label header is for Graphic LCD.
Now Connect you hardware, the D4..D7 pins
LCD will be connected to RD4..RD7 through
jumpers, RS pin to RC0 and E pin to RC1. Notice the last header pin is LED back light of
LCD. If jumper is placed it will be turned ON
and if removed it will be turned OFF. Beware
of powering LCD with USB, some Laptop ports may not have enough power to drive it.
MikroC provides a precompiled Library to send proper commands to the LCD. We can therefore call the appropriate
function to manipulate the display.
In order to use these functions we have to describe the hardware connections using special bit names as described by

compiler. We can not change the names, but can change the pins where these are connected. Then we have to call the
Lcd_Init() function, only once in the program to initialize the LCD.
Close to the LCD header there is a POT to adjust the contrast of your display. If you do not see data on first time use,
rotate the POT slowly in both directions to get the appropriate contrast for display.
// Lcd pinout settings
sbit LCD_RS at PORTC.B0;
sbit LCD_EN at PORTC.B1;
sbit LCD_D7 at PORTD.B7;
sbit LCD_D6 at PORTD.B6;
sbit LCD_D5 at PORTD.B5;
sbit LCD_D4 at PORTD.B4;
// Pin direction
sbit LCD_RS_Direction
sbit LCD_EN_Direction
sbit LCD_D7_Direction
sbit LCD_D6_Direction
sbit LCD_D5_Direction
sbit LCD_D4_Direction

at
at
at
at
at
at

TRISC.B0;
TRISC.B1;
TRISD.B7;
TRISD.B6;
TRISD.B5;
TRISD.B4;

char txt[16];
void main() {
int d;
Lcd_Init();
d=100;
Lcd_out(1,1,"Hello World");
ShortToStr(d,txt);
Lcd_out(2,1,txt);
}

Analog to Digital Conversion


Although PIC18F4550 has a large number of ADC channels numbered from AN0 to AN11. PIC Lab-III provides
two preconditioning circuits to acquire analog data. The pre conditioning circuits allow you to reduce the incoming
volts to a safe level where PIC can tolerate. This is done by a voltage divider. PIC Lab-III has POT in each circuit to
allow you voltage division. See the schematic for detailed understanding. For now we will put a jumper on analog
input pin and VDD to give 5V supply voltage into the POT. And then connect the output of POT to RA0 and RA3 to
sample the inputs. The measured voltage is then displayed on LCD. Rotating the POTS will change the incoming
Volts.

// Lcd pinout settings


sbit LCD_RS at PORTC.B0;
sbit LCD_EN at PORTC.B1;
sbit LCD_D7 at PORTD.B7;
sbit LCD_D6 at PORTD.B6;
sbit LCD_D5 at PORTD.B5;
sbit LCD_D4 at PORTD.B4;
// Pin direction
sbit LCD_RS_Direction
sbit LCD_EN_Direction
sbit LCD_D7_Direction
sbit LCD_D6_Direction
sbit LCD_D5_Direction
sbit LCD_D4_Direction

at
at
at
at
at
at

TRISC.B0;
TRISC.B1;
TRISD.B7;
TRISD.B6;
TRISD.B5;
TRISD.B4;

char txt[16];
void main() {
unsigned int x;
float b;
ADCON1=0x08;
TRISA.B0=1;
TRISA.B2=1;
ADC_Init();
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
while(1)
{
x=adc_read(0);
b=x*0.00488;
sprintf(txt,"AN0 : %4.2f",b);
Lcd_Out(1,1,txt);
x=adc_read(3);
b=x*0.00488;
sprintf(txt,"AN1 : %4.2f",b);
Lcd_Out(2,1,txt);
delay_ms(500);
}
}

Result of the above program sampling analog input from two POTS connected to VDD power source.

Graphic LCD
Although Character LCD works well to communicate with user, graphic LCD goes a step ahead by manipulating
fonts and images. The Graphic LCD supported by PIC Lab-III is KS0108 based monochrome LCD. This LCD can
draw graphics, but does not have fonts in it, so we need to make fonts ourselves. The configuration header used is
same as used by character LCD. The contrast pot is however not the same one, it is located close to buttons.

// Glcd module connections


char GLCD_DataPort at PORTD;
sbit
sbit
sbit
sbit
sbit
sbit

GLCD_CS1
GLCD_CS2
GLCD_RS
GLCD_RW
GLCD_EN
GLCD_RST

at
at
at
at
at
at

LATB0_bit;
LATB1_bit;
LATC0_bit;
LATC2_bit;
LATC1_bit;
LATB2_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;


sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISC0_bit;
sbit GLCD_RW_Direction at TRISC2_bit;
sbit GLCD_EN_Direction at TRISC1_bit;
sbit GLCD_RST_Direction at TRISB2_bit;
// End Glcd module connections
void main() {
int x;
ADCON1=0x0f;
Glcd_Init();
while(1)

{
Glcd_Fill(0x00);
for(x=0;x<128;x+=2)
{
Glcd_Line(x,0,127-x,63,1);
}
delay_ms(5000);
}
}

Note the configuration header has following jumper connections: CS1:RC3, CS2:RC4 and RST:RC5. As we
have previously talked RC3 is not implemented in 18F4550 and RC4, RC5 are used by USB. Therefore these
jumpers are not suitable for GLCD on 18F4550. They are Ok for other controllers. So we have removed those
jumpers and connected the CS1, CS2 and RST directly to RB0, RB1 and RB2.

Anda mungkin juga menyukai