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

Decision making on serial character

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



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

Decision making on serial character
PostPosted: Sun Dec 25, 2011 5:51 am     Reply with quote

Hi

Hope all are well, and enjoying Christmas day.

I got a problem again with reading and making decisions on the result, this could be a issue with my C-coding again (Still new), but the code executes fine every second time, when I press the Input (In1) button the first cycle the code executes without waiting at the getc part of the code, the second time it waits, I understand something about debounce but surely the code should sit and wait at the getc() till a character has been entered?

Also when the getc() part executes does it actually clear the buffer or must I manually clear this after the getc() command.

There was also mentioned that I must make use of ISR can somebody please send me an example code of how this works of even a link that I can read more about this...

Please find code below: (Hope I'm using the Code button correctly this time :-)))

Code:
 #use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_B7,rcv=PIN_B5,bits=8,ERRORS)


#define In1 PIN_C0
#define Op1 PIN_C1

void main(void)
{
 //   char cbuffer[24];
 //   char look_for[]="Word";

   char c[24];   
    char delim[]=",";
    char start[]="+";
   char stop[]=".";
   

    setup_port_a(sAN2);
    setup_adc( ADC_CLOCK_DIV_16 );

   
   while(1)
   {
      If (Input(In1) == 1)
      {
         
         printf("Enter a character: \n");
         c[0]=getc();
         //printf("The character is %c\n",c);   
         
         If (c=='2')
         {
            output_high (Op1);
            delay_ms(1000);
            Printf("The character is correct %c\n\r",c);
            output_low (Op1);
         }      
          else if (c=='3')
            {
            output_high (Op1);
            delay_ms(2000);
            output_low (Op1);
            Printf("The character is %c\n\r",c);
            }
         else
         {
            Printf("This is not a valid character");
          }

      }
   }
   
}

Regards,
Labjac
temtronic



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

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 6:53 am     Reply with quote

1) please stae which PIC you're using. Usually it's the first line of code.

2) In the examples folder, CCS supplies lots of code including one called ex_sisr.c. That is the EXample of the Serial ISR.

3) EVERYTIME the PIC is in a 'delay_ms(xxx)' it does NOTHING else so if you send the PIC more data, it will ignore it. That's why you need to use ISRs.

hope this help...
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 7:14 am     Reply with quote

Quote:
Also when the getc() part executes does it actually clear the buffer or must I manually clear this after the getc() command.

Assuming you are using a hardware UART the only buffer in this case is the 2 character UART hardware buffer. Getc() automatically removes the character from the buffer.
If you are using a software UART there is no buffer at all. In this case you must be waiting at getc() BEFORE the serial Start bit arrives or the character will be corrupted.

If you have a scope I would set a pin just before getc() and clear it after getc(). Use the scope to see this pin and the serial data. If the pin changes in the middle of a serial byte that is where your problem lies.
_________________
The search for better is endless. Instead simply find very good and get the job done.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 8:16 am     Reply with quote

A few more comments:
- You are setting up the analog ports but not using them. The program would be shorter without these codes.
- Remove all lines in comments, this again makes the program shorter. Easier to read for us means you will get more and better responses.
- The function setup_port_a is not mentioned in my July 2011 CCS manual. I think it is an old function name and now replaced by the more meaningful name setup_adc_ports().

Code:
    char delim[]=",";
    char start[]="+";
   char stop[]=".";
These definitions are not used in your code and could be left out for simplicity. But, you do understand you are declaring strings here? Strings are great but you can only test the received values by using an strcmp function call. Declaring a simple single character constant is probably what you wanted and results in less code overhead:
Code:
#define DELIM ',';
#define START '+';
#define STOP '.';



Code:
char c[24];
...
Printf("The character is correct %c\n\r",c);
This is not going to work as c is an array, i.e. the reference to c results in the _address_ of the first character where the %c specifier requires you to give the _contents_ of the character. Correct would have been:
Code:
char c[24];
...
Printf("The character is correct %c\n\r", &c);   <<-- note the '&'

Why make things difficult to yourself using an array when you want to hold just one character? Address calculations are difficult for the PIC and take a lot of memory. Easier and shorter code is:
Code:
char temp;      <<-- note, no []
...
Printf("The character is correct %c\n\r", temp);


Edit: fixed syntax error


Last edited by ckielstra on Tue Dec 27, 2011 1:43 pm; edited 1 time in total
Labjac



Joined: 15 Dec 2011
Posts: 20

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 9:40 am     Reply with quote

Hallo

I missed the first line of code when I pasted the code sorry, using a pic16f690.

The analog will be used that's why it was setup, For now I just using characters but will be using a string in future, this was a test code to try and understand characters and to setup a base for a working test program, still very new with Pic.

How would I know when I'm using hardware uart or software?
SherpaDoug



Joined: 07 Sep 2003
Posts: 1640
Location: Cape Cod Mass USA

View user's profile Send private message

PostPosted: Sun Dec 25, 2011 2:32 pm     Reply with quote

Labjac wrote:
How would I know when I'm using hardware uart or software?

The BEST place to look is the listing file. See if the compiler is sending bytes to UART registers or to a software UART routine.

The EASIEST place to look is the PIC datasheet. See what the hardware UART RX and TX pins are. If those are the pins you are using in your #use RS23() line the compiler will most likely use the hardware, otherwise it will use software to do the job. I think sometimes if you ask for an oddball serial format the hardware can not do, the compiler must still use software even if you call for the hardware pins.
_________________
The search for better is endless. Instead simply find very good and get the job done.
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