Problem with transmitting data

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Problem with transmitting data

Postby ignorances » Thu Aug 02, 2012 6:49 pm

Hi all,

I am currently doing a simple using serial communication between a PIC and PC.

Scenario:
From the PC, I sent a character (let's say character "A") to the PIC to turn on the LED2. I need to press the transmit button on the PC several times only I can to turn on the LED2.

I now place a 16x2 LCD to display all the data received by the PIC, and I get some funny characters appears first before the actual character.

IMAG0330.jpg
IMAG0330.jpg (55.75 KiB) Viewed 2397 times


Relevent Info:
Using SKds40A communicate (UART2) with UC00A
Standard Settings (9600, 8/None/1)
Using C#
Using the header and source code provided by Cytron


why this happen? any explaination?
ignorances
Greenhorn
 
Posts: 3
Joined: Thu Nov 03, 2011 4:01 pm

Re: Problem with transmitting data

Postby yonghui » Sun Aug 05, 2012 8:11 pm

probably u will need to post somemore information. mayb portion of code?
lack of information to see anything.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Problem with transmitting data

Postby ignorances » Sun Aug 05, 2012 9:28 pm

so sorry... here are the codes

dsPIC main:
CODE: SELECT_ALL_CODE
int main(void)
{   
   io_initialize();
   lcd_initialize();
   uart2_initialize();

   lcd_putstr("SW1 to Transmit");
   lcd_goto(0x40);
   lcd_putstr("SW2 to Receive");

   while(1)
   {
      if (SW1==0)             // Transmit data to the PC
      {
         lcd_clear();
         lcd_putstr("Transmit Mode");
         lcd_goto(0x40);
         uart2_putstr("\nqwerty");
         delay_ms(100);
      }

      else if (SW2==0)             // Receive data from the PC
      {
         lcd_clear();
         lcd_putstr("Receive Mode");
         lcd_goto(0x40);
         while(1)
         {
            unsigned char rxData;
            uart2_receive();
            delay_ms(200);
            rxData = U2RXREG;
            lcd_putchar(rxData);
         }
      }

      else
      {
         LED1=0;
         LED2=0;
      }
   }
}


dsPIC uart2.h
CODE: SELECT_ALL_CODE
/*******************************************************************************
*  UART2 Configurations                                                        *
********************************************************************************/
void uart2_initialize(void)
{
   // Set the Baud Rate.
   U2BRG = 129;                        // ((Fosc/4)/(16 * Baud rate)) - 1
                                    // 129 = 9600bps baudrate for 10MHz crystal with 8xPLL
                                    // 20 = 57600bps baudrate for 10MHz crystal with 8xPLL
                                    // 10 = 115200bps baudrate for 10MHz crystal with 8xPLL
   
   U2STAbits.URXISEL = 0;                   // Interrupt flag bit is set for every character received.
   IPC6bits.U2RXIP   = 5;                   // UART2 Receive Interrupt Priority = 4.
   U2STAbits.OERR    = 0;                    // Clear the Receive Overflow Flag.
   IFS1bits.U2RXIF   = 0;                   // Clear the UART2 Receive Interrupt flag.
   IEC1bits.U2RXIE   = 0;                   // Disable UART2 Receive Interrupt.
   U2MODEbits.UARTEN = 1;                  // Enable UART2.
   U2STAbits.UTXEN = 1;                       // Enable UART2 Transmit.
}

void uart2_transmit(unsigned char uc_data)
{
   // Indicates the dsPIC is transmitting data
   LED2 = ~LED2;
   delay_ms(25);
   LED2 = ~LED2;

   // Wait until the transmit buffer is ready for new data.
   while (U2STAbits.UTXBF == 1);
   
   // Transmit the data.
   U2TXREG = uc_data;
}

unsigned char uart2_receive(void)
{
   // Indicates the dsPIC is receiving data
   LED1 = ~LED1;
   delay_ms(25);
   LED1 = ~LED1;

   // Wait until there is data available in the receive buffer.
   while (U2STAbits.URXDA == 0);
   
   // Clear the overflow bit and return the received data.
   U2STAbits.OERR = 0;
   return U2RXREG;
}

void uart2_putstr(const char* csz_string)
{
   // Loop until the end of string.
   while (*csz_string != '\0') {
      uart2_transmit(*csz_string);
         
      // Point to next character.
      csz_string++;
   }
}


C# code
CODE: SELECT_ALL_CODE
        private void buttonTransmit_Click(object sender, EventArgs e)
        {
            sp.Write("A".ToCharArray(), 0, 1);
            System.Threading.Thread.Sleep(100);     // Delay
            Console.WriteLine("[Transmitted]");
        }
ignorances
Greenhorn
 
Posts: 3
Joined: Thu Nov 03, 2011 4:01 pm

Re: Problem with transmitting data

Postby yonghui » Mon Aug 06, 2012 11:35 am

else if (SW2==0) // Receive data from the PC
{
lcd_clear();
lcd_putstr("Receive Mode");
lcd_goto(0x40);
while(1)
{
unsigned char rxData;
uart2_receive();
delay_ms(200);
rxData = U2RXREG;
lcd_putchar(rxData);
}
}


form this portion of code. seems like the uart receive module is always enabled. i think it will be good to clear the uart receive register buffers before entering the receive mode. to throw away all possible previous noises.

CODE: SELECT_ALL_CODE
while ("uart receive flag"== 1)  temp=U2RXREG;


and the function uart2_receive should return the value as shown by the function. so
CODE: SELECT_ALL_CODE
unsigned char rxData;
rxData= uart2_receive();
lcd_putchar(rxData);


also, it hink this will be better:

CODE: SELECT_ALL_CODE
unsigned char uart2_receive(void)
{
// do not put too much delay in receive loop which may cause buffer overflow if there is a lot data coming in. in short time

   // Wait until there is data available in the receive buffer.
   while (U2STAbits.URXDA == 0);
   
   // Clear the overflow bit and return the received data.
if( U2STAbits.OERR ==1)
{
   U2STAbits.OERR=0;
  return 0;
}
else {
   return U2RXREG;
}
}
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: Problem with transmitting data

Postby ignorances » Tue Aug 07, 2012 4:13 pm

thanks yonghui for your tip. I finally able to display the text without any problem. Thanks a million :)
ignorances
Greenhorn
 
Posts: 3
Joined: Thu Nov 03, 2011 4:01 pm


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 4 guests