Albrecht Schmidt's Homepage Publications & Presentations Projects Events Ubicomp@Lancaster  
Ubicomp Development Basics & Tools Schematics Software    

Software
(mainly for use within Equator and Smart-Its)

Albrecht Schmidt, albrecht@comp.lancs.ac.uk
Computing Department, Lancaster University,
UK

Core Board demo - RF-Library - Load sensing - Contact Details

 

Note: these pieces of source code should give the reader an idea how programming the devices works. The full code (e.g. all include files) will be available to with the hardware.

 

 

Demo software for the core board

#include "core.h"

unsigned long value;
int i,j;

main() {
	init_core();
	restart_wdt();
	
	j=0; // counter
	 
	led_on();		   
	delay_ms(400);
   	led_off();
   	delay_ms(400);
   	led_on();		   
	delay_ms(400);
   	led_off();
   	
   	restart_wdt();
 
	setup_port_a( ALL_ANALOG );
   	setup_adc( ADC_CLOCK_INTERNAL );


   	while(1) {
   		restart_wdt();
 		j++;
 		
   		// read all analog channels
   		for(i=0;i<=5;i++){
   			set_adc_channel( i );
			delay_us(10);
			value = Read_ADC();
			printf("Ch[%i]=%li\n", i, value);
		}
		
		led_on();		   
		delay_ms(500);
	   	led_off();

		// write 7 bits to port b 
		for(i=0;i<1000;i++){
			output_high(PIN_B1);
			output_high(PIN_B2);
			output_high(PIN_B3);
			output_high(PIN_B4);
			output_high(PIN_B5);
			output_high(PIN_B6);
		   	output_high(PIN_B7);
		   	delay_us(1);
		   	output_low(PIN_B1);
			output_low(PIN_B2);
			output_low(PIN_B3);
			output_low(PIN_B4);
			output_low(PIN_B5);
			output_low(PIN_B6);
		   	output_low(PIN_B7);
		}
		
		led_on();		   
		delay_ms(500);
	   	led_off();

		//read 7 bits from port b
		printf("B1-7: %i ", input(PIN_B1));
		printf("%i ", input(PIN_B2));
		printf("%i ", input(PIN_B3));
		printf("%i ", input(PIN_B4));
		printf("%i ", input(PIN_B5));
		printf("%i ", input(PIN_B6));
		printf("%i\n", input(PIN_B7));
		
		led_on();		   
		delay_ms(500);
	   	led_off();
	   	
	   	reset_rf_buffer();
	   	printf(to_rf_buffer, "counting ... %i\n", j); 
	   	RF_printf();
	}
}

Core Board Software
Library for RF communication

This library is used for RF communication in the core board. The RF-chip used is the BIM2 module from Radiomextrix.

// Basic RF functions
// designed for the BIM2 board using a PIC16F876

// Lancaster University, http://www.comp.lancs.ac.uk/
// 27.03.2002 by Albrecht Schmidt
// http://www.comp.lancs.ac.uk/~albrecht/

// developed for the vision smart-it / weight boards 
// compiled with CCS PCM C Compiler, Version 3.080

// packet format
// <10011001>

// you must define the following in your code!
// #define RF_TX_PIN PIN_C3
// #define RF_RX_PIN PIN_C0
// #define RF_CD_PIN PIN_B0
// #define RF_TX_ENABLE_PIN PIN_C2
// #define RF_RX_ENABLE_PIN PIN_C1
// #define RF_SPEED 19200
//                or 38400

// functions:
// void reset_rf_buffer() - clear buffer, use before printf
// void to_rf_buffer(char c) - as first argument in printf 
// void RF_printf() - print the buffer over RF
//
// void RF_put_long(unsigned long ldata) - write a long to RF
// void RF_putc(char data) - write a char to RF
// char RF_getc() - get a char from RF
// int RF_kbhit() - check for a char on RF
//
// unsigned long crc16(char * msg, int len) - calculate a crc16 offline
//
// void rfPowerDown() - switch BIM2-module in power down mode
// void rfTxOn() - switch BIM2-module in transmission mode
// void rfRxOn() - switch BIM2-module in receive mode
// void rfSelfTest() - switch BIM2-module in self test mode
//
// int rfTransmit(char * msg, int len) - transmit a number of characters via RF
// int rfReceive(char *buf, int maxLen) - receive a packet via RF
// int rfReceiveOnCD(char *buf, int maxLen, long timeOut) - receive a packet via RF
//
// void RF_version() - print the version over RF


// important global variable:
// rf_buffer

// maximal number of chars to sent/receive in one packet
#define MAXBUF 80

