tonyyy
Member level 1
- Joined
- Dec 26, 2012
- Messages
- 38
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,286
- 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.
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.