Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

HI Tech C + PIC16F877A + GSM SIM900D

Status
Not open for further replies.

razgriz66

Newbie level 5
Newbie level 5
Joined
Mar 24, 2013
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,336
k. so im having trouble understanding why my gsm and pic cant understand each other.
so far i've been able to do PC to gsm and PC to pic, no luck with gsm to pic.
im using pic16f877a, sim900D, and usb to uart(PL2303) for testing
i have 3 files in the project folder
here's the main code:
main.c
Code:
#define _LEGACY_HEADERS
#include <htc.h>
#include <stdio.h>
#include "usart.h"

__CONFIG(HS & WDTDIS & PWRTEN & BOREN & LVPDIS & UNPROTECT);

//DELAY===================================================================
void sysd(time)
{
	while(time != 0)
	{
		time = time-1;
	}
}
//SEND MSG================================================================
void sendmsg(x)
{
	printf("\rAT\n");
	sysd(20000);
	printf("\rAT+CMGF=1\n");
	sysd(20000);
	printf("\rAT+CMGS=\"09654545564\"\n");
	sysd(20000);
	
	if(x == 0)
	{
		if(RB6 == 0)
			printf("\rBin1 Status: OK!");
		else if(RB6 == 1)
			printf("\rBin1 Status: WARNING!");
		if(RB5 == 0)
			printf("\rBin2 Status: OK!");
		else if(RB5 == 1)
			printf("\rBin2 Status: WARNING!");
		if(RB4 == 0)
			printf("\rBin3 Status: OK!");
		else if(RB4 == 1)
			printf("\rBin3 Status: WARNING!");
		if(RB3 == 0)
			printf("\rBin4 Status: OK!");
		else if(RB3 == 1)
			printf("\rBin4 Status: WARNING!");
		printf("\n");
	}
	else if(x == 1)
	{
		printf("\rThis is a test message!\n");
	}
	sysd(10000);
}
//TRIGGER FEEDING=========================================================
void triggerfeeding()
{
	RB7 = 0;
	sysd(20000);
	RB7 = 1;
}
//PROGRAM START===========================================================
void main(void)
{
	ADCON1 = 0x0F;
	INTCON=0;	// purpose of disabling the interrupts.
	init_comms();	// set up the USART - settings defined in usart.h

	TRISB = 0x0F;	//Feeder Module 1111 0000
					//rx/tx
	TRISE = 0x07;	//Trouble Shooting Switches(Trigger and GSM) + Clock 
					//0000 0111
	TRISD = 0x00;

	PORTB = 0x00;
	PORTE = 0x00;
	
	RB7 = 1;

	while(1)
	{
		sendmsg(1);	//for testing
		if(RB0 == 1)
		{
			triggerfeeding();
			sendmsg(0);
		}
		if(RE1 == 1)	//test feeder
		{
			triggerfeeding();
		}
		if(RE2 == 1)	//test GSM
		{
			sendmsg(1);
		}
	}
	
}
the ff codes are from the example:
usart.c
Code:
#include <htc.h>
#include <stdio.h>
#include "usart.h"

void 
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

unsigned char 
getch() {
	/* retrieve one byte */
	while(!RCIF)	/* set when register is not empty */
		continue;
	return RCREG;	
}

unsigned char
getche(void)
{
	unsigned char c;
	putch(c = getch());
	return c;
}
usart.h
Code:
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600      
#define FOSC 4000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#if defined(_16F87) || defined(_16F88)
	#define RX_PIN TRISB2
	#define TX_PIN TRISB5
#else
	#define RX_PIN TRISC7
	#define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
	RX_PIN = 1;	\
	TX_PIN = 1;		  \
	SPBRG = DIVIDER;     	\
	RCSTA = (NINE_BITS|0x90);	\
	TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif

so far what i know is commands are terminated with CR and messages are sent with substitute
https://www.asciitable.com/
the problem i believe lie on my printf's
 
Last edited:

isnt it the usart.h?
im using all 3 files im just stating that the other 2 didnt came from me(i didnt program it) but from an example
 

as stated above
usart.h
Code:
#ifndef _SERIAL_H_
#define _SERIAL_H_

#define BAUD 9600      
#define FOSC 4000000L
#define NINE 0     /* Use 9bit communication? FALSE=8bit */

#define DIVIDER ((int)(FOSC/(16UL * BAUD) -1))
#define HIGH_SPEED 1

#if NINE == 1
#define NINE_BITS 0x40
#else
#define NINE_BITS 0
#endif

#if HIGH_SPEED == 1
#define SPEED 0x4
#else
#define SPEED 0
#endif

#if defined(_16F87) || defined(_16F88)
	#define RX_PIN TRISB2
	#define TX_PIN TRISB5
#else
	#define RX_PIN TRISC7
	#define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
	RX_PIN = 1;	\
	TX_PIN = 1;		  \
	SPBRG = DIVIDER;     	\
	RCSTA = (NINE_BITS|0x90);	\
	TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif
 

its in the usart.h
i posted all 3 programs on the 1st post
these include
main.c (main program)
usart.c (dont know what it does but im pretty sure its important. i think its the one that triggers sending)
usart.h (usart settings)

PC-GSM and PC-PIC is working
GSM-PIC isnt
i believe PC-PIC wont be working if i got those wrong
 

from usart.h

Code:
#if defined(_16F87) || defined(_16F88)
	#define RX_PIN TRISB2
	#define TX_PIN TRISB5
#else
	#define RX_PIN TRISC7
	#define TX_PIN TRISC6
#endif

/* Serial initialization */
#define init_comms()\
	RX_PIN = 1;	\
	TX_PIN = 1;		  \
	SPBRG = DIVIDER;     	\
	RCSTA = (NINE_BITS|0x90);	\
	TXSTA = (SPEED|NINE_BITS|0x20)

void putch(unsigned char);
unsigned char getch(void);
unsigned char getche(void);

#endif
 

still nothing.
i think my printf codes are a broken.
the gsm needs carriage return to execute commands (0D in hex)
and needs substitute to send a msg (1A in hex)
 

Last edited:

rd is a char variable. It is cleared first and then if modem sends response it is read and rd will contain some character and so it is tested for true. If true then led is turned ON. It will show if modem is sending response for AT command.
 

i made something like this to try it out still nothing

Code:
void sendtestmsg()
{
	putch('A');		//AT
	putch('T');
	putch(0x0D);
	sysd(20000);
	putch('A');		//AT+CMGF=1
	putch('T');
	putch('+');
	putch('C');
	putch('M');
	putch('G');
	putch('F');
	putch('=');
	putch('1');
	putch(0x0D);
	sysd(20000);
	putch('A');		//AT+CMGS="09123456789"
	putch('T');
	putch('+');
	putch('C');
	putch('M');
	putch('G');
	putch('S');
	putch('=');
	putch('"');
	putch('0');
	putch('9');
	putch('1');
	putch('2');
	putch('3');
	putch('4');
	putch('5');
	putch('6');
	putch('7');
	putch('8');
	putch('9');
	putch('"');
	putch(0x0D);
	sysd(20000);
	putch('T');		//Test Message
	putch('E');
	putch('S');
	putch('T');
	putch(' ');
	putch('M');
	putch('S');
	putch('G');
	putch('.');
	putch(0x1A);
	sysd(10000);
}

this is the putch code
Code:
putch(unsigned char byte) 
{
	/* output one byte */
	while(!TXIF)	/* set when register is empty */
		continue;
	TXREG = byte;
}

i also did the led indicator that turns on/off when i send. it does turn on and off but im not receiving anything on the other phone.
simulations from proteus works like a charm though. its showing what i want it to show
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top