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

Need help with RX on 16F887

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



Joined: 06 Apr 2010
Posts: 7

View user's profile Send private message

Need help with RX on 16F887
PostPosted: Wed Nov 03, 2010 3:28 pm     Reply with quote

The pic I'm using is a 16F887. The code is for a remote control for a project that is hooked up to an LCD screen (4x20). What I'm having trouble with is receiving a packet that is being sent back to me. Initially this code sends out a packet of 8 bytes, and the robot responds by sending a 16 byte packet. I've hooked up the PICkit2 to the robot to ensure that it is sending the packets back and it is, so this means I'm sending the packets just fine. To double check it I hooked the remote (which sends the 8 byte packet) up to the RX of the PICkit and it is receiving packets from the 16F887 just fine.

An example of an outgoing packet is '31 31 00 00 77 32 00 00' and an incoming packet should look like ''31 31 00 00 77 32 00 00 00 00 00 00 00 00 00 00'. I've set up the code as follows below using incPKT as the packet to be received, and outPKT as the outgoing packet.


Code:

#include <Jinx_TestModule.h>
#include <Flex_LCD420.c> 

#define PUSH_UP      PIN_B5
#define PUSH_DOWN    PIN_B4
#define PUSH_LEFT    PIN_B3
#define PUSH_RIGHT   PIN_B2
#define LED_R        PIN_B1
#define LED_L        PIN_B0
#define incPKT_sz 16          // incoming packet size
#define outPKT_sz 8          // outgoing packet size
 
#define ixSenderID       0 //
#define ixReceiverID     1 // jinx is 1
#define ixFlags          2
#define ixNumber         3
#define ixDirection      4 // w a d s
#define ixSpeed          5 // a
#define ixReserved       6
#define ixCRC            7
#define BUFFER_SIZE 24        // size of buffer (1 packet =  8bytes = 64bits)

char incPKT[incPKT_sz];       // x byte incoming packet
char outPKT[outPKT_sz];       // x byte outgoing packet
int i = 0;

void setSpeed(char direction, int8 spd) // function for sending a packet
{
   outPKT[ixSenderID]   = '1';
   outPKT[ixReceiverID] = '1';
   outPKT[ixFlags]      = 0; //possible that error is due to ixSenderID = 0??
   outPKT[ixNumber]     = 0x00;
   outPKT[ixDirection]  = direction;       
   outPKT[ixSpeed]      = spd;
   outPKT[ixReserved]   = 0x00;   
   outPKT[ixCRC]        = 0x00;
}
//an outgoing packet example is '31 31 00 00 77 32 00 00'

//The below code is for an incoming packet!!!

char buffer[BUFFER_SIZE];
int8 next_in = 0;
int8 next_out = 0;
int8 ID = '1';                  // this robot's id
int8 serverID = '1';            // the server this robot will listen to

//incPKT[0] = serverID;
//incPKT[1] = ID;

int1 getPacket()
{
   while((next_in - next_out) >= incPKT_sz)
   {
      // if the next byte and the one after that are [ID],[ServerID]
      if(buffer[next_out] == ID && buffer[next_out + 1] == serverID)
      {
         for(i = 0; i<incPKT_sz; i++)
         {
            incPKT[i] = buffer[next_out];
            next_out++;
         }
         next_out = next_in;        // erase the rest of the packet
         return 1;
      }
      next_out++; // move forward in the buffer
   }
   return 0;
}

#int_rda
void serial_isr()
{
   do
   {
      buffer[next_in]=getc();
      if (++next_in >=BUFFER_SIZE) next_in=0;
      if(next_in==next_out)
      {
         if (++next_out >= BUFFER_SIZE) next_out=0;
      }
      // Buffer full, throw oldest character - opposite of CCS here
   } 
   while(kbhit());
}

void initInts()
{
   //enable_interrupts(INT_TIMER1);                // enable .5sec failsafe
   enable_interrupts(INT_RDA);                     // enable int on rx from wireless
   enable_interrupts(GLOBAL);                      // enable ints (master switch)
}
// interupt on uart data in (robot sends data to remote)
// this assumes the data is coming in via bytes (eg rs232)



