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

16F876A crash on serial comms

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







16F876A crash on serial comms
PostPosted: Mon Apr 21, 2003 10:39 am     Reply with quote

I'm writing some software for the 16F876A, and having some rather unexpected results. Specifically, my software crashes and resets itself spontaneously. I'm using a bootloader to load the software, but the BL has been proven over the last two years, so that isn't it.

The comms protocol is RS485, and there's a little bit of custom software that I can't include here that deals with that. Suffice to say, all it does is handle tx/rx buffers and set the transmit enable pin (RS485 is serial like RS232, but simplex; only one side can talk at a time).

I understand if it's impossible to debug my code without the extra library code included, but even if you guys can look at my code and say, "Looks good to me", that would be helpful. I'm using CCS version 3.044.

I put in some comments explaining the code that I didn't include. It was written between a year and two years ago, and has worked in more than a dozen other applications, which makes me think I'm doing something funky...

#include <16f876.h>

#device 16f876 *=16
#use delay(clock=4096000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
#define TX_EN PIN_C5

#include <pic.h>
#include <bootload.h> //tells the program not to occupy the last page of ROM
#include <mystdlib.h> //atoi, itoa, etc.

#define RDA_SIZE 16
#define RDA_SOL "#"
#define RDA_EOL "\r\n"
#include <rda.h> //handle comms buffer

#define CLOCK 4096000
#define NTIMERS 1
#include <timers.h>

void rda_handle(void);

void main() {
xmit_on();
printf("test firmware!\r\n");

rda_init();
printf("rda_init completed, starting enable_interrupts\r\n");
enable_interrupts(GLOBAL);

pin_high(PIN_C1);

while(1) {
// printf("[looping!]\r\n");
if(get_rda_flag()) {
printf("rda_flag = \%u", get_rda_flag());
rda_handle();
reset_rda_flag();
}
}
}

void rda_handle()
{
char *buf;
buf=get_rda_buffer();
printf("got rda buffer\r\n");
delay_ms(1);
xmit_on();
printf("\%s\r\n",buf);
xmit_off();
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13872
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: 16F876A crash on serial comms
PostPosted: Mon Apr 21, 2003 12:10 pm     Reply with quote

Nick
Your include 16F876 statment should be include 16F876A you forgot the A if you are using an A version chip. In the devices folder there are two 16F876 files.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13878
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

Re: 16F876A crash on serial comms
PostPosted: Mon Apr 21, 2003 12:11 pm     Reply with quote

:=I'm writing some software for the 16F876A, and having some rather unexpected results. Specifically, my software crashes and resets itself spontaneously. I'm using a bootloader to load the software, but the BL has been proven over the last two years, so that isn't it.
:=
:=The comms protocol is RS485, and there's a little bit of custom software that I can't include here that deals with that. Suffice to say, all it does is handle tx/rx buffers and set the transmit enable pin (RS485 is serial like RS232, but simplex; only one side can talk at a time).
:=
:=I understand if it's impossible to debug my code without the extra library code included, but even if you guys can look at my code and say, "Looks good to me", that would be helpful. I'm using CCS version 3.044.
:=
:=I put in some comments explaining the code that I didn't include. It was written between a year and two years ago, and has worked in more than a dozen other applications, which makes me think I'm doing something funky...
:=
:=#include <16f876.h>
:=
:=#device 16f876 *=16
:=#use delay(clock=4096000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
:=#define TX_EN PIN_C5
:=
:=#include <pic.h>
:=#include <bootload.h> //tells the program not to occupy the last page of ROM
:=#include <mystdlib.h> //atoi, itoa, etc.
:=
:=#define RDA_SIZE 16
:=#define RDA_SOL "#"
:=#define RDA_EOL "\r\n"
:=#include <rda.h> //handle comms buffer
:=
:=#define CLOCK 4096000
:=#define NTIMERS 1
:=#include <timers.h>
:=
:=void rda_handle(void);
:=
:=void main() {
:= xmit_on();
:= printf("test firmware!\r\n");
:=
:= rda_init();
:= printf("rda_init completed, starting enable_interrupts\r\n");
:= enable_interrupts(GLOBAL);
:=
:= pin_high(PIN_C1);
:=
:= while(1) {
:=// printf("[looping!]\r\n");
:= if(get_rda_flag()) {
:= printf("rda_flag = \%u", get_rda_flag());
:= rda_handle();
:= reset_rda_flag();
:= }
:= }
:=}
:=
:=void rda_handle()
:={
:= char *buf;
:= buf=get_rda_buffer();
:= printf("got rda buffer\r\n");
:= delay_ms(1);
:= xmit_on();
:= printf("\%s\r\n",buf);
:= xmit_off();
:=}

How do you turn the transmit enable pin off? If you do it with the transmit interupt you may be cliping the last byte off of your transmision. Are you turning the transmit interupt off after you have loaded the last byte? In reguard to your buffers are they protected from overruns? Buffer overruns can cause some strange results.
This is how I eliminate buffer overruns;
if( bit_test(PacketIndex,6)) // if packet index larger than packetbuffer
PacketIndex = 0; // zero packet index value
This gives me 2^6 or 64 bytes of buffer. When the buffer index increments to 64 it will be cleared to zero. This statement also compiles very well.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13880
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: 16F876A crash on serial comms
PostPosted: Mon Apr 21, 2003 12:11 pm     Reply with quote

:=I'm writing some software for the 16F876A, and having some rather unexpected results. Specifically, my software crashes and resets itself spontaneously. I'm using a bootloader to load the software, but the BL has been proven over the last two years, so that isn't it.
:=
:=The comms protocol is RS485, and there's a little bit of custom software that I can't include here that deals with that. Suffice to say, all it does is handle tx/rx buffers and set the transmit enable pin (RS485 is serial like RS232, but simplex; only one side can talk at a time).
:=
:=I understand if it's impossible to debug my code without the extra library code included, but even if you guys can look at my code and say, "Looks good to me", that would be helpful. I'm using CCS version 3.044.
:=
:=I put in some comments explaining the code that I didn't include. It was written between a year and two years ago, and has worked in more than a dozen other applications, which makes me think I'm doing something funky...
:=
:=#include <16f876.h>
:=
:=#device 16f876 *=16
:=#use delay(clock=4096000)
:=#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
:=#define TX_EN PIN_C5
:=
:=#include <pic.h>
:=#include <bootload.h> //tells the program not to occupy the last page of ROM
:=#include <mystdlib.h> //atoi, itoa, etc.
:=
:=#define RDA_SIZE 16
:=#define RDA_SOL "#"
:=#define RDA_EOL "\r\n"
:=#include <rda.h> //handle comms buffer
:=
:=#define CLOCK 4096000
:=#define NTIMERS 1
:=#include <timers.h>
:=
:=void rda_handle(void);
:=
:=void main() {
:= xmit_on();
:= printf("test firmware!\r\n");
:=
:= rda_init();
:= printf("rda_init completed, starting enable_interrupts\r\n");
:= enable_interrupts(GLOBAL);
:=
:= pin_high(PIN_C1);
:=
:= while(1) {
:=// printf("[looping!]\r\n");
:= if(get_rda_flag()) {
:= printf("rda_flag = \%u", get_rda_flag());
:= rda_handle();
:= reset_rda_flag();
:= }
:= }
:=}
:=
:=void rda_handle()
:={
:= char *buf;
:= buf=get_rda_buffer();
:= printf("got rda buffer\r\n");
:= delay_ms(1);
:= xmit_on();
:= printf("\%s\r\n",buf);
:= xmit_off();
:=}
___________________________
This message was ported from CCS's old forum
Original Post ID: 13882
Nick Foster
Guest







Re: 16F876A crash on serial comms
PostPosted: Tue Apr 22, 2003 8:17 am     Reply with quote

My compiler version, 3.044, does not include a 16f876a.h file. Does this file really exist in newer compilers? That would make sense, given that the 876A came out after my compiler did... but on the other hand, shouldn't the 876A be code-compatible with the 876?

--nick

:=Nick
:=Your include 16F876 statment should be include 16F876A you forgot the A if you are using an A version chip. In the devices folder there are two 16F876 files.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13918
sar



Joined: 08 Sep 2003
Posts: 36

View user's profile Send private message

Re: 16F876A crash on serial comms
PostPosted: Tue Apr 22, 2003 9:18 am     Reply with quote

Nick
These are the added defines for the 16F876A;

//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,NOPUT,PUT,PROTECT,DEBUG,NODEBUG
//////// Fuses: NOPROTECT,NOBROWNOUT,BROWNOUT,LVP,NOLVP,CPD,NOCPD,WRT,NOWRT
////////

////////////////////////////////////////////////////////////////// COMP
// Comparator Variables: C1OUT, C2OUT
// Constants used in setup_comparators() are:
#define A0_A3_A1_A3 0xfff04
#define A0_A3_A1_A2_OUT_ON_A4_A5 0xfcf03
#define A0_A3_A1_A3_OUT_ON_A4_A5 0xbcf05
#define NC_NC_NC_NC 0x0ff07
#define A0_A3_A1_A2 0xfff02
#define A0_A3_NC_NC_OUT_ON_A4 0x9ef01
#define A0_VR_A1_VR 0x3ff06
#define A3_VR_A2_VR 0xcff0e

#bit C1OUT = 0x9c.6
#bit C2OUT = 0x9c.7

Hope this helps...SAR
___________________________
This message was ported from CCS's old forum
Original Post ID: 13921
Nick Foster
Guest







Re: 16F876A crash on serial comms
PostPosted: Wed Apr 23, 2003 7:30 am     Reply with quote

Well, it still doesn't explain the crash I'm seeing, but it does make me think that if the headers were updated for the 876A, maybe the compiler was, too. Guess this gives me an excuse to update my ancient version.

Thanks for all your help.

--nick

:=Nick
:=These are the added defines for the 16F876A;
:=
:=//////// Fuses: LP,XT,HS,RC,NOWDT,WDT,NOPUT,PUT,PROTECT,DEBUG,NODEBUG
:=//////// Fuses: NOPROTECT,NOBROWNOUT,BROWNOUT,LVP,NOLVP,CPD,NOCPD,WRT,NOWRT
:=////////
:=
:=////////////////////////////////////////////////////////////////// COMP
:=// Comparator Variables: C1OUT, C2OUT
:=// Constants used in setup_comparators() are:
:=#define A0_A3_A1_A3 0xfff04
:=#define A0_A3_A1_A2_OUT_ON_A4_A5 0xfcf03
:=#define A0_A3_A1_A3_OUT_ON_A4_A5 0xbcf05
:=#define NC_NC_NC_NC 0x0ff07
:=#define A0_A3_A1_A2 0xfff02
:=#define A0_A3_NC_NC_OUT_ON_A4 0x9ef01
:=#define A0_VR_A1_VR 0x3ff06
:=#define A3_VR_A2_VR 0xcff0e
:=
:=#bit C1OUT = 0x9c.6
:=#bit C2OUT = 0x9c.7
:=
:=Hope this helps...SAR
___________________________
This message was ported from CCS's old forum
Original Post ID: 13941
Nick Foster
Guest







Re: 16F876A crash on serial comms
PostPosted: Wed Apr 23, 2003 10:50 am     Reply with quote

I found the solution: it's in the bootloader, after all, or to be precise, in the compiler libraries. The 16F876A differs from the 16F876 in that it writes to FLASH in four-byte words rather than in single bytes. So, the bootloader, using the CCS libraries, never writes bytes that are in words that aren't four bytes long; I'm missing bytes at the end of every page. Thus, without the trailing GOTO at the end of my functions, the program is running headlong through NOPs and ADDLWs until it hits something, which in the case of my setup, is usually the bootloader, whose first bytes are GOTO program start: it just resets over and over again.

So, the solution is what I thought before -- get a new compiler. =)

Thanks again for all your help.

--nick
___________________________
This message was ported from CCS's old forum
Original Post ID: 13946
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