#use rs232(baud=RF_SPEED,xmit=RF_TX_PIN,rcv=RF_RX_PIN)

// buffer used for printing to RF and for receiving packages
byte rf_buffer[MAXBUF];
// pointer to the position in the buffer
int rf_buffer_ptr;

// version number of this program
const float rf_version_no=0.01;

// the table for the CRC calculation
const unsigned long crc_tbl[16]=
{
	0x0000, 0xCC01,	0xD801,	0x1400,	0xF001,	0x3C00,	0x2800,	0xE401,
	0xA001,	0x6C00,	0x7800,	0xB401,	0x5000,	0x9C01,	0x8801,	0x4400
};

// reset the buffer pointer
void reset_rf_buffer()
{
	rf_buffer_ptr=0;
}

// fill the buffer with chars
// used as first parameter in printf
void to_rf_buffer(char c)
{ 
	if (rf_buffer_ptr> 4);
		crc = crc_tbl[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
	}
	return crc;
}



// switch BIM2-module in power down mode
void rfPowerDown()
{
	// disable RX, disable TX
	output_high(RF_TX_ENABLE_PIN);
	output_high(RF_RX_ENABLE_PIN);
}

// switch BIM2-module in transmission mode
void rfTxOn()
{
	// disable RX, enable TX
	output_low(RF_TX_ENABLE_PIN);
	output_high(RF_RX_ENABLE_PIN);
}

// switch BIM2-module in receive mode
void rfRxOn()
{
	// enable RX, disable TX
	output_high(RF_TX_ENABLE_PIN);
	output_low(RF_RX_ENABLE_PIN);
}

// switch BIM2-module in self test mode
void rfSelfTest()
{
	// enable RX, enable TX
	output_low(RF_TX_ENABLE_PIN);
	output_low(RF_RX_ENABLE_PIN);
}

// transmit a number of characters via RF
int rfTransmit(char * msg, int len)
{
#use rs232(baud=RF_SPEED,xmit=RF_TX_PIN,rcv=RF_RX_PIN)
	int i;
	char ch;
	// for inline crc calculation
	unsigned long crc;
	crc = 0xFFFF;

	i=0;

	// transmitter on
	rfTxOn();
	delay_ms(3); // according to the datasheet of BIM (old BIM1 version)

	//preample 18Byte ~ 180 bit ~ fastest case @57600bit/s ~ 3ms
	for (i=0;i<18;i++){
		RF_putc(85); // sent '01010101'
	}
	// two bytes with FF
	RF_putc(255);
	RF_putc(255);
	// indicate the start id '10011001' - selected by me 
	// no reason for the pattern...
	RF_putc(153);
	// sent len - number of data bytes to follow
	ch=len;
	RF_putc(ch);
	// sent the message - be aware will perform poorly if data is unsymetric!!!
	for (i=0;i> 4);
		crc = crc_tbl[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
	}
	// sent two bytes CRC - low byte first, the high byte
	RF_put_long(crc);
	rfPowerDown();
	
	return 1;
}

// receive a packet via RF
int rfReceive(char *buf, int maxLen)
{
#use rs232(baud=RF_SPEED,xmit=RF_TX_PIN,rcv=RF_RX_PIN)
	int j, packetLen;

	// storage for bytes returned
	byte  ch, crc0, crc1;
	// crc is used for inline CRC calculation, 
	// crc_rec is used to store the crc received in crc0 and crc1
	unsigned long i, crc, crc_rec;

	// initialize CRC
	crc = 0xFFFF;

	for (j=0;j= MAXBUF) 
	{
		// packet to long
		packetLen=MAXBUF - 2;
	}

	// read the data bytes now
	i=0;
	while ( i> 4);
		crc = crc_tbl[((ch >> 4) ^ crc) & 15] ^ (crc >> 4);
		// store it in the buffer
		buf[i]=ch;
		i++;
	}
	// data bytes done, ... read the CRC
	crc0 = RF_getc();
	crc1 = RF_getc();
	// calc 16bit long from the bytes
	crc_rec = crc1*256+crc0;

	// put a zero at the end - make it printable for a string 
	buf[packetLen]=0;		

	// check the CRC...
	if (crc==crc_rec) {
		// packet ok
		return 0;
	} else {
		// packet currupt
		return 2;
	}
}

// print the buffer over RF
void RF_printf()
{
	rfTransmit(rf_buffer,rf_buffer_ptr);	
}

// print the version over RF
void RF_version()
{
	reset_rf_buffer();
	printf(to_rf_buffer, "RF V%f, 27/03/02", rf_version_no);
	RF_printf();
}

