CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

LCD 16x2 with pic16f877a
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

LCD 16x2 with pic16f877a
PostPosted: Thu Mar 31, 2011 8:37 pm     Reply with quote

Hello i am truly newbie in the ccs programming.
Right now i am doing my project for solar tracker.
So i just create program and i don't know what is the problem with it.
So please help me correct this program.

Maybe some of the mistake really simple but i am truly new in this field...

This my program

sensor cds at port a
lcd at port d
motor at port c
Code:

#include <16f877a.h>
#device adc=8
#use delay(clock=200000000)
#fuses hs, noprotect, nowdt, nolvp
#define <lcd.c>


#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8


void main()
{

   int r1, r2, r3;

   set_tris_a(0b11111111);
   set_tris_b(0b11111111);
   set_tris_c(0b00000000);
   set_tris_d(0b00000000);
   setup_port_a(ALL_ANALOG);
   setup_adc( ADC_CLOCK_INTERNAL );
   lcd_init();
   

   do

   {
      set_adc_channel(0);
      delay_ms(10);
      r1=read_adc();

      set_adc_channel(1);
      delay_ms(10);
      r2=read_adc();

      lcd_gotoxy(5,1);
      lcd_putc("sensor 1 = ");
      lcd_putc(%r1\n);
      delay_ms(1000);
      lcd_putc("\f");

      lcd_gotoxy(5,2);
      lcd_putc("sensor 2 = ");
      lcd_putc(%r2\n);
      delay_ms(1000);

      if(r1=r2)

         {
            void motor_stp1()
         }

      else if(r1 > r2)

         {
            void motor_fwd()
         }

      else if(r1 < r2)

         {
            void motor_bwd()
         }
      else if((r1 >= 205)||(r2 >= 205))

         {
            void motor_stp1()
         }
      else if((input_high(PIN_B1))&&(input_low(PIN_B2)))

         {

            void hold()
         }

      else if((input_high(PIN_B2))&&(input_low(PIN_B1)))

         {

            void face_east()
         }

      output_low(PIN_C0);
      output_low(PIN_C1);

   }

   while(1);
}


void motor_fwd()
   {
      r3 = r1 - r2

      if(r3 > 10)
      {
         output_high(PIN_C0);
         output_low(PIN_C1);
         delay_us(280);
      }
   }


void motor_bwd()
   {
      r3 = r2 - r1

      if(r3 > 10)
      {
         output_low(PIN_C0);
         output_high(PIN_C1);
         delay_us(280);
      }
   }


void motor_stp1()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      delay_us(280);
   }

void face_east()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      SLEEP(14400);
      output_low(PIN_C0);
      output_hight(PIN_C1);
      delay_us(280);
   }

void hold()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      SLEEP(3600);
   }
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 31, 2011 11:31 pm     Reply with quote

I count at least 10 errors in syntax. You need to learn C.
We can't learn it for you. You need to compile the code and
fix each one of the errors. Look in the CCS manual and
learn how to use the CCS functions. Learn the correct names
for the functions.

We don't want to have you spend one hour learning C, and
then have us fix all your coding mistakes. That's not the purpose
of this forum. It's not a C learning forum.

If you had only 1 or 2 errors, and you were trying hard to learn
then it would be OK. But you have 10+ errors. You need to
study and fix the errors.
hussain_kiet



Joined: 22 Sep 2008
Posts: 6

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Fri Apr 01, 2011 12:53 am     Reply with quote

The problem is very simple "Syntax Error..."
Have you compiled the code..?
it is probably the easiest of errors to fix, the compiler guides you with each error....
_________________
!!!!...HUSSAIN AFTAB...!!!!
Wayne_



Joined: 10 Oct 2007
Posts: 681

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 1:37 am     Reply with quote

This line will compile but is most likely wrong :-

Code:

    if(r1=r2)

What you are doing is an assignment, set r1 to have the same value as r2 and if it is not 0 (not FALSE but TRUE >0) then ...

To do a check for equality in a C if statement it is == so
Code:

    if(r1 == r2)
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 10:01 am     Reply with quote

tq... Smile


i don't know what the problem here, there is error with this program

Code:

      else if(r1 > r2)

         {
            void motor_fwd()
         }

i try to test my lcd but its didn't work properly, i already run it and tested on lcd.

Code:

#include <16f877a.h>
#device adc=8
#use delay(clock=200000000)
#fuses hs, noprotect, nowdt, nolvp
#include <lcd.c>


#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8

