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.

How to do a serial port streaming in PIC xc8 compiler

Status
Not open for further replies.

magvitron

Active Member
Hi I came accorss this on the arduino play ground. How can this be implemented on xc8 compiler of PIC?
Iam reading this stream via PIC

HTML:
44 05 00 95 00 44 05 00 95 00 44 05 00 95 00 44        D..•.D..•.D..•.D
05 00 95 00 44 05 00 95 00 44 05 00 95 00 44 05        ..•.D..•.D..•.D.
00 95 00 44 05 00 95 00 44 05 00 95 00 44 05 00        .•.D..•.D..•.D..
95 00 44 05 00 95 00 44 05 00 95 00 44 05 00 95        •.D..•.D..•.D..•
00 44 05 00 95 00 44 05 00 95 00 44 05 00 95 00        .D..•.D..•.D..•.
44 05 00 95 00 44 05 00 95 00 44 05 00 95 00 44        D..•.D..•.D..•.D
05 00 95 00 44 05 00 95 00 44 05 00 95 00 44 05        ..•.D..•.D..•.D.
00 95 00 44 05 00 95 00 44 05 00 95 00 44 05 00        .•.D..•.D..•.D..
95 00 44 05 00 95 00 44 05 00 95 00 44 05 00 95        •.D..•.D..•.D..•
00 44 05 00 95 00 44 05 00 95 00 44 05 00 95 00        .D..•.D..•.D..•.
44 05 00 95 00 44 05 00 95 00 44 05 00 95 00 44        D..•.D..•.D..•.D
@baud rate of 203400bps
my code is:
Code:
unsigned int rem,temp_num,i;
static char retnum[21];
/**************************
Initialize the serial port
@ 160000 and 230400bps
***************************
Requires :none
Returns : none
**************************/

void uart_init()
{//Enables Serial port  
/*RCSTA RECIEVER CONTROL REGISTER*/  
RCSTA1bits.SPEN = 1;
RCSTA1bits.RX9=0;//8 bit reception
RCSTA1bits.CREN = 1; //enable the reciever

  TRISCbits.TRISC7=0;
  TRISCbits.TRISC6=0;
/*TXSTAx: TRANSMIT STATUS AND CONTROL REGISTER */
TXSTA1bits.BRGH=1;//baud rate high bits    
TXSTA1bits.SYNC=0;//assynchronous mode      
TXSTA1bits.TX9=0; //8 bit reception
TXSTA1bits.TRMT=1;//reset the buffer
TXSTA1bits.CSRC=0;// Master mode  (clock generated internally from BRG)
/*BAUDCONx: BAUD RATE CONTROL REGISTER*/
BAUDCON1bits.BRG16=1;//16 bit Baud rate is enabled

SPBRGH1 = 0;
SPBRG1 = 16;//2;
//(((_XTAL_FREQ/baud)/4)-1);
//FCY/64)/BAUD) - 1;
// set baud to 9600  FCY=20000000
//Turn On the Tx and Rx
//interrptbased reception
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
  PIE1bits.RC1IE=1;
TXSTA1bits.TXEN=1;
RCSTA1bits.CREN=1;
}

//////////////////////////
//Function for sending char
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_char(char data)
{
TXREG1= data;
  while(!TXSTA1bits.TRMT);

}
///////////////////////////
//Function for read to a buffer
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_read2buff()
{
int count;
char ch;

count = 0;
ch = uart_read();
while ((ch > 0) && (count<64))
    {
  g_rxr[count] = ch; // write to buffer
  count=count+1; // increment count
  //uart_numb(count); // echo
  ch = uart_read(); // read next char
}
g_rxr[count]='\0';
//uart_string("out of here");
}
///////////////////////////
//Function for reciveing from serial
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
char uart_read()
{

while(!RC1IF);
RC1IF=0;
return RCREG1;

}
///////////////////////////
//Function for sending number
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_numb(int num)
{

temp_num=num;
if(num==0)
{
    uart_char(48);
}
else
{
while(temp_num!=0)
{
  temp_num=temp_num/10;
  rem=rem++;
}
  sprintf( retnum, "%ul", num );
  for(i=0;i<=(rem-1);i++)
  {
  uart_char(retnum[i]);
  }
  rem=0;
}


}

///////////////////////////
//Function for new line,uart char
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////
void uart_newline()
{
  uart_char(0x0A);
  uart_char(0x0D);

}
///////////////////////////
//Function for sending string
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////

void uart_string(char *str)
//take address vaue of the string in pointer *str
{
    int i=0;
    while(str[i]!='\0')              
    // loop will go on till the NULL charaters is soon in string
    {
        uart_char(str[i]);              
        // sending data on CD byte by byte
        i++;
    }
    return;
}
///////////////////////////
//Function for dumping string
//until 0xff is recieved
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////

void uart_dump(char *str)
//take address vaue of the string in pionter *str
{
  
    for(static int i=0;i<=UART_ARRAY;i++)
    {
        uart_newline();
        uart_numb(i);
        uart_char(':');
        uart_numb(str[i]);
      
                  
        // sending data on CD byte by byte
    }
  
    return;
}


///////////////////////////
//Function for sending string
///////////////////////////
//Requires  : <Xc.h>,Uartinit
//Returns  : None
//Parameters: None
///////////////////////////

void uart_stringln(char *str)
//take address vaue of the string in pionter *str
{
    uart_newline();
//    int i=0;
    while(str[i]!='\0')              
    // loop will go on till the NULL charaters is seen in string
    {
        uart_char(str[i]);              
        // sending data on CD byte by byte
        i++;
    }

}

main:

Code:
#include<xc.h>
#include<string.h>
#include<stdlib.h>
#include "UART.h"
#include "MICRO.h"
#include "global.h"
#include "delays.h"
#define _XTAL_FREQ 16000000

#pragma config XINST  = OFF
#pragma config WDTEN  = OFF
#pragma config FOSC  = HS2
#pragma config CANMX  = PORTB
#pragma config PLLCFG = OFF
void cli();
void sei();


void main()
{
    char read1tag[3]={0x44,0x05,0x01};
    char read2tag[3]={0x44,0x05,0x02};
    char read3tag[3]={0x44,0x05,0x03};
    char read4tag[3]={0x44,0x05,0x04};
    char read5tag[3]={0x44,0x05,0x05};
      
    char errorcode[]="error code";
    uart_init();
    //uart_string("Done!");

  
  
    micro91_check_hardware();
    delay(100);
      micro91_check_firmware();
      uart_newline();
    uart_stringln(errorcode);
    uart_numb(error_code);
    delay(1000);
    g_clear_rxbuff();
    ///////////////////////
    cli();
    char a;
    while(1)
    {
      
        micro91_start_scan();
        a=uart_read();
        if(a==0x44)
        {
            uart_string("oh yeah");
        }
        else
        {uart_string("oh NO");
          
        }
        micro91_stop_scan();      
        g_clear_rxbuff();
    }
    //(strstr(g_rxr,read2tag)))
    //////////////////////

//    uart_dump(g_rxr);
  
while(1);

}


/********************************
Set interrrupt
********************************/

void sei()
{
INTCONbits.GIE=1;
INTCONbits.PEIE=1;
//PIE1bits.RC1IE=1;
}
/********************************
Clear interrrupt
********************************/

void cli()
{
INTCONbits.GIE=0;
INTCONbits.PEIE=0;
// PIE1bits.RC1IE=0;
}
/**********************************
fuction to read from UART interrupt
***********************************
configured on asynchronous mode
configured in uart.c in the function
uart_init()
*********************************/
void interrupt eusart_read()
{

    if(g_count<=UART_ARRAY)
    {
        if (PIR1bits.RC1IF==1)
        {
              RC1IF=0;
            g_rxr[g_count] = RCREG1 ;
              g_count=g_count+1;
        }
    }
    else
    {
    g_count=0;
    }

}


i want to stream until the characters : 0x44, 0x16 is received.
do I need state machine or something?
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top