void main()
{
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
                                 //Setup_Oscillator parameter not selected from Intr Oscillotar Config tab

   lcd_init(); // Initialize the LCD
   initInts(); // Initialize the Interrupts
   printf(lcd_putc, "\f");  // Clear the LCD.
   delay_ms(500);
   
output_low(LED_R);
   while(TRUE)
   {
      delay_ms(100);
      if(getPacket()) //packet received or not
      {
         printf(lcd_putc, incPKT);
      }
     
      for(i=0; i <=10 ; i++)
      {

         if(!input(PUSH_UP))
         {
            printf(lcd_putc, "\fMoving Forward");           
            setSpeed('w',50);
            output_high(LED_R);
            output_high(LED_L);
            break;
         }

         if(!input(PUSH_DOWN))
         {
            printf(lcd_putc, "\fMoving Backwards");           
            setSpeed('s',50);
            output_high(LED_R);
            output_high(LED_L);
            break;
         }
         
         if(!input(PUSH_LEFT))
         {
            printf(lcd_putc, "\fTurning Left");           
            setSpeed('a',50);
            output_high(LED_L);
            break;
         }
         
         if(!input(PUSH_RIGHT))
         {

            printf(lcd_putc, "\fTurning Right");           
            setSpeed('d',50);
            output_high(LED_R);
            break;
         }

         // wait and send data
         delay_ms(50); 
         if(i==10)
         {
            setSpeed('x',1);                 // Stopped State
            for(i=0; i < outPKT_sz; i++)
            {
               putc(outPKT[i], wireless);
            }
            output_low(LED_L);
            output_low(LED_R);
            printf(lcd_putc, "\f");
         }
         
      }
     
      for(i=0; i < outPKT_sz; i++)
      {
            putc(outPKT[i], wireless);
      }
   }
}


Any help or input on why the RX line isn't working would be greatly appreciated.
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Wed Nov 03, 2010 7:42 pm     Reply with quote

I would suggest deleting the
Code:
while(kbhit());

Your interrupt should be fast enough to execute before the next character arrives.

I don't see how you are setting up your UART, either.
I would suggest using the compiler directives(with appropriate values for your system);
Code:

#use delay(clock=20MHz)
#USE RS232(BAUD=9600, XMIT=PIN_B2,RCV=PIN_B3)

I have used the RS232 that way on almost every design and on a variety of PIC's for debugging & testing without any trouble.
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Wed Nov 03, 2010 7:46 pm     Reply with quote

My bad. (copied first one from CCS manual).
you should ALWAYS include ERRORS in the #USE RS232 directive or the uart will hang if it has an error.
Code:

#use rs232(baud=9600, xmit=PIN_A2,rcv=PIN_A3,ERRORS)
trentonite



Joined: 06 Apr 2010
Posts: 7

View user's profile Send private message

PostPosted: Thu Nov 04, 2010 4:22 pm     Reply with quote

I'm using this setup

Code:

#use delay(clock=4000000)
#use rs232(stream=wireless,baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,STOP=1)


I'll try throwing errors at the end to see what happens. Thanks for the heads up, I'll keep ya posted.
trentonite



Joined: 06 Apr 2010
Posts: 7

View user's profile Send private message

PostPosted: Sat Nov 06, 2010 10:18 pm     Reply with quote

Yeah still no such luck. I included errors, but received a warning that it was never used, so I doubt that was it. Also, after hooking up the tx/rx to the uart on pickit2 its still reading absolutely nothing for incoming packets.

/headscratch =\

Any more ideas? I'm lost lol.
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 3:24 am     Reply with quote

No, you did not receive a warning that errors was never used. You received a warning that the _variable_ RS232_ERRORS was never used. Different.....

The 'ERRORS' directive does two things. It adds code to the getc, so that if an error occurs, it clears it. This is _essential_. If you do not have the ERRORS in your RS232 declaration, then _you_ must perform the same clearing operations yourself. It also declares a new variable called 'RS232_ERRORS', and when it does find an error, records this in the variable. If you never read this variable, you get the 'warning'. This however does _not_ mean that the actual recovery code is never used, it just means 'you' are not looking at the variable to see if anything has gone wrong.

Best Wishes
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 8:12 am     Reply with quote

Okay, at this point I would back up a step.
disable your interrupts (don't enable them) and comment out your main code and replace it with something like this:


Code:

char NewChar;
while(TRUE)
{
    if(kbhit())
    {
        NewChar=getch();   
        printf(lcd_putc, "\fNewChar: %c ",NewChar + '0');    // use this line if data is binary
         //   printf(lcd_putc, "\fNewChar: %c ",NewChar);  // use this line if data is ascii 
    }
}

I don't know if your data is binary or ascii, but I think you get the drift.
Just do something simple and see if you can detect a kbhit(). Then we can work on that. Once the Kbhit is detected, we can display some of the data. Either you're not getting any data, or something is going wrong in your code for processing it, this will tell us where to look next.

You did say you were using some pickit thing that has hardware that works, or used to, right? Or are you comparing your board to the pickit?
If this is a new board there may be some hardware issue involved.
pmuldoon



Joined: 26 Sep 2003
Posts: 218
Location: Northern Indiana

View user's profile Send private message

PostPosted: Sun Nov 07, 2010 8:29 am     Reply with quote

just an FYI

Code:

setup_spi(SPI_SS_DISABLED);

isn't turning off the SPI, it is enabling the SPI with SLAVE SELECT disabled.
There is no conflict with the RX/TX lines so it doesn't affect this problem, but may be an issue if you try to use PIN_C3-C5 for something.
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