0% found this document useful (0 votes)
63 views4 pages

Code

The document contains a C code for a GSM module interfacing with a PIC microcontroller, which initializes the GSM module, sets message modes, and sends SMS messages. It also includes functions for displaying GPS data such as time, date, latitude, longitude, and speed on an LCD. The code handles USART communication and interrupts for receiving GPS data continuously.

Uploaded by

Giang Lệ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views4 pages

Code

The document contains a C code for a GSM module interfacing with a PIC microcontroller, which initializes the GSM module, sets message modes, and sends SMS messages. It also includes functions for displaying GPS data such as time, date, latitude, longitude, and speed on an LCD. The code handles USART communication and interrupts for receiving GPS data continuously.

Uploaded by

Giang Lệ
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

#include "Pic_GSM_demo.

h"

printf("AT+CMGF=0");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);

#include ".\myLCD16x2.c"
#include <math.h>
#define Led0 PIN_C1
#define Led1 PIN_C2
#define GSM_PW PIN_E2
#define GSM_SENSE PIN_E1
#define TEXTMODE 5;
#define PDUMODE 0;
void GSM_init(void);
void GSM_start(void);
void GSM_sel_mess_mode(char mode);
void GSM_init(void)
{
output_bit(GSM_PW,1);
output_bit(Led1,1);
output_float(GSM_SENSE);
}
void GSM_start(void)
{
//start up GSM module
delay_ms(2000);
lcd_init();
lcd_putc("\fwww.elabvn.com\n");
lcd_putc("Pic GSM rev 1.0");
//printf("GSM demo \n \r");
output_bit(GSM_PW,0);
output_bit(Led1,0);
delay_ms(2000);
output_bit(GSM_PW,1);
output_bit(Led1,1);
lcd_putc("\nGSM initializing");
delay_ms(50000);
}
void GSM_sel_mess_mode(char mode)
{
if(mode>0) //text mode
{
output_bit(Led1,0);
printf("AT+CMGF=1");
putc(13);//enter CR
output_bit(Led1,1);
delay_ms(1000);
output_bit(Led1,0);
}
else //PDU mode
{
output_bit(Led1,0);

}
}
void main()
{
int1 gsm_ok;
int cnt;
int1 temp;
char c;
int i;

GSM_init();
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2);
setup_spi(SPI_SS_DISABLED);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);// This device
COMP currently not supported by the PICWizard
//Setup_Oscillator parameter not selected from Intr
Oscillator Config tab
// TODO: USER CODE!!
GSM_start();
GSM_sel_mess_mode(5);
printf("ATE1"); //echo on
putc(13);//enter CR
delay_ms(1000);//
printf("AT");
putc(13);//enter CR
output_bit(Led1,1);
lcd_putc("\nGSM send message
delay_ms(1000);
output_bit(Led1,0);
printf("AT+CMGS=");
putc(34);//"
printf("0989703330");
putc(34);//"
putc(13);//enter CR
delay_ms(1000);//cho ki tu ">"
printf("SIM300 Ready");
delay_ms(1000);//
putc(0x1A);//CTRL+Z
output_bit(Led1,1);

");

delay_ms(2000);//
lcd_putc("\nGSM sent done
while(1);

");

Phn code cc bn c th tham kho code sau:


void pass_x_char(unsigned char x_char);
void wait_RMC(void);
void hour_display(unsigned char x, unsigned char y);
void date_display(unsigned char x, unsigned char y);
void lat_display(unsigned char x, unsigned char y);
void lon_display(unsigned char x, unsigned char y);
void speed_display(unsigned char x, unsigned char
y);
void hour_display(unsigned char x, unsigned char y)
// 020908
{
lcd_gotoxy(x,y);
lcd_putchar(hour[0]);
lcd_putchar(hour[1]);
lcd_putchar(':');
lcd_putchar(hour[2]);
lcd_putchar(hour[3]);
lcd_putchar(':');
lcd_putchar(hour[4]);
lcd_putchar(hour[5]);
}
//---------------------void date_display(unsigned char x, unsigned char y)
// 050510
{
lcd_gotoxy(x,y);
lcd_putchar(date[0]);
lcd_putchar(date[1]);
lcd_putchar('/');
lcd_putchar(date[2]);
lcd_putchar(date[3]);
lcd_putchar('/');
lcd_putchar(date[4]);
lcd_putchar(date[5]);
}
//---------------------void lat_display(unsigned char x, unsigned char y) //

2057.2950
{
unsigned char i;
lcd_gotoxy(x,y);
//lcd_putchar(lat0);
for (i=0;i<9;i++)
{
lcd_putchar(lat[i]);
}
}
//---------------------void lon_display(unsigned char x, unsigned char y) //
10550.3465
{
unsigned char i;
lcd_gotoxy(x,y);
for (i=0;i<10;i++)
{
lcd_putchar(lon[i]);
}
}
//---------------------void speed_display(unsigned char x, unsigned char
y)// 0.16
{
unsigned char i;
lcd_gotoxy(x,y);
for (i=0;i<4;i++)
{
lcd_putchar(speed[i]);
}
lcd_putsf(" Knots");
}
//---------------------void pass_x_char(unsigned char x_char)
{
for (i=0;i<x_char;i++)
{
Gps = getchar1();
}
}
//$GPRMC,020908.000,A,2057.2950,N,10550.3465,
E,0.16, 218.51,050510,,,A*6F Nha KHOA
void wait_RMC(void)
{
//----- wait for "$GPRMC" ---Gps = getchar1();
while (Gps != '$')

{
Gps = getchar1();
};
Gps = getchar1();
while (Gps != 'G')
{
Gps = getchar1();
};
Gps = getchar1();
while (Gps != 'P')
{
Gps = getchar1();
};
Gps = getchar1();
while (Gps != 'R')
{
Gps = getchar1();
};
Gps = getchar1();
while (Gps != 'M')
{
Gps = getchar1();
};
Gps = getchar1();
while (Gps != 'C')
{
Gps = getchar1();
};
//----- Hour detection -- $GPRMC,020908
pass_x_char(1); // ','
for (i=0;i<6;i++)
{
Gps = getchar1();
hour[i] = Gps;
hour[7] = '\0';
//lcd_gotoxy(i,0);
//lcd_putchar(Gps);
//delay_ms(1000);
}
//----- Latitude detection -$GPRMC,020908.000,A,2057.2950
pass_x_char(7); // ','
for (i=0;i<9;i++)
{
Gps = getchar1();
latx[i] = Gps;
latx[10] = '\0';
//lcd_gotoxy(i,1);
//lcd_putchar(Gps);

}
for (i=0;i<10;i++)
{
lat[i] = latx[i];
}
lat0 = lat[0];
//----- Longitude detection -$GPRMC,020908.000,A,2057.2950,N,10550.3465
pass_x_char(3); // ','
for (i=0;i<10;i++)
{
Gps = getchar1();
lon[i] = Gps;
lon[11] = '\0';
//lcd_gotoxy(i,0);
//lcd_putchar(Gps);
}
//----- Speed detection -$GPRMC,020908.000,A,2057.2950,N,10550.3465,E,
0.16
pass_x_char(3); // ','
for (i=0;i<4;i++)
{
Gps = getchar1();
speed[i] = Gps;
speed[5] = '\0';
//lcd_gotoxy(i,1);
//lcd_putchar(Gps);
}
//-----Date detection -$GPRMC,020908.000,A,2057.2950,N,10550.3465,E,
0.16, 218.51,050510,,,A*6F
pass_x_char(8); // ','
for (i=0;i<6;i++)
{
Gps = getchar1();
date[i] = Gps;
date[7] = '\0';
//lcd_gotoxy(i,0);
//lcd_putchar(Gps);
}
}

#include <p18f2610.h>
#include <delays.h>
#include <usart.h>
#include <adc.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main (void){

unsigned int j=0; //counter to print gps string


char GPS[100];
unsigned int count=0; //count number of char

#define CR 0x0D //carriage return char


TRISC = 0x80;
PORTC = 0;
#pragma config OSC=HSPLL, WDT=OFF,
LVP=OFF, DEBUG=ON

char gps[100];
int i = 0;
int data=0;

char getc_GPS();
void putc_GPS(char xc);

volatile char signal, received;


void high_int_handler (void);

OpenUSART(USART_TX_INT_OFF &
USART_RX_INT_ON &
USART_ASYNCH_MODE &
USART_EIGHT_BIT &
USART_CONT_RX &
USART_BRGH_LOW, 64);

RCONbits.IPEN = 1; // Enable interrupt


priority
IPR1bits.RCIP = 1; // Make receive interrupt
high priority
INTCONbits.GIEH = 1; // Enable all high
priority interrupts

#pragma code high_interrupt = 0x08


received = 0;
void high_int (void){
_asm goto high_int_handler _endasm
}
#pragma code
#pragma interrupt high_int_handler

while(1){

//while(data<100)

if (received == 1) {
received = 0;
//GPS[data] = signal; //copy into array

void high_int_handler (void){


received = 1;
signal = getcUSART(); // Get a character
from the USART
i = RCREG; //clear RCREG to clear receive
interrupt flag
}

putcUSART(signal);
//data++;
}
}
//putsUSART(GPS);
}

You might also like