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

PIC16F628A no interrupts when serial data received

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



Joined: 08 Sep 2014
Posts: 20

View user's profile Send private message

PIC16F628A no interrupts when serial data received
PostPosted: Mon Nov 10, 2014 9:52 am     Reply with quote

Hello to all.

I am new to electronics, I have came from developing software for computers, but now I am developing software for PICS, and I'm a lot of noob this.

I have a 16F628A and also I am using Proteus ISIS to simulate the pic.

I want to interrupt the program when a serial message has been received.
I found the int_rda, so I used it but I don't receive anything in the pic.

Here is my code if you could help me

Code:
#include <16F628A.h>

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O

#use delay(internal=4MHz)
#use rs232 (UART1, baud=1200,bits=8,ERRORS)


char lastc = 0;
int recvi = 0;
char recv[16] = "";

#int_RDA
void RDA_isr()
{
   lastc = getc();
   puts(lastc);
   recvi++;
   if (recvi > 16) {
      puts(recv);
      recv = "";
      recv[recvi] = lastc;
      lastc = 0;
   } else {
      recv[recvi] = lastc;
      lastc = 0;
   }
}
   

void main()
{
   enable_interrupts(GLOBAL);
   while(TRUE)
   {      //TODO: User Code
   }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19492

View user's profile Send private message

PostPosted: Mon Nov 10, 2014 10:07 am     Reply with quote

You are not enabling the interrupt......

enable_interrupts(GLOBAL);

allows all enabled interrupts to work.

enable_interrupt(INT_RDA);

enables the receive interrupt.

You need both lines.

However there is a severe problem with the code in the interrupt.

A received character, is _not_ a string....

To send it you need to use putc, which sends a character.

But then there is the second problem. recv, can accept indexes from 0 to 15. At 16, you will have overwritten the next variable in memory....

Then there is the issue of time. It takes as long to send a character as it may take for one to arrive. When the counter gets to 16 (wrong), it'll try to send seventeen characters, and anything else in memory beyond this point that is not zero (a string _requires_ a 0 terminator) meanwhile anything arriving will be lost.

Then you try to 'clear' a character buffer by writing directly to the pointer. This is not how C works, strcpy is needed to do this.

The routine is fundamentally flawed. Move the print of anything but the character received, into the main code, not the interrupt.
ercarlitosg



Joined: 08 Sep 2014
Posts: 20

View user's profile Send private message

PostPosted: Mon Nov 10, 2014 12:21 pm     Reply with quote

Ttelmah wrote:
You are not enabling the interrupt......

enable_interrupts(GLOBAL);

allows all enabled interrupts to work.

enable_interrupt(INT_RDA);

enables the receive interrupt.

You need both lines.

However there is a severe problem with the code in the interrupt.

A received character, is _not_ a string....

To send it you need to use putc, which sends a character.

But then there is the second problem. recv, can accept indexes from 0 to 15. At 16, you will have overwritten the next variable in memory....

Then there is the issue of time. It takes as long to send a character as it may take for one to arrive. When the counter gets to 16 (wrong), it'll try to send seventeen characters, and anything else in memory beyond this point that is not zero (a string _requires_ a 0 terminator) meanwhile anything arriving will be lost.

Then you try to 'clear' a character buffer by writing directly to the pointer. This is not how C works, strcpy is needed to do this.

The routine is fundamentally flawed. Move the print of anything but the character received, into the main code, not the interrupt.


Sorry, I have said I'm very noob to C and I make some mistakes because of Java Embarassed . I wil fix it soon and tell you how it's working.

Thank you a lot
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