默れ

ADXL345应用——计步器设计(主程序)

0
阅读(5459)

    下面分享一下计步器的主程序代码,大家可以仔细分析一下,原理并不是很难,尤其是综合了ADXL345单双击检测、旋转检测、摇晃检测等等诸多功能后,如何应用如此小巧的一块芯片发挥出最大的实力。

 

#include "main.h"
#include "xl345.h"
#include "Common.h"
//#include "irq_arm.c"
#include "myxl345.h"
#include 
//Three Axis Acceleration Flag
#define     X_CHANNEL   	0		
#define     Y_CHANNEL		1
#define     Z_CHANNEL		2
unsigned int r_data[20];
unsigned int fifo_flag;
int left_right;
long timer_counter=0;
long long_r_data[30];
long temp;
long temp_mean;
long temp_max_1,temp_max;
long temp_min_1,temp_min;
long temp_max_2;
long temp_min_2;
long temp_up;
long temp_down;
unsigned int xl345_all[29];
long mean_x;
long mean_y;
long mean_z;
long static_x;
long static_y;
long static_z;
unsigned int calorie,weight;
int step;
int run_step=0;
unsigned int status;
int main(void)
{
  int i;
  int flag;
  
  uchar str[10];
  lcd_initial();
  ADuC7026_Initiate();
  ADuC7026_once_2();
  long_delay(100);
  fifo_flag = 0;
  temp =0;
  temp_mean=static_x;
    temp_max=static_x;
    temp_min=static_x;
    temp_max_1 = static_x;
    temp_min_1 = static_x;
    temp_up = -169;
    temp_down = -220;
    step=0;
    run_step=0;
    flag = 0;
     ADuC7026_static_rotate();
     	
  while(1)
  {
    if(fifo_flag==0)
    {
        disp_str(0,0,"    ADI MEMS     ");
        disp_str(0,2,"    Workshop     ");
        disp_str(0,4,"    2012/2/9     ");
        if(flag==1)
              disp_str(0,6,"   AT HUST STI   ");
        else if(flag==2)
              disp_str(0,6,"   sleep mode    ");
        ADuC7026_static_rotate_4();
        while(!fifo_flag);
        if(fifo_flag==3)
            fifo_flag= 9;
        else if(fifo_flag==2)
    	{
            fifo_flag=0;
            flag=1;
            disp_str(0,6,"   AT HUST STI   ");
    	}
        else
    	{
            flag=2;
            disp_str(0,6,"   sleep mode    ");
            fifo_flag=0;
    	}
    }
    else if(fifo_flag==9)
    {
        status = 170;
        int2str(status,str,8);
        disp_str(0,0,"input high:     ");
        disp_str(0,2,"         cm     ");
        disp_str(0,4,"                ");
        disp_str(0,6,"                ");
        disp_str(0,2,str);
        ADuC7026_static_rotate_2();
        while(1)
    	{
            if(fifo_flag==1)
    		{
                fifo_flag=10;
                SPI_ADXL345_READ(XL345_INT_SOURCE);
                SPI_ADXL345_WRITE(XL345_INT_ENABLE,0x00);
                fifo_flag=10;
                mean_x = 0;
                mean_y = 0;
                mean_z = 0;
                for(i=0; i<1; i++)
        		{
                    ADuC7026_once();
                    mean_x+=long_r_data[0];
                    mean_y+=long_r_data[1];
                    mean_z+=long_r_data[2];
        		}
                mean_x/=1;
                mean_y/=1;
                mean_z/=1;
                mean_x-=static_x;
                mean_y-=static_y;
                mean_z-=static_z;		
                long_delay(1000000);
                if(mean_y>0)
        		{
                    left_right =0 ;
                    if(status>0)
                		status-=1;
        		}
        		else
        		{
                    left_right =1;
                    if(status<255)
                		status+=1;
        		}
                ADuC7026_once_2();
    			
                if(static_y>200)
        		{
                    fifo_flag=0;
            		break;
        		}
                else if(static_y<-200)
        		{
                    fifo_flag=20;
            		break;
            	} 	  
                long_delay(50000);
                int2str(status,str,8);
                disp_str(0,0,"input high:      ");
                disp_str(0,2,"         cm      ");
                disp_str(0,4,"                 ");
                disp_str(0,2,str);
                ADuC7026_static_rotate_2();	
    		}
    	}
    }
    else if(fifo_flag==20)
    {
        weight = 60;
        int2str(status,str,8);
        disp_str(0,0,"input weight:   ");
        disp_str(0,2,"         kg     ");
        disp_str(0,4,"                ");
        disp_str(0,6,"                ");
        long_delay(1000000);
    	
        disp_str(0,2,str);
        ADuC7026_static_rotate_2();
        while(1)
    	{
            if(fifo_flag==1)
    		{
                fifo_flag=10;
                SPI_ADXL345_READ(XL345_INT_SOURCE);
                SPI_ADXL345_WRITE(XL345_INT_ENABLE,0x00);
                fifo_flag=10;
                mean_x = 0;
                mean_y = 0;
                mean_z = 0;
                for(i=0; i<1; i++)
        		{
                    ADuC7026_once();
                    mean_x+=long_r_data[0];
                    mean_y+=long_r_data[1];
                    mean_z+=long_r_data[2];
        		}
                mean_x/=1;
                mean_y/=1;
                mean_z/=1;
                mean_x-=static_x;
                mean_y-=static_y;
                mean_z-=static_z;		
                long_delay(1000000);
                if(mean_y>0)
        		{
                    left_right =0 ;
                    if(weight>0)
                		weight-=1;
        		}
        		else
        		{
                    left_right =1;
                    if(weight<255)
                		weight+=1;
        		}
                ADuC7026_once_2();
    			
                if(static_y>200)
        		{
                    fifo_flag=0;
            		break;
        		}
                else if(static_y<-200)
        		{
                    fifo_flag=5;
            		break;
            	} 	  
                long_delay(50000);
                int2str(weight,str,8);
                disp_str(0,2,str);
                ADuC7026_static_rotate_2();	
    		}
    	}
    }
    else if(fifo_flag==5)
    {
        T1CON = 0xC4;	
        IRQEN=GP_TIMER_BIT;
        disp_str(0,0,"step:         s");
        long_delay(6000000);	
        while(1)
        { 	
            for(i=0; i<500; i++)
    		{
                ADuC7026_once_2();
                temp+=static_x;
                if(static_x>temp_max)
                    temp_max_1 = static_x;
                if(static_xtemp_up&&flag==0)
        		{
                	step+=1;
                	flag = 1;
        		}
                else if(static_x