How to use mcc generated code for UART ISR

This forum handles questions and discussions concerning Microchip’s MPLAB® Code Configurator (MC2).

How to use mcc generated code for UART ISR

Postby garryp123 » Tue Feb 07, 2017 4:46 am

Hello all,
I am working on Some embedded project[PIC MCU] where I will receive the data over UART..The received data will be usede to manipulate the operation of some RGB LEDs
For the moment I use polling method but I want to use Uart_ISR function to operate this
SO my question is How to use the MCC generated and overall the ISR function?
many thanks
Here is my code:
main.c

Code: Select all
#include "mcc_generated_files/mcc.h"
#include <stdlib.h>
#include <stdio.h>
#include "atoh.h"
#include "LED.h"
#define _XTAL_FREQ 16000000
#define FRAMESIZE 18
void main(void)
{
 uint8_t data ,i,j;
 
 uint16_t R_value, G_value ,B_value;
 uint8_t value;
 uint8_t RX_Buffer[FRAMESIZE] = {0};
 uint8_t RGB_data[6] ,HEX_data[6];
// initialize the device
 SYSTEM_Initialize();
 INTERRUPT_GlobalInterruptEnable(); // Enable the Global Interrupts
 INTERRUPT_PeripheralInterruptEnable(); // Enable the Peripheral Interrupts
 
 
 while(1)
 {
 // EUSART_Write(0x61);
 // if(RCIF == 1)
 while (!RCIF)
 {
 data = EUSART_Read(); // Read received character
 for (i = 0; i<FRAMESIZE ; i++)
 {
 RX_Buffer[i] = data;
 }
 
 EUSART_Write(data);
 }
 
 //check if any data is received
 
 for (j = 0 ; j = 5; j++ ) // get the RGB value in the separate array
 {
 RGB_data[j] = RX_Buffer[j+3];
 HEX_data[value] = RGB_data[j]/16;
 }
if(RX_Buffer[0]=='R' && RX_Buffer[FRAMESIZE-1] == '\n')
 {
 //ASCII to HEX separate values by string functions
 
 uint32_t number = (uint32_t)strtol(HEX_data, NULL, 16);
 R_value = number >>16;
 G_value = (number & 0xffff) >> 8;
 B_value = (number & 0x0000FF);
//ASCII to HEX separate values by AtoH function
 // R_value = (uint16_t) atoh(HEX_data[0], HEX_data[1]);
 // G_value = (uint16_t) atoh(HEX_data[2], HEX_data[3]);
 // B_value = (uint16_t) atoh(HEX_data[4], HEX_data[5]);
}
 
 SetLedColor(R_value,G_value,B_value);
 }
}


and my EUSART.c
Code: Select all
#include "eusart.h"
/**
 Section: Macro Declarations
*/
#define EUSART_TX_BUFFER_SIZE 8
#define EUSART_RX_BUFFER_SIZE 8
/**
 Section: Global Variables
*/
static uint8_t eusartTxHead = 0;
static uint8_t eusartTxTail = 0;
static uint8_t eusartTxBuffer[EUSART_TX_BUFFER_SIZE];
volatile uint8_t eusartTxBufferRemaining;
static uint8_t eusartRxHead = 0;
static uint8_t eusartRxTail = 0;
static uint8_t eusartRxBuffer[EUSART_RX_BUFFER_SIZE];
volatile uint8_t eusartRxCount;
/**
 Section: EUSART APIs
*/
void EUSART_Initialize(void)
{
 // disable interrupts before changing states
 PIE1bits.RCIE = 0;
 PIE1bits.TXIE = 0;
// Set the EUSART module to the options selected in the user interface.
// ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
 BAUDCON = 0x08;
// SPEN enabled; RX9 8-bit; CREN enabled; ADDEN disabled; SREN disabled;
 RCSTA = 0x90;
// TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
 TXSTA = 0x24;
// Baud Rate = 9600; SPBRGL 160;
 SPBRGL = 0xA0;
// Baud Rate = 9600; SPBRGH 1;
 SPBRGH = 0x01;

 // initializing the driver state
 eusartTxHead = 0;
 eusartTxTail = 0;
 eusartTxBufferRemaining = sizeof(eusartTxBuffer);
eusartRxHead = 0;
 eusartRxTail = 0;
 eusartRxCount = 0;
// enable receive interrupt
 PIE1bits.RCIE = 1;
}
uint8_t EUSART_Read(void)
{
 uint8_t readValue = 0;
 
 while(0 == eusartRxCount)
 {
 }
readValue = eusartRxBuffer[eusartRxTail++];
 if(sizeof(eusartRxBuffer) <= eusartRxTail)
 {
 eusartRxTail = 0;
 }
 PIE1bits.RCIE = 0;
 eusartRxCount--;
 PIE1bits.RCIE = 1;
return readValue;
}
 
void EUSART_Write(uint8_t txData)
{
 while(0 == eusartTxBufferRemaining)
 {
 }
if(0 == PIE1bits.TXIE)
 {
 TXREG = txData;
 }
 else
 {
 PIE1bits.TXIE = 0;
 eusartTxBuffer[eusartTxHead++] = txData;
 if(sizeof(eusartTxBuffer) <= eusartTxHead)
 {
 eusartTxHead = 0;
 }
 eusartTxBufferRemaining--;
 }
 PIE1bits.TXIE = 1;
}
void EUSART_Transmit_ISR(void)
{
// add your EUSART interrupt custom code
 if(sizeof(eusartTxBuffer) > eusartTxBufferRemaining)
 {
 TXREG = eusartTxBuffer[eusartTxTail++];
 if(sizeof(eusartTxBuffer) <= eusartTxTail)
 {
 eusartTxTail = 0;
 }
 eusartTxBufferRemaining++;
 }
 else
 {
 PIE1bits.TXIE = 0;
 }
}
void EUSART_Receive_ISR(void)
{
if(1 == RCSTAbits.OERR)
 {
 // EUSART error - restart
RCSTAbits.CREN = 0;
 RCSTAbits.CREN = 1;
 }
// buffer overruns are ignored
 eusartRxBuffer[eusartRxHead++] = RCREG;
 if(sizeof(eusartRxBuffer) <= eusartRxHead)
 {
 eusartRxHead = 0;
 }
 eusartRxCount++;
}
void EUSART_Put(const unsigned char *string)
{
 int i;
 for (i=0;string[i]!='\0';i++)
 {
 EUSART_Write(string[i]);
 }
}
garryp123
 
Posts: 1
Joined: Thu Dec 22, 2016 11:10 am

Re: How to use mcc generated code for UART ISR

Postby AussieSusan » Mon Mar 06, 2017 2:50 am

Right now I can't see where the EUSART is initialised (the call presumably to EUSART_Initialize' in the EUSART.c file) but that is where the EUSART will be set up for use. Perhaps it is in the 'SYSTEM_Initialize' function.
However in that function it enables the receive interrupt which will process each received character and put it the 'eusartRxBuffer' array and increment the 'eusartRxCount' variable. To read a character you need to call 'EUSART_Read' understanding that it is a blocking function that will only return when a character has been received. Personally I would like to see a function that let you see if there are received characters but not block - but oh well.
I have not used MCC (in my opinion much of what it generates is overhead and hides what is really going on - and I think I can write specific code that does what I want that I know how it behaves!)
Susan
AussieSusan
Verified identity
 
Posts: 115
Joined: Mon Jun 16, 2014 4:45 am
PIC experience: Experienced Hobbyist


Return to MPLAB® Code Configurator

Who is online

Users browsing this forum: No registered users and 1 guest