Sunday, 27 September 2009

How to write text to an LCD with MikroC using a Pic

In this project, we'll use a pic chip to write text to a 2 row LCD screen (HD44780 or similar). MikroC has a built in library that provides communication through a four bit interface. We will only be using Lcd_Init, Lcd_Cmd and Lcd_Out. Read more on programming a pic to power an lcd by going that page!

Friday, 25 September 2009

Declaring Variables in MikroC

I've been getting emails lately asking if there is any special sequence for declaring variables or functions using mikroc. The answer is no! You declare them just as you would in normal / embedded C. For example, I will use a snippet from a sample MikroC project. The following program prints text to a 2x16 lcd display.


char *text = "mikroElektronika";


void main() {
  Lcd_Init(&PORTD);             // Lcd_Init_EP4, see Autocomplete


  LCD_Cmd(LCD_CLEAR);           // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Delay_ms(1000);
  LCD_Out(1,1, text);           // Print text to LCD, 1st row, 1st column
  Delay_ms(1000);
  LCD_Out(2,6,"mikroE");        // Print text to LCD, 2nd row, 6th column
}


If you were to change the name of the main() function, you could use it in another project to write text to a 2x16 lcd. Have a look at this:


char *text = "mikroElektronika";


void print_LCDtxt() {
  Lcd_Init(&PORTD);             // Lcd_Init_EP4, see Autocomplete


  LCD_Cmd(LCD_CLEAR);           // Clear display
  LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Delay_ms(1000);
  LCD_Out(1,1, text);           // Print text to LCD, 1st row, 1st column
  Delay_ms(1000);
  //LCD_Out(2,6,"mikroE");        // Print text to LCD, 2nd row, 6th column
}


If you were to copy and paste this in an existing mikroC project (provided this code doesn't conflict with yoou existing ports), you would be able to call this function at any time, printing whatever text you have placed in the *text char.

Hope this answers your question! Feel free to comment :)

Friday, 9 November 2007

Hex pad with LCD output

Hello again. I havent posted in ages because I've been really busy lately. This post won't go into detail but I will at a later date. Its basically a hex pad interfaced with a pic chip and an lcd display. I have included the files to be downloaded, any questions??....comment or email me :D
Files: Hexpad.rar

Wednesday, 17 October 2007

Traffic Lights Using Pic 16f84

Here is a way of making a set of traffic lights using a 16f84 microchip mcu. It is fairly simple, all you have to do is wire up the mcu with a pair of red, green and yellow leds. Note the addresses of all leds connected to the pic chip. From now on I'll refer to the red lights as R1 and R2, green lights as G1 and so on.


I connected my leds in this configuration:
R1: B0
Y1: B1
G1: B2
R2: B3
Y2: B4
G2: B5
After this, map out the sequence you want. Mine was the standard sequence for a simple two way traffic light system in the UK. Here it is (format:R1Y1G1R2Y2G2):
100001;
010001;
001001;
001011;
001100;
001010;
001001;
011001;

Just literally translate this line for line into embedded C for the main routine but remember to place delays between each line for however long you want the lights to stay in that state before changing.

Here is my final code:
//Glen Lashley
//Traffic Lights
void InitialisePorts(void)
{
TRISA=0; //Make all ports outputs
TRISB=0;
}
void Routine(void)
{
PORTB = 0b00100001;
Delay_ms(100);
PORTB = 0b00010001;
Delay_ms(100);
PORTB = 0b00001001;
Delay_ms(100);
PORTB = 0b00001011;
Delay_ms(100);
PORTB = 0b00001100;
Delay_ms(100);
PORTB = 0b00001010;
Delay_ms(100);
PORTB = 0b00001001;
Delay_ms(100);
PORTB = 0b00011001;
Delay_ms(100);
}
MAIN(void)
{
InitialisePorts();
while(1)
{
Routine();
}
}

Feel free to question or comment guys! :)

Monday, 15 October 2007

Simple H-Bridge concept

Ever had the need to change the polarity of a power source and had no idea where to start? Well this simple h-bridge will give you a starting stone to work with. This can be used for motor direction control, bi colour LED's or anything involving the direction in which current flows.



The concept is simple. Use an array of transistors for switching and re-routing the direction in which the current flows.

A picture of this concept is shown below:



Keep in mind that this is a concept and in order for it to work resistors and/or any protection circuits have to be added. Remember that motors give emf and can cause the transistors to burn up. Some people use darlington pairs and relays to trigger a higher voltage (for the motor in this case).

Hope this entry has been of use!