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 support@ccsinfo.com

problem assigning port_a with input_a()

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
c0de



Joined: 14 May 2007
Posts: 14

View user's profile Send private message

problem assigning port_a with input_a()
PostPosted: Fri Nov 23, 2007 6:48 am     Reply with quote

Hello.

I'm trying to make a simple program to write the state of portA pins to a lcd.
Code:
#include <16F876.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#include <flex_lcd.c>

void read_port_a()
{
    int8 x;
    x = input_a();
    int inc=0;
    char bit[6];
    for (inc=0; inc<=5; inc++)
    {
   if ( bit_test(x,inc) )
           bit[inc] = '1';
   else
       bit[inc] = '0';
    }
    lcd_gotoxy(2,3);
    printf(lcd_putc,"%c %c %c %c %c %c", bit[5],bit[4],bit[3],bit[2],bit[1],bit[0]);
    delay_ms(20);
}

void main() {

   int i = 0;
   set_tris_a(0b00111111);
   delay_ms(20);
   lcd_init();
   printf(lcd_putc,"\f");
   printf(lcd_putc,  " PIC16F76  LCD  ");
   printf(lcd_putc,"\n -------------  ");
   printf(lcd_putc,"\n                ");
   printf(lcd_putc,"\n -------------  ");
   while ( 1 )
   {
      read_port_a();
      delay_ms(20);
   }
}


everytime I try to compile the code, i get this:
Compiling: cos_lcd.c
1 Errors, 0 Warnings, Time: 1 Seconds
Error[51] cos_lcd.c 10 : A numeric expression must appear here
1 Errors, 0 Warnings.

which is this line:
Code:
 int inc=0;

... I cannot figure what I'm missing
using pic16f876 and PCM compiler version 4.032; (linux)

Any help would be appreciated. Thanks

EDITED:
By some reason, i cannot define variables within custom functions. the program works if i define x, bit and inc variables globally (before the read_port_a function)
Ttelmah
Guest







PostPosted: Fri Nov 23, 2007 7:40 am     Reply with quote

You cannot declare a type once you have 'started' the function.
All declarations must be at the beginning of the function block (or global).
Code:

void read_port_a() {
    //First the declarations
    int8 x;
    int inc=0;
    char bit[6];
    //Now the code
    x = input_a();
    for (inc=0; inc<=5; inc++) {
       if ( bit_test(x,inc) )
           bit[inc] = '1';
       else
           bit[inc] = '0';
    }
    lcd_gotoxy(2,3);
    printf(lcd_putc,"%c %c %c %c %c %c", bit[5],bit[4],bit[3],bit[2],bit[1],bit[0]);
    delay_ms(20);
}

Technically you could declare variables inside a sub function
(so have a leading '{', then a declaration, then more functions, and a trailing '}'), but in CCS, these are not identified as separate variables, so should be used with care.

Best Wishes
c0de



Joined: 14 May 2007
Posts: 14

View user's profile Send private message

PostPosted: Fri Nov 23, 2007 10:10 am     Reply with quote

I didn't knew that:)
Thanks alot.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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