Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] UART with LCD using PIC24FJ128GA010

Status
Not open for further replies.

Mansoor Ahmed

Newbie level 4
Joined
Jan 1, 2014
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
76
hello everybody,
i am using PIC24FJ128GA010. i am trying for UART with LCD display. Aim of my project is that i am just transmitting a character (for ex. "ABCD") but its not working for me, only the U2TX bit gets enable but in LCD nothing is getting displayed. i don't know what mistake i have done as i am new to this controller i am unable to rectify it. i have attached the code below kindly please verify and reply me.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#define FOSC 8000000        //device clock frequency (8MHz osc on Explorer16)
#define FOSC/2
#include<p24FJ128GA010.h>
#include "uart.h"
//#define BAUDRATE 9600
#define EN PORTDbits.RD4
#define RS PORTBbits.RB15
#define DATA PORTE
 
void delay(unsigned int a);
void LCD_CMD(unsigned char);        //Declaring LCD_CMD function
void LCD_DATA(unsigned char*);       //Declaring LCD_DATA function
 
void __attribute__ ((interrupt, no_auto_psv)) _U2RXInterrupt(void) {
    LATA = U2RXREG;
    IFS1bits.U2RXIF = 0;
}
void __attribute__ ((interrupt, no_auto_psv)) _U2TXInterrupt(void) {
    IFS1bits.U2TXIF = 0;
}
 
 
void main()
{
    unsigned char a[]="ABCD";
    unsigned int i;
       // TRISF=0x0000;
    #if defined (__PIC24FJ256GB110__) || defined (__PIC24FJ256GA110__)
    // remap pins before intialising SPI2
    // Unlock Registers
    __builtin_write_OSCCONL(OSCCON & 0xbf);
    // Configure Input Functions **********************
    // Assign UART2RX To Pin RP10
    RPINR19bits.U2RXR = 10;
    // Configure Output Functions *********************
    // Assign UART2TX To Pin RP17
    RPOR8bits.RP17R = U2TX_IO;
    // Lock Registers
    __builtin_write_OSCCONL(OSCCON | 0x40);
#endif
    TRISA=0x0000;
    TRISD=0x0000;
    TRISE=0x0000;
    TRISB=0x0000;
        RCONbits.SWDTEN = 0;
        U2MODEbits.UARTEN = 0;  // Bit15 TX, RX DISABLED, ENABLE at end of func
    U2MODEbits.USIDL = 0;   // Bit13 Continue in Idle
    U2MODEbits.IREN = 0;    // Bit12 No IR translation
    U2MODEbits.RTSMD = 0;   // Bit11 Simplex Mode
    U2MODEbits.UEN = 0;     // Bits8,9 TX,RX enabled, CTS,RTS not
    U2MODEbits.WAKE = 0;    // Bit7 No Wake up (since we don't sleep here)
    U2MODEbits.LPBACK = 0;  // Bit6 No Loop Back
    U2MODEbits.ABAUD = 0;   // Bit5 No Autobaud (would require sending '55')
    U2MODEbits.RXINV = 0;   // Bit4 IdleState = 1
    U2MODEbits.BRGH = 0;    // Bit3 16 clocks per bit period
    U2MODEbits.PDSEL = 0;   // Bits1,2 8bit, No Parity
    U2MODEbits.STSEL = 0;   // Bit0 One Stop Bit
 
    U2BRG = 9600;   // baud rate
 
    // Load all values in for U1STA SFR
    U2STAbits.UTXISEL1 = 0; //Bit15 Int when Char is transferred (1/2 config!)
    U2STAbits.UTXINV = 0;   //Bit14 N/A, IRDA config
    U2STAbits.UTXISEL0 = 0; //Bit13 Other half of Bit15
    U2STAbits.UTXBRK = 0;   //Bit11 Disabled
    U2STAbits.UTXEN = 0;    //Bit10 TX pins controlled by periph
    U2STAbits.UTXBF = 0;    //Bit9 *Read Only Bit*
    U2STAbits.TRMT = 0;     //Bit8 *Read Only bit*
    U2STAbits.URXISEL = 0;  //Bits6,7 Int. on character recieved
    U2STAbits.ADDEN = 0;    //Bit5 Address Detect Disabled
    U2STAbits.RIDLE = 0;    //Bit4 *Read Only Bit*
    U2STAbits.PERR = 0;     //Bit3 *Read Only Bit*
    U2STAbits.FERR = 0;     //Bit2 *Read Only Bit*
    U2STAbits.OERR = 0;     //Bit1 *Read Only Bit*
    U2STAbits.URXDA = 0;    //Bit0 *Read Only Bit*
 
    IFS1bits.U2TXIF = 0;    // Clear the Transmit Interrupt Flag
    IEC1bits.U2TXIE = 1;    // Enable Transmit Interrupts
    IFS1bits.U2RXIF = 0;    // Clear the Recieve Interrupt Flag
    IEC1bits.U2RXIE = 1;    // Enable Recieve Interrupts
 
    U2MODEbits.UARTEN = 1;  // And turn the peripheral on
 
    U2STAbits.UTXEN = 1;
        LCD_CMD(0x01);                   //Set command to clear display screen
        delay(100);                   //Delay
        LCD_CMD(0x38);                   //Set command 2lines and 5x7 matrix
        delay(100);                   //Delay
        LCD_CMD(0x0F);                   //Set command to display ON cursor blinking
        delay(100);                   //Delay
        LCD_CMD(0x85);
        while(1)
        {
            
            for(i=0;i<4;i++)
            {
             U2TXREG= a[i];
             LCD_DATA(a[i]);
        //     delay(50);
            }
        
        }
}
 
void delay (unsigned int a)
{
    unsigned int m,n;
    for(m=0;m<=a;m++)
        for(n=0;n<=125;n++);
}
 
void LCD_CMD(unsigned char ldata)         //LCD_CMD function defination
{
    DATA=ldata;                           //Sending the data to ldata
    RS=0;                                 //RS=0 means it reading command
    EN=1;                                 //Make EN high
    delay(10);                            //Delay
    EN=0;                                 //Make EN low
    delay(10);                            //Delay
}
 
void LCD_DATA(unsigned char *ldata)        //LCD_DATA function defination
{
    while(*ldata!='\0')
    {
    RS=1;
    DATA=*ldata;                           //Sending the data to ldata
                                    //RS=1 means it reading data
    EN=1;                                 //Make EN high
    delay(100);                            //Delay
    EN=0;                                 //Make EN low
    delay(100);                            //Delay
    *ldata++;
    }
}



thank you.
 
Last edited by a moderator:

hello,


I don't know PIC24F serie
but on PIC18F serie we must use LAT instead PORT to write a bit into a PORT

Code:
#define EN LATDbits.LATD4
#define RS LATBbits.LATB15
#define DATA  LATE

and a remark
it is not a good habit to use label definition as DATA
sometimes compiler can be confused

No probleme in french, i use "Donnee" (for variable DATA)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top