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.

PIC24FJ128GA010 serial port programming(UART1) using debugger MPLAB ICD 2

Status
Not open for further replies.

nikhilrajg

Member level 3
Joined
Mar 2, 2012
Messages
63
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Manipal,Karnataka,India
Activity points
1,834
Hello,

I am working on PIC24FJ128GA010 micro-controller on Explorer-16 Development Board,..using MPLAB ICD 2 Debugger/Programmer.
Well I tried a lot to execute the serial port program just to send & receive the data from Micro controller to the HYPER TERMINAL, i.e., on the real hardware connected to PC via RS232 wire to the COM1 port... Nothing is working. The code works in the simulator and I am getting the result in Uart1 window. I dont know whether my code is wrong in some places like to set the baud rates and enabling the UART ect ... ??

Can please someone check this bellow code and reply me back whether the code is CORRECT or WRONG !! If found wrong can you please post the correct one ??? And I beg for your suggestions for the exact method to be followed to get the proper output on the terminal...

Code:
/***********************************************************************************************************/
/***********************************************************************************************************/
#include<p24fj128ga010.h>
#define CTS _RF12
#define RTS _RF13
#define TRTS TRISFbits.TRISF13
#define BRATE 129
#define U_ENABLE 0X8008
#define U_TX 0X0400
#define DELAY 1
void initU2()
{
    U1BRG = BRATE;
    U1MODE = U_ENABLE;
    U1STA = U_TX;
    TRTS = 0;
    T1CON = 0x8030;
    TMR1=0;
    while(TMR1 < DELAY);
    TMR1 = 0;
    RTS = 1;
}

int putU2(int c)
{
    while(CTS);
    while(U1STAbits.UTXBF);
   
    U1TXREG = c;
    return c;
}

char getU2(void)
{
    RTS = 0;
    while(!U1STAbits.URXDA);
    return U1RXREG;
    RTS = 1;
}

//=======================================================
int putsU2(char *s)
{
printf("%s",(*s));
while(*s)
    putU2(*s++);
}

char *getsnU2(char *s,int len)
{
    char *p = s;
    do
    {
        *s = getU2();
        if(*s=='\r')
            break;
        s++;
        len--;
    }while(len>1);
    *s = '\0';
    return p;
}

//=======================================================

void main(void)
{
    initU2();
while(1)
        putsU2("hello\t");
}

/**********************************************************************************************************/
/**********************************************************************************************************/

I have set BRATE(a defined new variable) as 129 in the code for 9600 baud rate.. Is it the correct one ??
Please correct the code and repost it. Can anyone please tell me how to find out the oscillator frequency ?


Thanks for your consideration.
Nikhil Raj
 

hello,

one mistake that I came to know is that, the printf statement in "putU2" function..,,

Code:
   printf("%s",(*s));

Please specify other errors and code that should be added if necessary to configure the UART...
Is there any other method to initialize the UART other than the above "initU2" fucntion ??
How to initialize UART2 ?? Can anyone plz help me...

Thanks
Nikhil Raj
 

Before I check the code, tell me have you used the correct oscillator?

129 for 9600 bps is for 40MHz oscillator. Did you use a 40MHz oscillator? And, you did not set the configuration bits. You need to set the configuration bits. Have you done this in the Configure menu?
 
Thank You Tahmid. I checked the board for oscillator frequency there are 2 oscillators I do not know which oscillator frequency to be considered ?
On the oscillator its written that JWT 20.000 and in another oscillator its written JWT 8.000...as I am using Explorer 16 development board.. Is it 20MHz and 8MHz ?? Which frequency should be used ?

Thanks.
Nikhil Raj
 

According to the circuit diagram of the EXPLORER 16 board (here: User Guide) on pages 38, 39 and 43, you can see that the oscillator used is an 8MHz crystal.

Hope this helps.
Tahmid.
 
Hello,

Thank you Tahmid. I have set 51 for baud rate 9600 for the frequency 8MHz. Is it proper ? I am able to get the output on simulator but not on the hyper terminal when run through explorer board. It is showing some junk values.

Thank You
Nikhil Raj
 

That's wrong.

FOSC = 8MHz
So, FCY = 4MHz (as FCY = FOSC/2)
BRG = [FCY / (16*Baud Rate)]-1
This comes to 25.

And by junk values, what do you mean? Which simulator are you using? Proteus?

