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 support@ccsinfo.com

RS232 Problem

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



Joined: 16 Mar 2009
Posts: 29

View user's profile Send private message

RS232 Problem
PostPosted: Wed Jan 13, 2010 6:34 am     Reply with quote

Hi. I have a 16f877 connected to a lcd and a rs232 xbee

Code:

#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES PUT                      //Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

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


Code:

#include "C:\pic\xbee_test\xbee_lcd_877\main.h"
#include <flex_lcd420.c>

#int_RDA
RDA_isr()
{
   disable_interrupts(INT_RDA);   
   
   if(kbhit())
   {
      lcd_putc(getc());
   }
   
   enable_interrupts(INT_RDA);
}

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   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);
   setup_vref(FALSE);

   lcd_init();
   delay_ms(400);
   
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   
   printf(lcd_putc, "\fStarted");
   
   while(1)
   {   
      delay_ms(10);
     
   }
   
}


I see "Started" on lcd, but lcd_putc(getc()) don't work. I'm sure data arrives, because if I connect it to a pc I can see the data.

Help please
Thanks[/code]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jan 13, 2010 12:41 pm     Reply with quote

Quote:
#int_RDA
RDA_isr()
{
disable_interrupts(INT_RDA);

if(kbhit())
{
lcd_putc(getc());
}

enable_interrupts(INT_RDA);
}

The lines shown in bold are not necessary and should be deleted.
If you're in the RDA isr, you automatically have a "kbhit". You don't
need to check for it. Global interrupts are automatically disabled
while inside the isr, so you don't need to disable/enable RDA interrupts.
Also, there are no nested interrupts.

Quote:
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
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);
setup_vref(FALSE);

None of this Wizard code is needed. All these modules are disabled
upon power-on reset. The CCS start-up code sets Port A to be all
digitial. The setup_spi() statement, as shown, will actually enable
the SPI module. In some PICs that share the SDO pin with the UART
Rx pin, this will stop the UART from receiving chars. (However, the
16F877A doesn't share a pin for Rx and SDO, so it's OK).
Delete all these lines.

Quote:

delay_ms(400);

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

Here you have a large initial 400ms delay before interrupts are enabled.
It's possible that characters are coming into the UART during this time,
but they cannot be handled for 400 ms. If more than 2 characters come
in, this will exceed the capacity of the UART's hardware receive fifo.
This will cause the UART receiver to lock-up, and it will stay locked up.
You won't be able to receive any characters.

To fix this problem, add the ERRORS parameter to your #use rs232()
statement, as shown below. This will cause the compiler to add code
to check if a UART receiver overrun error has occurred, and if so, then
it will clear the error. This will prevent lock-ups. You will still lose the
characters that caused the overrun, but at least future characters can
be received.
Quote:
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,ERRORS)
adalucio



Joined: 16 Mar 2009
Posts: 29

View user's profile Send private message

PostPosted: Thu Jan 14, 2010 3:23 am     Reply with quote

WOW THANKS. now I'm your fan

if I wanted to deepen my studies on CCS pic, what book should I buy?


Thanks again
Very Happy Very Happy
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jan 14, 2010 7:52 pm     Reply with quote

Here is a thread on books and other resources:
http://www.ccsinfo.com/forum/viewtopic.php?t=38632
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