⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gps__.c

📁 利用avr的芯片MEGA64L的ADC功能来计算电压
💻 C
字号:
#include<mega64.h>
#include<delay.h>
#include<stdlib.h>

#define GPS_RST         PORTE.5             //1    DDRE=00100010
#define GPS_TX          PORTD.2             //0    DDRD=11111011
#define GPS_RX          PORTD.3             //1    DDRB=01100000
#define SHDN_232        PORTB.6             //1
#define EN_232          PORTB.5             //1
#define RX_EPOS         PORTE.0             //0
#define TX_EPOS         PORTE.1             //1

#define FE0             4
#define DOR0            3
#define UPE0            2
#define UDRE0           5
#define VREF           3.3

#define FE1             4
#define DOR1            3
#define UPE1            2
#define UDRE1           5
#define RXC1            7

#define SRAM0           PORTD.4
#define SRAM1           PORTD.5
#define SRAM2           PORTD.6
#define SRAM3           PORTD.7

volatile unsigned char th;
volatile unsigned char sh;
volatile unsigned char buffer[100];
volatile unsigned int  i;
volatile unsigned int sectionid;
volatile unsigned char buf[7];
 volatile unsigned char section6;
volatile unsigned char sectioncount;
volatile unsigned char buf_sram[100];
volatile  unsigned int sram_count;
volatile unsigned int j;
volatile unsigned char sram0;
volatile unsigned char sram1;
volatile unsigned char sram2;
volatile unsigned char sram3;
volatile unsigned int count=0;
volatile unsigned int status;
unsigned char sate;
unsigned char alarm;
volatile unsigned  int n;
volatile unsigned long int  offset=0x1100;
volatile unsigned long int  offset1=0x1100;
volatile unsigned char str;
volatile unsigned int p;
unsigned char a;
unsigned char b;
unsigned char c;
unsigned char gpscount;
volatile unsigned gps_alarm;
volatile unsigned power_alarm;
volatile unsigned position_alarm;
unsigned char power_disconnect;
unsigned char engps_alarm;
unsigned char position_breakaway;
unsigned char all;

volatile unsigned int alarm_count;
volatile unsigned char gps_gdr;
volatile unsigned  char gps_gsc;
volatile unsigned char gps_sramvisible;


void gpsreset(void)             //GPS复位
{
 GPS_RST=0;
 delay_ms(500);
 GPS_RST=1;
 delay_ms(500);
 }

void initusart0(void)          //串口0的初始化
{
  UCSR0C =0x06;
  UBRR0L=0x07;
  UBRR0H=0x00;
  UCSR0A = 0x00;
  UCSR0B = 0x98;
}
void initusart1(void)             //串口1的初始化
{
  UCSR1A=0X00;
  UCSR1B=0X98;     //10011000
  UCSR1C=0X06;     //00000110
  UBRR1L=95;
  UBRR1H=0X00;
  }
  void waikuo(void)                //外部扩展的初始化
  {
    MCUCR=0X80;
    XMCRA=0X00;
    XMCRB=0X00;
    }

   void put_c(char c)
   {
   while(!(UCSR0A&(1<<UDRE0)));
   UDR0=c;
   }
  void write1(unsigned long int add,unsigned char val)          //将数据写入相应的地址
 {
   unsigned char *d;
   d=(unsigned char *)(add);
   *d=val;
 }
   char  read1(unsigned long int add)
 {
   unsigned char *t;
   t=(unsigned char *)(add);
   return *t;
 }
  void gpsput(unsigned char command)
  {
    while(!(UCSR1A&(1<<UDRE1)));
     UDR1=command;
  }
  void gpsputs(flash unsigned char *ptr)
{
	while (*ptr)
	{
		gpsput(*ptr++);
	}
}