You should remember that, when you're using an 8MHz oscillator, the instruction clock is 4MHz (FOSC/2).

Hope this helps.
Tahmid.

---------- Post added at 20:03 ---------- Previous post was at 20:03 ----------

Refer to page 140 in the datasheet.
 
Thank You Tahmid.

I am Using MPLAB IDE v8.56 the old version. And the Hyper terminal Private edition. And I even tried in putty terminal the results are same, i.e., the jusk vales.

Thank You.
Nikhil Raj

---------- Post added at 09:48 ---------- Previous post was at 09:47 ----------

Sorry its the Junk Values.

Thank you.
Nikhil
 

Hello,

Can you please tell me how to enable the UART (code to enable it ) ?

#define U_ENABLE 0x8008 // enable UART, BREGH=1, 1 stop, no parity

how to enable the UART if BREGH=0 ??
The above value 25 for baud rate 9600 is when BRGH = 0 if I am not wrong.
So is there any change in the configuration for enabling UART ?
Is the code same for UART-1 and UART-2 to enable them?? I am confused with this. Because when with the above initialization of UART for UART-1 only it works But when for UART-2 it is set as above code it says.,in the simulator window when run in MPLAB SIM.

UART-W0005: Uart is enabled but no output destination has been assigned. (File or Output Window)

Thanks for your consideration.
Nikhil Raj.
 
Last edited:

Refer to pages 190 to 193 in the datasheet.

Add these to the beginning of the code after you include the header file:
Code:
_CONFIG1(FWDTEN_OFF)
_CONFIG2(FNOSC_PRI & POSCMOD_XT)
That's the configuration settings. I've selected the crystal oscillator as the oscillator and disabled watchdog timer.

For BRGH=0, you need to define this:
Code:
#define U_ENABLE 0x8000 // enable UART, BRGH=0, 1 stop, no parity
Go to page 142 in the datasheet.

UART2 is set in the same way as UART1.

For UART2, you have to set U2MODE, U2BRG, U2STA, U2TXREG, etc.

Hope this helps.
Tahmid.
 
you can refer this for BREG
https://ww1.microchip.com/downloads/en/DeviceDoc/en026583.pdf

---------- Post added at 15:22 ---------- Previous post was at 15:13 ----------

To simulate this code on Proteus..what changes are required?

---------- Post added at 15:39 ---------- Previous post was at 15:22 ----------

can you tell me .. the procedure to check UART connection by different ways (hardware, software)
 

No specific changes are required for a program that works, to work in Proteus.

In hardware, you must ensure that all connections are proper and that there are no loose wirings or connections. In software, it depends on your code. What kind of checking do you want in software?
 
I want send some data from pic to UART and also same data should get at receiver of pic.
 

yes..will it required any changes?

PIC.JPG
 

Thanks Tahmid,

Am referring the book "Learning to Fly the PIC24 by Lucio Di Jasio". In this book he has used UART2 to connect MC to pc via RS232, in the coding part there is no initialization for U2TXREG as you mentioned that to be included for UART-2.. initialation code is as bellow please make necessary changes if there are any.,,.

Code:
void initU2(void)
{
U2BRG = BRATE; // initialize the baud rate generator
U2MODE = U_ENABLE; // initialize the UART module
U2STA = U_TX; // enable the Transmitter

TRTS = 0; // make RTS an output pin

TMR1 = 0;
while(TMR1 < delay);
TMR1=0;

RTS = 1; // set RTS default status (not ready)
} // initU2


Please Help.

Thanks for You consideration.
Nihkil Raj.

---------- Post added at 11:23 ---------- Previous post was at 11:22 ----------

Is there any code to set U2TXREG ??

Thanks.
Nikhil Raj

---------- Post added at 11:26 ---------- Previous post was at 11:23 ----------

values for BRATE = 25 (9600) and U_ENABLE = 0x8000 & U_TX = 0x0400

Thanks.
Nikhill Raj

---------- Post added at 12:26 ---------- Previous post was at 11:26 ----------

Hello Tahmid,

Finally I got the out put on the terminal. Thank you very much for your great help. The error was in the CTS.. When I run the program the controll will be stuck in clearing the CTS line in the putU2() function as bellow.

