Need help to solve the program

Thread Starter

Suneeth Nair

Joined Jun 14, 2014
1
Hello friends,

I am a starter in PIC and I have done a program in pic c complier for the following problem
_____________________________________________________________
When PIC A0 receives an input of more than 4 v, it is converted into digital output and that is fed to a counter. When the count reaches 5 the memory is incremented by 1 and is multiplied with 3 and displayed as cost as well as the counter is reset.

These are displayed in a LCD 16*2 screen. whenever the cost goes above 20, a message is sent via sms using a gsm 900 module to a pre described number. and finally the cost is reset to 0.

I have attached a word file of the program. The problems i face is the LCD display is not working as well as the message is not sent over the gsm.

_________________________________________________________________

Now the problem is program works fine with proteus model but in real time it dosen't work. Even the LCD 1682 display is not diplaying the result as well as there is no message being sent over the gsm.

Can anyone help me with this program and tell me what are the errors i have done..

Thanks in advance.. Hoping to receive some help.

This the program which i did for PIC 16f877a:

Rich (BB code):
#include"C:\Users\vijay\Desktop\project\Program for\main.h"
#bit lcdrs=0X09.2
#bit lcde=0X09.0
#bit lcdrw=0X09.1
#byte lcddata = 0x8
void lcdinit();
void lcdclear();
void lcdclock();
void lcddly();
void line1();
void line2();
void lcdputc(char c);
//unsigned char disp[16],discou,dl;
void lcddly()
{
delay_ms(1);
}
void lcdxy(char r,char c)
{	 
if(r==1)
{
lcdrs=0;
lcddata=0x80+c;
lcdclock();
lcdrs=1;
}
else if(r==2)
{
lcdrs=0;
lcddata=0xC0+c;
lcdclock();
lcdrs=1;
}
}
void lcdinit()
{
lcde=0;
lcddata=0x38;
lcdrs=0;
lcdclock();
lcddata=0x14;
lcdclock();
lcddata=0x0c;
lcdclock();
lcddata=0x06;
lcdclock();
lcddata=0x01;
lcdclock();
lcdrs=0;
lcddata=0x80;
lcdclock();
}
void lcdclock()
{
lcde=1;
lcddly();
lcde=0;
lcddly();
}
void lcdclear()
{
lcdrs=0;
lcddata=0x01;
lcdclock();
lcddata=0x0c;
lcdrs=0;
lcdclock();
}
void lcdputc(char c)
{
switch(c)
{
case '\f':
{
lcdrs=0;
lcddata=0x01;
lcdclock();
lcddata=0x0c;
lcdrs=0;
lcdclock();
lcdrs=0;
lcddata=0x80;
lcdclock();
lcdrs=1;
}break;
case '\n':
{
lcdrs=0;
lcddata=0xc0;
lcdclock();
lcdrs=1;
}break;
default:
lcddata=c;
lcdclock();
}
}

int value;
int8 c=0,cost=0,d,u=0,a;
void main()
{
start:
setup_adc_ports(AN0);
setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_16,127,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
set_adc_channel(0);
delay_us(20);
set_tris_b(0xFF);
set_tris_d(0X00);
set_tris_e(0X00);
set_tris_a(0xF0);
set_tris_c(0x0F);
lcdinit();
lcdclear();
lcdxy(1,0);
lcdclear();
// for(qw=0;qw<=2;qw++)
while(1)
{ 
delay_ms(1);
value=read_adc();
lcdinit();
lcdclear();
lcdxy(1,0);
lcdclear();
lcdxy(1,0);
printf(lcdputc, "count:%d",d);
if(value>=31)
{ 
c++;
d=c;
lcdinit();
lcdclear();
lcdxy(1,0);
lcdclear();
lcdxy(1,0);
//delay_ms(100);
printf(lcdputc, "count:%d",d);
}
if(d>=5)
{
u++;
a=u;
c=0;
cost=(u*3);
lcdinit();
lcdclear();
lcdxy(1,0);
lcdclear();
lcdxy(1,0);
//delay_ms(100);
printf(lcdputc,"Units:%d",a);
printf(lcdputc, " Cost:%d", cost);
goto start;
}
if (cost>=20)
{
//serial_init(9600);
printf("AT\n");
delay_ms(20); // 2 sec delay
printf("AT+CMGF=1\n");
delay_ms(20); // 2 sec delay
printf("AT+CMGS=\"09825858509\"\n");
delay_ms(20); // 2 sec delay
printf ("Cost=%d ", cost); // sends ADC value as SMS
putchar(26); // Ctrl-Z indicates end of SMS and transmit 
delay_ms(20); // 2 sec delay 
cost=0;
} 
else
{ goto start;
}
 

Attachments

Last edited by a moderator:

tshuck

Joined Oct 18, 2012
3,534
You should break the problem down into parts.

A simulator doesn't capture all the nuances of real hardware, so a working system in a simulator does not mean working hardware...
 

absf

Joined Dec 29, 2010
1,968
I assume you're simulating on the proteus. What is the clock speed you used in the simulator and what is the crystal frequency you used in the real HW?

As Tshuck has said, break you problems into parts and tackle them one at a time. The crystal frquency you used is important because the LCD is a slow device and needed a lot of time delays in sending commands and data to it.

Try to make the LCD work first before tackling other problems.

Allen
 
Top