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.

Strange RS232 board behavior

Status
Not open for further replies.

tonyyy

Member level 1
Member level 1
Joined
Dec 26, 2012
Messages
38
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,663
Hi.
My board works fine with this code but then it doesn't. I am using Tera Term for sending
commands to the board. The board gets the command and executes it and send me back the
string. The problem is that after 3 or 4 attempts or after some time the board regonize
only the trasmitted command but any replies is received from it. Here
th code (pic is 16f876a):

#include <stdio.h>
#include <htc.h>
#include "usart.h"
#include <stdlib.h>


#ifndef _XTAL_FREQ
#define _XTAL_FREQ 10000000
#endif

__CONFIG(CP_OFF & BOREN_OFF & LVP_OFF & WDTE_OFF & PWRTE_ON & FOSC_HS );

void putch(unsigned char byte)
{
while(!TXIF)
continue;
TXREG = byte;
}


char getch() {/* retrieve one byte */
while(!RCIF) /* set when register is not empty */
continue;
return RCREG;
}

unsigned char
getche(void)
{
unsigned char c;
putch(c = getch());
return c;
}

void init_enable()
{

BRGH = 1; /* high baud rate */
SPEN = 1; /*attiva porta seriale */
SYNC = 0; /* asyncrono */
//passo 3
TXIE = 0; /* disable tx interrupts */
TX9 = 0; /* 8- or 9-bit transmission */
RX9 = 0; /* 8- or 9-bit reception */
TXEN = 1; /* enable the transmitter */
RCIE = 0; /* disable rx interrupts */
CREN = 1; /* enable reception */
SREN = 0; /* no effect */
}



void main(void){
PORTB = 0;
TRISB = 0;
TRISC = 0;
INTCON=0;
INIT_COMMS(); // set up the USART - settings defined in usart.h
init_enable();
char input;
while(1){
input = getch(); // read a response from the user
int i ;
for(i=0;i!=10; i++)
{
printf("test");
}

if (input == '1') {
RC0 = 1;
RC1 = 1;
RC2 = 1;
RC3 = 1;
RB3 = 1;
RB4 = 1;
RB5 = 1;
}


if (input == '2') {
RC3 = 1;
__delay_ms(1000);
RC3 = 0;
__delay_ms(2000);
RC3 = 1;
__delay_ms(1000);
RC3 = 0;
}
}

}



usart.h

#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600
#define FOSC 10000000L
#define NINE 0 /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#define RX_PIN TRISC7
#define TX_PIN TRISC6

/* Serial initialization */
#define INIT_COMMS()\
RX_PIN = 1;\
TX_PIN = 1;\
SPBRG = DIVIDER;\
RCSTA = (NINE_BITS|0x90);\
TXSTA = (SPEED|NINE_BITS|0x20)


void putch(unsigned char);
char getch(void);
unsigned char getche(void);

#endif

Thanks a lot.
 

There is no code which sends or received strings. Is your commands 1 byte wide? Use Serial interrupt to receive data. Also make a function to send strings through UART.

Hi. Thanks for your reply.
I see, so why istruction like

printf("test")

works for a while?

Yes, there is no string you are right. So the problem
should not occur using the putch below.

void putch(unsigned char byte)
{
while(!TXIF)
continue;
TXREG = byte;
}

Then I will use array :

while ((input=getch())!='\r') {
co = input;
putch(co); // echo to the terminal
i++;
}

// First command
if(strcmp(co, "com1") == 0)
{
RB2 = 1;
RB3 = 1;
RB4 = 1;
RB5 = 1;

_send_reply();

}


So assuming I want to reply to the command with a string the best is to create a function (i.e. _send_reply() )
which will take a string as parameter and uses TXREG for sending it char by char.


Thanks
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top