Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Serial Port configuration

Status
Not open for further replies.

rems

New Member
I have configured my serial port.But its not working.

#define TIMER0 0b11010011;
#define SYS_OSC 0b01101010; SYNC = 0 ; // Transmisson is asynchronous
TRISC7 = 1 ;
TRISC6 = 1 ;
BRGH = 1 ;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1 ; // Enable EUSART Transmit Operation
CREN = 1 ; // Enable EUSART Receive Operation
SPEN = 1 ; // Enable serial port
RCIE = 1 ; // Enable USART receive interrupt
TXIE = 1 ; // Enable USART transmit interrupt
RCIF = 0 ; // Clear USART receive interrupt flag
TXIF = 0 ; // Clear USART transmit interrupt flag
OPTION_REG = TIMER0;
TMR0 = 0; // Clear timer - always write upper byte first
TMR0IF = 0; // Clear Timer 0 interrupt flag
TMR0IE =1; // Configure TMR0
TRISC = 0xBB;
// Set up global interrupts
GIE=1;
PEIE=1;
OSCCON = SYS_OSC;

is there anything i missed in my configuratin.could someone help.
 
is there anything i missed in my configuratin.could someone help.
To name a few: microcontroller type, compiler, header files and libraries used, source code for interrupt routines, checking that global interrupt enable is set, etc. etc.
 
Last edited:
Microcontroller Type: PIC16F1937
Compiler:Hitech C -MPLAB
Header FIles:#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <htc.h>
#include <pic.h>
#include "Rserial.h"




code for interrupt
#define TIMER0 0b11010011;
#define SYS_OSC 0b01101010; //< PLL is disabled;4 MHz; Fosc is Internal

void sysinit (void)
{
OPTION_REG = TIMER0;
TMR0 = 0; // Clear timer - always write upper byte first
TMR0IF = 0; // Clear Timer 0 interrupt flag
TMR0IE =1; // Configure TMR0
TRISC = 0xBB;
// Set up global interrupts
GIE=1;
PEIE=1;
OSCCON = SYS_OSC;
}

static void interrupt isr(void)
{
if (RCIF) // If a new character has been received
{
Serial_ISR();

RCIF = 0; // Receive event has been serviced
}
if (TMR0IF)
{
Timer_ISR () ;

TMR0IF=0; // Interrupt has been serviced
}
}


void main(void)
{
_delay(10);
_delay(10);
sysinit () ;
ADCInit();
usart_init();

while (1)
; // halt and catch fire
}



#define BAUD2400_16MHZ 103 ///< UART setting for 2400 baud at 16 MHz Fosc
#define BAUD9600_16MHZ 25 ///< UART setting for 9600 baud at 16 MHz Fosc
# define MAX_In_Buf_SZ 8
double delay = 40 ;
unsigned char inbuf[ MAX_In_Buf_SZ] ;
unsigned int rx = 0;





void usart_init(void)
{
SYNC = 0 ; // Transmisson is asynchronous
TRISC7 = 1 ;
TRISC6 = 1 ;
BRGH = 1 ;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1 ; // Enable EUSART Transmit Operation
CREN = 1 ; // Enable EUSART Receive Operation
SPEN = 1 ; // Enable serial port
RCIE = 1 ; // Enable USART receive interrupt
TXIE = 1 ; // Enable USART transmit interrupt
RCIF = 0 ; // Clear USART receive interrupt flag
TXIF = 0 ; // Clear USART transmit interrupt flag
}



#define false 0
#define true !false

unsigned char STXSeen = false ;

void Serial_ISR (void)
{
unsigned char datin = RCREG ;

if (STXSeen)
{
if (datin =='\3') // ETX ?
{
inbuf [rx] = '\0' ; // terminate string
// reset state variables ready for next command

processReceivedCommand () ;

rx = 0 ;
STXSeen = false ;
}
else
{
// store the received data if there is room in the buffer
if (rx < sizeof (inbuf))
inbuf[rx++] = datin; // Fetch the recieved byte value into Rx buffer
}
}
if (datin =='\2')
{
STXSeen = true ; // set flag
rx = 0 ;
}
}

double calculate_calibration(int chan, double temp)
{
return (temp*calibration [chan].m) + calibration[chan].c;
}


void usart_write_data(unsigned char data) //Reads the character and move to TXREG register
{
while(!TXIF)
; // wait for tx to empty
TXREG = data;
}
void USART_putstring(char* StringPtr)
{
while(*StringPtr != '\0')
{
usart_write_data(*StringPtr);
StringPtr++;
}
}
 
I have configured my serial port.But its not working.

#define TIMER0 0b11010011;
#define SYS_OSC 0b01101010; SYNC = 0 ; // Transmisson is asynchronous
TRISC7 = 1 ;
TRISC6 = 1 ;
BRGH = 1 ;
SPBRG = 25; // 4mhz xtal 9600 BAUD
TXEN = 1 ; // Enable EUSART Transmit Operation
CREN = 1 ; // Enable EUSART Receive Operation
SPEN = 1 ; // Enable serial port
RCIE = 1 ; // Enable USART receive interrupt
TXIE = 1 ; // Enable USART transmit interrupt
RCIF = 0 ; // Clear USART receive interrupt flag
TXIF = 0 ; // Clear USART transmit interrupt flag
OPTION_REG = TIMER0;
TMR0 = 0; // Clear timer - always write upper byte first
TMR0IF = 0; // Clear Timer 0 interrupt flag
TMR0IE =1; // Configure TMR0
TRISC = 0xBB;
// Set up global interrupts
GIE=1;
PEIE=1;
OSCCON = SYS_OSC;

is there anything i missed in my configuratin.could someone help.

Is SPBRG meant to be 25 (an unusual way to represent a port config in decimal) or 0x25 which can make much more sense.

You show other ports/flags that are hexidecimal.

Any clue to what processor you are using?
 
Last edited:
Do you need to turn off analog functions?:

ANSELA = 0
ANSELB = 0
ANSELD = 0
ANSELE = 0
ADCON0 = 0
ADCON1 = 0
CM1CON0 = 0
CM1CON1 = 0
CM2CON0 = 0
CM2CON1 = 0
 
You really don't need the TXIE to be enabled as you are polling the TXIF..... We had an issue with this a tad back...

The TXIF was proving to be a bad check... We were using the TRMT in the TXSTA reg instead .... The data was corrupting the module
A " CREN / OFF " followed by a " CREN / ON " was used to reboot the serial port.

If you are sill having problems, I'll try to simulate what the error is.....

Incidentally!! Why dont you use the libraries own USARTInit() routine ?
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top