📄 term1.c
字号:
//-----------------------------------------------------------------------------
// T2Cal33x.c
//-----------------------------------------------------------------------------
// Copyright 2002 Cygnal Integrated Products, Inc.
//
// AUTH: CATHY
// DATE: 04 FEB 03
// 资源配置情况:VREF既作为内部A/D基准,又输出给外部电路
// 1根DA
// 3根A/D
// 4根串口现
// 1根复位
// 2根电源
// 一个定时器+
// 两个GPIO,
// 3根SPI,
// Target: C8051F33x
// Tool chain: KEIL C51 6.03 / KEIL EVAL C51
// 程序说明:
// 用在带GPS天线短路保护的便携式SVA导航仪上
// SJQQC版本,带日期输出
// 带天线短路告警,没告警输出为0,有告警输出为85
// 算法改为:(X * 5 - Y)/ 25 = 角度 X:采样数字 Y:基准
// 本程序用于500套正式生产
// 与SDA程序的区别:1.FM接口定义变动
// 2.增加日期
// 3.增加长按关机,同时给WINCE存盘提供时间
// 4.增加背光调节协议
//
//
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f330.h> // SFR declarations
#include<math.h>
#include <intrins.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F33x
//-----------------------------------------------------------------------------
sfr16 TMR2RL = 0xca; // ???2???????
sfr16 TMR3RL = 0x92; // ???3???????
//sfr16 TMR0 = 0xCC; // ???0?????
//sfr16 TMR1 = 0xCC; // ???1?????
sfr16 TMR2 = 0xcc; // ???2?????
sfr16 TMR3 = 0x94; // ???3?????
sfr16 ADC0 = 0xbd; // ADC Data Word Register
//----------------------------------------------------------------------------
//终端模块的地址定义
#define ADDR_H 0x00
#define ADDR_L 0x01
#define ORDER 0x1E
#define LENGTH 19 //有效数据为18个字节
//----------------------------------------------------------------------------
//global constant
//----------------------------------------------------------------------------
#define BAUDRATE 57600 //?????SW_UART???
#define SYSCLK 24500000 //????????18.432MHz
#define BAUDRATE1 9600
#define TIME_COUNT SYSCLK/BAUDRATE1/4
#define TH_TIME_COUNT TIME_COUNT*3/2
#define TIMER2_RATE 20 // Timer 2 overflow rate in Hz
#define SAMPLE_RATE 50000
#define HeaderLength 6
#define UTCtimeLength 6
//----------------------------------------------------------------------------
//全局变量
//----------------------------------------------------------------------------
sbit SLEEP = P1^0;
sbit RESRT = P1^1;
sbit Batt_Low = P1^2;
sbit LED = P1^3;
sbit KEY = P1^4;
sbit GPS_SW = P0^1;
sbit SW_TX = P0^2; // SW_UART发送引脚
sbit SW_RX = P0^3; // SW_UART接收引脚
bit SRI; // SW_UART接收完成标志
bit STI; // SW_UART发送完成标志
bit SREN; // SW_UART接收允许
bit STXBSY; // SW_UART发送忙标志
char RDR; // SW_UART接收数据存储器(锁存器)
char TDR; // SW_UART发送数据寄存器
void PORT_Init(void); // Initialize Port I/O
void SYSCLK_Init (void); // Initialize Oscillator
void SW_UART_Init (void);
void UART0_Init (void);
void AD_Init (void);
void Timer0_Init (void);
void Timer2_Init (int);
//void Timer3_Init (void);
void wait_one_second (void);
void Send_GPS(void);
void Variable_Init(void);
void GPS_Detect (void);
void Delay_1uS(void);
void send_wireless(void);
unsigned char ii;
unsigned char CRByte;
unsigned char LFByte;
unsigned char RecvByte;
unsigned char UartBuff;
unsigned char IsGpsFrame;
unsigned char FrameLength;
unsigned char Header[6];
unsigned char DGPS;
unsigned char UTCtime[4];
unsigned char Date[5];
// Date[0] //year
// Date[1] //month
// Date[2] //day
// Date[3] //hour
// Date[4] //minute
unsigned char Latitude[9]; //纬度
short int Latitude_Integer; //纬度的整数部分
short int Latitude_Decimal; //纬度的小数部分
unsigned char Longitude[10]; //经度
short int Longitude_Integer; //经度的整数部分
short int Longitude_Decimal; //经度的小数部分
unsigned char HDOP[4];
unsigned int Dactyl; //卫星信号强弱
unsigned char Batt_State; //电池的供电状态
unsigned char weixing[2];
unsigned char Planet_Quan; //卫星颗数
unsigned int GPS_Current;
unsigned int GPS_Current_pre;
unsigned char GPS_Alarm;
unsigned int GPS_Alarm_js;
unsigned char gps_ok;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
// Disable Watchdog timer
PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer
// enable)
PORT_Init(); // Initialize Port I/O
SYSCLK_Init (); // Initialize Oscillator
SW_UART_Init ();
UART0_Init ();
AD_Init ();
Timer0_Init ();
// Timer3_Init ();
Variable_Init();
EIE1 |= 0x10; // enable PCA interrupt
EIP1 |= 0x10;
ET0=1;
ES0=1;
EA = 1;
//------------------------------------------------------
while(1)
{
GPS_Detect();
if(IsGpsFrame) //if a GPS frame received
{
if (GPS_Alarm_js==0)
{
GPS_Alarm_js=200;
if((GPS_Current_pre>500)&&(GPS_Current>500))
{
GPS_Alarm = 85;
GPS_SW=1;
GPS_Alarm_js=2000;
}
else
{
GPS_Alarm =0;
GPS_SW=0;
}
GPS_Current_pre=GPS_Current;
}
//--------------------------
if(Batt_Low == 1)
{
Batt_State = 0x88; //电源电压正常
}
else
Batt_State =0x00; //电源电压已经底于2.6V
//--------------------------
Date[3] = UTCtime[0]*10+UTCtime[1]; //Hour
Date[4] = UTCtime[2]*10+UTCtime[3]; //minute
Latitude_Integer = Latitude[0]*1000+Latitude[1]*100+Latitude[2]*10+Latitude[3]; //纬度整数部分
Latitude_Decimal = Latitude[5]*1000+Latitude[6]*100+Latitude[7]*10+Latitude[8]; //纬度小数部分
Longitude_Integer =Longitude[0]*10000+Longitude[1]*1000+Longitude[2]*100+Longitude[3]*10+Longitude[4]; //经度整数部分
Longitude_Decimal =Longitude[6]*1000 +Longitude[7]*100 +Longitude[8]*10 +Longitude[9]; //经度整数部分
Planet_Quan = weixing[0]*10+weixing[1]; //卫星的颗数
if(HDOP[1] == '.')
Dactyl = HDOP[0]*100+HDOP[2]*10+HDOP[3];
else if(HDOP[2] == '.')
Dactyl = HDOP[0]*1000+HDOP[1]*100+HDOP[3]+10;
Send_GPS();
IsGpsFrame = 0;
}
}
}
/////////////////////////////////////
// Config2 Code Configuration File //
/////////////////////////////////////
//----------------------------------------------------------------------------
// P0.0---VREF; SW_TX0(由于目前在该程序中ADC和DAC尚未初始化,所以SW_TX0暂被
// 分配在P0.0口上面;否则一切正常初始化完成后,已被分配
// 在P0.2脚上)
// P0.1---IDA;
// P0.2---GPIO; 待一切初始化正常后,将作为SW_TX0使用。
// P0.3---CEX0; SW_RX0
// P0.4---TX0;
// P0.5---RX0;
// P0.6---T0
// P0.7---GPIO; SW_SPI
// P1.0---GPIO; SW_SPI
// P1.1---GPIO; SW_SPI
// P1.2---AIN0
// P1.3---GPIO
// P1.4---GPIO
// P1.5---GPIO;
// P1.6---GPIO;
// P1.7---GPIO;
// Peripheral specific initialization functions,
// Called from the Init_Device() function
void Port_Init()
{
P0MDIN = 0xFC;
P0SKIP = 0x07;
P0MDOUT |= 0x94;
P1MDIN = 0xFB;
P1SKIP = 0x04;
P1MDOUT |= 0x0B;
XBR0 = 0x01;
XBR1 = 0x51;
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
//
// This routine initializes the system clock to use the internal oscillator
// at its maximum frequency.
// Also enables the Missing Clock Detector.
//
void SYSCLK_Init (void)
{
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector
}
//-----------------------------------------------------------------------------
// UART0_Init
//-----------------------------------------------------------------------------
//
// Configure the UART0 using Timer1, for <BAUDRATE> and 8-N-1.
//
void UART0_Init (void)
{
SCON0 = 0x10; // SCON0: 8-bit variable bit rate
// level of STOP bit is ignored
// RX enabled
// ninth bits are zeros
// clear RI0 and TI0 bits
if (SYSCLK/BAUDRATE/2/256 < 1) {
TH1 = -(SYSCLK/BAUDRATE/2);
CKCON &= ~0x0B; // T1M = 1; SCA1:0 = xx
CKCON |= 0x08;
} else if (SYSCLK/BAUDRATE/2/256 < 4) {
TH1 = -(SYSCLK/BAUDRATE/2/4);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 01
CKCON |= 0x09;
} else if (SYSCLK/BAUDRATE/2/256 < 12) {
TH1 = -(SYSCLK/BAUDRATE/2/12);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 00
} else {
TH1 = -(SYSCLK/BAUDRATE/2/48);
CKCON &= ~0x0B; // T1M = 0; SCA1:0 = 10
CKCON |= 0x02;
}
TL1 = TH1; // init Timer1
TMOD &= ~0xf0; // TMOD: timer 1 in 8-bit autoreload
TMOD |= 0x20;
TR1 = 1; // START Timer1
TI0 = 1; // Indicate TX0 ready
}
void SW_UART_Init (void)
{
PCA0MD = 0x02; //PCA counter use SYSCLK/4 as timebase
PCA0CPM1 = 0x49; //PCA0CPM1:capture triggered by
//negative edge on P0.3,enable module1
//interrupt
PCA0CPM0 = 0x11; //PCA0CPM0:software timer mode,enable
//module 0 interrupt
PCA0CN |= 0x40; //enables the PCA Counter/Timer.
CCF0 = 0;
CCF1 = 0;
SRI = 0; //????????
STI = 0; // ????????
SW_TX = 1; // TX????????
STXBSY = 0; // ??SW_UART???
SREN = 1; //????
}
//-----------------------------------------------------------------------------
// ???0???
//-----------------------------------------------------------------------------
void Timer0_Init (void)
{
TMOD|=0X06; //COUNT0
TL0=0X0FF; //interrupt when every pulse come
TH0=0X0FF;
EA=1;
ET0=1; //COUNT0 INT ENABLE
TR0=1; //COUNT0 START
}
//-----------------------------------------------------------------------------
// ADC0_Init ADBUSY, LP tracking, no Interrupt, ADC disabled
//-----------------------------------------------------------------------------
//
// Configure ADC0 to use ADBUSY as conversion source, and to sense the output
// of the temp sensor. Disables ADC end of conversion interrupt. Leaves ADC
// disabled.
//
void AD_Init (void)
{
ADC0CN = 0x40; // ADC0 disabled; LP tracking
// mode; ADC0 conversions are initiated
// on a write to ADBusy
AMX0P = 0x0a; // Temp sensor selected at + input
AMX0N = 0x11; // Single-ended mode
ADC0CF = (SYSCLK/3000000) << 3; // ADC conversion clock <= 3MHz
ADC0CF &= ~0x04; // Make ADC0 right-justified
REF0CN = 0x0E; // enable temp sensor, VREF = VDD, bias
// generator is on.
//EIE1 |= 0x08; // Enable ADC0 interrupt
ADC0CN = 0x80;
AD0BUSY = 1;
}
/*
void Timer3_ISR (void) interrupt 14 //2.5ms
{
TMR3CN &= ~0x80;
key_scan_time++;
count_time++;
key_press_time++;
if (LcdBackLight_Delay_AfterS2410>0) LcdBackLight_Delay_AfterS2410--;
else if (key_zt==99)
{
LcdBackLight_Delay_AfterS2410=0;
BG_EN=0;
}
if ((key_press_time>400) && (key_zt==99))
{
BG_EN=1;
S2410_PWR=1;
key_press_time=0;
key_zt=0;
}
if (key_time>0) key_time--;else key_time=0;
test++;
if (GPS_Alarm_js>0) GPS_Alarm_js--;else GPS_Alarm_js=0;
if (GPS_time>0) GPS_time--;else GPS_time=0;
if (FM_delay>0) FM_delay--;else FM_delay=0;
}
*/
void GPS_Detect (void)
{
AMX0P = 0x00;
AMX0N = 0x11;
AD0INT = 0;
AD0BUSY = 1;
while (!AD0INT); // Wait for conversion to complete
AD0INT = 0; // Clear end-of-conversion indicator
GPS_Current=ADC0; // Store latest ADC conversion
}
/*-----------------------------------------------------------------------------
// ???3???
//-----------------------------------------------------------------------------
void Timer3_Init ()
{
TMR3CN = 0x00;
CKCON |= 0x40;
TMR3RL = -60000;
TMR3 = TMR3RL;
EIE1 |= 0x80;
TMR3CN |= 0x04;
}
/*****************************************
* Uart Interrupt
*
*****************************************/
void Rcv_INT(void) interrupt 4
{
unsigned char tmp;
RI0=0;
tmp=RecvByte;
UartBuff=SBUF0;
RecvByte++;
if (RecvByte<HeaderLength+1)
{
Header[tmp]=UartBuff;
}
if(RecvByte==HeaderLength)
{
if((Header[0]=='$')&&(Header[1]=='G')&&(Header[2]=='P')&&(Header[3]=='G')&&(Header[4]=='G')&&(Header[5]=='A'))
{ //if it is a "$GPGGA" frame
FrameLength=1;
}
else if((Header[0]=='$')&&(Header[1]=='G')&&(Header[2]=='P')&&(Header[3]=='R')&&(Header[4]=='M')&&(Header[5]=='C'))
{ //if it is a "$GPGGA" frame
FrameLength=3;
}
else
{
FrameLength=2; //if it is other frame,discard it
}
}
if(FrameLength==2) //deal with other frame
{
if(UartBuff==0x0D) CRByte=UartBuff; //received CR
if(UartBuff==0x0A) LFByte=UartBuff; //received LF
if(CRByte&&LFByte) //received CR and LF,end of frame
{
RecvByte=0; //clear byte counter
FrameLength=0; //clear frame length
CRByte=0; //clear CR byte
LFByte=0; //clear LF byte
for (ii=0;ii<9;ii++) // Longitude
{
Longitude[ii]=0x30;
}
for (ii=0;ii<10;ii++) // Latitude
{
Latitude[ii]=0x30;
}
DGPS=0x30;
for (ii=0;ii<4;ii++) // HDOP
{
HDOP[ii]=0x30;
}
//IsGpsFrame=1;
//gps_ok=99;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -