Hello,
I am working on a project to build a tracking device for sports. It is required to capture GPS, compass, accelerometer and the heartbeat data every second and store to an EEPROM. I have written the C code for this, but am having some issues with storing the data onto the EEPROM. It picks up the sensors data, but gets stuck at the STORE_DATA function in my code.
This function is meant to count the characters from the strings each of the sensor outputs (GPS, COMPASS, Heart Beat per minute) and write them to the eeprom. If anyone can notice anything wrong with the code or offer any advice it would be really appreciated!
Thanks.
I am working on a project to build a tracking device for sports. It is required to capture GPS, compass, accelerometer and the heartbeat data every second and store to an EEPROM. I have written the C code for this, but am having some issues with storing the data onto the EEPROM. It picks up the sensors data, but gets stuck at the STORE_DATA function in my code.
This function is meant to count the characters from the strings each of the sensor outputs (GPS, COMPASS, Heart Beat per minute) and write them to the eeprom. If anyone can notice anything wrong with the code or offer any advice it would be really appreciated!
Thanks.
Code:
#define Clock_8MHz
#define Baud_9600
#include <16F819.h>
#include <24512.c>
#include <stdio.h>
#include <stdlib.h>
#fuses INTRC,NOWDT,NOPROTECT,MCLR,NOBROWNOUT,PUT
#use delay(clock=8MHZ)
#use rs232(baud=9600, xmit=PIN_A7, STREAM=HOSTPC) //DEFINE RS232 PINS HERE *******
#use rs232(baud=9600, xmit=PIN_B1, rcv=PIN_B0, STREAM=GPS)
#use I2C(master, sda=PIN_A1, scl=PIN_A0) //DEFINE I2C PINS HERE *********
#define STATUS_LED PIN_A3
#define BUTTON PIN_A2 //Press to upload to PC
#define PORTA 0x05
#define TRISA 0x85
#define PORTB 0x06
#define TRISB 0x86
#define HEARTIN PIN_B5
#define I2C_DELAY 10
#define ACK 0
#define NO_ACK 1
#define DEVICE_WRITE 0x32 //Default HMC6343 I2C address - write
#define DEVICE_READ 0x33 //Default HMC6343 I2C address - read
#define uns8 unsigned char
#bit SCL = PORTA.0
#bit SDA = PORTA.1
void boot_up(void);
void send_command(unsigned char command_byte);
void i2c_ack_polling(uns8 device_address);
void GET_COMPASS();
void TestEprom();
void GET_GPS();
void STORE_DATA();
void UPLOAD_DATA();
void CLEAR_EPROM();
unsigned char gps_data[45], gps_data2[40], hmc_data[6], c, HR, HB, HC;
unsigned long long count, address;
#INT_RB
void HEARTBEAT(){
HR+=1;
}
void main(){
fprintf(HOSTPC, "\nBooting up...");
output_high(STATUS_LED);
boot_up(); //Init ports and settings
delay_ms(1000);
output_low(STATUS_LED);
fprintf(HOSTPC, "\nRunning...:\n\n");
delay_ms(100);
while(1){
GET_GPS();
GET_COMPASS();
if(HC==60){
HC=0;
HB = HR;
HR = 0;
}
STORE_DATA();
if(!input(PIN_A2)) UPLOAD_DATA();
delay_ms(1000);
}
}
//****************************************************************************** SETUP ***************************
void boot_up(void){
setup_adc_ports(NO_ANALOGS);
output_a(0);
set_tris_a(0);
output_b(0);
set_tris_b(0x06);
address=count=0;
HR=HB=HC=0;
init_ext_eeprom();
enable_interrupts(GLOBAL);
enable_interrupts(INT_RB);
ext_int_edge(L_TO_H);
// CLEAR_EPROM();
}
//**************************************************************************** GPS *********************************
void GET_GPS(){
c=0;
while(fgetc(GPS)!='$');
while(fgetc(GPS)!='G');
while(fgetc(GPS)!='P');
while(fgetc(GPS)!='G');
while(fgetc(GPS)!='G');
while(fgetc(GPS)!='A');
fgetc(GPS);
while(c<45){
gps_data[c]=fgetc(GPS);
c++;
}
gps_data[c+1]='\0';
fprintf(HOSTPC, "\f%s", gps_data);
}
}
//***************************************************************************** EEPROM ***************************
void TestEprom(){
int c;
printf("\nInitialising Eprom");
init_ext_eeprom();
printf("..Ok\n");
while(c<128){
write_ext_eeprom(c, 0x20);
printf("%c:%X ", c, read_ext_eeprom(c));
c++;
}
}
void STORE_DATA(){
c=0;
for(count=address;count<(address+45);count++){
write_ext_eeprom(count, gps_data[c]); //Write GPS Data
c++;
}
address+=45;
c=0;
for(count=address;count<(address+6);count++){
write_ext_eeprom(count, hmc_data[c]); //Write COMPASS Data
c++;
}
address+=6;
write_ext_eeprom(count, HB);
address+=1;
fprintf(HOSTPC, "Data stored in EEPROM");
}
void UPLOAD_DATA(){
for(count=0;count<200;count++) fprintf(HOSTPC, "%c", read_ext_eeprom(count));
address=0;
}
void CLEAR_EPROM(){
for(count=0; count<500000;count++) write_ext_eeprom(count, 0);
address=count=0;
}
//************************************************************************ COMPASS *********************************
void GET_COMPASS(){ //Reading and writing to HMC6343
send_command(0x50); //Post heading data to hmc_data array
fprintf(HOSTPC, "\n%u, %u, %u, %u, %u, %u", hmc_data[0], hmc_data[1], hmc_data[2],hmc_data[3],hmc_data[4],hmc_data[5]);
}
//Gets bulk data for the four different post types (Accel, Mag, Heading, Tilt)
void send_command(uns8 command_byte){
uns8 in_byte, i;
i2c_ack_polling(DEVICE_WRITE);
i2c_start();
i2c_write(DEVICE_WRITE);
i2c_write(command_byte); //Send command
//i2c_stop();
i2c_start(); //Repeat start (SR)
i2c_write(DEVICE_READ); //Now ask the IC to report on the last command
for(i = 0 ; i < 6 ; i++) {
in_byte = i2c_read();
hmc_data[i] = in_byte; //Read in 6 data bytes
}
i2c_stop();
i2c_stop();
}
void i2c_ack_polling(uns8 device_address){
while(1){
i2c_start();
if (i2c_write(device_address) == ACK) break;
}
i2c_stop();
}