Jump to content
Sign in to follow this  
Yannux

16f628 And Rs232

Recommended Posts

Hi all !

 

I wanted to test rs232 communication and i tried this code :

 

#include <system.h>

#pragma DATA _CONFIG, _HS_OSC & _WDT_OFF & _CP_OFF & _LVP_OFF & _MCLRE_ON
#pragma CLOCK_FREQ 4000000

void main()
{

option_reg = 0x80;

/* USART Configuration */

set_bit(trisb, 2);
set_bit(trisb, 1);
set_bit(rcsta, SPEN);			/* Serial port activation */
spbrg = 103;					/* Baudrate 9600 with 4Mhz Clock */
clear_bit(txsta, SYNC);			/* Asynchronous mode */
set_bit(txsta, TXEN);			/* TXEN = 1 */

txreg = 0x31;

while(1)
{
	while (!test_bit(pir1, TXIF));
	txreg = 0x31;

}

}

 

But the mcu seems to never pass the while instruction. Someone can help me ?

 

Thx,

 

Yann.

Share this post


Link to post
Share on other sites

Hi Yannux,

 

I'm not sure which PIC MCU you're trying to program, and I've not had any experience with RS-232, so I might be completely off-track.

 

However, with that said, in my experience when you are working PIC MCUs you need to set the relevant interrupt enable (IE) bit before any test the corresponding interrupt flag (IF) bit.

 

Therefore, in your case, my suggestion would be to check whether your MCU has a TXIE bit in something like the PIE1 register. If it does, then use this bit to either enable or disable the TX interrupt. So, you may need to set the TXIE bit before you enter into your while loop.

 

For example:

 

    set_bit(pie1, TXIE);

   while(1)
   {
       while (!test_bit(pir1, TXIF));
       txreg = 0x31;
               
   }

Another thing to consider is whether your MCU has a "Global Interrupt Enable" (GIE) bit in something like the INTCON register. If it does then you would need to set this bit too before you set the TXIE bit in order to enable all interrupts on your MCU.

 

Something like this may work:

 

    set_bit(intcon, GIE);
   set_bit(pie1, TXIE);

   while(1)
   {
       while (!test_bit(pir1, TXIF));
       txreg = 0x31;
               
   }

However, check the Microchip datasheet for your MCU, in particular the section on Interrupts and the UART (or EUSART).

 

...AudiTTR

Share this post


Link to post
Share on other sites

The following code can be found in a library of functions I've made available at:

 

http://www.geocities.com/ted_rossin/Electronics/Pic/Pic.html

 

This is written for the 16F87x series but I believe it will work if you just change the port initialization from port C bits 6 and 7 to port B bits 1 and 2. I think your biggest problem is that you need to change:

 

set_bit(trisb, 2);

 

to:

 

clear_bit(trisb, 2); // Enable TX to drive

 

in order to enable the transmit output pin. I'm not sure about the other settings but you should be able to compare and contrast with my settings and yours with the data sheet in your hand.

 

 

void RS232putch(unsigned char ByteVal)
{
   while(!(test_bit(pir1,4)));
   txreg = ByteVal;
}

unsigned char RS232getch(void)
{
   while(!(test_bit(pir1,5)));
   clear_bit(pir1,5);
   return(rcreg);
}

unsigned char RS232peekch(void)
{
   if(test_bit(pir1,5)) return(1);
   return(0);
}

/* Set up serial port to run at high baud rate */

void RS232Init(unsigned char BaudRate)
{
set_bit(trisc,7);	// PORTC[RX] is Input
clear_bit(trisc,6); // PORTC[TX] is Output
spbrg = BaudRate;	// Set baud. see header file
txsta = 0x24;		// Set TXEN and BRGH to 1
rcsta = 0x90;		// Set SPEN and CREN to 1

/*
movlw	(1<<TXIE)|(1<<RXIE)
iorwf	PIE1		; Enable Transmit and receive interrupts
   */
}

Share this post


Link to post
Share on other sites

Thank you for replies !

 

Finally, I found that my crystal oscillator produces a bad frequency, so the baudrate wasn't good.

 

 

I just changed capacitors values and now it works fine ;-)

 

To AudiTTR : Enable interrupt is not needed. The TXIF is set by uart regardless interrupt settings. Enable interrupt is needed only when you want to produce an interrupt when a data has been sent.

 

Regards,

 

Yann

 

PS : excuse my poor english but i'm a french guy :(

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...