Jump to content
Sign in to follow this  
j3n4

Rs232 Trouble :(

Recommended Posts

Hi,

for some days I am trying to interfacing the serial port of my computer on max232 of my pic.

I built (I hope), all ok but I have some doubts on the code.

I have read a bit on the topics of this forum and I have copied a bit of code but its does not work.

Somebody can help me?

 

Here's the code:

 

#include <system.h>
#pragma CLOCK_FREQ 4000000

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_ON & _BODEN_ON & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF

// Sample PIC16F87x defaults for software emulated USART support
#define TX_PORT	0x07
#define TX_TRIS	 0x87
#define TX_BIT	   6
#define RX_PORT	0x07
#define RX_TRIS	 0x87
#define RX_BIT	   7
#define e_SPBRG	0x99
#define e_RCREG	0x1a
#define e_TXREG	 0x019
#define e_TXSTA	 0x98
#define e_RCSTA	 0x18
#define e_TXIF_PIR  0x0c
#define e_RCIF_PIR  0x0c
#define e_TXIF_BIT   4
#define e_RCIF_BIT  5
#define MODE		  (USART_reset_wdt)

#define bit_time 104		// 9600 baud at 40MHz

#include "rs232_driver.h"

void interrupt( void )
{
}

void main( void ){
trisc = 0x80;
portc = 0x00;
uart_init(1,25); // set high speed divisor mode and divisor value - 9600
puts("Hello, world");
while (1){
if (kbhit()){
   putc(getc());
}
 }
}

 

Reguards

j3n4

Share this post


Link to post
Share on other sites

AH! I forgot to say: I am using 16F876 pic with external 4Mhz crystal.

 

If you need i can post the circuit scheme.

 

Reguards

 

j3n4

Share this post


Link to post
Share on other sites
AH! I forgot to say: I am using 16F876 pic with external 4Mhz crystal.

 

If you need i can post the circuit scheme.

 

Reguards

 

j3n4

 

The pic17F876 has a perfectly good USART, why are you trying to software emulate one?

Share this post


Link to post
Share on other sites

hi,

i have solved.

 

here is the code:

#include <system.h>
// 4.0 Mhz
#pragma CLOCK_FREQ 4000000

#define FOSC 4000000
#define spBAUD 9600
#define fSPBCLK (FOSC)  // UART Baud rate generator clock (high speed)
// #define fSPBCLK (FOSC / 4)   // UART Baud rate generator clock (low speed)
#define SPBRG_VAL ((fSPBCLK / (spBAUD * 16L)) - 1L)

// bit_time FOSC / 4 / BAUDRATE 
#define bit_time 104		// 9600 baud at 4MHz

// Hardware dependant defines RC6 & 7
#define RX_PIN	  7
#define TX_PIN	  6

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_ON & _BODEN_ON & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF

// PIC16F87X defaults for hardware USART support
// DS30292C-page 95 (TX) & DS30292C-page 104 (RX)

#define TX_PORT	 PORTC
#define TX_TRIS	 TRISC
#define TX_BIT	  TX_PIN
#define RX_PORT	 PORTC
#define RX_TRIS	 TRISC
#define RX_BIT	  RX_PIN
#define e_SPBRG	 SPBRG
#define e_RCREG	 RCREG
#define e_TXREG	 TXREG
#define e_TXSTA	 TXSTA
#define e_RCSTA	 RCSTA
#define e_TXIF_PIR  PIR1
#define e_RCIF_PIR  PIR1
#define e_TXIF_BIT  TXIF
#define e_RCIF_BIT  RCIF
#define MODE		(USART_reset_wdt | USART_HW)

#define bit_time 104		// 9600 baud at 40MHz

#include "rs232_driver.h"

void interrupt( void )
{
}

void main( void )
{
// RS232 Communications
uart_init(1,SPBRG_VAL);		 // set high speed divisor mode and divisor value - 9600
puts("PIC16F877A test");
delay_ms(100);
while (1){	
if (kbhit()){
		putc(getc());
	}

}

}

 

This was the problem:

#define MODE (USART_reset_wdt)

 

Now whit this :

#define MODE (USART_reset_wdt | USART_HW)

 

It work fine.

 

Best reguards

j3n4

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...