void main()
{

   int r1, r2, r3;

   set_tris_a(0b11111111);
   set_tris_b(0b11111111);
   set_tris_c(0b00000000);
   set_tris_d(0b00000000);
   setup_port_a(ALL_ANALOG);
   setup_adc( ADC_CLOCK_INTERNAL );
   lcd_init();


   do

   {

      set_adc_channel(0);
      delay_ms(10);
      r1=read_adc();

      set_adc_channel(1);
      delay_ms(10);
      r2=read_adc();


      lcd_putc("\f");
      lcd_gotoxy(5,1);
      lcd_putc("sensor 1 = ");
      lcd_putc(r1);
      delay_ms(1000);
      lcd_putc("\n");

      lcd_gotoxy(5,2);
      lcd_putc("sensor 2 = ");
      lcd_putc(r2);
      delay_ms(1000);
     
     
   }

   while(1);
}
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 10:40 am     Reply with quote

Your PIC will NOT run with 200,000,000 Hz xtal !

It helps if you put commas in big numbers to quickly see errors like this ..
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Fri Apr 01, 2011 12:19 pm     Reply with quote

i use lcd from cytron.com 16x2. did i need put this command in my programm...?


#define rs RB4
#define e RB5

#define lcd_data PORTD

#define SW1 RB0
#define SW2 RB1

#define LED1 RB6
#define LED2 RB7
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Sat Apr 02, 2011 6:17 am     Reply with quote

someone please comment my program.... ? i dont know where the mistake i do ......

Code:

#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt, nolvp
#define use_portd_lcd TRUE
#include <lcd.c>

#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9

void motor_fwd();
void motor_stp1();
void motor_bwd();
void face_east();
void hold();

void main()
{

   int r1, r2, r3;


   set_tris_a(0b11111111);
   set_tris_b(0b11111111);
   set_tris_c(0b00000000);
   set_tris_d(0b00000000);
   setup_port_a(ALL_ANALOG);
   setup_adc( ADC_CLOCK_INTERNAL );
   lcd_init();

   do

   {
     set_adc_channel(0);
      delay_ms(10);
      r1 = read_adc();

      set_adc_channel(1);
      delay_ms(10);
      r2 = read_adc();


      lcd_putc("\f");

      lcd_gotoxy(5,1);
      lcd_putc("sensor 1 = ");
      lcd_putc(r1);
      delay_ms(1000);

      lcd_putc("\n");

      lcd_gotoxy(5,2);
      lcd_putc("sensor 2 = ");
      lcd_putc(r2);
      delay_ms(1000);


      if(r1 == r2)

         {
            void motor_stp1();
         }

      else if(r1 > r2)

         {
            void motor_fwd()
         }

      else if(r1 < r2)

         {
            void motor_bwd()
         }
     
 // motor programming


      if(input(pin_b2)==0)

         {

            void hold()
         }

      else

         {
            void motor_stp1()
         }

      if(input(pin_b3)==0)

         {

            void face_east()
         }

      else

         {
            void motor_stp1()
         }

      output_low(PIN_C0);
      output_low(PIN_C1);

   }

   while(1);
}




void motor_fwd()
   {
      r3 = r1 - r2

      if(r3 > 10)
      {
         output_high(PIN_C0);
         output_low(PIN_C1);
         delay_us(280);
      }
   }


void motor_bwd()
   {
      r3 = r2 - r1

      if(r3 > 10)
      {
         output_low(PIN_C0);
         output_high(PIN_C1);
         delay_us(280);
      }
   }


void motor_stp1()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      delay_us(280);
   }

void face_east()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      SLEEP(14400);
      output_low(PIN_C0);
      output_hight(PIN_C1);
      delay_us(280);
   }

void hold()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      SLEEP(3600);
   }
temtronic



Joined: 01 Jul 2010
Posts: 9246
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Apr 02, 2011 6:53 am     Reply with quote

I suggest the following

1) remove the set_tris_x... lines, let the compiler do that for you automatically. If you do it wrong, you run into problems !

2) breakdown the program into two sections.
a) get the LCD to function correctly do the classic 'Hello World' program.

b) then just read and display the two voltages in a loop, say every 500ms.


If you can't get the LCD driver working right, use the flexlcd driver ( that's what I use all the time).

3) now add your 'motor control logic 'code, but display the result on the LCD, this will SHOW you the result of your actions. If the action is supposed to STOP the motors, display STOP on the LCD.

4) once you have it right, then add the code to turn on/off the motor signals.

I save each version of my programs as programnameV1, ...V2,...V3,etc. that way I can always go back to a known good program and make changes, save as version +1, it is too difficult to rememeber what worked, what didn't after a few hours ! It is common for me to have 30-40 'versions' of the program. Once the project is completed, all but the last 3 are erased.
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Sat Apr 02, 2011 12:50 pm     Reply with quote

in this part always show "undefined identifier r1" when i compile the program... why?



else if(r1 > r2)

