[SOLVED] Using USB and UART simultaniously on PIC18F4550

Status
Not open for further replies.

The DarK EvIL

Newbie level 3
Joined
May 2, 2011
Messages
4
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Activity points
1,305
Hi everybody!!
I'm using the PIC 18F4550 to establish communication between a GSM module and a PC.
The USB communication works perfectly, however I can't get the Serial communication to work.
I'm using a 8MHz cristal. I can't send data through the Tx pin but I can't get any data back.
I noticed that an interruption occurs when a data is received but the data is not correct.
here is my code:

Code:
char uart_rd ='0';
unsigned char Read_buffer[64];
unsigned char Write_buffer[64];


void interrupt()
{
   HID_InterruptProc();                   // Keep alive
   TMR0L = 100;                           // Reload TMR0L
   INTCON.TMR0IF = 0;                     // Re-enable TMR0 interrupts
   portb =~ portb;
}

void main() {

   // Disable all interrupts
   // Disable GIE, PEIE, TMR0IE, INT0IE,RBIE
   INTCON=0;
   INTCON2=0xF5;
   INTCON3=0xC0;
   // Disable Priority Levels on interrupts
   RCON.IPEN=0;
   PIE1=0;
   PIE2=0;
   PIR1=0;

   // Timer 0
   T0CON  = 0x47;                         // Prescaler = 256
   TMR0L  = 100;                          // Timer count is 256-156 = 100
   INTCON.TMR0IE = 1;                     // Enable T0IE
   T0CON.TMR0ON = 1;                      // Turn Timer 0 ON
   INTCON = 0xE0;                         // Enable interrupts
   
   ADCON1 = 0xFF;                         // Set PORTB to digital I/O
   TRISB = 0;                             // Set PORTB to outputs
   PORTB = 0;                             // PORTB all 0s to start with
   
   // Enable USB port
   Hid_Enable(&Read_buffer, &Write_buffer);
   Delay_ms(1000);
   Delay_ms(1000);
   strcpy(Write_buffer,"hi\r\n");
   delay_ms(100);
   UART_Init(9600);
   Delay_ms(100);
   while(1)
   {
        if(UART_Data_ready())
        {
              uart_rd = UART1_Read();
              UART1_Write(uart_rd);
        }
   }
}

I also tried the Soft_UART fucntion but it doesn't work either.

Please can someone help me??
Thx.
 

Hi,

You need to set your oscillator to make sure it is oscillating at 8 MHz. It might be too low for a particular baud rate.
 

Hi,
Thx for your reply
Actually the 8MHz is an external oscillator, however my MCU runs under 48 MHz (96/2) which make it easily support the 9600 baud rate.
At least that is what I understood.
 

have you tried without the usb?
 

have you tried without the usb?

Yes I did and it worked perfectly.
I'm wondering if there is a problem with the USB interruption or the configuration that is just after the main !!!
 

Hi john blue
thx for your help!!
I figured out that there where some missing parametre in the initialization of the UART
Here a working code
Code:
char uart_rd = '0';
unsigned char Read_buffer[64];
unsigned char Write_buffer[64];
unsigned char num,i;


void interrupt()
{
   if(UART1_Data_Ready())
   {
      uart_rd = UART1_Read();
   }
   HID_InterruptProc();                   // Keep alive
   TMR0L = 100;                           // Reload TMR0L
   INTCON.TMR0IF = 0;                     // Re-enable TMR0 interrupts
   //portb =~ portb;
}

void main() {

   // Disable all interrupts
   // Disable GIE, PEIE, TMR0IE, INT0IE,RBIE
   INTCON=0;
   INTCON2=0xF5;
   INTCON3=0xC0;

   // Timer 0
   T0CON  = 0x47;                         // Prescaler = 256
   TMR0L  = 100;                          // Timer count is 256-156 = 100
   INTCON.TMR0IE = 1;                     // Enable T0IE
   T0CON.TMR0ON = 1;                      // Turn Timer 0 ON
   INTCON = 0xE0;                         // Enable interrupts
   
   ADCON1 = 0xFF;                         // Set PORTB to digital I/O
   TRISB = 0;                             // Set PORTB to outputs
   PORTB = 0;                             // PORTB all 0s to start with
   
   // Enable USB port
   Hid_Enable(&Read_buffer, &Write_buffer);
   Delay_ms(1000);
   Delay_ms(1000);
   strcpy(Write_buffer,"salut les zouzou\r\n");
   HID_Write(&Write_buffer,64);

   UART1_INIT(19200);
   delay_ms(100);
   ADCON1 = 0x0F;
   RCSTA.SPEN = 1;
   RCSTA.RX9 = 0;
   RCSTA.CREN = 1;
   RCSTA = 0x90;
   TRISC.TRISC6 = 0;
   TRISC.TRISC7 = 1;
   INTCON.PEIE = 1;
   INTCON.GIE = 1;
   PIE1.RCIE=1;
   while(1)
   {
       UART1_Write('a');
       UART1_Write(uart_rd);
       delay_ms(500);
   }
}
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…