Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

integer input

Status
Not open for further replies.

ProFPGA

Banned
I am trying to write a rountine for pic18f4523 in C using ccs-picc.
i couldnt find anything like getch() for capturing a integer input via RS 232 ??
Does anyone have a hint how i can achieve it like refer to the following piece of code :

#include <18F4523.h>
#include <stdio.h>
#use delay(internal=8M)// internal oscillator 8Mhz
#fuses NOWDT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=,bits=8)

// using internal oscillator with CKO at RA6
int menu(void);
void main()
{

while(1)
{


switch (menu()) {

case 1: {
output_bit(PIN_A0,1);
delay_ms(1000);
output_bit(PIN_A0,0);
break;
}


case 2: {
output_bit(PIN_A1,1);
delay_ms(1000);
output_bit(PIN_A1,0);
break;
}

case 3: {
output_bit(PIN_A2,1);
delay_ms(1000);
output_bit(PIN_A2,0);
break;
}


default: {
output_bit(PIN_A0,0);
output_bit(PIN_A1,0);
output_bit(PIN_A2,0);

break;
}


}
}

///////User Selection Menu//////////////////////////////
int menu(unsigned int)
{
unsigned int ch;
printf ("\n\r Press A to turn LED on RA0 .......");
printf ("\n\r Press B to turn LED on RA1 .......");
printf ("\n\r Press C to turn LED on RA2 .......");
ch = getc(); //wait for and get serial character
delay_ms(100);
return ch;
}
/////////////////////////////////////////////////////////

/// it gives me an error that numeric should appear next to menu ????
 
Not clear what your question is but: getc does get a number. the number is the ascii code of the character you sent to the port. if you want to send a number that is encoded as ascii characters you need an ascii conversion routine. For one character numbers ( 0 to 9 ) you can convert the ascii character to the associated number by subtracting hex 30 from the code.
Look at: ASCII : Java Glossary

More generally use strings and do conversion on them. this is pretty standard and normally there are library routines for this.

When you say it gives me an error what is "it" and what exactly is the message?
 
the compiler points to the top line of this code :

-> int menu(unsigned int)
{
unsigned int ch;
printf ("\n\r Press A to turn LED on RA0 .......");
printf ("\n\r Press B to turn LED on RA1 .......");
printf ("\n\r Press C to turn LED on RA2 .......");
ch = getc(); //wait for and get serial character
delay_ms(100);
return ch;
}

/// And the error messages i get are these
Executing: "C:\Program files\Picc\CCSC.exe" +FH "pic18f4523ledo_p.c" I+="C:\Program Files\PICC\Devices" +DF +LN +T +A +M +Z +Y=9 +EA
>>> Warning 203 "pic18f4523ledo_p.c" Line 13(1,1): Condition always TRUE
*** Error 51 "pic18f4523ledo_p.c" Line 54(1,4): A numeric expression must appear here
*** Error 127 "pic18f4523ledo_p.c" Line 62(10,16): Return value not allowed in void function
*** Error 79 "pic18f4523ledo_p.c" Line 66(57,58): Expect }
3 Errors, 1 Warnings.
 
At the top of your code you have,

int menu(void);

instead of

int menu(unsigned int);

and you need to name the variable in your function

int menu(unsigned int VarName){


Mike.
 
hi ,

i edited it as you have pointed out , now the error is on the line where its returning the value .
i have attached the screen shoot .
 

Attachments

  • ccserror.jpg
    ccserror.jpg
    119.7 KB · Views: 148
You are getting that error because menu requires an integer passed to it. However, your menu function doesn't use the parameter that is passed and so you may as well change it back to void.

Try,
Code:
#include <18F4523.h>
#include <stdio.h>
#use delay(internal=8M)// internal oscillator 8Mhz
#fuses NOWDT
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,st ream=,bits=8)

// using internal oscillator with CKO at RA6
int menu(void);

void main() 
{
    while(1)
        {    
        switch (menu()) {
        
            case 1: {
                output_bit(PIN_A0,1);
                delay_ms(1000); 
                output_bit(PIN_A0,0);
                break;
            }
    
            case 2: {
                output_bit(PIN_A1,1);
                delay_ms(1000); 
                output_bit(PIN_A1,0);
                break;
            }
    
            case 3: { 
                output_bit(PIN_A2,1);
                delay_ms(1000);
                output_bit(PIN_A2,0);
                break;
            }
    
            default: { 
                output_bit(PIN_A0,0);
                output_bit(PIN_A1,0);
                output_bit(PIN_A2,0);             
                break; 
            }
        }
    }
}

///////User Selection Menu//////////////////////////////
int menu(void)
    {
    unsigned int ch;
    printf ("\n\r Press A to turn LED on RA0 .......");
    printf ("\n\r Press B to turn LED on RA1 .......");
    printf ("\n\r Press C to turn LED on RA2 .......");
    ch = getc(); //wait for and get serial character 
    delay_ms(100);
    return ch;
}
/////////////////////////////////////////////////////////

Note, to post code type [code] before it and [/code] after it so that it keeps its formatting.

Mike.
 
than i am back to square one . what if i change it all to char


code char menu(void);
void main()
{

while(1)
{


switch (menu()) {

case '1': {
output_bit(PIN_A0,1);
delay_ms(1000);
output_bit(PIN_A0,0);
break;
}


case '2': {
output_bit(PIN_A1,1);
delay_ms(1000);
output_bit(PIN_A1,0);
break;
}

case '3': {
output_bit(PIN_A2,1);
delay_ms(1000);
output_bit(PIN_A2,0);
break;
}


default: {
output_bit(PIN_A0,0);
output_bit(PIN_A1,0);
output_bit(PIN_A2,0);

break;
}


}
}

///////User Selection Menu//////////////////////////////
char menu(void)
{
char ch;
printf ("\n\r Press A to turn LED on RA0 .......");
printf ("\n\r Press B to turn LED on RA1 .......");
printf ("\n\r Press C to turn LED on RA2 .......");
ch = getc(); //wait for and get serial character
//delay_ms(100);
return ch;
}
/////////////////////////////////////////////////////////code
 
although it still gives me the following messages :

>> Warning 203 "pic18f4523ledo_p.c" Line 13(1,1): Condition always TRUE
*** Error 51 "pic18f4523ledo_p.c" Line 54(1,5): A numeric expression must appear here
*** Error 127 "pic18f4523ledo_p.c" Line 62(10,16): Return value not allowed in void function
*** Error 79 "pic18f4523ledo_p.c" Line 66(57,58): Expect }
3 Errors, 1 Warnings.
 
Are those errors with the code I posted?

Please, if you post errors then post the full code.

I must admit I'm loosing the help factor with you. I posted (possible) answers to your questions and you come back with crap. You didn't even read (or act on) my suggestion of how to post code.

Nope, lost it, not going to participate any more.

Have a good life.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top