EmbeddedRelated.com

ADXL345 Driver

May 29, 20131 comment Coded in C for the Microchip PIC16
#define ADXL_SDA  PIN_C4
#define ADXL_SCL  PIN_C3
#define ADXL_CS   PIN_C0

#use i2c(master, sda=ADXL_SDA, scl=ADXL_SCL)

void init_adxl345() 
{
   output_float(ADXL_SCL);
   output_float(ADXL_SDA);
   output_high(ADXL_CS);
}

BOOLEAN adxl345_ready() 
{
   int1 ack;
   i2c_start();            // If the write command is acknowledged,
   ack = i2c_write(0x3a);  // then the device is ready.
   i2c_stop();
   return !ack;
}

void write_adxl345(BYTE address, BYTE data) 
{
   while(!adxl345_ready());
   i2c_start();
   i2c_write(0x3a);
   i2c_write(address);
   i2c_write(data);
   i2c_stop();
}

BYTE read_adxl345(BYTE address) 
{
   BYTE data;

   while(!adxl345_ready());
   i2c_start();
   i2c_write(0x3a);
   i2c_write(address);
   i2c_start();
   i2c_write(0x3b);
   data=i2c_read(0);
   i2c_stop();
   return(data);
}

int16 read_adxl345_axis(BYTE address) 
{
   BYTE msb,lsb;

   while(!adxl345_ready());
   i2c_start();
   i2c_write(0x3a);
   i2c_write(address);
   i2c_start();
   i2c_write(0x3b);
   lsb=i2c_read(1);
   msb=i2c_read(0);
   i2c_stop();
   return((msb<<8)|lsb);
}

Interfacing SM630

May 6, 2013 Coded in C for the Microchip PIC16
#define cmd_add_fingerprint	0x40
#define cmd_search_fingerprint 0x44
#define cmd_packet	0x10
#define data_packet	0x20
#define res_packet	0x30

#define res_rcv_correct 0x01
#define res_rcv_error	0x02
#define res_opr_success	0x31
#define res_finger_detected	0x32
#define res_timeout	0x33
#define res_process_fail 0x34
#define res_para_error	0x35
#define res_fingerprint_found 0x39
#define res_fingerprint_unfound 0x3A

#use rs232(baud=57600,xmit=PIN_C6,rcv=PIN_C7,bits=8,parity=n,stop=1,stream=Finger,timeout=1000)//,force_sw)

int8 cmd_buffer[10],response_buffer[15];

int8 find_checksum(int8 total_byte)
{
	int8 byte_count;
	int16 check_sum=0;
	for(byte_count=0;byte_count<total_byte;byte_count++)
	{
		check_sum+=cmd_buffer[byte_count];
	}
	
	return(make8(check_sum,0));
}

void cmd_to_sm630(int8 total_byte)
{
	int8 byte_count;
	for(byte_count=0;byte_count<total_byte;byte_count++)
	{
		fputc(cmd_buffer[byte_count],Finger);
		delay_us(10);
	}		
}

void response_from_sm630(int8 total_byte)
{
	int8 byte_count;
	while(fgetc(Finger)!=0x4D);
	response_buffer[0]=0x4D;
	for(byte_count=1;byte_count<total_byte;byte_count++)
	{
		response_buffer[byte_count]=fgetc(Finger);
	}			
}		
	

int8 add_finger(int16 finger_id)
{
	cmd_buffer[0]=0x4D; //Packet Head
	cmd_buffer[1]=0x58; //Packet Head
	cmd_buffer[2]=cmd_packet; //Command Packet
	cmd_buffer[3]=0x03; //3 byte length
	cmd_buffer[4]=cmd_add_fingerprint; //Add finger Print cmd
	cmd_buffer[5]=make8(finger_id,1);//Higher byte of finger print id
	cmd_buffer[6]=make8(finger_id,0);//Lower byte of finger print id
	cmd_buffer[7]=find_checksum(7);//Check sum of 7 bytes
	
	cmd_to_sm630(8);
	response_from_sm630(6); //Read 6 bytes
	
	if(response_buffer[4] == res_rcv_correct)
	{
		
		response_from_sm630(7); //Read 6 bytes
		if(response_buffer[5] == res_opr_success)
		{
			//Display Press finger again
			//lcd_goto(2,1);
			//lcd_send_byte("  Press again   ");
			response_from_sm630(7); //Read 6 bytes
		}
	}
	return (response_buffer[5]);		
}	

