0
votes

I am starting a project by PIC16f877A and TFT smart lcd (chinese brand). I designed a code to interface PIC with TFT to send data. the protocol of the TFT is designed to send 8 bits at a time, which is prepared in the program. The initial results were good, but there is a little bug. The transferred data have lost bits during transfer, and I could not find an answer for this problem. for example, I am supposed to transfer 0x000290FA but the transferred code became 0x000200FA.

This is the code of the project.

typedef unsigned char  BOOLEAN;
typedef unsigned char INT8U;
typedef unsigned short INT16U;
typedef unsigned long INT32U;
BOOLEAN HMT_WriteVPN32(INT32U Addr,INT32U Data);

void main() {

  UART1_Init(115200);               // Initialize UART module at 115200 bps
  Delay_ms(100);                  // Wait for UART module to stabilize

       HMT_WriteVPN32(0x00020000,0x000290FA);
       delay_ms (500);
       HMT_WriteVPN32(0x00020004,0x0008AD6D);
}

BOOLEAN HMT_WriteVPN32(INT32U Addr,INT32U Data)
{
    INT16U Hdata = 0, Ldata = 0;
    INT16U AddrH = 0, AddrL =0 ;
    INT16U AddrHH = 0, AddrHL = 0, AddrLH = 0, AddrLL = 0;
    INT16U HHdata = 0, HLdata = 0, LHdata = 0, LLdata = 0;
    if((Addr < 0x00020000) || (Addr > 0x0002FFFF))
    {
        return (FALSE);
    }

    AddrH = Addr>>16;                // Take high 16 data
    AddrL = Addr;                    // Lower 16 data

    AddrHH = AddrH >> 8;
    AddrHL = AddrH;
    AddrLH = AddrL >> 8;
    AddrLL = AddrL;

    Hdata = Data >> 16;   // Take the 32-bit high byte
    Ldata =  Data ;    // Take the 32-bit low byte

     HHdata =Hdata >> 6;
     HLdata = Hdata  ;
     LHdata = Ldata >> 6;
     LLdata = Ldata ;
    SendHead();                    // Send frame header
    UART1_Write(0x44);                // send command
    UART1_Write(AddrHH);
    UART1_Write(AddrHL);
    UART1_Write(AddrLH);
    UART1_Write(AddrLL);
    UART1_Write(HHdata);
    UART1_Write(HLdata);
    UART1_Write(LHdata);
    UART1_Write(LLdata);
    SendTail();                   // Send end of frame
    return (TRUE);
}
1
How does UART1_Write() (or your program) wait for the transmission of the previous byte to be completed, i.e. the transmit shift register is empty? Variables AddrHH, AddrHL, ... LLdata should be declared INT8U instead of INT16U. Another bug: the shift operations for HHdata and LHdata appear to be incorrect.sawdust
That is a point. I do not know how to make the program wait for the transmission to be complete. this is a library in MikroC and I use it directly My bad, I have modified the shift of HHdata and LHdata to 8 bit shift. but still no resultAyman hussien
To simplify debugging you might try temporarily putting constants in your calls to UART1_Write and see if you receive what you expect.jolati
@Aymanhussien -- Is this routine available in the library that you are using: UARTx_Tx_Idle()?sawdust
UART_Tx_Idle() is available. But using it did not give any result. I have made some modifications on the code, and the code worked directly. I replaced (unsigned short) by (unsigned long) or (unsigned int)Ayman hussien

1 Answers

0
votes

The code worked when I changed the type of data entered to BOOLEAN HMT_WriteVPN32 function.

I should enter long data, but instead I wanted to enter float value which is not acceptable

Thank you guys for your efforts to solve this mystery with me