Hi everyone,
I have easyPIC5. Is there any one who can help me how can I set the LCD ports in PIC C. CCS?
PLZ help.
I go crazy...
PIC C CCS LCD PORT PROBLEM
LCD on EasyPic5 using CCS Compilr
:D
I too had problems getting the LCD to work on EasyPic5. Attached is my solution, hope it helps. It is based on code from CCS.
Put the following port defines in your favourite global .h
#byte PORTA = 0x05
#bit RA7 = PORTA.7
#bit RA6 = PORTA.6
#bit RA5 = PORTA.5
#bit RA4 = PORTA.4
#bit RA3 = PORTA.3
#bit RA2 = PORTA.2
#bit RA1 = PORTA.1
#bit RA0 = PORTA.0
#byte PORTB = 0x06
#bit RB7 = PORTB.7
#bit RB6 = PORTB.6
#bit RB5 = PORTB.5
#bit RB4 = PORTB.4
#bit RB3 = PORTB.3
#bit RB2 = PORTB.2
#bit RB1 = PORTB.1
#bit RB0 = PORTB.0
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
#define PIN_A6 46
#define PIN_A7 47
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
Put this cod into lcd.c
////////////////////////////////////////////////////////////////////////////
//// LCD.C ////
//// Driver for common LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
////////////////////////////////////////////////////////////////////////////
// As defined in the following structure the pin connection is as follows:
//
// B0 D4
// B1 D5
// B2 D6
// B3 D7
// B4 rs
// B5 enable
// B6 rw
//
// LCD pins D0-D3 are not used and PIC B3 is not used.
#include "LCD Test.h"
#ifdef __MikroC
#define LCD_DATA4 RB0 // these are input / output
#define LCD_DATA5 RB1
#define LCD_DATA6 RB2
#define LCD_DATA7 RB3
#define LCD_RS RB4 // these are always output
#define LCD_EN RB5
#else
#define LCD_DATA4 RA3 // these are input / output
#define LCD_DATA5 RB0
#define LCD_DATA6 RB4
#define LCD_DATA7 RB5
#define LCD_RS RA0 // these are always output
#define LCD_EN RA2
#endif
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
#define byte unsigned int8
/* These bytes need to be sent to the LCD to start it up. */
byte LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
void lcd_send_nibble(byte n)
{
LCD_DATA4 = n;
LCD_DATA5 = n >> 1;
LCD_DATA6 = n >> 2;
LCD_DATA7 = n >> 3;
delay_cycles(1);
LCD_EN = 1;
delay_us(2);
LCD_EN = 0;
}
void lcd_send_byte(byte address, byte n)
{
LCD_RS = 0;
delay_ms(10);
LCD_RS = address;
delay_cycles(1);
delay_cycles(1);
LCD_EN = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
void lcd_init()
{
byte i;
LCD_RS = 0;
LCD_EN = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0, LCD_INIT_STRING);
}
void lcd_gotoxy(byte x, byte y)
{
byte address;
if(y != 1)
address = lcd_line_two;
else
address = 0;
address += x - 1;
lcd_send_byte(0, 0x80|address);
}
void lcd_putc(char c)
{
switch (c) {
case '\f' : lcd_send_byte(0, 1); break;
case '\n' : lcd_gotoxy(1, 2); break;
case '\b' : lcd_send_byte(0, 0x10); break;
default : lcd_send_byte(1, c); break;
}
}
[/img][/quote]
I too had problems getting the LCD to work on EasyPic5. Attached is my solution, hope it helps. It is based on code from CCS.
Put the following port defines in your favourite global .h
#byte PORTA = 0x05
#bit RA7 = PORTA.7
#bit RA6 = PORTA.6
#bit RA5 = PORTA.5
#bit RA4 = PORTA.4
#bit RA3 = PORTA.3
#bit RA2 = PORTA.2
#bit RA1 = PORTA.1
#bit RA0 = PORTA.0
#byte PORTB = 0x06
#bit RB7 = PORTB.7
#bit RB6 = PORTB.6
#bit RB5 = PORTB.5
#bit RB4 = PORTB.4
#bit RB3 = PORTB.3
#bit RB2 = PORTB.2
#bit RB1 = PORTB.1
#bit RB0 = PORTB.0
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
#define PIN_A6 46
#define PIN_A7 47
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
Put this cod into lcd.c
////////////////////////////////////////////////////////////////////////////
//// LCD.C ////
//// Driver for common LCD modules ////
//// ////
//// lcd_init() Must be called before any other function. ////
//// ////
//// lcd_putc(c) Will display c on the next position of the LCD. ////
//// The following have special meaning: ////
//// \f Clear display ////
//// \n Go to start of second line ////
//// \b Move back one position ////
//// ////
//// lcd_gotoxy(x,y) Set write position on LCD (upper left is 1,1) ////
//// ////
//// lcd_getc(x,y) Returns character at position x,y on LCD ////
//// ////
////////////////////////////////////////////////////////////////////////////
// As defined in the following structure the pin connection is as follows:
//
// B0 D4
// B1 D5
// B2 D6
// B3 D7
// B4 rs
// B5 enable
// B6 rw
//
// LCD pins D0-D3 are not used and PIC B3 is not used.
#include "LCD Test.h"
#ifdef __MikroC
#define LCD_DATA4 RB0 // these are input / output
#define LCD_DATA5 RB1
#define LCD_DATA6 RB2
#define LCD_DATA7 RB3
#define LCD_RS RB4 // these are always output
#define LCD_EN RB5
#else
#define LCD_DATA4 RA3 // these are input / output
#define LCD_DATA5 RB0
#define LCD_DATA6 RB4
#define LCD_DATA7 RB5
#define LCD_RS RA0 // these are always output
#define LCD_EN RA2
#endif
#define lcd_type 2 // 0=5x7, 1=5x10, 2=2 lines
#define lcd_line_two 0x40 // LCD RAM address for the second line
#define byte unsigned int8
/* These bytes need to be sent to the LCD to start it up. */
byte LCD_INIT_STRING[4] = {0x20 | (lcd_type << 2), 0xc, 1, 6};
void lcd_send_nibble(byte n)
{
LCD_DATA4 = n;
LCD_DATA5 = n >> 1;
LCD_DATA6 = n >> 2;
LCD_DATA7 = n >> 3;
delay_cycles(1);
LCD_EN = 1;
delay_us(2);
LCD_EN = 0;
}
void lcd_send_byte(byte address, byte n)
{
LCD_RS = 0;
delay_ms(10);
LCD_RS = address;
delay_cycles(1);
delay_cycles(1);
LCD_EN = 0;
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
void lcd_init()
{
byte i;
LCD_RS = 0;
LCD_EN = 0;
delay_ms(15);
for(i=1;i<=3;++i) {
lcd_send_nibble(3);
delay_ms(5);
}
lcd_send_nibble(2);
for(i=0;i<=3;++i)
lcd_send_byte(0, LCD_INIT_STRING);
}
void lcd_gotoxy(byte x, byte y)
{
byte address;
if(y != 1)
address = lcd_line_two;
else
address = 0;
address += x - 1;
lcd_send_byte(0, 0x80|address);
}
void lcd_putc(char c)
{
switch (c) {
case '\f' : lcd_send_byte(0, 1); break;
case '\n' : lcd_gotoxy(1, 2); break;
case '\b' : lcd_send_byte(0, 0x10); break;
default : lcd_send_byte(1, c); break;
}
}
[/img][/quote]
-
- Posts: 28
- Joined: 04 Mar 2007 03:35
- Location: Brasil
LCD Test.h
LCD Test.h is only the global inclusions necessary for all modules. Some people call it Globals.h, I prefer to use the name of the project. The only necessary entries in it is the device header eg. #include <PIC16F648A>.