int8 search_finger(int16& result_id,int16 num_fingerprint)
{
	output_low(PIN_A5);
	cmd_buffer[0]=0x4D; //Packet Head
	cmd_buffer[1]=0x58; //Packet Head
	cmd_buffer[2]=cmd_packet; //Command Packet
	cmd_buffer[3]=0x05; //5 byte length
	cmd_buffer[4]=cmd_search_fingerprint; //Search finger Print cmd
	cmd_buffer[5]=0x00; //Higher byte of Starting id
	cmd_buffer[6]=0x00; //Lower byte of Starting id
	cmd_buffer[7]=make8(num_fingerprint,1);//Higher byte of number of  fingerprints searched
	cmd_buffer[8]=make8(num_fingerprint,0);//Lower byte of number of  fingerprints searched
	cmd_buffer[9]=find_checksum(9);//Check sum of 9 bytes
	
	cmd_to_sm630(10);
	response_from_sm630(6); //Read 6 bytes
	if(response_buffer[4] == res_rcv_correct)
	{
		response_from_sm630(7); //Read 7 bytes
		if(response_buffer[5] == res_opr_success)
		{
				delay_ms(10);
				response_from_sm630(6); //Read 6 bytes
				//disp_response(6);
				if(response_buffer[5] == res_fingerprint_found)
				{
					response_buffer[0]=fgetc(Finger);
					response_buffer[1]=fgetc(Finger);
					result_id=make16(response_buffer[0],response_buffer[1]);
				}
		
	
		}

	}
	else
		response_buffer[5]=response_buffer[4];		
	return (response_buffer[5]);	
		

}

Interfacing SIM300

May 6, 2013 Coded in C for the Microchip PIC16
#use rs232(baud=19200,xmit=pin_C6,rcv=pin_C7,parity=n,bits=8,stop=1,stream=GSM,timeout=100)
 
int8 sms_buffer[40],ph_number[15]; 
 

 
void sync()
 {
	fprintf(GSM,"AT\n\r");
	delay_ms(100);
	fprintf(GSM,"AT\n\r");
	delay_ms(100);
	fprintf(GSM,"AT\n\r");
	delay_ms(100);
	fprintf(GSM,"AT\n\r");
	delay_ms(100);
}

void dial_modem()
{
	int8 i=0;
	fputc('A',GSM);
	fputc('T',GSM);
	fputc('D',GSM);
	fputc(' ',GSM);
	
	while(ph_number[i] !='\0' && i<16)
	{
		fputc(ph_number[i],GSM);
		i++;
	}
	fputc(';',GSM);
	fputc(0x0D,GSM);
	fputc(0x0A,GSM);
} 

void hang_call()
{
	fputc('A',GSM);
	fputc('T',GSM);
	fputc('H',GSM);
	fputc(0x0D,GSM);
	fputc(0x0A,GSM);
}
void set_text_mode()
{
	sync();
	fprintf(GSM,"AT+CMGF=1\n\r");
	
}
int1 get_sms(int8 intex_no)
{
	int8 count=0;
	set_text_mode();
	delay_ms(100);
	fprintf(GSM,"AT+CMGR=%u\n\r",intex_no);
	while(fgetc(GSM)!=0x0A);
	if(fgetc(GSM)=='+')
	{	
		
		while(fgetc(GSM)!='"');
		while(fgetc(GSM)!='"');
		while(fgetc(GSM)!='"');
		do
		{
			ph_number[count]=fgetc(GSM);
			count++;
		}while(ph_number[count-1] !='"' && count<15);
		ph_number[count-1]='\0';
		while(fgetc(GSM)!=0x0A);
		count=0;
		do
		{
			sms_buffer[count]=fgetc(GSM);
			count++;
		}while(sms_buffer[count-1] !=0x0D && count<24);
		sms_buffer[count-1]='\0';
		delay_ms(500);
		sync();	
		fprintf(GSM,"AT+CMGD=%u\n\r",intex_no);//delete sms
		
	}
	else
	{
		count=24;
	}	
	if(count>=24)
		return 0;
	else
	{
		return 1;	
	}	
		
}
void send_sms()
{
	int8 array_index;
	set_text_mode();
	delay_ms(100);
	fprintf(GSM,"AT+CMGS=");
	fputc('"',GSM);
	array_index=0;
	while(ph_number[array_index] !='\0')
	{
		fputc(ph_number[array_index],GSM);
		array_index++;
	}
	fputc('"',GSM);
	fputc(0x0D,GSM);
	fputc(0x0A,GSM);
	while(fgetc(GSM) !=' ');
	array_index=0;
	while(sms_buffer[array_index] !='\0')
	{
		fputc(sms_buffer[array_index],GSM);
		array_index++;
	}
	fputc(0x1A,GSM);// Ctrl-Z
	delay_ms(100);
}		

