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

UART problem with PIC18F4550

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







UART problem with PIC18F4550
PostPosted: Tue Aug 29, 2006 4:59 am     Reply with quote

Hello!

I'm trying to connect my pic with the computer but no characters appears. I see with a multimeter that it seems work well but when I try to see the send data with some program like "hyerterminal" I don't recieve nothing. That's my code:

Code:
#include <18F4550.h>
#device ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN

#use delay(clock=12000000)
#use fixed_io(c_outputs=PIN_C6)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

void main(void) {

   char respuesta;
   long conversion;

   //configuracion
   set_tris_a(0xFF);       // todo entradas
   set_tris_b(0xFF);       // todo entradas
   set_tris_c(0xFF);      // todo entradas
   set_tris_d(0x00);      // todo salidas
   set_tris_e(0xFF);      // todo entradas

   setup_comparator(NC_NC_NC_NC);

   while(true){
      
      printf("Es esto lo que esperabas (s,n)?");
      respuesta=getch();
      if (respuesta == 'a'){
         bit_set(PORTC,2);
      }else{
         bit_clear(PORTC,2);
      }
   }
}


Thank you all for your time.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Tue Aug 29, 2006 6:03 am     Reply with quote

My first guess is that your CPU timing is incorrect. The PIC18F4550 has clock frequency settings very differrent from the other PIC processors and it looks like your fuses are configured wrong.

I assume you are using a 12MHz crystal?
When reading the datasheet correctly fuse HSPLL means you are using the 96MHz PLL as a base frequency, then CPUDIV1 will divide it by 2, meaning you are running at 48MHz instead of 12MHz. Try changing HSPLL to HS instead.

Change
Code:
#use fixed_io(c_outputs=PIN_C6)
to
Code:
#use fixed_io(c_outputs=PIN_C6, PIN_C2)

Remove all lines setting the TRIS registers because they are not needed and conflict with the above portC setting.

Also add the ERRORS directive to the #use RS232 line to avoid the UART stalling on buffer overflows.
Code:
#use fixed_io(c_outputs=PIN_C6)
joviwap
Guest







PostPosted: Fri Sep 15, 2006 3:24 pm     Reply with quote

Thanks for your help!!

Now I have another problem, I can write but I can't read from the serial port.
I have tryed diferent codes but they don't work:

common parts:
Code:
#include <18F4550.h>
#device ICD=TRUE
#device ADC=10
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL3,CPUDIV1,VREGEN
#use delay(clock=48000000)
#use fixed_io(c_outputs=PIN_C6)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

void main(void) {

   char respuesta;

   setup_comparator(NC_NC_NC_NC);

Differences:
Code:

   while(true){
      
      delay_ms(2000);
      printf("\nQuieres que el led se encenda (s,n)?  ");

      do {
         respuesta=getch();
      } while(respuesta!='s'&& respuesta!='n');
      }
}

Code:

   while(true){
      
      delay_ms(2000);
      printf("\nQuieres que el led se encenda (s,n)?  ");

      if(kbhit())  {respuesta=getch();}
   }
}

Code:

   while(true){
      
      delay_ms(2000);
      printf("\nQuieres que el led se encenda (s,n)?  ");   
   
        if(kbhit()){
         respuesta=getc();
         printf("\nQuieres que el led se encenda (s,n)?  ");
         if (respuesta == 's'){
            bit_set(PORTC,2);
            printf("\nEl led se ha encendido.\n");
         }if(respuesta == 'n'){
            bit_clear(PORTC,2);
            printf("\nEl led se ha apagado.\n");
         }
      }
        }
}


Thank you all for your time.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Sep 15, 2006 3:38 pm     Reply with quote

Quote:
#use fixed_io(c_outputs=PIN_C6)

Get rid of the statement above. It isn't necessary. The compiler
will automatically set the correct TRIS for each pin, if you use CCS
i/o functions such as output_high() and output_low().

Quote:
do {
respuesta=getch();
} while(respuesta!='s'&& respuesta!='n');
}
What if the user types in capital letters ? Then your code won't work,
since you are only testing for lower case. Fix this by converting the
input characters to upper case with toupper(). Then change your
test code to check for upper case characters. See the changes shown
in bold below:
Quote:
do {
respuesta=getch();
respuesta=toupper(respuesta);
} while(respuesta != 'S' && respuesta != 'N');
}



Change your code to use the standard CCS pin i/o functions, as
shown below in bold:
Quote:
if (respuesta == 's'){
output_high(PIN_C2);
printf("\nEl led se ha encendido.\n");
}if(respuesta == 'n'){
output_low(PIN_C2);
printf("\nEl led se ha apagado.\n");
}
}
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Sep 15, 2006 5:11 pm     Reply with quote

In addition to all the above add the ERRORS directive to your #use RS232 line. In situations where the UART input buffer receives more than 2 characters before your program has time to read them, the UART will set an error flag and stops receiving. By adding the ERRORS directive you tell the compiler to clear the error flag on every getc() call.
joviwap
Guest







PostPosted: Sat Sep 16, 2006 8:58 am     Reply with quote

It is working, the probles was a bad conexion Smile

Thank you all for your anwers.
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