Tuesday, October 23, 2012

Scroll text on 8x8 LED Matrix Display using PIC18F4550 and MikroC compiler

Lights and LEDs always fascinate me. In this experiment with 8x8 led matrix display, I have interfaced an 8x8 led matrix without using any external buffer IC to the PIC microcontroller. This saves cost and wiring hassle as well as makes the design simple. PIC microcontrollers can sink and source 20mA through its pins. That's what made me think of eleminating external buffer IC which people normally use when they use other microcontrollers which can not sink and source at 20mA directly.


In this experiment I have used PIC18F4550 because it was already present in my component box. You may replace PIC18F4550 with other PIC mikrocontrollers. The 8x8 LED matrix displays have total of 64 leds which are wired in 8 columns and 8 rows (Please see figure 1). The 8x8 LED Dot matrix display that I used was 2.3" GMC2X88C. I had further made an excel sheet to understand wiring inside this 8x8 (Please see figure 2). Sometimes when you buy an 8x8 led matrix you may not know its manufacture part number (as the part number printed on these matrix sometimes are not searchable and you cant find the datasheet), therefore you may need to find yourself the pin-outs of your led matrix. And that's what I did, I used multimeter to figure out cathode and anodes of the display and later found that my display closely matches Fairchild's Part Number GMC2X88C.

Video




Figure 1:


Figure 2 - 8x8 Led Matrix Pinouts explained in detail:



Schematics:
















Pdf copy of the Schematics can be downloaded from here:
8x8_Led_Matrix_Schematics.pdf

Source Code:
C Source Code zip file can be downloaded from here:
LED_Matrix_8x8_timer.zip

The Oscillator settings for this project can be copied from following snapshot of MikroC compiler. PIC18F4550 in this example the PIC microconteoller is running from internal 8.000MHz oscillator. The dialog box can be accessed by pressing Ctrl+Shift+E


Datasheets:
1. GMC2X88C - 8x8 dot matrix led display:
http://media.digikey.com/pdf/Data%20Sheets/Fairchild%20PDFs/GMA2288C,GMC2288C.pdf
2. PIC18F4550 - PIC 18F series microcontroller from Microchip:


Thursday, October 11, 2012

Experiment with an 8 pin PIC12F683 microcontroller using MikroC

This week I bought PIC12F683 (a very small pin count PIC microcontroller) from a local online electronic kits and parts store : www.evselectro.com. I ordered a small list of parts from this store on last saturday and to my surprise the parts I ordered were delivered to me at an additional cost of just RS200 delivery charges, at my home in Islamabad on Tuesday evening. The newly opened on-line store seems to have a very large stock of parts (if you compare it with other local on-line part suppliers in Pakistan such as www.digipak.org or electronicspk.com).
Pakistan have very few on-line electronic kits and parts stores and this addition of a new store is definitely a good news for all the enthusiasts belonging to my country.

Running PIC12F683 to blink an LED was relatively very easy task.
1. I just had to wire this small circuit (see figure 1) on a breadboard,
2. Created a PIC12F683 C project using MikroC IDE,
3. Edited configuration of microcontroller (see figure 2)
4. wrote a small program (see figure 3),
5. hooked up my home-brew PICKIT2 programmer and programmed hex file to this tiny 8 pin microcontroller using PICKIT2 progmming software.
And was able to see the LED blinking!

Figure-1:

PIC12F683 has two built-in oscillators.
1. 8.0MHz Oscillator
2. 32 KHz Oscillator
In this example I am using 8 Mhz internal oscillator. The 8 MHz internal oscillator can also be further divided using built-in post scaler by writing to ICRF[2:0] bits in OSCCON register.
The settings for running microcontroller by internal  oscillator can be accessed in MikroC IDE by pressing CTRL+SHIFT+E. The first setting Oscillator [ Internal RC Clockout ] configures PIC12F683 microcontroller to run from 8 MHz internal oscillator and output divided by 4 clock (2 MHz in our case) at pin 3 of the microcontroller. You may also select Oscillator [ Internal RC No Clock ] in case if you dont want to output internal clock output. This way you can save one pin and can use it for your purposes.

Here is screen shot of settings (Figure 2):

The code in this project toggels LED at every 250msec. The first line configures oscillator postscaller to get 8 MHz from internal oscillator by not dividing the original 8MHz.

Here is the screen shot of code written for MikroC Pro Version 4.15:
Figure 3:

PIC12F683 PWM Example: 
Here is another code sample for PWM example using PIC12F683. In this code the LED is connected at pin 5 (GP2/AN2/T0CKI/INT/COUT/CCP1 - PWM channel 1). The intensity of LED light in this example is increased as the variable "dc" is incremented, and after reaching maximum intensity it becomes zero and led turns off. So you keep observing led intensity ramping up and then becoming zero while this code runs forever.
The code uses MikroC library functions to initialize /set pwm frequency and set pwm duty cycle. MikroC is a great compiler to easily setup and run but in case of PWM peripherial, its functions do not utilize full functionality of pic microcontroller's pwm channel. The PWM1_Set_Duty() function takes 8 bit char value which it assigns to upper 8 bits of the pwm duty cycle register and compromises the full resolution of 10 bits.

Figure 4:


Saturday, February 4, 2012

Running Microchip PIC18F4550 Microcontroller at 20.000MHz using an external oscillator

MikroC is an easy programming and debugging suite for PIC microcontrollers that gives you a useful library functions that let you quickly setup and run a complete embedded system based on PIC microcontrollers. The configuration dialog of MikroC sometimes become pain for many engineers or enthusiasts as it does not relate very well to the registers shown in the device datasheet.

Following you can find MikroC version 4.15 Configuration Bits Dialog Picture that shows configuration that you will need to set for running your PIC18F4550 or PIC18F2550 Microcontroller at 20.000MHz using an external crystal oscillator:


Make sure that you don't forget to change the "Oscillator Frequency [MHz]" to 20.000MHZ otherwise MikroC compiler will not be able to accurately time its built-in library functions.
Hope you will find this a quick way to run your PIC18F4550 / PIC18F2550 device at 20.000MHz.
Have fun!

Saturday, August 27, 2011

My experiment with PIC18F4550 and Graphic LCD interface using MikroC Compiler

In this post i have tested MikroC built-in Graphic LCD Library functions with the example they provide in the help section. I had to make slight changes to the code to run on my PIC18F4550 microcontroller. The original source code is for PIC18F887 which does not gets compiled for PIC18F4550 device.

I ran this code on internal 8.000Mhz oscillator. To run the cpu on internal oscillator one needs to set Oscillator to : INTOSC:USB-HS or to INTOSC:USB-XT. My Breadboard prototype circuit runs on USB power, As i use a home built USB connector to get +5v dc from USB interface.

Configuraion:


Schematics:

Video:




Source Code:

// Glcd module connections
char GLCD_DataPort at PORTD;

sbit GLCD_CS1 at RB0_bit;
sbit GLCD_CS2 at RB1_bit;
sbit GLCD_RS at RB2_bit;
sbit GLCD_RW at RB3_bit;
sbit GLCD_EN at RB4_bit;
sbit GLCD_RST at RB5_bit;

sbit GLCD_CS1_Direction at TRISB0_bit;
sbit GLCD_CS2_Direction at TRISB1_bit;
sbit GLCD_RS_Direction at TRISB2_bit;
sbit GLCD_RW_Direction at TRISB3_bit;
sbit GLCD_EN_Direction at TRISB4_bit;
sbit GLCD_RST_Direction at TRISB5_bit;
// End Glcd module connections

void delay2S(){ // 2 seconds delay function
Delay_ms(2000);
}

void main() {
unsigned short ii;
char *someText;

#define COMPLETE_EXAMPLE // comment this line to make simpler/smaller example
CMCON = 0x07; // Disable comparators
ADCON1 = 0x0F; // Disable Analog functions
OSCCON = 0x70; // configures oscillator divider for 8MHz int. oscillator

Glcd_Init(); // Initialize GLCD
Glcd_Fill(0x00); // Clear GLCD

while(1) {
Glcd_Fill(0x00); // Clear GLCD
Glcd_Box(62,40,124,56,1); // Draw box
Glcd_Rectangle(5,5,84,35,1); // Draw rectangle
Glcd_Line(0, 0, 127, 63, 1); // Draw line
delay2S();

for(ii = 5; ii < 60; ii+=5 ){ // Draw horizontal and vertical lines
Delay_ms(250);
Glcd_V_Line(2, 54, ii, 1);
Glcd_H_Line(2, 120, ii, 1);
}

delay2S();

Glcd_Fill(0x00); // Clear GLCD
#ifdef COMPLETE_EXAMPLE
Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32); // Choose font, see __Lib_GLCDFonts.c in Uses folder
#endif
Glcd_Write_Text("mikroE", 1, 7, 2); // Write string

for (ii = 1; ii <= 10; ii++) // Draw circles
Glcd_Circle(63,32, 3*ii, 1);
delay2S();

Glcd_Box(12,20, 70,57, 2); // Draw box
delay2S();

#ifdef COMPLETE_EXAMPLE
Glcd_Fill(0xFF); // Fill GLCD

Glcd_Set_Font(Font_Glcd_Character8x7, 8, 7, 32); // Change font
someText = "8x7 Font";
Glcd_Write_Text(someText, 5, 0, 2); // Write string
delay2S();

Glcd_Set_Font(Font_Glcd_System3x5, 3, 5, 32); // Change font
someText = "3X5 CAPITALS ONLY";
Glcd_Write_Text(someText, 60, 2, 2); // Write string
delay2S();

Glcd_Set_Font(Font_Glcd_System5x7, 5, 7, 32); // Change font
someText = "5x7 Font";
Glcd_Write_Text(someText, 5, 4, 2); // Write string
delay2S();

Glcd_Set_Font(Font_Glcd_5x7, 5, 7, 32); // Change font
someText = "5x7 Font (v2)";
Glcd_Write_Text(someText, 50, 6, 2); // Write string
delay2S();
#endif
}
}

Thursday, August 25, 2011

PIC18F2550 and PWM Experiment, using MikroC Pro for PIC

As the title suggest this experiment tests Microchips PIC18F2550 Microcontroller and its PWM Peripheral. The Code presented here turns on led connected at pin 13 (RC2/CCP1) of the PIC18F4550 with 0% brightness and then gradually increases the brightness upto 100% with change in the duty cycle by calling PWM1_Set_Duty (duty) function by increasing duty variable after every 10 mili sec.

Schematics:


PWM Library Functions Description:
There are following 4 functions provided by MikroC Compiler which lets you control PIC microcontroller's PWM Channel.

1. PWM1_Init (const long freq)
This routine needs to be called before using other functions from PWM Library.
Takes "freq" parameter as frequency in hertz, and initializes the PIC microcontroller's PWM channel.
All PWM modules use Timer2 for its operation, so you can not set different frequencies for different PWM modules.

2. void PWM1_Set_Duty(unsigned short duty_ratio)
Sets PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is 0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can be calculated as (Percent*255)/100.

3. void PWM1_Start(void)
The function returns nothing and takes no input parameter. This function starts PWM.
4. void PWM1_Stop(void)
The function returns nothing and takes no input parameter. This function stops PWM.

The source code presented here runs on internal 8.000Mhz built-in oscillator.

Configuration:

Source Code:

void main()
{
unsigned char duty = 0;
OSCCON = 0x70; // configures oscillator divider for 8MHz int. oscillator
PORTC = 0; // sets port c to all 0
TRISC = 0; //configures port c as output port
PWM1_Init (5000); // sets pwm frequency to 5000Hz
PWM1_Start(); // starts pwm1 peripheral

while (1)
{
PWM1_Set_Duty(duty); // sets duty cycle
duty++; // increments duty cycle by 1
Delay_ms(10); // adds 10 msec delay
}
}

Video 1: Showing signal observed at pin 13 (RC2/CCP1) changing duty cycle from 0% to 100% in a loop.




Video 2: Showing LED changing its brightness from 0% to 100% in a loop




Conclusion: PIC microcontroller's PWM peripheral is very easy to program using MikroC Library.


Sunday, August 21, 2011

My experiement with PIC18F2550 and Character Type LCD, using MikroC Pro for PIC

This weekend i wanted to test a character type LCD library supplied with MikroC Pro for PIC. I ran the LCD code on PIC18F2550 device. I ran my PIC18F2550 Device using internal oscillator. To correctly run PIC on internal oscillator, i selected oscillator to INTOSC:USB+HS or INTOSC:USB+XT from the Project Settings Dialog box (which can be accessed by pressing CTRL+SHIF+E in MikroC). To run the microcontroller on internal 8.000mhz clock i also added following statement to my C code:

OSCCON = 0x70; // configures oscillator divider for 8MHz int. oscillator


Schematics:


Picture of my assembled circuit:


MikroC Project Settings




Source Code:

// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;

// End LCD module connections

char txt1[] = "mikroElektronika";
char txt2[] = "EasyPIC6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i; // Loop variable

void Move_Delay() { // Function used for text moving
Delay_ms(250); // You can change the moving speed here
}

void main(){
CMCON = 0x07; // Disable comparators
ADCON1 = 0x0F; // Disable Analog functions
OSCCON = 0x70; // configures oscillator divider for 8MHz int. oscillator

Lcd_Init(); // Initialize LCD

Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,6,txt3); // Write text in first row

Lcd_Out(2,6,txt4); // Write text in second row
Delay_ms(2000);
Lcd_Cmd(_LCD_CLEAR); // Clear display

Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,5,txt2); // Write text in second row

Delay_ms(2000);

// Moving text
for(i=0; i<4; i++) { // Move text to the right 4 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}

while(1) { // Endless loop
for(i=0; i<8; i++) { // Move text to the left 7 times
Lcd_Cmd(_LCD_SHIFT_LEFT);
Move_Delay();
}

for(i=0; i<8; i++) { // Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
}
}
}


Conclusion: Using MikroC built-in LCD library it is very quick and easy to run character type LCDs.