int8 read_strength()
{
	int8 rssi[3],rssi_byte;
	//output_low(PIN_A5);
	fprintf(GSM,"AT+CSQ\n\r");
	while(fgetc(GSM) !=' ');
	//output_high(PIN_A5);
	rssi[0]=fgetc(GSM) & 0x0F;
	rssi[1]=fgetc(GSM) & 0x0F;
	rssi_byte=rssi[0]*10;
	rssi_byte=rssi_byte+rssi[1];
	return rssi_byte;
}

Interfacing MAX6952

April 15, 2013 Coded in C for the Microchip PIC16
#define max_sdi PIN_C5
#define max_sdo PIN_C4
#define max_clk PIN_C3
#define max_cs  PIN_C2

const int8 user_font[120]= {0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C,
                            0x77,0x6B,0x5D,0x6B,0X77,
                            0x1C,0x22,0x7F,0x22,0x1C};
                                                        

#define config_addr 0x04
#define font_addr   0x05
#define test_addr   0x07
#define plane01_addr     0x20
#define plane02_addr     0x21
#define plane03_addr     0x22
#define plane04_addr     0x23
#define plane11_addr     0x40
#define plane12_addr     0x41
#define plane13_addr     0x42
#define plane14_addr     0x43

int8 config_byte;

void write_to_6952(int8 cmd,int8 data)
{
    int8 i;
    int16 ser_data;
    ser_data=make16(cmd,data);
    //shift_right(&ser_data,2,0);
    output_low(max_clk);
    output_low(max_cs);
    for(i=1;i<=16;++i)
    {
        output_bit(max_sdi,shift_left(&ser_data,2,0));
        output_high(max_clk);
        delay_us(10); 
       // if(i<16)       
            output_low(max_clk);
        //delay_us(6); 
    }
    output_high(max_cs);
    output_low(max_clk);
}   

void write_user_font()
{
    int8 j;
    write_to_6952(font_addr,0x80);
    for(j=0;j<120;j++)
    {
        write_to_6952(font_addr,user_font[j]);
    }    
}    
/*******************************************************************
if state=1,normal operation
if state=0,shutdown
if wrt=1,write to 6952,otherwise update the config_byte register
*******************************************************************/
void shutdown(int1 state, int1 wrt)
{
    if(state)
        config_byte |= 0x01;
    else
        config_byte &= 0xFE;
    if(wrt)
        write_to_6952(config_addr,config_byte);
}
/*******************************************************************
if state=1,enable blinking
if state=0,disable blinking
if wrt=1,write to 6952,otherwise update the config_byte register
*******************************************************************/
void blink_enable(int1 state,int1 wrt )
{
    if(state)
       config_byte |= 8;
    else
       config_byte &= 0xF7;
    if( wrt )
        write_to_6952(config_addr,config_byte);
}

/*************************************************************************
if state=1,put the display in test mode
if state=0,normal mode
Does not affect plane data - original display is restored when set 0.
**************************************************************************/
void display_test(int1 state )
{
     if(state)
        write_to_6952(test_addr,1);
     else
        write_to_6952(test_addr,0);
}

Interfacing ADS7807

April 10, 2013 Coded in C for the Microchip PIC16
#define sclk  PIN_D3
#define sdata PIN_D4
#define rc   PIN_D5
#define busy  PIN_D6
#define cs    PIN_C5

void init_ads7807()
{
    output_low(sclk);
    output_high(rc);
    delay_us(2);
    output_high(cs);
}

int32 read_ads7807()
{
BYTE i;
int32 adc_result=0;

while(!input(busy));
output_low(rc);
delay_cycles(1);
output_low(cs);
delay_us(1);
output_high(cs);
delay_cycles(1);
output_high(rc);
while(!input(busy));
output_low(cs);
    for(i=1;i<=16;++i)
    {
        output_high(sclk);
        shift_left(&adc_result,4,input(sdata));
        output_low(sclk);
    }
output_high(cs);
adc_result*=7629;
return adc_result/10000;
}

SR-87 GPS Module

April 2, 2013 Coded in C for the Microchip PIC16
#use rs232(baud=4800,xmit=PIN_C6,rcv=PIN_C7,bits=8,stop=1,parity=n,stream=GPS,force_sw)

#include<string.h>
#include<stdlib.h>
//char latitude[17],longitude[17],buffer[17];

// Read the lattitude and longitude
void get_position()
{
	int8 rx_count,rx_buffer;
	rx_count=0;
	strcpy(buffer,"GPRMC");
	do
	{
		rx_count=0;
		output_low(PIN_A5);
		while(fgetc(GPS) !='$');
		while((rx_buffer=fgetc(GPS))!=',')
		{
			latitude[rx_count]=rx_buffer;
			rx_count++;
		}
		latitude[rx_count]='\0';
	}while(strcmp(latitude,buffer));
//*****************************************************************
	while(getc(GPS)!=',');
	while(getc(GPS)!=',');
	rx_count=0;
	do
	{
		rx_buffer=fgetc(GPS);
		latitude[rx_count]=rx_buffer;
		rx_count++;
	}while(rx_buffer!=',');
	latitude[rx_count]=fgetc(GPS);
	rx_count++;
	latitude[rx_count]='\0';
	rx_buffer=fgetc(GPS);
	rx_count=0;
	do
	{
		rx_buffer=fgetc(GPS);
		longitude[rx_count]=rx_buffer;
		rx_count++;
	}while(rx_buffer!=',');
	longitude[rx_count]=fgetc(GPS);
	rx_count++;
	longitude[rx_count]='\0';

}	
//wait untill a valid data
void get_fix()
{
   int8 rx_count,rx_buffer;
   rx_count=0;
   strcpy(buffer,"GPRMC");
   do
   {
      do
      {
         rx_count=0;
         while(fgetc(GPS) !='$');
         while((rx_buffer=fgetc(GPS))!=',')
         {
            latitude[rx_count]=rx_buffer;
            rx_count++;
         }
         latitude[rx_count]='\0';
      }while(strcmp(latitude,buffer));
      
      while(fgetc(GPS) !=',');
      rx_buffer=fgetc(GPS);
   }while(rx_buffer !='A');   
   

}

Interface matrix Keypad

March 25, 2013 Coded in C for the Microchip PIC16
/Keypad connection: 

#define col0 PIN_B3
#define col1 PIN_B2
#define col2 PIN_B1
#define col3 PIN_B0
#define row0 PIN_B7
#define row1 PIN_B6
#define row2 PIN_B5
#define row3 PIN_B4

#define num_col 4

// Keypad layout:
char const KEYS[4][4] = {{1,2,3,13},
                         {4,5,6,14},
                         {7,8,9,15},
                         {11,10,12,16}};

void init_keypad()
{
	output_high(row0);
	output_high(row1);
	output_high(row2);
	output_high(row3);
	output_low(col0);
	output_low(col1);
	output_low(col2);
	output_low(col3);	
}

short int ALL_ROWS (void)
{
   if (input (row0) & input (row1) & input (row2) & input (row3))
      return (0);
   else
      return (1);
}

//Will return a key value if pressed or 0 if not 

int8 get_key()
{
int8 row,col=0;
	do	
	{
		switch (col)
		{
			case 0   :
				output_low(col0);
				output_high(col1);
				output_high(col2);
				output_high(col3);
				break;
			case 1   :
				output_high(col0);
				output_low(col1);
				output_high(col2);
				output_high(col3);
				break;
			case 2   : 
				output_high(col0);
				output_high(col1);
				output_low(col2);
				output_high(col3);
				break;
			case 3   : 
				output_high(col0);
				output_high(col1);
				output_high(col2);
				output_low(col3);
				break;                    
		}
		
		if(!input (row0))
			row=1;
		else if(!input (row1))
			row=2;
		else if(!input (row2))
			row=3;
		else if(!input (row3))
			row=4;
		else
			row=0;
		col++;
	}while(!row && col<num_col);	
	if(row)
        return KEYS[row-1][col-1];
    else
        return 0;		
}
//function waits until any key is pressed and released.
int8 get_key_released()
{
int8 _key_temp ;
    do
    {
        _key_temp=get_key();
    }while(!_key_temp);
    init_keypad();
    do
    {
        while(ALL_ROWS());
        delay_ms(50);
    }
    while(ALL_ROWS());
return _key_temp;
}