📄 term3.c
字号:
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f330.h> // SFR declarations
#include<math.h>
#include <intrins.h>
//-----------------------------------------------------------------------------
// 16-bit SFR Definitions for 'F33x
//-----------------------------------------------------------------------------
sfr16 DAC0 = 0x96; // 10?DAC?????
//sfr16 TMR0RL = 0xca; // ???0???????
//sfr16 TMR1RL = 0xca; // ???1???????
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 ADDRESS 65535
#define ORDER 0x1E
#define LENGTH 19 //有效数据为19个字节
//----------------------------------------------------------------------------
//global constant
//----------------------------------------------------------------------------
#define BAUDRATE 57600 //?????SW_UART???
#define SYSCLK 24500000 //????????18.432MHz
#define BAUDRATE1 9600
#define TIME_COUNT SYSCLK/BAUDRATE1/4
//#define TIME_COUNT SYSCLK/BAUDRATE/4 //????????PCA???
//(PCA?????SYSCLK/4??)
#define TH_TIME_COUNT TIME_COUNT*3/2 //3/2???,?????????
//??????????RX????
//?????????,????
//??????????????
#define TIMER2_RATE 20 // Timer 2 overflow rate in Hz
#define SAMPLE_RATE 50000
#define HeaderLength 6
#define UTCtimeLength 6
#define SW_HeaderLength 7
//----------------------------------------------------------------------------
//全局变量
//----------------------------------------------------------------------------
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 idata RDR; // SW_UART接收数据存储器(锁存器)
char idata 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 Key_scan(void);
void Send_GPS(void);
void Variable_Init(void);
void Key_process(void);
void Measure(void);
void Delay_1uS(unsigned int j);
void SW_Uart_Receive(void);
void Send_GPS1(void);
void Send_Term(void);
void SW_UartRecevie();
unsigned int test;
unsigned int currval;
unsigned int DA_data;
unsigned char ii;
unsigned char tx_data;
unsigned char key_pre;
unsigned char key_work;
unsigned char key_scan_time;
unsigned int key_time;
unsigned char key_zt;
unsigned int LcdBackLight_Delay_AfterS2410;
unsigned char idata SW_RecvByte; //接收到的字节数
unsigned char idata SW_UartBuff; //接收数据缓冲
unsigned char idata SW_Header[7]; //接收数据侦头
unsigned char idata SW_Recv_Command; //接收到的命
unsigned char idata SW_Command[2];
unsigned int idata SW_RecvAddress; //接收到的地址
unsigned char idata SW_Address[5]; //
unsigned char idata SW_FrameLength; //判断是否为寻找自己的数
unsigned char idata SW_IsFrame;
unsigned char idata SW_RecvOver;
unsigned char idata SW_CRByte;
unsigned char idata SW_LFByte;
unsigned char CRByte;
unsigned char LFByte;
unsigned char PulseCount;
unsigned char S_PulseCount[3];
unsigned char RecvByte;
unsigned char UartBuff;
unsigned char IsGpsFrame;
//unsigned char SW_FrameLength;
unsigned char FrameLength;
unsigned char idata Header[6];
unsigned char idata UTCtime[10];
unsigned char idata Latitude[11];
unsigned char idata Longitude[10];
unsigned char HDOP[4];
unsigned char DGPS;
unsigned char weixing[2];
unsigned char gps_ok;
unsigned char fm[4];
unsigned char fmbak;
unsigned char fmbak1;
unsigned char fmbak2;
unsigned char fmbak3;
unsigned char surt_js;
unsigned char he;
unsigned char FM_delay;
unsigned char FM_flag;
unsigned char S_Pulse_s[3];
unsigned char S_AdResult0[4]; //Ad Result backup for sending
unsigned char S_AdResult1[4]; //Ad Result backup for sending
unsigned char S_AdResult2[4]; //Ad Result backup for sending
unsigned char S_AdResult3[4]; //Ad Result backup for sending
unsigned char S_AdResult4[4]; //Ad Result backup for sending
unsigned char t1;
unsigned char ii;
unsigned int key_bg_time;
unsigned char POWER_SW_delay;
unsigned int zero_all;
unsigned int zero_front;
unsigned char zero_time;
unsigned int zero;
unsigned int zero_all_front;
unsigned char init_flag;
unsigned char zero_time1;
unsigned int zero_all1;
unsigned int zero_single;
unsigned char bujiu;
unsigned char bujiu_js;
unsigned char count_time;
unsigned char sample_time;
unsigned char Pulse_s;
unsigned char S_Pulse_s[3];
unsigned int AdResult[5];
unsigned int tempAdResult;
unsigned int tl;
unsigned int TL_Adresult;
unsigned int AdR[5];
unsigned int GPS_Current;
unsigned int GPS_Current_pre;
unsigned char riqi[6];
unsigned char GPS_Alarm[2];
unsigned int GPS_Alarm_js;
unsigned int GPS_time;
unsigned char b_light;
unsigned int key_press_time;
unsigned int test1;
unsigned char Batt_State;
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{
unsigned char G = 0;
// 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)
{
SW_UartRecevie();
// Send_GPS1();
// Send_GPS();
// Send_Term();
LED = 1;
}
}
/////////////////////////////////////
// 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 = 0xFE;
P0SKIP = 0x07;
P0MDOUT |= 0x14;
P1MDIN = 0xFF;
P1SKIP = 0x00;
P1MDOUT |= 0x00;
XBR0 = 0x01;
XBR1 = 0xC1;
}
//-----------------------------------------------------------------------------
// 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
}
/*
void Timer0_ISR (void) interrupt 1
{
Pulse_s++;
}
*/
//-----------------------------------------------------------------------------
// 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 Measure (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;
}
}
if(FrameLength==1) //deal with the "$GPGGA" frame
{
if((RecvByte>7)&&(RecvByte<18)) UTCtime[tmp-7]=UartBuff; //received UTCtime
if((RecvByte==20)&&(UartBuff==',')) FrameLength=2;//????,???????,???????
if((RecvByte>18)&&(RecvByte<28)) Longitude[tmp-18]=UartBuff; //received jing du
if((RecvByte>30)&&(RecvByte<41)) Latitude[tmp-30]=UartBuff; //received wei du
if (RecvByte==44) DGPS=UartBuff;
if((RecvByte>45)&&(RecvByte<48)) weixing[tmp-45]=UartBuff; //received weixing ge shu
else if ((RecvByte>48)&&(RecvByte<53)&&(UartBuff!=','))
{
HDOP[tmp-48]=UartBuff;
}
if(RecvByte>60) //52
{
if(UartBuff==0x0D) CRByte=UartBuff; //received CR
if(UartBuff==0x0A) LFByte=UartBuff; //receive
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
IsGpsFrame=1;
gps_ok=99;
}
}
}
if(FrameLength==3) //deal with the "$GPGGA" frame
{
// if((RecvByte>55)&&(RecvByte<62)) riqi[tmp-55]=UartBuff; //received Data
if(RecvByte>62) //52
{
if(UartBuff==0x0D) CRByte=UartBuff; //received CR
if(UartBuff==0x0A) LFByte=UartBuff; //receive
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
//IsGpsFrame=1;
//gps_ok=99;
}
}
}
TI0=0;
}
void Send_GPS(void)
{
unsigned char i;
unsigned char CK1;
unsigned char CK2;
short int j=0,m,n;
STI=0;
//--------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -