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

serial rs232 reading problem

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







serial rs232 reading problem
PostPosted: Thu Feb 19, 2009 5:47 pm     Reply with quote

Hi to all!

I am programming a pic18f2550, and all works correct by now, except that I can not read any data from the serial. I tried hardware and software rs232, different baud rates, (errors, and no erros), different clock speeds and oscilators... but nothing worked till now.

I paste my last sample code (that keeps printing letters 'a'):

Code:

#int_TIMER0
void  TIMER0_isr(void)
{
   output_toggle(PIN_A0);
}

#int_TIMER1
void  TIMER1_isr(void)
{

}



void main()
{
   char c = 'f';

   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_TIMER0);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ|OSC_NORMAL|OSC_31250|OSC_PLL_OFF);

   // TODO: USER CODE!!
   while(true)
   {
      putc(c);
      printf(" test\r\n");
      while(!kbhit())
      {
         delay_ms(500);
         putc('a');
      }
      c = getc();
      printf("char: ");
      putc(c);
   }
}


Code:

#include <18F2550.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV20                   //Brownout reset at 2.0V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES STVREN                   //Stack full/underflow will cause reset
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT                    //Program memory not write protected
#FUSES NOWRTD                   //Data EEPROM not write protected
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES PBADEN                   //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOEBTR                   //Memory not protected from table reads
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOMCLR                   //Master Clear pin used for I/O
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL5                     //Divide By 5(20MHz oscillator input)
#FUSES CPUDIV1                  //No System Clock Postscaler
#FUSES USBDIV                   //USB clock source comes from PLL divide by 2
#FUSES VREGEN                   //USB voltage regulator enabled

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


I tried getc() with and without kbhit() ,but the result is the same :(

I hope you can help me!!! ;)
(the ccs version is 4.084)

thanks a lot!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 5:59 pm     Reply with quote

Quote:
void main()
{
char c = 'f';

setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

Delete the line shown in bold. On this PIC, the UART's Rx signal and the
SPI SDO signal are on the same pin. The line in bold will enable the SPI
module and set the SDO pin as an output. This is the opposite of what
is required for the Rx pin to be operational.

The truth is that all the Wizard lines above can be deleted. They don't
add anything to the program. All those modules are disabled upon
power-on reset anyway.
elmo23!
Guest







PostPosted: Thu Feb 19, 2009 6:47 pm     Reply with quote

Thanks a lot for the fast answer, unfortunately that didn't work.

I deleted that line and the behaviour is the same. I suppose there must be something else wrong because if I use a soft the same is happening...
Code:

#use rs232(baud=57600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8,ERRORS)

All the time letter 'a' is printed and the led in PIN_A0 is blinking (at the right speed). Later I also erased other code generated by the project wizard, without any success.

(I think there are no hardware errors because I connected a gps device I have to the pins of the socket, and it is able to send and receive data, I also use another new pic18f2550, in case this was damaged).

thank you Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 6:52 pm     Reply with quote

Create a very simple loop that echoes characters back to the PC.
See if that works.
Code:

while(true)
  {
   c = getc();
   putc(c);
  }
elmo23!
Guest







PostPosted: Thu Feb 19, 2009 7:09 pm     Reply with quote

Unfortunately with that simple code (soft and hardware rs232, and different speeds) is not working (sending always worked).

Any new ideas? I will revise everything tomorrow.

thanks a lot for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 19, 2009 7:22 pm     Reply with quote

If this program doesn't work, when typing characters in a serial terminal
window on your PC, then I'd say there is something wrong with your
hardware.
Code:
#include <18F2550.h>
#fuses HS, NOWDT, PUT, NOBROWNOUT, NOLVP
#use delay(clock=20000000)
#use rs232(baud=57600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

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

while(1)
  {
   c = getc(); // Get char from PC
   putc(c);   // Send it back to the PC
  }

}
Guest








PostPosted: Sat Feb 21, 2009 7:35 am     Reply with quote

Hi,

I hope you can help me. I used the echo routine you posted and I am able to echo character keypresses. The problem is that the characters echoed is not character that was pressed (mostly 0XFAs).

This is on a 16F690 using the internal oscillator. The exact code I am using is as follows:
Code:

#include <16F690.h>
#fuses HS, NOWDT, PUT, NOBROWNOUT,INTRC_IO
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, parity=N, rcv=PIN_C7, ERRORS)

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

setup_oscillator(OSC_4MHZ); /* tried this with and without */

while(1)
  {
   c = getc(); // Get char from PC
   putc(c);   // Send it back to the PC
  }

}


I suspect that this is a clock/timing issue but I am at a loss what to do next.

TIA
agimat



Joined: 09 Feb 2009
Posts: 1

View user's profile Send private message Visit poster's website

Solved
PostPosted: Sat Feb 21, 2009 1:31 pm     Reply with quote

Replaced
#use delay(clock=20000000)

with
#use delay(clock=8000000)


I was using the 16F690's internal clock at 8Mhz.

That fixed it.
_________________
http://rlachenal.com
Ttelmah
Guest







PostPosted: Sat Feb 21, 2009 3:01 pm     Reply with quote

Also, as a 'comment', get rid of the HS fuse at the start of the fuse line.
You can't select both the HS (high speed crystal) oscillator, and the internal RC oscillator. On some chips, selecting multiple setups like this, will stop it working at all, it is just 'luck' that it is working.

Best Wishes
obarcia



Joined: 14 May 2009
Posts: 1

View user's profile Send private message Send e-mail

serial rs232 reading problem (problema resuelto)
PostPosted: Thu May 14, 2009 4:06 pm     Reply with quote

Esta es una solución que alguna vez utilice en la serie de PIC 16Fxxx, leyendo los comentarios de todos ustedes compañeros, he llegado hacer funcionar el PIC18F2550, también tuve problema con la RX (el usart y SPI ) están sobre el mismo pin, borrando esa línea de código setup_spi(SPI_SS_DISABLED) funcionó (PCM programmer), gracias a todos ustedes porque también me sirvió la ayuda de todos ustedes.

Code:
#include <18F2550.h>
#device adc=8
#use delay(clock=20000000)
#use rs232(baud=57600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

//------------------------- Variables Globales -------------------------\\
char C='f';

//---------------------------INTERRUPCIONES-----------------------------\\
#int_RDA
void serial_isr() {                    // Interrupción recepción serie USART
   C=0x00;                       // Inicializo caracter recibido
   if(kbhit()){                  // Si hay algo pendiente de recibir ...
      C=getc();                  // lo descargo y ...
   }
   printf("char: ");
   putc(c);
   printf("\r\n");
}

//----------------------PROGRAMA PRINCIPAL-------------------------------\\
void main()
{
   enable_interrupts(INT_RDA);
   enable_interrupts(global);
   
   // TODO: USER CODE!!
   printf("Test: ");
   putc(c);
   printf("\r\n");
   while(true)
   {
   }
}


Solo lo he simulado para este pic18f2550, pero en la serie 16Fxxx si lo he cargado al pic y ha funcionado

obarcia
MONTECRISTI-MANABI-ECUADOR
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