Converting C files to PDE

How should I convert C/ C++ files to pde files? I have some example code for Atmega written in C, and also had .h header files. How do I turn them into arduino compatible? And load the libraries? Thanks so much!

How should I convert C/ C++ files to pde files?

Using a text editor. There is no one-size-fits-all magic answer. The Arduino is programmed using C++. But, not all C++ code will work on the Arduino. There is no operating system, for instance, so OS-specific stuff won't. There is limited memory, so image processing is out.

What does the mysterious C/C++ code do?

PDE? Aren't we on .ino files this week?

Mark

You could just put the .c and .h files into your sketch directory, and add calls to them in your sketch. Depending what the code does, you might find that it needs some work to make it coexist with the rest of the Arduino runtime software, but without seeing the code I have no idea how hard that might be.

I am using sparkfun transceivers for radio transmission. https://www.sparkfun.com/products/10154They have an example code attached at the bottom of the page. I copied it here, this is called the Main and is a C source file. There is also have a main-transmit.hex file, and a Make File and a header file for RFM22. Not sure how exactly to load them…I just copied them to arduino due library. I tried opening by changing the extension to .pde. Didn’t really work. I am using 1.5.2 version.

/======================//
//======================//
#include <avr/io.h>
#include <stdio.h>
#include "RFM22.h"
//======================//
//======================//

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))

//======================//
//Arduino Pro Specific Defines
#define LED 5

//RFM22 Defines
#define CS 0 //pin for chip select
#define NIRQ 4
#define TXANT 5	// PD5
#define RXANT 6	// PD6

//======================//
//======================//

void ioinit(void);
void delay_ms(uint16_t x);
void delay_us(uint16_t x);

//======================//
//======================//

static int uart_putchar(char c, FILE *stream);
uint8_t uart_getchar(void);
static FILE mystdout = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE);

//======================//
//======================//

void init_SPI(void);
void init_RFM22(void);
void to_tx_mode(void);
void checkINT(void);
void get_packet(void);
void txdata(char data);
char rxdata(void);
char read(uint8_t address);
void write(uint8_t address, char data);

//======================//
//Global Variables
unsigned char tx_buf[17] = {0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F, 0x78};

int main(void)
{
	char temp, inkey;
	unsigned char i;

	ioinit();
	init_SPI();
	sbi(PORTB,CS);
	printf("/n/n********RFM22 Communication Test********\n");

	//====================//
	//Communications Test
	
	temp = read(DTYPE);
	temp = read(DVERS);
	temp = read(INTEN1);
	temp = read(INTEN2);
	temp = read(OMFC1);
	temp = read(OMFC2);
	
	printf("*****************************************\n\n");
	
	init_RFM22();	// Initialize all RFM22 registers
	to_tx_mode();	// Send test packet	'0123456789:;<=>?"
	
	/*// This exampes sends '0123456789:;<=>?' once a second
	while(1)
	{
		to_tx_mode();
		printf("Transmit Done\n");
		delay_ms(1000);
	}
	*/
	
	// This example allows you to enter a 16-byte packet to send
	printf("Entering TX Mode...Give me a 16-byte packet\n\n");
	while(1)
	{
		
		get_packet();
		printf("Transmitting: ");
		for (i=0; i<17; i++)
			printf("%c", tx_buf[i]);
		printf("\n");
		to_tx_mode();
		printf("Transmit done...Press any key to transmit again\n\n");
	}
	
	/*
	// This example sends a 'beep' to the terminal every second
	// You'll love it...
	int count = 0;
	unsigned char chksum;
	
	for (i=0; i<15; i++)
		tx_buf[i] = 0x07;
	tx_buf[15] = 0x31;
	
	chksum = 0;
	for(i=0; i<16; i++)
		chksum += tx_buf[i];
	
	tx_buf[16] = chksum;
	printf("chksum == %x\r", tx_buf[16]);
	
	while(1)
	{
		to_tx_mode();
		delay_ms(1000);
		
		printf("Transmission Complete...%d\n", count);
		count++;
		
	}*/
}

