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

Usart problem
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 17, 2005 12:52 am     Reply with quote

Quote:

I've fought with this thing for hours and hours. Can anyone help? You've
all been a tremendous help so far... do I need to know something else
about the getc() command that I don't know?... or is there another method of doing this?


If I may... it's because you're not reading everyone's posts closely.

Ttelmah told you:
Quote:
Second thing, you need a 'trap' at the end of your code, to stop the processor falling of the end, and putting the processor to sleep.

He could have been a bit more explicit about how this affects the hardware
USART.

If you look at all the sample code that he or I posted, it always has a
continuous while() loop at the end. Example:

Quote:
void main()
{
int8 data[6] = {0x06, 0xFF, 0x21, 0x01, 0x00, 0x00};
int16 crc;

crc = calculate_crc16(data, sizeof(data) -2);
printf("crc = %LX", crc);
while(1);
}


The reason this is necessary is because CCS puts a hidden sleep
instruction at the end of main(). The hardware UART has a 1-deep
transmit fifo, as well as the transmit shift register itself. So it can
"buffer up" two characters. So as soon as the printf() or putc()
statements finish sending bytes to the UART, the code will hit the
hidden sleep instruction and shut down the whole PIC, including
the UART. The result is that the last two bytes inside the hardware
UART don't get sent. That is one of the reasons why we recommend
putting a while(1) statement at the end of main(). You need to
start doing that automatically, as part of writing a CCS program.
Guest








PostPosted: Thu Nov 17, 2005 1:44 am     Reply with quote

My apologies to both PCM programmer and Ttelmah for not paying closer attention... That's a very interesting fact, and very useful to know. I will attempt this tomorrow and let you all know how it goes. Thank you everyone for your tremendous amount of help. You'll be in my credits if I can get this stuff to work. Smile
Guest








PostPosted: Thu Nov 17, 2005 11:46 am     Reply with quote

After further testing, you did indeed call it correctly, PCM Programmer. Thanks for all the assistance you've all provided. You've made my day. Very Happy
Guest








PostPosted: Thu Nov 17, 2005 5:57 pm     Reply with quote

This may be too much, but I have one more problem I can't figure out.
I'm trying to get a character at the end of my byte checking from the RFID... this will be received from a PC. The problem is... the program locks in the getc(), as if it never receives it at all.

Code:
#include <16F877A.H> //16F877A pic


#fuses HS,NOWDT,NOPROTECT,NOLVP
#DEVICE *=16 // Use 16 bit pointers
#define TX_DATA PIN_C6
#define RX_DATA PIN_C7

   
#use delay(clock=10000000)
#use rs232(baud=38400, xmit=TX_DATA, rcv=RX_DATA, PARITY=E, bits=8)
#include "lcd.c"
#include "string.h"



#pragma zero_ram                     // Interesting command ....

int check_rfid(void)
{
  int i,j;
  char c;
  char buf[8]={23,23,23,23,23,23,23,23};
  char tag[3][8]={{0xe0,0x07,0x00,0x00,0x17,0xf2,0x5f,0x31},{0xe0,0x07,0x00,0x00,0x01,0xbc,0xc5,0xa8},{0xe0,0x07,0x00,0x00,0x01,0xde,0xd0,0xc8}};
  delay_ms(500);
  putc(0x06);
  putc(0xFF);
  putc(0x21);
  putc(0x01);
  putc(0x2A);
  putc(0xA5);

   c = getc();
   if (c==0x06) {
      lcd_putc("6");
      for (i=0;i<5;i++)
         getc();
       return 0;
   }
   else if (c==0x11) {
      lcd_putc("11");
      for (i=0;i<6;i++)
         getc();
      for (i=0;i<8;i++) {
         buf[i]=getc();
       }
       getc(); //CRC1
       getc(); //CRC2
 
       for (j=0;j<3;j++) {
         for (i=0;i<8;i++) {
           if (buf[i]!=tag[j][i]) {
             break;
           }
           if (i==7) {
             return j+1;
           }
         }
       }
   }

   else {
       lcd_putc("\fRFID error!");
       return 0;
   }
}

