|
|
View previous topic :: View next topic |
Author |
Message |
thivanka321
Joined: 23 May 2010 Posts: 3
|
CCS C code to interface pic 18f452 with LCD 16x2 module |
Posted: Sat Oct 23, 2010 11:32 am |
|
|
Hi,
I'm doing a project with involving displaying of water level of a tank on a lcd 16x2 module. I'm a having problem with writing the code in CCS C.
I don't know which libraries should I use, which function should I use for writing code for lcd display.
Please, if you're an expert please let me know how to write code in CCS C to display some characters, numbers on the LCD. If you can elaborate those thing with examples it is better.
Also let me know how to configure CCS C program, as it is compatible with LCD libraries if there are any. Please, this is urgent.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
thivanka321
Joined: 23 May 2010 Posts: 3
|
|
Posted: Sat Oct 30, 2010 10:15 am |
|
|
I tried with your examples. I wrote a code to measure water level (below it is posted). It did work with 'Proteus' software well, but not in real lcd+pic set up. I can't find the reason behind that.
Only dout lies in the voltage of contrast pin. Should I keep it as o V. If I keep this as zero, will it be ok?
Code: |
#include "G:\ccsfiles\lcd-C.h"
#include <flex_lcd.c>
void main()
{
int a,b,a1,a2,a3,d0,d1,d2,d3;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_oscillator(False);
// TODO: USER CODE!!
SET_TRIS_A( 0xF1 );
a1= input(PIN_A0); // input from A0
a2=input(PIN_A4); // input from A4
a3=input(PIN_A5) ; // input from A5
lcd_init(); // Always call this first.
lcd_putc(" Water Level\n "); // Display 'water level' text on first line
d0=0,d1=10,d2=20,d3=30; // water level heights
while(1)
{
if(a1==0) // zero water level
{
//lcd_putc('\f');
//lcd_putc("Water Level\n ");
printf(lcd_putc," %2u",d0);
lcd_putc("m ");
lcd_gotoxy(1,2);
}
else if(a1==1 && a2==0 ) // 10 m water level
{
printf(lcd_putc," %2u",d1);
lcd_putc("m ");
lcd_gotoxy(1,2);
}
else if(a2==1 && a3==0) // 20 m water level
{
printf(lcd_putc," %2u",d2);
lcd_putc("m ");
lcd_gotoxy(1,2);
}
else if(a3==1) // 30 m water level
{
printf(lcd_putc," %2u",d3);
lcd_putc("m ");
lcd_gotoxy(1,2);
}
//else if(a1==1 )
//{
// printf(lcd_putc,"%2u",d1);
// lcd_putc("m");
// }
//else if(a2==1 && a3==0)
//{
// printf(lcd_putc,"%2u",d2);
// lcd_putc("m");
// }
//else if(a3==1)
// printf(lcd_putc,"%2u",d3);
// lcd_putc("m");
}
//a=30;
//b=40;
//lcd_init(); // Always call this first.
//lcd_putc("\Water Level \n");
//lcd_putc(a );
//printf(lcd_putc,"%2u\%2u",a,b);
//lcd_putc("m");
//while(1);
} |
Please, help me sort this problem out.
Thanks. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 31, 2010 4:59 pm |
|
|
Quote: | It did work with 'Proteus' software well, but not in real lcd+pic set up.
I can't find the reason behind that. |
1. Quite often the reason is in the #fuses, and #use delay(), etc.
I assume those lines are in here:
Quote: | #include "G:\ccsfiles\lcd-C.h" |
Also post the #include line for your PIC. We don't know what your PIC is.
2. Post the list of PIC pins that are used for your LCD.
3. Post your compiler version.
4. Make a simple test program for your LCD, that just displays
"Hello World". Get your LCD working first (in hardware), and then
work on the water level code. |
|
|
thivanka321
Joined: 23 May 2010 Posts: 3
|
|
Posted: Sun Oct 31, 2010 7:27 pm |
|
|
Pic I'm using is 18f452.
These are the pins i used. I made corresponding changes in flex_lcd dirver according to this.
#define LCD_DB4 PIN_D4
#define LCD_DB5 PIN_D5
#define LCD_DB6 PIN_D6
#define LCD_DB7 PIN_D7
#define LCD_E PIN_A1
#define LCD_RS PIN_A3
#define LCD_RW PIN_A2
heres is the code in #include "G:\ccsfiles\lcd - D.h",
Code: | #include <18F452.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOOSCSEN //Oscillator switching is disabled, main oscillator is source
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOCPD //No EE protection
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#use delay(clock=10000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) |
regarding compiler,
PCB,PCM,PCH -4.013 VERSION
IDE-4.010
I wrote another code for same water level measuring case, using B port interrupts. It is not working in proteous even. Can you please check whether there are any wrong in coding in interuppts.
Code: |
#include "G:\ccsfiles\lcd - E.h"
#include <flex_lcd.c>
#int_RB
int a1,a2,a3,d1;
RB_isr()
{
if(a1==0 && a2==0 && a3==0 ) // zero water level
{
d1=0;
lcd_gotoxy(1,1);
}
else if(a1==1 && a2==0 ) // 10 m water level
{
d1=10;
lcd_gotoxy(1,1);
}
else if(a2==1 && a3==0) // 20 m water level
{
d1=20;
lcd_gotoxy(1,1);
}
else if(a3==1) // 30 m water level
{
d1=30;
lcd_gotoxy(1,1);
}
}
void main()
{
// int a1,a2,a3,d0,d1,d2,d3;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
setup_oscillator(False);
// TODO: USER CODE!!
lcd_init(); // Always call this first.
//SET_TRIS_A( 0xF1 );
SET_TRIS_B( 0xFF );
a1= input(PIN_B7); // input from B7
a2=input(PIN_B6); // input from B6
a3=input(PIN_B5) ; // input from B5
//lcd_init(); // Always call this first.
//lcd_putc(" Water Level\n "); // Display 'water level' text on first line
d1=0; // water level heights
lcd_putc('\f');
while(1)
{
printf(lcd_putc," %2u",d1);
lcd_putc("m ");
lcd_gotoxy(1,1);
}
}
|
please, reply me quicly. [/code] |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Oct 31, 2010 7:56 pm |
|
|
Quote: | #FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18) |
Are you using a Low Voltage Programmer ? Almost certainly you are not.
99.9% of all programmers are High Voltage Programmers. These include
ICD2, ICD3, PicKit2, PicKit3, ICD-U40, ICD-U64, etc. These are all
high voltage programmers. Change the fuse to NOLVP.
Quote: | PCB,PCM,PCH -4.013 VERSION |
This is not a good version to use. When used with lcd_putc(), it sends
out a bad character at the end of the text, as explained in this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661&start=67
Vs. 4.013 is too old. It has too many bugs.
Quote: |
Can you please check whether there are any wrong in coding in interrupts.
#int_RB
int a1,a2,a3,d1;
RB_isr()
{
|
Yes, there is something wrong. You can't put any lines of code between
the #int_rb and the RB_isr() line. Move the line in bold above #int_RB.
In addition to that, you are not doing the #int_rb code correctly.
You must read Port B inside the RB_isr() function to clear the
interrupt-on-change condition.
You should do a small "Hello World" program for the LCD.
Make the LCD work first. Then try other things. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|