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.

uart driver for pic16f877a

Status
Not open for further replies.

swapna u

Member level 1
Joined
May 10, 2012
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,657
Hi all,

I have been working on the uart module of pic16f877a micro controller. I have gone through the registers used and I am aware of the configuration to use 8 bit communication.I am using interrupts for Tx and Rx.But I am facing problem in transmitting and receiving the data bytes as the RCREG buffer is not gettin updated, Please help me with the logic for transmission and reception.The initial conditions for uart configuration is done.I am tryin to transmit the recieved data through the code snippet below. Please help me with it.

/*ISR*/
void interrupt uart_rx(void)
{
if(RCIE && RCIF)
{
ClearUartRxIntFlag;
UART_TX(RCREG);
}
}
/*Tx function definition */
void UART_TX(unsigned int TX_BYTE)
{
TXREG = TX_BYTE;
while(!TRMT);
}



Thank you
 

1. RCIE will not change during the interrupt and must be set for the interrupt to occur so you can remove it from the check, just use "if(RCIF)". You should use a single '&' anyway!
2. I'm almost certain the RCIF bit is cleared by reading RCREG (it is on similar PICs) so you don't need to clear it in your program.
3. Check the transmit register is empty before you put new data into it, not afterwards so swap the last two lines.

Brian.
 
Hi Betwixt,

I tried with the changes that you told.But still the RCREG is not getting updated. In my program I have initially enabled RCIE so that the interrupt happens during reception. But when I run the program there is no change in the RCIF.It is always reset. This indicates that transfer to RCREG is not happening with this code.And my TXREG is getting updated with random values though m sending the data through TX_BYTE. And according to data sheet TRMT is the bit which tells if my TSR(transmit shift register)is full or empty. TRMT=1(TSR IS EMPTY).Data is always transmitted from TXREG to TSR so swapping those two lines as you said won't work i feel.Please help me to collect data to RCREG which is not happening. Please suggest the logic if my code is wrong.


Thankyou
 

I think, changing few lines of your code would make your communication successful.
Code:
/*ISR*/
void interrupt uart_rx(void)
{
while(!RCIF)
continue;
ClearUartRxIntFlag;
UART_TX(RCREG);
}


---------- Post added at 12:38 ---------- Previous post was at 12:38 ----------

I think, changing few lines of your code would make your communication successful.
Code:
/*ISR*/
void interrupt uart_rx(void)
{
while(!RCIF)
continue;
ClearUartRxIntFlag;
UART_TX(RCREG);
}
 

can you add your registers configuration?
 

Actually, you shouldn't be transmitting random values because the transmit routine should only be called when a byte has been received in RCREG.
I suspect the problem is to do with other registers not being set up properly, can you show us the routine for configuring the USART, the clock speed you are using and if possible the contents of the receive and transmit status registers and the SPBRG register.

Brian.
 

Hi all,

I am using 4MHz clock.Contents of TXSTA=0x24 and RCSTA=0x90.SPBRG=0x19(9.6KHz baudrate) and BRGH=1(HIGH SPEED).Apart from the ISR and Tx function which I had posted in my first thread I have configued TXSTA and RCSTA to the values above and I have enabled the GIE,PEIE and RCIE. I don't know why TXREG is taking random values and the reason why RCREG is not getting updated. Please help.


Thankyou

---------- Post added at 14:22 ---------- Previous post was at 14:19 ----------

If possible please share the code for USART communication to happen successfully.



Thankyou

---------- Post added at 14:22 ---------- Previous post was at 14:22 ----------

If possible please share the code for USART communication to happen successfully.



Thankyou
 

The registers look fine and banking should be controlled by the compiler so I can't see anything wrong with the code. My preference would be to use 'return' rather than 'continue' though. I assume 'ClearUartRxIntFlag; is a macro as it is missing function brackets. Is it built-in to your compiler or something you have written yourself. Sorry if that's a silly question, I normally program in assembly language and when I use 'C' it's with a different compiler to yours.

Brian.
 

Hi Betwixt/Gopintj/yuvko ,

I have made the changes in the code as you people told.And ClearUartRxIntFlag is a macro to which I have assigned values. However the code still is'nt working fine though I have made the changes.Please can you people suggest me the right way to program so that the communication is possible. Please hlp me. As I need to develop the prototype within this week:( I am using Hitech C Compiler and MPLAB version8.30


Thankyou
 

Have you polled the TXIF bit while you transmit the received byte? Let me show you how i perform Transmission and Reception operations by a simple coding.

let c be a character variable that holds a character as 'a'. Now, i am going to transmit this character.

Code:
main()
{
unsigned char c, r;
------
tx(c);                             \\Function to transmit a character.
DelayMs(100);                  \\A simple Delay Function.
r=rx();                            \\Function to receive a character.
lcd(r);                            \\Lcd Display of the received char.
}

void tx(unsigned char ch)    \\Body of the function to perform Tx operation
{
     TXEN=1;
     TXREG=ch;
     tx_wait();
}

unsigned char rx()          \\Body of the function to perform Rx operation
{
        unsigned char ch;
	CREN=1;
	rx_wait();
	letter2=RCREG;
	return ch;
}

void tx_wait()
{
	while(!TXIF)
	continue;
	TXIF=0;
}

void rx_wait()
{
	int i;
	while(OERR){
	for(i=0;i<2000;i++)
	{i=i+1;}
	CREN=0;
	OERR=0;
	}
	CREN=1;
	while(!RCIF)
	continue;
	RCIF=0;
}

This is how i programmed for UART Data Transmission and Reception. And i got the correct output.

Are you using a Simulator like PROTEUS or Hardware?
 

Hi,

Thanks for the code. I will try this logic also.I am not using any simulator. I am using Pic16f877a h/w. In the code which you posted,RCREG contents is moved to variable 'letter2'.But where is that variable defined?And which hardware did you use for displaying the results?If this works then I need to develop communication with the hyperterminal.That is the end result which is required. Hope you guys will support me with it too. :)Will get back with the results of this execution.


Thankyou

---------- Post added at 11:42 ---------- Previous post was at 10:00 ----------

Hi,

I tried the logic which you told. I changed unsigned char to unsigned int but when I execute I am gettin error which says"illegal character(0x5C)". I have not used characters in my implementation and still it is giving this error. May I know the reason for this error?The program is not getting built due to this error. Please help


Thankyou
 

hi,
have you set TRISC bit 6,7 ?
 

Hi,

I have set TRISC7(RX) and have reset TRISC6(TX) so that whatever is received gets transmitted. But the communication is not happening.
 

as far as i remember, the datasheet say you should set both of them
 

Hi,

I have set the TRISC6 and TRISC7 bits as in data sheet but still I am not getting the output..Please help!!I ll publish the circuit diagram later. Right now I dont have access to add image. Please bear. I want to know how to establish communication with the hyperterminal.The data which we send through hyperterminal gets updated in RCREG of pic16f microcontroller is it?Please give me a brief idea as to how exactly the uart communication takes place with hyperterminal.


Thankyou
 

To use Hyperterminal through a serial port you MUST use a level converter such as the MAX232 or MAX3232 (or similar parts) because the voltage levels between the PIC and serial port are not only different but inverted. A '1' from the PIC is a '0' at the PC. This does not mean you can invert the bits in software before sending them! The start and stop bits are produced by the USART circuit inside the PIC and those would still be upside down.

Brian.
 

Hi,
What exactly is MAX232 level shifter?And how should I implement it for the communication to happen successfully?What are the hardware connections to be made with the pic microcontroller and also what are the inputs and outputs to verify my communication is happening correctly?I m sorry if its too many questions but I am not clear as to how I should use it. Please help.


Thankyou
 

It's an IC made by MAXIM but there are other manufacturers too. Basically, it has three circuits inside it, one converts 0-5V levels to +/- 12V, one does the opposite, converting +/- 12V to 0-5V and the third generates the + and - 12 volts levels from the IC's 5V supply pin.

For more information, look at the data sheet for the MAX232, it is a very common IC. The MAX3232 is the same IC but it works on 3.3V supply instead of 5V.

All you do is connect the PIC TX to the data in pin on the MAX232, the PIC RX pin to the data out pin on the MAX232 and it will produce and accept the RS232 (+12 to -12V) voltage levels your PC uses at it's serial port. You also have to fit some capacitors between pins on the MAX232 to allow it to generate the 12V signals from a 5V supply. It's really very simple, the schematics are in the data sheet, No software is needed, it's a hardware solution.

Brian.
 

Hi,

Is it necessary to make use of MAX-232 level converter for communication with the hyperterminal or will the RS-232 cable take care of it?
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top