void main( void )
{
  int i=0;
  int code=0;
  int fail_tag=0;
  char c;
  //setup tags
  const char name[3][20]={"Person1","Person2","Person3"};
  lcd_init();  // initialize the LCD!
  lcd_putc("\fSecurity activated");

  delay_ms(3000); //wait for the RFID to power on and be ready

  // Begin the massive security loop!
  do {
  int id_found=255;
  // Check for RFID tag by calling the check_rfid function
  do {
    id_found=check_rfid();
  } while(id_found==0);
 
  // We're out of the do while, so we found something!
  printf(lcd_putc,"\fHello %s",name[id_found++]);
 //  lcd_putc(name[id_found++]);
  delay_ms(2000);
   lcd_putc("\fPlease enter your");
   delay_ms(700);
   lcd_putc("\fpassword");

  while(1) {
  c = getc();    // [b]Not ever getting anything!!![/b]
  lcd_putc('a');
  }

  } while (fail_tag==0);
 while(1); //Always keep this at the end so the PIC doesn't fall asleep
}


I know this is a lot of code, and without the RFID reader, probably hard to simulate the exact problem... any suggestions at all would be worth while.
Thanks!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 17, 2005 11:48 pm     Reply with quote

Quote:

this will be received from a PC. The problem is... the program locks in
the getc(), as if it never receives it at all.

Can you explain the hardware connections between pins C6 and C7
on the PIC, and whatever they go to ?

You said you're not getting anything "from a PC", but I thought
you had the PIC's UART connected to the RFID chip ?

Where is the connection to the PC ?
Guest








PostPosted: Fri Nov 18, 2005 10:20 am     Reply with quote

Quote:
Can you explain the hardware connections between pins C6 and C7
on the PIC, and whatever they go to ?

Yeah, actually, they're connected to the same spot. I thought I could do that because I'm nearly sure that there's no data coming in on the RFID serial line once I get to that point in the code... but I'm guessing there's a reason why my assumption was incorrect.
Ttelmah
Guest







PostPosted: Fri Nov 18, 2005 10:59 am     Reply with quote

Serial logic lines are 'driven' all the time, even when 'idle'. Lines which allow you to 'wire OR' the logic signals together do this, by having the line only driven in one direction, and then having a resistor to provide the 'drive' the other way, so that there is not a problem with having multiple devices attached to one point. You cannot do this is RS232, or TTL level serial lines, without adding a logic gate (or a circuit to give the same function). It is not suprising the other input does not work...

Best Wishes
Guest








PostPosted: Fri Nov 18, 2005 2:24 pm     Reply with quote

Thanks for the information once again. Looks like I have to use a soft UART... and I'm guessing it'll work better then.

Since I haven't used the software UART, I should ask... is it similar to using the hardware? Since I don't have any timing constraints since I order the data to come (i.e., sending a data request to each device), I shouldn't need interrupts, so I can use it the same way, correct? I know there's some exceptions, i.e. one of the pins needs to be inverted, etc, but is there anything else I should know?

As a final word of note, if either of you (PCM Programmer or Ttelmah) are ever in central Illinois, I owe you both a drink. Smile
Ttelmah
Guest







PostPosted: Fri Nov 18, 2005 4:00 pm     Reply with quote

The 'big' ones are:
1) Do you want to do anything else at the same time?. If so, then you need to think carefully. The software UART, needs to accurately time the incoming/outgoing data, and except at very low data rates, if an interrupt event for something else occurs during a transaction, data will be lost/corrupted. There are 'work arounds' for this, by disabling interrupts on a 'per character' basis, which may often work.
2) The software serial, if used for 'receive', needs to actually be 'waiting' for the character when it comes, or start waiting _very soon_ afterwards. This has implications on the program flow in any loop waiting for data.
3) With the software UART, you can actually implement a "wire or'ed" transmit connection between multiple PICs, if the option 'float_high' is selected, and a pull-up resistor added to the line.
4) The software UART, supports signal inversion, while the hardware one does not.
5) The software UART, uses a lot of processor time (this ties up with '1', and '2')...

Best Wishes
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 Previous  1, 2
Page 2 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