// receive a packet via RF
int rfReceiveOnCD(char *buf, int maxLen, long timeOut)
{
	long i;
	int ret;
 	rfRxOn();
	i=0;
	ret = 5;
	while(i
RF-Library
Mini Software for the Load Sensing Board
#include <16F876.H>

#device PIC16F876 *=16 ADC=10

#fuses HS, WDT, NOBROWNOUT, NOPROTECT, NOLVP



#use delay(clock=20000000)
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)

#define INIT_WAIT_TIME 5


#define RF_TX_PIN PIN_C3
#define RF_RX_PIN PIN_C0
#define RF_CD_PIN PIN_B0
#define RF_TX_ENABLE_PIN PIN_C2
#define RF_RX_ENABLE_PIN PIN_C1
#define RF_SPEED 19200
//#define RF_SPEED 38400

#include "bim2rf.c"

long value[5], sum, wait_time;
int display_mode, spool_mode, rf_mode;

// char rec_buffer[MAXBUF];

const char object_id = 'F';

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
void put_long(unsigned long ldata)
{
	#byte ldataL = ldata
	#byte ldataH = ldata+1

	putchar(ldataL); // low byte first
	putchar(ldataH);
}

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
print_version()
{
   printf("Load/RF OS 0.1, build 001 - 27/03/2002\r\n");
   printf("Albrecht Schmidt\r\n");
}

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
void read_all()
{
	int i;
	for(i=0;i<=4;++i) {
		set_adc_channel( i );
		delay_us(50);
		value[i] = Read_ADC();
	}
	sum =  value[0] + value[1] + value[2] + value[3];
}

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
void print_bin_value()
{
	put_long(value[0]);
	put_long(value[1]);
	put_long(value[2]);
	put_long(value[3]);
	put_long(sum);
	put_long(value[4]);
}

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
void print_values()
{
	if (display_mode == 0 || display_mode ==3) {
		printf("%4lu:%4lu:%4lu:%4lu#%4lu@%4lu", 
		       value[0], value[1], value[2], value[3], sum, value[4]);
		if (display_mode == 3) {
			printf("\r");
		} else {
			printf("\n\r");
		}
	}
	if (display_mode == 1) {
		put_long(value[0]);
		put_long(value[1]);
		put_long(value[2]);
		put_long(value[3]);
		put_long(sum);
		put_long(value[4]);
		putchar(0);
	}
	if (display_mode == 2) {
		printf("%x:%x:%x:%x#%x@%x\r\n", 
		       value[0], value[1], value[2], value[3], sum, value[4]);
	}
}  

print_values_rf()
{
	disable_interrupts(global);
	disable_interrupts(int_rda);

	reset_rf_buffer();
	if (display_mode != 2 ) {
		printf(to_rf_buffer, "%c:%4lu:%4lu:%4lu:%4lu;%4lu;%4lu\r\n",
		       object_id, value[0], value[1], value[2], 
		       value[3], sum, value[4]);
	} else {
		printf(to_rf_buffer, "%c:%x:%x:%x:%x#%x@%x", 
		       object_id, value[0], value[1], value[2], 
		       value[3], sum, value[4]);
	}
	RF_printf();

	enable_interrupts(global);
	enable_interrupts(int_rda);

}

void set_time()
{
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
	int i;
	char inp_c;
	printf("\n\r *** Timing Sub Menu            ***\n\r");
	printf("\n\r ----\n\r");
	printf("\n\r 5 ~ 1000  Hz\n\r");
	printf("\n\r 4 ~  250  Hz\n\r");
	printf("\n\r 3 ~  100  Hz\n\r");
	printf("\n\r 2 ~   10  Hz\n\r");
	printf("\n\r 1 ~    1  Hz\n\r");
	printf("\n\r ----\n\r");
	printf("\n\r ----------------please select ...\n\r");

	inp_c=0;
	while (inp_c==0) // wait for the start id '10011001'
	{
		// check if a character is available
		if (kbhit())
		{
			// if yes then get
			inp_c=getc();
		}
		restart_wdt();
	}
	printf("\n\rOK ... %c\n\r", inp_c);
	switch (inp_c) {
		case '5':	wait_time = 1;
				break;
		case '4':	wait_time = 4;
				break;
		case '3':	wait_time = 10;
				break;
		case '2':	wait_time = 100;
				break;
		case '1':	wait_time = 1000;
				break;
	};
}

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
#int_rda
void data_in()
{
  
	char input_char;
	input_char = getc();

	if ((input_char=='G')||(input_char=='g')||(input_char=='p')) {
		read_all();
		if (input_char =='G') {
			print_bin_value();
		} else {
			if (input_char == 'g') {
				print_values();
			} else {
				print_values_rf();
			}
		}
	} else {
#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
		switch (input_char) {
			case 'a':	display_mode = 0;
					printf("\n\rASCII\n\r");
					break;
			case 'b':	display_mode = 1;
					printf("\n\rBIN\n\r");
					break;
			case 'x':	display_mode = 2;
					printf("\n\rHEX\n\r");
					break;
			case 'e':	display_mode = 3;
					printf("\n\rEXCEL\n\r");
					break;
			case 'r':	printf("\n\rRF receive\n\r");
					rf_mode = 1;
					break;
			case 't':	printf("\n\rRF transmit\n\r");
					rf_mode = 2;
					break;
			case 'n':	printf("\n\rno RF\n\r");
					rf_mode = 0;
					break;
			case 'i':	set_time();
					break;
			case 's':	if (spool_mode==1) { 
						spool_mode =0;
					} else {
						spool_mode = 1;
					}
					printf("\n\rSPOOL\n\r");
					break;
			case 'v':	print_version();
					break;
			default:	printf("\n\r *** Mini Load/RF OS 0.1 ***\n\r");
					printf(" ----\n\r");
					printf(" m = menu\n\r");
					printf("\n\r a = ascii\n\r");
					printf(" b = binary\n\r");
					printf(" e = excel mode\n\r");
					printf(" x = hex\n\r");
					printf("\n\r s = streaming on/off\n\r");
					printf("\n\r g = get a ascii value\n\r");
					printf(" p = sent value via RF\n\r");
					printf(" G = get a bin value\n\r");
					printf("\n\r r = RF-receive mode\n\r");
					printf(" t = RF-transmit mode\n\r");
					printf(" n = RF off\n\r");
					printf("\n\r i = time interval\n\r");
					printf(" v = version\n\r");
					printf("\n\r ----");
					printf(" ----------------please select ...\n\r");
		}
	}
}




#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)
main() {
   int i,j, ret;
   
   setup_wdt(WDT_2304MS);
   printf("booting load sensor...\r\n");
   print_version();
	   
   output_high(PIN_B2);
   delay_ms(200);
   output_high(PIN_B3);
   delay_ms(1000);
   
   restart_wdt();
   output_low(PIN_B2);
   delay_ms(200);
   output_low(PIN_B3);
   delay_ms(500);
   output_high(PIN_B2);
   delay_ms(200);
   
   restart_wdt();
   output_high(PIN_B3);
   delay_ms(1000);
   output_low(PIN_B2);
   output_low(PIN_B3);
 
   restart_wdt();
   setup_port_a( ALL_ANALOG );
   setup_adc( ADC_CLOCK_INTERNAL );

   display_mode = 0;
   spool_mode = 0;
   rf_mode=0;
   wait_time = INIT_WAIT_TIME;
   enable_interrupts(global);
   enable_interrupts(int_rda);

#use rs232(baud=115200,xmit=PIN_C6,rcv=PIN_C7)  
   printf("\n\rOS is running!.('M' for menu)\r\n");
 
   set_adc_channel( 0 );

   while(1) {
	restart_wdt();
	if (display_mode ==0) {	output_high(PIN_B2);}
	if (display_mode ==1) {	output_low(PIN_B2);}
	if (spool_mode ==0) {	output_high(PIN_B3);}
	if (spool_mode ==1) {	output_low(PIN_B3);}

     	if (spool_mode == 1) {
		read_all();
		print_values();
	}

	disable_interrupts(global);
	disable_interrupts(int_rda);
     	if (rf_mode == 1) {
		ret = rfReceiveOnCD(rf_buffer, MAXBUF, 900);
		if (ret==0) {
			printf("%s", rf_buffer);
		}
	}

     	if (rf_mode == 2) {
		read_all();
		print_values_rf();
	}

	enable_interrupts(global);
	enable_interrupts(int_rda);
     	
	if (rf_mode != 1) {
		delay_ms(wait_time);	
	}
	
   } while (TRUE);
}

    

Load sensing Software

Albrecht Schmidt, MSc, Dipl. Inf.

Computing Department
Engineering Building, Room A13
Lancaster University
Lancaster, UK
LA1 4YR

 

Tel: +44 (0) 1524 593786
Fax: +44 (0) 1524 593608

E-Mail: albrecht@comp.lancs.ac.uk
Web: http://www.comp.lancs.ac.uk/~albrecht/

Contact Details