// Initialize the RFM22 for transmitting
void init_RFM22(void)
{
	write(INTEN2, 0x00);	// Disable all interrupts
	write(OMFC1, 0x01);		// Set READY mode
	write(0x09, 0x7F);		// Cap = 12.5pF
	write(0x0A, 0x05);		// Clk output is 2MHz
	
	write(0x0B, 0xF4);		// GPIO0 is for RX data output
	write(0x0C, 0xEF);		// GPIO1 is TX/RX data CLK output
	write(0x0D, 0x00);		// GPIO2 for MCLK output
	write(0x0E, 0x00);		// GPIO port use default value
	
	
}

void to_tx_mode(void)
{
	unsigned char i;
	
	write(0x07, 0x01);	// To ready mode
	cbi(PORTD, RXANT);
	sbi(PORTD, TXANT);
	delay_ms(50);
	
	write(0x08, 0x03);	// FIFO reset
	write(0x08, 0x00);	// Clear FIFO
	
	write(0x34, 64);	// preamble = 64nibble
	write(0x3E, 17);	// packet length = 17bytes
	for (i=0; i<17; i++)
	{
		write(0x7F, tx_buf[i]);	// send payload to the FIFO
	}

	write(0x05, 0x04);	// enable packet sent interrupt
	i = read(0x03);		// Read Interrupt status1 register
	i = read(0x04);
	
	write(0x07, 9);	// Start TX
	
	while ((PIND & (1<<NIRQ)) != 0)
		; 	// need to check interrupt here
	
	write(0x07, 0x01);	// to ready mode
	
	cbi(PORTD, RXANT);	// disable all interrupts
	cbi(PORTD, TXANT);
}

void get_packet(void)
{
	unsigned char i, chksum;
	
	for(i=0; i<16; i++)
	{
		tx_buf[i] = uart_getchar();
		printf("Received %c, %d characters remaining for packet\n", tx_buf[i], 15-i);
	}
	
	chksum = 0;
	for(i=0; i<16; i++)
		chksum += tx_buf[i];
	
	tx_buf[16] = chksum;
}

void checkINT(void)
{
	if ((PIND & (1<<NIRQ)) == 0)
		printf("INT == 0\n");
	else
		printf("INT == 1\n");
}

void write(uint8_t address, char data)
{
	//write any data byte to any single address
	//adds a 0 to the MSB of the address byte (WRITE mode)
	
	address |= 0x80;

	cbi(PORTB,CS);
	delay_ms(1);
	txdata(address);
	delay_ms(1);
	txdata(data);
	delay_ms(1);
	sbi(PORTB,CS);
}

char read(uint8_t address)
{
	//returns the contents of any 1 byte register from any address
	//sets the MSB for every address byte (READ mode)

	char byte;
	
	address &= 0x7F;

	cbi(PORTB,CS);
	txdata(address);
	byte = rxdata();
	sbi(PORTB,CS);

	return byte;
}

char rxdata(void)
{
	SPDR = 0x55;
	while((SPSR&0x80) == 0x00)
		;

	return SPDR;
}

void txdata(char data)
{
	SPDR = data;
	while((SPSR&0x80) == 0x00);
}

void init_SPI(void)
{
	// enable SPI
	// make SPI master
	// SCLK idle low
	// sample data on rising edge
	// Fosc/4 is SPI frequency = 2MHz
	//SPCR |= 0b01010000;	// Fosc/4
	SPCR |= 0b01010011;		// Fosc/128
}

static int uart_putchar(char c, FILE *stream)
{
    if (c == '\n') uart_putchar('\r', stream);

    loop_until_bit_is_set(UCSR0A, UDRE0);
    UDR0 = c;

    return 0;
}

uint8_t uart_getchar(void)
{
    while( !(UCSR0A & (1<<RXC0)) );
    return(UDR0);
}

That appears to be a complete program. If you wanted to convert that to a sketch then you would need to remove the main() function and insert setup() and loop() functions. The setup() function will be called once and should contains the one-off initialisation; the loop() function is called repeatedly and should contain the recurring behaviour.