Should I post my code here? because I think every one in random searches are looking for this code
---------- Post added at 15:13 ---------- Previous post was at 15:10 ----------
This is main.c file of my source code. using atmel atmega2561.
I wants to sent data to remote server.. through gprs medium..
/*** Includes ***/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "display.h"
#include "locate.h"
#include "main.h"
#include "definitions.h"
#include "gps.h"
#include "uart0.h"
#include "uart1.h"
#include "imu.h"
/*** Defines ***/
//timer lengths
#define t1 30 //debounce time length
#define t2 500 //frequency to check for new gps stuff
#define t3 100 //frequency to check the imu
#define t4 1000 //frequency to calculate distance to destination
#define BUTTON (~PINC & (1<<2)) //button is on D7
//debounce states
#define NOPUSH 1
#define MAYBEPUSH 2
#define PUSHED 3
#define MAYBENOPUSH 4
#define MIN_ATTRACTION_MODE 48
/*** Variables ***/
//task counters
volatile uint16_t time1,
time2,
time3,
time4;
//this struct keeps track of where we are trying to go
Destination dest;
//this holds all of the current information about the
//user's state (speed, position, etc.)
Position pos;
//this holds the unfiltered data as we gather it
Position raw;
//state variables
uint16_t attraction_mode;
volatile uint8_t have_gps; //indicates that we have
//gps data
volatile uint8_t have_grid; //indicates that we have a grid
//current status of the device. indicates whether we are looking for gps data,
//grid data, or if everything is ok
volatile enum STATUS status;
//grid data receiving variables
volatile uint8_t grid_chunk[GRID_PACKET_SIZE]; //the grid chunk itself
volatile uint8_t grid_chunk_pos; //the position in the current grid chunk
volatile uint8_t grid_chunk_num; // the number of grid chunks processed
volatile uint8_t grid_data_ready;
//number of attractions detected
uint8_t max_attraction_mode;
//debouncing variables
uint8_t push_state;
//testing only:
volatile uint8_t buf[30];
//**********************************************************
//timer 0 compare ISR
ISR (TIMER0_COMPA_vect)
{
//Update the task time
if (time1>0) --time1;
if (time2>0) --time2;
if (time3>0) --time3;
if (time4>0) --time4;
}
//*******************************
//if the button has been pressed, do what the user has requested
void handle_button_press(void)
{
if(status == OK){
attraction_mode++;
if(attraction_mode >= max_attraction_mode){
attraction_mode= MIN_ATTRACTION_MODE;
}
//tell the user that calculations are underway
display_calculating(attraction_mode);
} else {
}
}
//**********************************************************
// task 1 checks for the button press
void task1 (void)
{
switch (push_state)
{
case NOPUSH:
if (BUTTON) push_state=MAYBEPUSH;
else push_state=NOPUSH;
break;
case MAYBEPUSH:
if (BUTTON)
{
push_state=PUSHED;
//only change mode once per key press
handle_button_press();
}
else push_state=NOPUSH;
break;
case PUSHED:
if (BUTTON) push_state=PUSHED;
else push_state=MAYBENOPUSH;
break;
case MAYBENOPUSH:
if (BUTTON) push_state=PUSHED;
else
{
push_state=NOPUSH;
}
break;
default:
push_state= NOPUSH;
break;
}
}
//*********************************************************
void task2 (void){
uint8_t gps_message[40];
;
//check to make sure the GPS point is valid before
//doing anything else
if(get_gps()){
//bad gps data
} else {
pos.x= raw.x;
pos.y= raw.y;
//we now have a gps reading, now we need to get
//a grid
if(status == NEED_GPS){
status= NEED_GRID;
display_need_grid();
}
//this allows us to try to load existing data instead of requesting a
//fresh grid
if(status == NEED_GRID){
if(init_locate(pos.x, pos.y, &max_attraction_mode)){
//we need to find new grid data
status= FETCHING_GRID;
//request the first chunk of data
uart0_send_buff(&grid_chunk_num, 1);
} else {
//we were abled to load data from memory
status= OK;
display_calculating(48);
}
} else if(status == FETCHING_GRID || status == FETCHING_BOUNDARIES){
//we already started fetching a grid
//slight hack to retry if there is no grid data for the last area we were in
if(grid_chunk_num == '0'){
status = NEED_GPS;
}
}
}
}
//*********************************************************
void task3 (void) {
char position_x_msg[30];
char position_y_msg[30];
char pos_x_msg_uart[50];
char pos_y_msg_uart[50];
imu_update(&pos, t3);
dtostrf(pos.x, 10, 5, position_x_msg);
dtostrf(pos.y, 10, 5, position_y_msg);
sprintf(pos_x_msg_uart,"X Position: %s \r\n", position_x_msg);
sprintf(pos_y_msg_uart,"Y Position: %s \r\n", position_y_msg);
while(uart1_busy()){};
while(uart1_send_buff(pos_x_msg_uart, strlen(pos_x_msg_uart))){};
while(uart1_busy()){};
while(uart1_send_buff(pos_y_msg_uart, strlen(pos_y_msg_uart))){};
while(uart1_busy()){};
}
//**********************************************************
//Calculate distance to the selected attraction and display that information
//on the screen
void task4 (void){
int8_t retval;
if(status == OK && attraction_mode != 48){
retval= calculate_distance(attraction_mode, pos.x, pos.y, &dest);
switch(retval){
case 0: //all is well
display_attraction(attraction_mode, dest.distance, dest.direction);
break;
case -1: //no data, need to fetch some
status= FETCHING_GRID;
//request the first chunk of data
uart0_send_buff(&grid_chunk_num, 1);
display_need_grid();
break;
case -2: //no data, need to fetch some
status= FETCHING_GRID;
//request the first chunk of data
uart0_send_buff(&grid_chunk_num, 1);
display_need_grid();
break;
case -3:
//cannot find this attraction
display_not_available(attraction_mode);
break;
}
}
}
//function to be executed when a character is received.
void uart0_func(char c){
//check to see if we are expecting grid data
if((status == FETCHING_GRID || status == FETCHING_BOUNDARIES
|| status == FETCHING_ATTRACTIONS)
&& grid_chunk_pos < GRID_PACKET_SIZE){
//put the data in the array
grid_chunk[grid_chunk_pos] = c;
grid_chunk_pos++;
if(grid_chunk_pos >= GRID_PACKET_SIZE){
//we are done with this chunk, tell, the main loop to put this in
//the eeprom
grid_data_ready= TRUE;
}
}
}
void initialize(void){
//init the task timer
time1=t1;
//set up timer 0 for 1 mSec timebase
OCR0A = 249; //set the compare re to 250 time ticks
TIMSK0= (1<<OCIE0A); //turn on timer 0 cmp match ISR
//set prescalar to divide by 64
TCCR0B= 3; //0b00000011;
// turn on clear-on-match
TCCR0A= (1<<WGM01) ;
//set up button
DDRC &= ~(1<<2);
//init the IMU
init_imu();
//nothing can be done until a GPS fix is acquired.
have_gps= FALSE;
have_grid= FALSE;
status= NEED_GPS;
//initialize the destination struct because we don't know how to get
//anywhere yet
dest.attraction= 48;
//looking for no attraction initially
attraction_mode= 48;
max_attraction_mode= '0';
time1= t1;
time2= t2;
time3= t3;
time4= t4;
push_state= NOPUSH;
//init the UART -- uart_init() is in uart.c
uart0_init(uart0_func);
init_gps(&raw);
grid_chunk_pos= 0;
grid_chunk_num= '0';
grid_data_ready= FALSE;
//this function should take care of everyhing
//to initialize the display
init_display();
display_need_gps();
sei();
}
void data_load(void){
uint8_t done_loading= FALSE;
uint8_t i= 0;
switch(status){
case FETCHING_GRID:
load(grid_chunk);
if(grid_chunk_num > MAX_GRID_PACKET){
//we are done loading grid data, time to load boundary data
status= FETCHING_BOUNDARIES;
//set up the variables so we can load a grid chunk in the future
}
break;
case FETCHING_BOUNDARIES:
//once we are done getting the map, we need to get the boundaries
load_boundaries(grid_chunk);
status= FETCHING_ATTRACTIONS;
//let the user know that we are ready to roll
break;
case FETCHING_ATTRACTIONS:
while(uart1_send_buff(&grid_chunk[0], GRID_PACKET_SIZE)){};
if(grid_chunk[0] == '!'){
done_loading = TRUE;
} else {
//the character '0' to pad the incoming strings
while(grid_chunk != '0'){
i++;
}
//mark the end of the string with a null terminator, so that it works with c
grid_chunk = 0;
//check to make sure the thing sent doesn't go outside of the array bounds
if(strlen(grid_chunk) > 0 && strlen(grid_chunk) < 17){
display_add_attr(grid_chunk, max_attraction_mode);
max_attraction_mode++;
}
}
break;
default:
break;
}
//ask for the next data chunk from the base station, unless we are done
if(done_loading){
while(uart1_busy()){};
sprintf(buf,"got here \r\n");
while(uart1_send_buff(buf, strlen(buf))){};
status= OK;
grid_chunk_num= '0';
display_calculating('0');
} else {
//store the max number of attractions in memory
set_num_attractions(max_attraction_mode);
uart0_send_buff(&grid_chunk_num, 1);
grid_chunk_num++;
}
}
//**********************************************************
//Entry point and task scheduler loop
int main(void)
{
initialize();
while(1)
{
// reset time and call task
if (time1==0){ time1=t1; task1();}
if (time2==0){ time2=t2; task2();}
//if (time3==0){ time3=t3; task3();}
if (time4==0){ time4=t4; task4();}
//if we have a full array of grid data, feed it to the eeprom and request
//the next chunk of data
if (grid_data_ready){
grid_data_ready= FALSE;
grid_chunk_pos= 0;
data_load();
}
}
}
//**********************************************************