📄 main.c
字号:
//-------------------------------------------------------------
// filename: main.C
// This file is programed for the Micro Auto Pilot System of the Wisewell Inc.
// Programer: wangr
//-------------------------------------------------------------
//------------------------ NOTES -------------------
//------------------------------------------------
/*********************************************************************************/
/*************************** ************************************/
/*************************** include section ************************************/
/*************************** ************************************/
/*********************************************************************************/
//#include <C8051F020.h>
#include <GPS_F023.c>
#include <intrins.h>
#include <ABSACC.H>
#include <math.h>
#include <stdio.h>
#include <string.h>
#define uchar unsigned char
#define uint unsigned int
sbit LED1 = P1^1;
sbit LED2 = P1^2;
sbit LED3 = P1^3;
sbit LED4 = P1^4;
sbit LED5 = P1^5;
sbit LED6 = P1^6;
sbit LED7 = P1^7;
bit LED_flag;
//uchar xdata r_buf[100];
//uchar xdata r_in;
bit GPS_received_data_OK, GPS_received_data_OK1, gps_en;
uint xdata main_count;
/************************************************/
/************* GPS recieve ****************/
#define PI 3.14
#define ILEN 85
#define OS_ENTER_CRITICAL() EA = 0
#define OS_EXIT_CRITICAL() EA = 1
#define VAH_Gain 10
//typedef unsigned char uchar ;
#define OS_GPS_LAT_EN 1
#define OS_GPS_LONG_EN 1
/* serial port 0 exchange data between gps and pc */
/*************************************************************/
/* GPS buffer */
/*************************************************************/
//uchar idata r_buf[]="$GPGGA,034745.94,3537.8333,N,11619.5870,E,1,06,2.2,-143.5,M,-12.6,M,000,000*7F,111";
// uchar xdata s_buf[]="$GPGGA,034745.94,3537.8333,N,11619.5870,E,0,06,2.2,-143.5,M,-12.6,M,000,000*7F,";
uchar idata r_buf[ILEN]="$GPRMC,022309.34,V,3537.8333,N,13944.6667,E,004.0,002.0,030222,,*02";
// uchar idata r_buf[ILEN];
/*
$GPGGA,022237.00,3537.8333,N,13944.6667,E,0,00,99.9,0100,M,,M,000,0000*7E
$GPGLL,3537.8333,N,13944.6667,E,022237,V*38
$GPRMC,022237,V,3537.8333,N,13944.6667,E,000.0,000.0,030222,,*0E
$GPZDA,022237,03,02,2022,,*4D
*/
/****************************************************************************/
/* 500m DBSAE = 120
/* 1km DBSAE = 60
/* 2km DBSAE = 30
/* 3km DBSAE = 20
/* 4km DBSAE = 15
/* 5km DBSAE = 12
/* 6km DBSAE = 10
/* 7km DBSAE = 9
/* 8km DBSAE = 8
/* 9km DBSAE = 7
/* 10km DBSAE = 6
/*
/****************************************************************************/
unsigned char code DBSAE_Table[]={60,30,15,10,7,6,5,4,4,3,3};
uchar idata r_in;
bit measure_detect;
bit Data_Request;
bit GPS_received_data_OK,GPS_received_data_OK1;
bit second_flag;
bit GPGGA_flag;
bit GPRMC_flag;
bit send_flag_OK;
bit gps_flag = 0;
bit TimeCorrect_flag;
bit Int0Send_flag;
// GPS data //
bit gps_en; /* control the GPS receiver status */
idata int L_X = 0;
idata int L_Y = 0;
typedef struct {
// utc time
union { double utc_time;
struct {uchar hour;
uchar min;
uchar sec;
uchar date;
uchar month;
uchar year_low;uchar year_high;} times;
} OS_UTC;
// latitude WGS-84.
#ifdef OS_GPS_LAT_EN
union { unsigned long latitude;
struct {uchar latitude_deg;
uchar latitude_min;
uint latitude_sec;} latitudes;
} OS_Latitude;
float f_latitude;
uchar latitude_deg;
float f_latitude_min;
#else
float f_latitude;
uchar latitude_deg;
float f_latitude_min;
#endif
uchar latitude_N_S;
// longitude WGS-84.
#ifdef OS_GPS_LONG_EN
union { unsigned long longitude;
struct {uchar longitude_deg;
uchar longitude_min;
uint longitude_sec;} longitudes;
} OS_Longitude;
float f_longitude;
uchar longitude_deg;
float f_longitude_min;
#else
float f_longitude;
uchar longitude_deg;
float f_longitude_min;
#endif
uchar longitude_E_W;
// orientation
uchar orientation;
// star number
uchar star_number;
// HDOP
float HDOP;
// altitude Altitude with respect to mean sea level.
float f_altitude; //海拔高度
// Geoidal Separationthe difference between the WGS-84 earth ellipsoid and mean-sea-level (geoid).
float f_separation; // - means that mean-sea-level below ellipsoid.
// speed
float f_speed;
// heading
float f_heading;
// megnitude angle
float f_magval;
char f_magsig;
// megnitude angle direction
} OS_GPS;
typedef struct os_point{
float Ex;
float Ny;
float Sz;
uchar G;
uint Heading;
uint Speed;
uint Distance;
} POINT;
idata OS_GPS gps_point;
xdata POINT OS_PTbl[2];
xdata POINT xdata *Start_Point;
xdata POINT xdata *Any_Point;
void delay(uchar x);
void delay20();
//void InitTimer0(void);
void serialGPS_init();
void putbyte(uchar c);
uchar Read_GPS_GPGGA_B();
uchar Read_GPS_GPRMC_B();
uchar Read_GPS_GPRMC_C();
void trans_data(uchar ch);
void send_data();
void sys_ini();
void Process_GPS();
void Init_Device(void);
uchar xdata ReportTime_buf[8] = {0x12};
uchar xdata ReportTimetoMach[30] = {0xAA, 0xBB, 0xFF, 0xFF, 0x12};
uchar xdata com_receive_data[20]={0x00,0x00,0x00,0x00,0x00};
uchar xdata com_receive_subf=5;
bit idata buff_flag=0;
uchar idata buff_count,buff_number;
bit com_receive_f=0;
bit com_send_f=0;
uint xdata day_count;
uchar xdata year_count,day_count_temp1, day_count_temp2;
/*********************************************************************************?
/******************* Function declare *******************************************/
/*********************************************************************************/
void config (void);
void MCU_init(void);
void delay(uchar m);
/*********************************************************************************/
/*************************** ************************************/
/*************************** main ************************************/
/*************************** ************************************/
/*********************************************************************************/
void main(void)
{
/*********************************************************************/
/*data and system Initialized
/*********************************************************************/
uchar idata GPS_Count = 0;
//float idata start_temp = 0;
// uchar idata i;
float idata any_temp = 0;
config();
MCU_init();
LED_flag = 0;
LED1 = 0;
/* main_flag = 1;
height_flag = 0;
do{
ADC0CN |= 0x90; // A/D trans enabled
delay(10);
height = AD_buf[0]*256 + AD_buf[1];
if(height>0x0010)
{
potentiometer_data--;
SPI0_Init(potentiometer_data);
}
else if(height<1)
{
potentiometer_data++;
SPI0_Init(potentiometer_data);
}
else
{
main_flag = 0;
}
delay(5);
}while(main_flag);
height_flag = 1;
ADC0CN &= 0x6F; // ADC Control Register:AD trans disabled
*/
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
sys_ini();
// while(1)
// {
// Indicator_LED=0;
// delay(50);
// Indicator_LED=1;
// delay(50);
// }
Start_Point = &OS_PTbl[0];
Any_Point = &OS_PTbl[1];
/*********************************************************************/
/* system self - testing by sending OK
/*********************************************************************/
Read_GPS_GPGGA_B();
Read_GPS_GPRMC_B();
Process_GPS();
send_data();
LED1 = 0; // make the led light and dark
putbyte('O');
putbyte('K');
delay(150);
do{
delay(60);
putbyte(0x51);
putbyte(0x52);
Process_GPS();
gps_point.orientation=0;
gps_point.star_number=3;
gps_point.OS_UTC.times.hour=8;
gps_point.OS_UTC.times.min=30;
gps_point.OS_UTC.times.sec=59;
L_X=100;////0x0064
L_Y=101;//0x0065
gps_point.f_heading=0x00;
gps_point.f_speed=0xab;//171
gps_point.f_altitude=0xcd;//171
gps_point.f_magval=0xef;//171
LED1=!LED1; // make the led dark
} while(!GPS_received_data_OK); // if no gps, flash.
LED1 = 1; // make the led dark
/*********************************************************************/
/* NAV Start Point Calculation
/*********************************************************************/
while(!gps_point.orientation||second_flag == 0)
{ //4
_nop_();
// GPS_received_data_OK = 1;
if(GPS_received_data_OK)
{ //3
GPS_received_data_OK = 0;
// Read_GPS_GPRMC_C();
// for (i = 0; i< 90 ; i++) r_buf[i]= s_buf[i];
Read_GPS_GPGGA_B();
Read_GPS_GPRMC_B();
// gps_point.orientation=1;
if(GPGGA_flag == 1)
{ //2
OS_ENTER_CRITICAL();
GPGGA_flag = 0;
GPRMC_flag = 0;
switch(gps_point.orientation)
{ //1
case 0x00:
_nop_();
GPS_Count = 0;
L_X = 0;
L_Y = 0;
putbyte(0x66);
break;
case 0x01:
GPS_Count ++;
if(second_flag == 0 && GPS_Count == 10)
{
Start_Point->Ex = gps_point.f_longitude;
Start_Point->Ny = gps_point.f_latitude ;
}
if (GPS_Count >= 10)
{ GPS_Count = 0; second_flag = 1; }
putbyte(0x77);
break;
case 2:
_nop_();
break;
default:
_nop_();
break;
} //1
OS_EXIT_CRITICAL();
send_data();
} //2
} //3
_nop_();
} //4
putbyte('A');putbyte('A');putbyte('A');putbyte('A');putbyte('A');
putbyte('\r');
putbyte('\n');
L_X = (int) (Start_Point->Ex );
L_Y = (int) (Start_Point->Ny );
LED1 = 1; // lighting
send_data();
Process_GPS();
// while(1);
/*********************************************************************/
/* NAV Point Calculation
/*********************************************************************/
while(1){ //4
_nop_();
_nop_();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -