Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need Help to debug the program for pic 16f877a

Status
Not open for further replies.

crystal123

Newbie
Joined
Jul 11, 2016
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
99
I am a starter in PIC and I have done a program in pic c compiler 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 doesn't work. Even the LCD 16*2 display is not displaying 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[Moderator action: removed advertising link]:

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;
        }
 
Last edited by a moderator:

Code:
printf("AT\n");
can't be expected to perform correct autobaud. Characters 'A' and 'T' must be send individually with surrounding delay, AT commands should be terminated by <CR> rather than <LF>.

Message send should wait for '>' request, or at least wait longer.

LCD initialization has required delays > 1 ms, read datasheet.
 

Hi,

I just took a quick view.

LCD:

* The init has to be done only once after power up (not every millisecond, not multiple times in one loop)
* LCD clear every millisecond makes no sense, too. It usually clears the whole display.
* you overwrite your LCD content several times within one loop

I recommend to overthink your complete LCD communcation (or the whole main loop)

Within one main loop run you do (in worst case):
* init, clear, clear, write, /maybe/ init, clear, clear, write /maybe/ init, clear, clear, write, .. and all this within milliseconds. it can´t work.


*****

--> draw a flow chart and a timing diagram.
Updating a display more than 3 times per second makes no sense, because your eyes and your brain are not fast enough to process all the input.

the display init needs to take care about timing. I assume you don´t follow the recommendations in the datasheet.

****
It seems you did all the programming at once and now wonder why it doesn´t work.
No professional will do it this way.
And for sure this is no good idea for a beginner.
--> Begin with a little program, then test it, then if it runs OK, then do the next step.
Start with LCD init,
Then LCD clear// then write text // then set xy and write a text //... step by step. And test each step.

*****
At the end of "while(1)" you don´t need a "goto Start", it automatically runs the loop

***



Klaus

- - - Updated - - -

Hi,

I´ve just read through your other threads.

It is bad habit to start a thread asking for help and then never respond to any post.
Communicate with us.

Klaus
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top