Code:
int putU2( int c)
{
//while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2

So i commented the line. After commenting its working properly now. Is there any idea why is it so ?
And will it effect on MC or the program result ? Is there other solution for this problem.

Thank you very much.
Nikhil Raj.
 

Thanks Tahmid,

Am referring the book "Learning to Fly the PIC24 by Lucio Di Jasio". In this book he has used UART2 to connect MC to pc via RS232, in the coding part there is no initialization for U2TXREG as you mentioned that to be included for UART-2.. initialation code is as bellow please make necessary changes if there are any.,,.

Code:
void initU2(void)
{
U2BRG = BRATE; // initialize the baud rate generator
U2MODE = U_ENABLE; // initialize the UART module
U2STA = U_TX; // enable the Transmitter

TRTS = 0; // make RTS an output pin

TMR1 = 0;
while(TMR1 < delay);
TMR1=0;

RTS = 1; // set RTS default status (not ready)
} // initU2


Please Help.

Thanks for You consideration.
Nihkil Raj.

---------- Post added at 11:23 ---------- Previous post was at 11:22 ----------

Is there any code to set U2TXREG ??

Thanks.
Nikhil Raj

---------- Post added at 11:26 ---------- Previous post was at 11:23 ----------

values for BRATE = 25 (9600) and U_ENABLE = 0x8000 & U_TX = 0x0400

Thanks.
Nikhill Raj

---------- Post added at 12:26 ---------- Previous post was at 11:26 ----------

Hello Tahmid,

Finally I got the out put on the terminal. Thank you very much for your great help. The error was in the CTS.. When I run the program the controll will be stuck in clearing the CTS line in the putU2() function as bellow.

Code:
int putU2( int c)
{
//while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2

So i commented the line. After commenting its working properly now. Is there any idea why is it so ?
And will it effect on MC or the program result ? Is there other solution for this problem.

Thank you very much.
Nikhil Raj.

U2TXREG isn't for initialization, just for sending. I think I confused you in the last post.
While transmitting, you don't need to CTS to clear. If you clear CTS, then transmission starts. So, it's for you to write to, not read. No need to check it and you've commented it out correctly. Furthermore, you disabled CTS in your initialization. So, in your code CTS is not needed at all.

The UART2 initialization is fine. You've done it exactly the same as the UART1. So, it's done properly with the same configuration as UART1.

Hope this helps.
Tahmid.
 

Thanks Tahmid,

Now I am able to get the output to write to hyperterminal. But now I am trying to communicate both ways facing problems in receiving data from hyper-terminal. Is it possible to get the received data from the hyper terminal to be printed on some other editor ?
or else on the same editor in which I am debugging the code such as on UART ?
I think UART is not possible because we can see the UART window only in simulator mode... Is there any particular and easy method to fetch the data from hyper-terminal and to display somewhere on the editor ?

Thanks a ton
Nikhil Raj

---------- Post added at 16:58 ---------- Previous post was at 16:39 ----------

Hello,

How to read the data from hyper-terminal if I type something on the terminal PIM should reply for that for example...,
If I type "AT" on hyper-terminal I should get the reply on hyper-terminal itself as "OK" how to do this ? Please explain the ligic. Great appreciation for the your reply.

Thank You very Much.
Nikhil Raj.
 

yes..will it required any changes?

View attachment 70330

Instead of directly shorting the output and input, you should connect it to the PC and use the PC to echo back. This is because, using the method you have drawn, the transmitted data is echoed instantly and the PIC is busy then. You should send the data back using the PC so that it is sent at a time when the PIC is ready to receive the signal. Or, you can use a second PIC to transmit data and do echo function as and when required.

Hope this helps.
Tahmid.
 
Thanks Tahmid,

Now I am able to get the output to write to hyperterminal. But now I am trying to communicate both ways facing problems in receiving data from hyper-terminal. Is it possible to get the received data from the hyper terminal to be printed on some other editor ?
or else on the same editor in which I am debugging the code such as on UART ?
I think UART is not possible because we can see the UART window only in simulator mode... Is there any particular and easy method to fetch the data from hyper-terminal and to display somewhere on the editor ?

Thanks a ton
Nikhil Raj

---------- Post added at 16:58 ---------- Previous post was at 16:39 ----------

Hello,

How to read the data from hyper-terminal if I type something on the terminal PIM should reply for that for example...,
If I type "AT" on hyper-terminal I should get the reply on hyper-terminal itself as "OK" how to do this ? Please explain the ligic. Great appreciation for the your reply.

Thank You very Much.
Nikhil Raj.

So, you can not read the data that is transmitted from hyper-terminal to the PIC?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top