ATcom
Newbie level 5
Can you please help find the mistake on this code because it cn't send SMS after programming it into PIC16f877
please help me. Submission is due tomorrow
#include <pic.h>
#include <conio.h>
#include <stdio.h>
//#include "always.h"
//#include "delay.h"
__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPEN & WRTEN &
DEBUGEN & DUNPROT & UNPROTECT);
#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
#define RX_PIN TRISC7
#define TX_PIN TRISC6
void serial_setup(void)
{
SPBRG=DIVIDER;
BRGH=HIGH_SPEED; //data rate for sending
SYNC=0; //asynchronous
SPEN=1; //enable serial port pins
CREN=1; //enable reception
SREN=0; //no effect
TXIE=0; //disable tx interrupts
RCIE=0; //disable rx interrupts
TX9=0; //8-bit transmission
RX9=0; //8-bit reception
TXEN=0; //reset transmitter
TXEN=1; //enable the transmitter
}
unsigned char dummy;
#define clear_usart_errors_inline \
if (OERR) \
{ \
TXEN=0; \
TXEN=1; \
CREN=0; \
CREN=1; \
} \
if (FERR) \
{ \
dummy=RCREG; \
TXEN=0; \
TXEN=1; \
}
//writes a character to the serial port
void putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=c;
_delay(60);
}
//gets a character from the serial port without timeout
unsigned char getch(void)
{
while(!RCIF)
{
CLRWDT();
clear_usart_errors_inline;
}
return RCREG;
}
unsigned char getch_timeout(void)
{
unsigned char i;
unsigned int timeout_int;
// retrieve one byte with a timeout
for (i=2;i!=0;i--)
{
timeout_int=timeout_int_us(240000);
while (hibyte(timeout_int)!=0) //only check the msb of the int for being 0, it saves space, see always.h for macro
{
CLRWDT();
timeout_int--;
if (RCIF)
{
return RCREG;
}
}
}
return 0;
}
void clear_usart_errors(void)
{
clear_usart_errors_inline;
}
/*
writes a character to the serial port in hex
if serial lines are disconnected, there are no errors
*/
void putchhex(unsigned char c)
{
unsigned char temp;
// transmits in hex
temp=c;
c=(c >> 4);
if (c<10) c+=48; else c+=55;
putch(c);
c=temp;
c=(c & 0x0F);
if (c<10) c+=48; else c+=55;
putch(c);
}
void putinthex(unsigned int c)
{
#define ramuint(x) (*((unsigned int *) (x)))
#define ramuint_hibyte(x) (*(((unsigned char *)&x)+1))
#define ramuint_lobyte(x) (*(((unsigned char *)&x)+0))
#define ramuchar(x) (*((unsigned char *) (x)))
putchhex(ramuint_hibyte(c));
putchhex(ramuint_lobyte(c));
#undef ramuint(x)
#undef ramuint_hibyte(x)
#undef ramuint_lobyte(x)
#undef ramuchar(x)
}
//if there has been a previous timeout error from getch_timeout, this returns TRUE
unsigned char usart_timeout(void)
{
// return usart_timeout_error;
return 0;
}
/*
writes a character to the serial port in decimal
if serial lines are disconnected, there are no errors
*/
void putchdec(unsigned char c)
{
unsigned char temp;
temp=c;
//hundreds
if ((c/100)>0) putch((c/100)+'0');
c-=(c/100)*100;
//tens
if (((temp/10)>0) || ((temp/100)>0)) putch((c/10)+'0');
c-=(c/10)*10;
//ones
putch((c/1)+'0');
}
void putst(register const char *str)
{
while((*str)!=0)
{
putch(*str);
if (*str==13) putch(10);
if (*str==10) putch(13);
str++;
}
}
/* 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);*/
void serial_setup(void);
void putch(unsigned char c);
unsigned char getch(void);
void putst(register const char * str);
unsigned char usart_timeout(void);
void putchdec(unsigned char c);
void putchhex(unsigned char c);
void putinthex(unsigned int c);
void Read_AD_Input(void);
#endif
/* This function initializes the A/D converter so that analog data can be
received from channel AN0 of the microcontroller */
void Initialize_AD(void)
{
ADCON1 = 0x8E; /* Configure AN0 for +5V reference */
ADCON0 = 0x41; /* Select A/D converter clock */
}
/* This function reads data from the A/D converter and stores it in
variable yk */
float Yk;
void Read_AD_Input(void)
{
unsigned int adc_value;
GODONE=1; /* Start A/D conversion */
while(GODONE)continue; /* Wait until convertion completes */
adc_value = (ADRESH<<2) + (ADRESL>>6);
Yk= 5 * (float)adc_value / 1023;
}
unsigned char;
void init(void)
{
TRISA = 1;
TRISB = 0;
TRISC = 0;
//TRISD = 0;
}
char High;
char counter;
const char GSM_0 = "AT$TT";
const char GSM_1 = "AT+CLCK='SC',0,00000"; //disable PIN
const char GSM_2 = "AT+CMGF=0";
const char GSM_3 = "AT+CMGS=37<CR>069162860280F801000B916256597941F30000114927555A9D269F4E10B1482D0EA94522<Ctrl+z>";
void main(void)
{
init();
counter = 0;
init_comms();
High = 1;
PORTB = 0;
PORTA = 0;
PORTC = 0;
#define putlf putst("\n")
putch(GSM_0);
_delay(2000); // wait half second
putch(GSM_1);
_delay(2000);
putch(GSM_2);
_delay(2000);
if(PORTA = High )
{
putch(GSM_3);
}
}
please help me. Submission is due tomorrow
Code:
#include <pic.h>
#include <conio.h>
#include <stdio.h>
//#include "always.h"
//#include "delay.h"
__CONFIG(XT & WDTDIS & PWRTDIS & BORDIS & LVPEN & WRTEN &
DEBUGEN & DUNPROT & UNPROTECT);
#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
#define RX_PIN TRISC7
#define TX_PIN TRISC6
void serial_setup(void)
{
SPBRG=DIVIDER;
BRGH=HIGH_SPEED; //data rate for sending
SYNC=0; //asynchronous
SPEN=1; //enable serial port pins
CREN=1; //enable reception
SREN=0; //no effect
TXIE=0; //disable tx interrupts
RCIE=0; //disable rx interrupts
TX9=0; //8-bit transmission
RX9=0; //8-bit reception
TXEN=0; //reset transmitter
TXEN=1; //enable the transmitter
}
unsigned char dummy;
#define clear_usart_errors_inline \
if (OERR) \
{ \
TXEN=0; \
TXEN=1; \
CREN=0; \
CREN=1; \
} \
if (FERR) \
{ \
dummy=RCREG; \
TXEN=0; \
TXEN=1; \
}
//writes a character to the serial port
void putch(unsigned char c)
{
while(!TXIF) //set when register is empty
{
clear_usart_errors_inline;
CLRWDT();
}
TXREG=c;
_delay(60);
}
//gets a character from the serial port without timeout
unsigned char getch(void)
{
while(!RCIF)
{
CLRWDT();
clear_usart_errors_inline;
}
return RCREG;
}
unsigned char getch_timeout(void)
{
unsigned char i;
unsigned int timeout_int;
// retrieve one byte with a timeout
for (i=2;i!=0;i--)
{
timeout_int=timeout_int_us(240000);
while (hibyte(timeout_int)!=0) //only check the msb of the int for being 0, it saves space, see always.h for macro
{
CLRWDT();
timeout_int--;
if (RCIF)
{
return RCREG;
}
}
}
return 0;
}
void clear_usart_errors(void)
{
clear_usart_errors_inline;
}
/*
writes a character to the serial port in hex
if serial lines are disconnected, there are no errors
*/
void putchhex(unsigned char c)
{
unsigned char temp;
// transmits in hex
temp=c;
c=(c >> 4);
if (c<10) c+=48; else c+=55;
putch(c);
c=temp;
c=(c & 0x0F);
if (c<10) c+=48; else c+=55;
putch(c);
}
void putinthex(unsigned int c)
{
#define ramuint(x) (*((unsigned int *) (x)))
#define ramuint_hibyte(x) (*(((unsigned char *)&x)+1))
#define ramuint_lobyte(x) (*(((unsigned char *)&x)+0))
#define ramuchar(x) (*((unsigned char *) (x)))
putchhex(ramuint_hibyte(c));
putchhex(ramuint_lobyte(c));
#undef ramuint(x)
#undef ramuint_hibyte(x)
#undef ramuint_lobyte(x)
#undef ramuchar(x)
}
//if there has been a previous timeout error from getch_timeout, this returns TRUE
unsigned char usart_timeout(void)
{
// return usart_timeout_error;
return 0;
}
/*
writes a character to the serial port in decimal
if serial lines are disconnected, there are no errors
*/
void putchdec(unsigned char c)
{
unsigned char temp;
temp=c;
//hundreds
if ((c/100)>0) putch((c/100)+'0');
c-=(c/100)*100;
//tens
if (((temp/10)>0) || ((temp/100)>0)) putch((c/10)+'0');
c-=(c/10)*10;
//ones
putch((c/1)+'0');
}
void putst(register const char *str)
{
while((*str)!=0)
{
putch(*str);
if (*str==13) putch(10);
if (*str==10) putch(13);
str++;
}
}
/* 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);*/
void serial_setup(void);
void putch(unsigned char c);
unsigned char getch(void);
void putst(register const char * str);
unsigned char usart_timeout(void);
void putchdec(unsigned char c);
void putchhex(unsigned char c);
void putinthex(unsigned int c);
void Read_AD_Input(void);
#endif
/* This function initializes the A/D converter so that analog data can be
received from channel AN0 of the microcontroller */
void Initialize_AD(void)
{
ADCON1 = 0x8E; /* Configure AN0 for +5V reference */
ADCON0 = 0x41; /* Select A/D converter clock */
}
/* This function reads data from the A/D converter and stores it in
variable yk */
float Yk;
void Read_AD_Input(void)
{
unsigned int adc_value;
GODONE=1; /* Start A/D conversion */
while(GODONE)continue; /* Wait until convertion completes */
adc_value = (ADRESH<<2) + (ADRESL>>6);
Yk= 5 * (float)adc_value / 1023;
}
unsigned char;
void init(void)
{
TRISA = 1;
TRISB = 0;
TRISC = 0;
//TRISD = 0;
}
char High;
char counter;
const char GSM_0 = "AT$TT";
const char GSM_1 = "AT+CLCK='SC',0,00000"; //disable PIN
const char GSM_2 = "AT+CMGF=0";
const char GSM_3 = "AT+CMGS=37<CR>069162860280F801000B916256597941F30000114927555A9D269F4E10B1482D0EA94522<Ctrl+z>";
void main(void)
{
init();
counter = 0;
init_comms();
High = 1;
PORTB = 0;
PORTA = 0;
PORTC = 0;
#define putlf putst("\n")
putch(GSM_0);
_delay(2000); // wait half second
putch(GSM_1);
_delay(2000);
putch(GSM_2);
_delay(2000);
if(PORTA = High )
{
putch(GSM_3);
}
}