EmbeddedRelated.com
Code Snippets

Integer to ASCII

Dr. Maykel Alonso January 18, 20132 comments Coded in C
/***** integerToAscii.h *****/

#define BASE_OCTAL			8
#define BASE_DECIMAL			10
#define BASE_HEXADECIMAL		16

#define BUFFER_SIZE			32

char* integerToASCII(long int value, int base);

/***** integerToAscii.c *****/

char* integerToASCII(long int value, int base){
	
	int aux;
	static char buf[BUFFER_SIZE] = {0};

        int isNeg=0;
        if (value == 0) {
                buf[0]='0';
                return &buf[0];
        }
        if (value<0) {
                isNeg = 1;
                value *= -1;
        }	

	for(aux=BUFFER_SIZE; value && aux ; --aux, value /= base){	
		buf[aux] = "0123456789abcdef"[value % base];
	}

	if (isNeg)  {
		buf[aux] = '-';
		return &buf[aux];
	}

	return &buf[aux+1];
}

CRC-16 Calculation

Dr. Maykel Alonso January 16, 20138 comments Coded in C
/***** crc16.h *****/
//Tested
#define CRC16_DNP	0x3D65		// DNP, IEC 870, M-BUS, wM-BUS, ...
#define CRC16_CCITT	0x1021		// X.25, V.41, HDLC FCS, Bluetooth, ...

//Other polynoms not tested
#define CRC16_IBM	0x8005		// ModBus, USB, Bisync, CRC-16, CRC-16-ANSI, ...
#define CRC16_T10_DIF	0x8BB7		// SCSI DIF
#define CRC16_DECT	0x0589		// Cordeless Telephones
#define CRC16_ARINC	0xA02B		// ACARS Aplications

#define POLYNOM		CRC16_XXX   // Define the used polynom from one of the aboves

// It calculates the new crc16 with the newByte. Variable crcValue is the actual or initial value (0).
unsigned int crc16(unsigned int crcValue, unsigned char newByte);

/***** crc16.c *****/

#include "crc16.h"

unsigned int crc16(unsigned int crcValue, unsigned char newByte) 
{
	unsigned char i;

	for (i = 0; i < 8; i++) {

		if (((crcValue & 0x8000) >> 8) ^ (newByte & 0x80)){
			crcValue = (crcValue << 1)  ^ POLYNOM;
		}else{
			crcValue = (crcValue << 1);
		}

		newByte <<= 1;
	}
  
	return crcValue;
}

/***** EXAMPLE *****/

unsigned int exampleOfUseCRC16 (unsigned char *Data, usigned char len){

	unsigned int crc;
	unsigned char aux = 0;

	crc = 0x0000; //Initialization of crc to 0x0000 for DNP
	//crc = 0xFFFF; //Initialization of crc to 0xFFFF for CCITT

	while (aux < len){
		crc = crc16(crc,Data[aux]);
		aux++;
	}

	return (~crc); //The crc value for DNP it is obtained by NOT operation

	//return crc; //The crc value for CCITT
}

Circular FIFO Buffer

Dr. Maykel Alonso January 15, 20137 comments Coded in C
/***** circularBuffer.h *****/

#ifndef CIRCULAR_BUFFER_H_
#define CIRCULAR_BUFFER_H_

#define BUFFER_SIZE 128
#define TYPE char

// Check if the buffer it is full
bool isFull();

// Check if the buffer it is empty	
bool isEmpty();

// Get the first element from the FIFO queue
TYPE getElement();

// Add an element to the FIFO queue
bool addElement(TYPE data);

// Return the number of Elements that are in the FIFO queue
unsigned char getNumberOfElements();

#endif /*CIRCULAR_BUFFER_H_*/

/***** circularBuffer.cpp *****/

#include "circularBuffer.h"

TYPE Buffer[BUFFER_SIZE + 1]; // It needs 1 extra byte to difference full and empty
unsigned char next = 0;
unsigned char first = 0;

bool isFull(){
	if (getNumberOfElements() == BUFFER_SIZE){
		return true;
	}else{
		return false;
	}
}
	
bool isEmpty(){
	if ( next == first ){
		return true;
	}else{
		return false;
	}	
}
	
TYPE getElement(){
	TYPE theElement = 0;
	if (! isEmpty()){	 
		theElement =  Buffer[first];	
		if ( first != BUFFER_SIZE ){
			first++;
		}else{
			first = 0;
		}		
	}

	return theElement;// Return 0 always if it is empty, must be checked before
}

bool addElement(TYPE data){	
	if (!isFull()){
		 Buffer[next] = data;
		 if ( next != BUFFER_SIZE ){
			next++;
		 }else{
			 next = 0;
		 }
		return true;
	}else{
		return false;
	}	
}

unsigned char getNumberOfElements(){	
	if (next >= first){
		return (next - first);
	}else{
		return (BUFFER_SIZE - next + first);
	}	
}