void sramvisible(void)
     {

    while(j<sram_count){
    if(sram0==1){
    if(offset==32752){
    offset=0x1100;
    offset1=0x1100;
    sram1=1;
    sram0=0;
    }
    else
    {
      PORTD=0X10;
      write1(offset,buf_sram[j]);
      p=read1(offset);
      put_c(p);
      offset++;


    }
    }
    if(sram1==1)
    {
      if(offset==32752)
     {
      offset=0x1100;
      offset1=0x1100;
      sram1=0;
      sram2=1;
      }
      else
      {
        PORTD=0X20;
        write1(offset,buf_sram[j]);
        p=read1(offset);
        put_c(p);
        offset++;
      }
      }
     if(sram2==1)
     {

      if(offset==32752)
      {
      offset=0x1100;
      offset1=0x1100;
      sram2=0;
      sram3=1;
      }
      else
      {
       PORTD=0X40;
       write1(offset,buf_sram[j]);
       p=read1(offset);
       put_c(p);

       offset++;
      }
      }
      if(sram3==1)
     {

      if(offset==32752)
      {
      offset=0x1100;
      offset1=0x1100;
      sram3=0;
      sram0=1;
      }
      else
      {
        PORTD=0X80;
        write1(offset,buf_sram[j]);
        p=read1(offset);
         put_c(p);
        offset++;
       }
      }
     j++;
      }
     j=0;
     offset=offset1+100;
     offset1=offset1+100;
     sram_count=0;
      }
 void gprmc(void)
  {
    gpscount++;
   if((sectionid==8||sectionid==9)&&buffer[i]==',') {

   buf_sram[sram_count]=0x2c;
   sram_count++;
   }
   if(buffer[i]==',')
         sectionid++;
       else
       {
        switch(sectionid)
       {
       case 2:
         if(buffer[i]=='A')
         status=1;
         if(buffer[i]=='V')
         status=0;
        break;
         case 8:

           buf_sram[sram_count]=buffer[i];

           sram_count++;
           break;
           case 9:

           buf_sram[sram_count]=buffer[i];

           sram_count++;
           break;
           default:
           break;
         }

        }


       }
   void gpgga(void)
  {
   gpscount++;
 if((sectioncount==0||sectioncount==1||sectioncount==2||sectioncount==3||sectioncount==4||sectioncount==5||sectioncount==9||sectioncount==10)&&buffer[i]==',')
  {

   buf_sram[sram_count]=0x2c;
   sram_count++;
   }
   if(buffer[i]==',')
    sectioncount++;
  else{
        switch(sectioncount)
        {
          case 1:                                          //shijian

              buf_sram[sram_count]=buffer[i];
              sram_count++;

              break;
          case 2:                                       //weidu

              buf_sram[sram_count]=buffer[i];

              sram_count++;
              break;
          case 3:                                      //n

              buf_sram[sram_count]=buffer[i];

              sram_count++;
              break;
          case 4:                                       //jingdu

              buf_sram[sram_count]=buffer[i];

              sram_count++;
               break;
          case 5:                                       //e

              buf_sram[sram_count]=buffer[i];

              sram_count++;
              break;
         case 7:
              if(buffer[i]=='0')
               sate=1;
               else
               sate=0;
              break;
          case 9:                                      //gaodu

              buf_sram[sram_count]=buffer[i];

              sram_count++;
              break;
          case 10:                                     //danwei

              buf_sram[sram_count]=buffer[i];

              sram_count++;
              break;
              default:
              break;
        }
      }
  }

 void gpvtg(void)
 {
 gpscount++;
 if((section6==7||section6==8)&&buffer[i]==',')
  {

  buf_sram[sram_count]=0x2c;
  sram_count++;
  }
  if(buffer[i]==',')
          section6++;
          else
          {
          switch(section6)
          {
           case 7:

            buf_sram[sram_count]=buffer[i];
            sram_count++;
            break;
            case 8:

            buf_sram[sram_count]=buffer[i];
            sram_count++;
            break;
            }
            }
   if(section6==9&&buffer[i]==',')
      {
       buf_sram[sram_count]=0x0d;
       sram_count++;
       buf_sram[sram_count]=0x0a;
       sram_count++;
       count++;
    if(count<1000)//&&status==1)
     {sramvisible();
     gpscount=0;
     }
    if(count>=1000)
       {
        sram0=1;
        sram1=0;
        sram2=0;
        sram3=0;
        count=0;
        offset=0x1100;
        offset1=0x1100;
        }

       if(status==1&&sate==1)
       {
         alarm_count++;
         if(alarm_count>=4)

           engps_alarm=1;


       }
       }
    }
interrupt[USART1_RXC]void usart1_isr(void)                      //USART1的接收中断。
   {


    sh=UDR1;
    buffer[i]=sh;
  if(buffer[i]=='C'&&buffer[i-1]=='M'&&buffer[i-2]=='R'&&buffer[i-3]=='P'&&buffer[i-4]=='G'&&buffer[i-5]=='$')
      {
      sectionid=0;
      buf[0]='$';
      buf[5]='C';
      }
  if(buffer[i]==0x0a)
       i=0;
  if(buffer[i]=='A'&&buffer[i-1]=='G'&&buffer[i-2]=='G'&&buffer[i-3]=='P'&&buffer[i-4]=='G'&&buffer[i-5]=='$')
  {
     sectioncount=0;
     buf[0]='$';
     buf[4]='A';
  }
  if(buffer[i]=='G'&&buffer[i-1]=='T'&&buffer[i-2]=='V'&&buffer[i-3]=='P'&&buffer[i-4]=='G'&&buffer[i-5]=='$')
  {
   section6=0;
   buf[0]='$';
   buf[5]='G';
   }
   if(buf[0]=='$'&&buf[4]=='A')
    { gpgga();

      }
   if(buf[0]=='$'&&buf[5]=='C')
   {   gprmc();

      }
   if(buf[5]=='G'&&buf[0]=='$')
       gpvtg();
   i++;

  }
 void gps5s(void)
  {
    gpsputs("$PSRF103,2,0,0,1*26\r\n");

    delay_ms(500);

    gpsputs("$PSRF103,3,0,0,1*27\r\n");

    delay_ms(500);
    gpsputs("$PSRF103,0,0,255,1*26\r\n");
    delay_ms(500);

    gpsputs("$PSRF103,4,0,255,1*22\r\n");
    delay_ms(500);

    gpsputs("$PSRF103,5,0,255,1*23\r\n");
    delay_ms(500);

    }
   void main(void)
   {

    #asm("sei")
    gpsreset();
    sram0=1;
    initusart0();
    initusart1();
    PORTB=0X40;
    DDRB=0X60;
    PORTD=0X00;
    PORTC=0X80;
    DDRC=0X80;
    DDRD=0XF3;
    DDRE=0X20;
    waikuo();
    sram0=1;
    sram1=0;
    sram2=0;
    sram3=0;
    gps5s();
   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -