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

#INT_RDA and PIC16F87
Goto page 1, 2  Next
 
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 Apr 06, 2006 10:24 pm     Reply with quote

If you don't PCW or PCWH, then you don't have the Device Editor
and you don't have the possibility of fixing it by editing the device file.

If so, then here is a demo program that shows how to make your own
putc() and get() routines if the CCS library code doesn't work.

To use this code, you enter the crystal frequency and the UART baud
rate in the #define statements below. At the start of main(), you call
the init_uart() function. Then use my_putc() to send a character and
my_getc() to receive a character. You can use printf() if you use the
re-direction feature to send output to the my_putc() routine.
The demo program below shows how to do that.
Code:

#include <16F87.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP

#define FREQ_OSC 10000000
#define BAUD_RATE 4800

#use delay(clock = FREQ_OSC)


#byte PIR1  = 0x0c
#byte SPBRG = 0x99
#byte RCSTA = 0x18
#byte TXREG = 0x19
#byte RCREG = 0x1A
#byte TXSTA = 0x98

#bit TXIF = PIR1.4
#bit RCIF = PIR1.5
#bit CREN = RCSTA.4

//------------------------
// Wait for the hardware UART's transmitter
// to become ready and then send the specified
// character.

void my_putc(char c)
{
while(!TXIF);

TXREG = c;
}

//------------------------
// Wait for character to be available from the
// hardware UART's receiver and then return it.
// If there is an overrun error, then clear it.

char my_getc(void)
{
int temp;
int retval;
   
while(!RCIF);

temp = RCSTA;
retval = RCREG;

if(bit_test(temp, 1))
  {
   CREN = 0;
   CREN = 1;
  }
 
return(retval);
}


//-------------------------
void init_uart(void)
{
SPBRG = (FREQ_OSC / (BAUD_RATE * 16)) -1;
TXSTA = 0x26;
RCSTA = 0x90;
}

//======================================
void main()
{
char c;

init_uart();

printf(my_putc, "Hello World\n\r");

while(1)
  {
   c = my_getc(); // Get a char and echo it back
   my_putc(c);
  }
}



If you want to test receiver interrupts, then substitute
the following code for the bottom part of the program.

Code:
#int_rda
void rda_isr(void)
{
char c;

c = my_getc();
my_putc(c);
}

//================================
void main()
{

init_uart();

printf(my_putc, "Hello World\n\r");

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

while(1);
}
PICman



Joined: 02 Nov 2007
Posts: 26

View user's profile Send private message

PostPosted: Fri Jul 31, 2009 2:29 pm     Reply with quote

Thank you VERY MUCH PCM programmer !!!

I had a real problem trying to activate the silicon UART on a PIC16F88.

I started to read here and there and, - BOY - , I was discouraged to see - NOT AGAIN - a version issue !!!!

But I finally found your "manual override" code (INIT_UART, MY_GETC and MY_PUTC) and BINGO ! Everything worked perfectly !!!!

Again thank you !
_________________
The ideal electronic world: A place where the words VERSION and REVISION do NOT exist !
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 3:21 am     Reply with quote

PICman wrote:
Thank you VERY MUCH PCM programmer !!!

I had a real problem trying to activate the silicon UART on a PIC16F88.

I started to read here and there and, - BOY - , I was discouraged to see - NOT AGAIN - a version issue !!!!

But I finally found your "manual override" code (INIT_UART, MY_GETC and MY_PUTC) and BINGO ! Everything worked perfectly !!!!

Again thank you !


I used to pic 16f886 that my problem i can not use same time INT_RDA and INT_RB, INT_TIMER1. INT_RDA works alone and INT_RB, INT_TIMER1 works alone as well. But when i would want to use all of them same time it does not works. What can be source of problem.
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 3:31 am     Reply with quote

You are spending too long in one or more of the interrupt handlers.

Remember that things like timer interrupts, INT_RDA, etc., reflect hardware events happening at intervals. You need to always handle the event, and get out _fast_, then do any processing in the main code, so that other events can be handled. In the example code here, simply the act of sending the byte 'out' again on a software UART, _will_ take as long as it takes for another character to arrive, so if the GPS sends reasonably fast, there will never be time to do anything else.....

Best Wishes
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 5:38 am     Reply with quote

Ttelmah wrote:
You are spending too long in one or more of the interrupt handlers.

Remember that things like timer interrupts, INT_RDA, etc., reflect hardware events happening at intervals. You need to always handle the event, and get out _fast_, then do any processing in the main code, so that other events can be handled. In the example code here, simply the act of sending the byte 'out' again on a software UART, _will_ take as long as it takes for another character to arrive, so if the GPS sends reasonably fast, there will never be time to do anything else.....

Best Wishes


Could you please check my code and give me idea.
http://www.ccsinfo.com/forum/viewtopic.php?t=43833
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 5:45 am     Reply with quote

No, because the problem will be in one (or more) of the interrupt handlers, which you don't show.

Best Wishes
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 6:04 am     Reply with quote

Ttelmah wrote:
No, because the problem will be in one (or more) of the interrupt handlers, which you don't show.

Best Wishes
Code:


#include "16f886.h"
#device *=16 ADC=10
#fuses INTRC_IO,NOWDT,NOPROTECT,NOPUT,NOLVP,NOFCMEN,NOMCLR, BROWNOUT // with internal oscillator

#use delay(clock=4000000)
#use rs232(baud=9600,uart1, parity=N, xmit=PIN_C6,rcv=PIN_C7, bits=8, errors, timeout=500, STREAM=ENCODER)

#byte PIE1      = 0x8c
#byte INTCON    = 0x8b


//BYTE data[15]={0};
BYTE i = 0;
#INT_RDA
void RS232(void)
{

data[i] = getc();

}

#INT_EXT
void ext_isr()
{
   if (!input(PIN_B0) )
   {
      // Interrupts are checked 6ms after to filter noise
      delay_6ms();
         //
   }   
}

#INT_TIMER1
void timer1_isr()
{
     bit_set(int_events, event_int_timer1);
}

void main()
{
   short bResult;
   
   disable_interrupts(GLOBAL);   
   
   output_a(0b00000000); // motor pinleri A2 ve A3 1 olacak
   output_b(0b00000001); // RB0(CRD_CON), RB4(ortak uç), RB5(switch), RB6(Manuel kilit), RB7(Kapı açık kapalı)
   output_c(0b10011011); // RC7 (RX input), RC6 (TX output), RC3(SCL), RC4(SDA),  RC5(motor pin output ), RC2 Buzzer, RCO ve RC1 saat içininput
   output_e(0x00);
   
   set_tris_a(0b00000000);
   set_tris_b(0b00000001); // RB0(CRD_CON), RB4(ortak uç), RB5(switch), RB6(Manuel kilit), RB7(Kapı açık kapalı), RC2 Buzzer,
   set_tris_c(0b10011011); // RC7 (RX input), RC6 (TX output), RC3(SCL), RC4(SDA),  RC5(motor pin output ), RCO ve RC1 saat içininput
   set_tris_e(0x00);

   
   port_b_pullups(TRUE);

   setup_adc(ADC_CLOCK_INTERNAL);   
   setup_adc_ports( sAN0|VSS_VDD );
   set_adc_channel(1);   
   
   
   setup_spi(FALSE);
   
   setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_1);   // setup interrupts
   setup_timer_2(T2_DISABLED,0,1);
   
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   
   setup_oscillator(osc_4mhz);
     
   last_port_b = input_b();
   bNotSleep = 1;
   
   // Enable interrupts
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(INT_RB);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_EXT);

     
   
   SET_TIMER1(0);
#ignore_warnings 203
   while(TRUE)
   {
      bNotSleep = 0;
#ignore_warnings none
      sleep();
#asm
      nop      
#endasm
}
[quote]
Could you please rewiew and give me ide how can handle time on this code.[/quote]

_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Mon Nov 08, 2010 7:32 am     Reply with quote

So, where are the other handlers?.
You must _never_ enable an interrupt without a handler for that interrupt present. INT_RB, and INT_RDA in particular, _must_ perform the operations to reset the hardware event that triggered them (read port B, and fetch a character from the UART), or they will trigger _for ever_, and lock the processor.
What you have poster _will_ hang.

Best Wishes
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 7:59 am     Reply with quote

Ttelmah wrote:
So, where are the other handlers?.
You must _never_ enable an interrupt without a handler for that interrupt present. INT_RB, and INT_RDA in particular, _must_ perform the operations to reset the hardware event that triggered them (read port B, and fetch a character from the UART), or they will trigger _for ever_, and lock the processor.
What you have poster _will_ hang.

Best Wishes


What do you mean? I can understand. Could you please explanin with example?
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 8:38 am     Reply with quote

Your latest code has an #INT_RDA handler, but is missing #INT_RB, which must be expected to cause your application to hang. The #INT_RDA is performing getc(), as required, but it most likely should also increment the buffer pointer i (and do additional things that you'll need for a buffered receive operation).
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 1:26 pm     Reply with quote

FvM wrote:
Your latest code has an #INT_RDA handler, but is missing #INT_RB, which must be expected to cause your application to hang. The #INT_RDA is performing getc(), as required, but it most likely should also increment the buffer pointer i (and do additional things that you'll need for a buffered receive operation).

Code:


#include "16f886.h"
#device *=16 ADC=10

#fuses INTRC_IO,NOWDT,NOPROTECT
#use delay(clock=8000000)
#use rs232(baud=9600, parity=N, xmit=PIN_C6,rcv=PIN_C7, bits=8)

#include "main.h"

#include "bcd.c"
#include "DateTime.c"
#include "Events.c"


#define event_int_timer1         1


BYTE i = 0;
unsigned char eeprom_buffer[32];      // Used for local storage data (EEPROM)
char       last_port_b;
long timeout;
unsigned char   _ServiceType;

#int_RB
void  RB_isr(void)
{
   char port_b;
   char changes;

   port_b = input_b();
   changes = last_port_b ^ port_b;
   last_port_b = port_b;

//********************************************************************************************************//
   
   if ( bit_test(changes,7) && !bit_test(last_port_b,7) )   // High to Low on B7
   {
      if ( !input(PIN_B7) ){
         // do something
      }
       return;
   }
}

#int_RDA
void  RDA_isr(void)
{
   while(!kbhit()&&(++timeout<50000)) // 1/2 second
    delay_us(10);
    if(kbhit()){ 
      eeprom_buffer[i] = getc();
       if ( eeprom_buffer[i] == 0x13 ){
         _ServiceType = eeprom_buffer[1];   
      }   
       i++;
    }else{
       memset(eeprom_buffer, 0x00, sizeof(eeprom_buffer));   
      _ServiceType = 0x00;
      i = 0;
   }

}

#INT_EXT
void ext_isr()
{
   if (!input(PIN_B0) )
   {
      // do something   
   }   
}

#INT_TIMER1
void timer1_isr()
{
   bit_set(int_events, event_int_timer1);
}


#define RED_LED         PIN_A7
#define BLUE_LED        PIN_A6
#define GREEN_LED       PIN_A5

void BlinkRed(int16 nStop ){
   output_high(RED_LED);                     
   delay_ms(nStop);         
   output_low(RED_LED);
}

void BlinkBlue(){
   output_high(BLUE_LED);                     
   delay_ms(1000);         
   output_low(BLUE_LED);
}

void BlinkGreen(int16 nStop){
   output_high(GREEN_LED);                     
   delay_ms(nStop);         
   output_low(GREEN_LED);
}


void main()
{

     disable_interrupts(GLOBAL);   
   
   output_a(0b00000000);
   output_b(0b00000001);
   output_c(0b10011011);
   output_e(0x00);
   
   set_tris_a(0b00000000);
   set_tris_b(0b00000001);
   set_tris_c(0b10011011);
   set_tris_e(0x00);

   port_b_pullups(TRUE);

   setup_adc(ADC_CLOCK_INTERNAL);   
   setup_adc_ports( sAN0|VSS_VDD );
   set_adc_channel(1);
   
   setup_spi(FALSE);
   
   setup_timer_1(T1_EXTERNAL|T1_CLK_OUT|T1_DIV_BY_1);   // setup interrupts
   setup_timer_2(T2_DISABLED,0,1);
   
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   
   ext_int_edge(0, H_TO_L); // sinyalin dü?en kenarynda kesme olursa interrup olsun (High to Low dü?en kenarda interrup olursa active ol)
   
   setup_oscillator(osc_8mhz);
      
   last_port_b = input_b();
   
   SET_TIMER1(0); // adjust the real time clock

    enable_interrupts(INT_TIMER1);
    enable_interrupts(INT_RB);
    enable_interrupts(INT_EXT);
    enable_interrupts(INT_RDA);
    enable_interrupts(INT_TBE);   
    enable_interrupts(GLOBAL);   
   
    i = 0; // Serail data counter. It was defined at the rs232.
     
   BlinkGreen(1000);

#ignore_warnings 203
   while(TRUE)
   {
      sleep();
   }
}


I have rewrite my acode acording to your insruction, however again it could not enter INT_RB, INT_TIMER1, INT_EXT.

You can see that when i enable all of interrupts it could not came BlinkGreen(1000);

If I disable enable_interrupts(INT_TBE); this code those interrupts (
INT_RB, INT_TIMER1, INT_EXT) are working.

I have changed according to your ex comment add SAMPLE_EARLY but did not change anything.

What can be source of problem ?
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 1:56 pm     Reply with quote

First, get rid of most of your RDA code.
1) You do not need to test if a character is available, if the interrupt occurs, one _is_.
2) Do _not_ write the the EEPROM in the interrupt. The EEPROM write, takes typically 4mSec. Characters can arrive at just over 1mSec intervals. Delaying in the interrupt for an EEPROM write _will_ overflow the UART. Repeat fifty times, the mantra - _KEEP INTERRUPT HANDLERS FAST, UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING_ - You don't...
3) _Always_ have ERRORS in the UART declaration for he hardware UART. The _only_ occasion not to have this, is if _you_ are handling UART errors. Without this, if the UART overflows, the chip _will_ hang.

Best Wishes
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Sat Nov 13, 2010 2:48 pm     Reply with quote

Ttelmah wrote:
First, get rid of most of your RDA code.
1) You do not need to test if a character is available, if the interrupt occurs, one _is_.
2) Do _not_ write the the EEPROM in the interrupt. The EEPROM write, takes typically 4mSec. Characters can arrive at just over 1mSec intervals. Delaying in the interrupt for an EEPROM write _will_ overflow the UART. Repeat fifty times, the mantra - _KEEP INTERRUPT HANDLERS FAST, UNLESS YOU KNOW EXACTLY WHAT YOU ARE DOING_ - You don't...
3) _Always_ have ERRORS in the UART declaration for he hardware UART. The _only_ occasion not to have this, is if _you_ are handling UART errors. Without this, if the UART overflows, the chip _will_ hang.

Best Wishes

Code:

Thanks for your comment.

#use rs232(baud=9600, parity=N, xmit=PIN_C6,rcv=PIN_C7, bits=8, ERRORS)


#int_RDA
void  RDA_isr(void)
{
      buffer[i] = getc();
       if ( buffer[i] == 0x13 ){
         _ServiceType = buffer[1];   
      }   
}


I changed my code according to your comment. But did not change anything. If you check my code can comment anything about WHY BlinkGreen(1000) is not working. When i enable INT_TBE it is not going to forward. 16886 Interrupt system does not support FAST, HIGH. After INT_RDA complete it is not going to in the sleep().
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Nov 14, 2010 3:43 am     Reply with quote

I notice that you're applying arbitrary changes to your code, fixing some bugs and add new at other places, one step forward and two steps back.
Previously you had int_rb enabled without an #int_rb handler. Now you have added int_tbe, but are missing the #int_tbe handler. What is next?

It's effectively impossible to help in this situation.
muratmaman



Joined: 30 Jan 2010
Posts: 19

View user's profile Send private message

PostPosted: Sun Nov 14, 2010 4:15 am     Reply with quote

FvM wrote:
I notice that you're applying arbitrary changes to your code, fixing some bugs and add new at other places, one step forward and two steps back.
Previously you had int_rb enabled without an #int_rb handler. Now you have added int_tbe, but are missing the #int_tbe handler. What is next?

It's effectively impossible to help in this situation.


Thanks for your comment. I would want to say that if i did not put #int_tbe and did not enable it was not working INT_RBA interrupt.

At the present i deleted the #int_tbe however does not change anything. In this situation INT_RDA interrupt does not work. If you can send me very simple program with INT_RDA, INT_RB, INT_TIMER1, INT_EXT. I will appreciate.
_________________
I have been developing pic and other microcontroller. I would want to share and get experiment form this site
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 1, 2  Next
Page 1 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