{
void motor_fwd()
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sat Apr 02, 2011 2:48 pm     Reply with quote

Because r1, r2, and r3, are declared as _local_ variables inside main, then you try to use them inside the motor routines.

Best Wishes
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Sun Apr 03, 2011 5:18 am     Reply with quote

After do the the change the program become like this. Please anyone give comment to this program....?

Code:

#include <16f877a.h>
#use delay(clock=20000000)
#fuses hs,noprotect,nowdt, nolvp
#define use_portd_lcd TRUE
#include <lcd.c>

#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9

void motor_fwd();
void motor_stp1();
void motor_bwd();
void face_east();
void hold();

int r1, r2, r3;


void main()
{

   lcd_init();


   setup_port_a(ALL_ANALOG);
   setup_adc( ADC_CLOCK_INTERNAL );


while(1)

   {

// sensor programming

      set_adc_channel(0);
      delay_ms(10);
      r1=read_adc();

      set_adc_channel(1);
      delay_ms(10);
      r2=read_adc();

// LCD programming

      lcd_putc("\f");

      lcd_gotoxy(2,1);
      lcd_putc("sensor 1 = ");
      lcd_putc(r1);

      lcd_gotoxy(2,2);
      lcd_putc("sensor 2 = ");
      lcd_putc(r2);
      delay_ms(500);



// motor programming

      if(r1 == r2)

         {
            void motor_stp1();
         }

      else if(r1 > r2)

         {
            void motor_fwd();
         }

      else if(r1 < r2)

         {
            void motor_bwd();
         }

 // switch programming


      if(input(pin_b2)==0)

         {

            void hold();
         }

      else

         {
            void motor_stp1();
         }

      if(input(pin_b3)==0)

         {

            void face_east();
         }

      else

         {
            void motor_stp1();
         }

      output_low(PIN_C0);
      output_low(PIN_C1);

   }

}




void motor_fwd()
   {
      r3 = r1 - r2;

      if(r3 > 10)
      {
         output_high(PIN_C0);
         output_low(PIN_C1);
         delay_us(280);

            do
            {
            lcd_putc("\f");
            lcd_putc("motor action\n");
            lcd_putc("FOWARD");
            }
            while(0);
      } 
   }


void motor_bwd()
   {
      r3 = r2 - r1;

      if(r3 > 10)
      {
         output_low(PIN_C0);
         output_high(PIN_C1);
         delay_us(280);

          do
            {
            lcd_putc("\f");
            lcd_putc("motor action\n");
            lcd_putc("BACKWARD");
            }
          while(0);
      }
   }


void motor_stp1()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      delay_us(280);

      do
      {
         lcd_putc("\f");
         lcd_putc("motor action\n");
         lcd_putc("STOP");
      }
      while(0);
   }

void face_east()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);

      sleep();
   
      output_low(PIN_C0);
      output_high(PIN_C1);
      delay_us(280);
   }

void hold()
   {
      output_low(PIN_C0);
      output_low(PIN_C0);
      sleep();
   }
Ttelmah



Joined: 11 Mar 2010
Posts: 19552

View user's profile Send private message

PostPosted: Sun Apr 03, 2011 2:34 pm     Reply with quote

It'll sleep for ever.
What is going to wake it up?.

Best Wishes
muzakyo



Joined: 31 Mar 2011
Posts: 8
Location: malaysia

View user's profile Send private message

PostPosted: Tue Apr 05, 2011 9:02 am     Reply with quote

Why when I try the lcd there are nothing display on it? It is look like below picture nothing on it....



this is the program:-
Code:

#include <16F877a.h>
#include <math.h>
#include <stdlib.h>
#include <float.h>

#fuses hs,noprotect,nowdt, nolvp
#use delay(clock=20000000)
#include <lcd.c>
           
//---------------------------------------------------
// Main
//---------------------------------------------------
void main()
{
SETUP_ADC_PORTS(NO_ANALOGS);

lcd_init();  // Always call this first.

while(1)
{
lcd_putc("\fHello World\n");
lcd_putc("Line Number 2");

delay_ms(500);
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Apr 05, 2011 4:28 pm     Reply with quote

Quote:
#include <lcd.c>

Are you using the Flex lcd driver or the CCS lcd driver ?

Flex driver is here:
http://www.ccsinfo.com/forum/viewtopic.php?t=24661

CCS driver is here:
Quote:

c:\program files\picc\drivers\lcd.c


1. Post a list of the connections between the LCD and the PIC.

2. If you're using the Flex driver, post a list of the #define statements
at the top of the file (edited by you, to match the connections on your
board, between the PIC and the LCD).

3. Measure the voltage on pin 3 of the LCD. What is it ? It should be
approximately 0.5 volts.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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