zemo161985
New Member
i want any prof. to read this (mikro c) and tell me what is the error and the correctionand if he is very professional send it completed and corrected on my e_mail: zimo161985@yahoo.com
note : we are using pic16f877a+max232+nokia6210+lcd
our goal is to send sms to the pic and get the report back
here is the code:
Microcontroller Code in C
Main program.h
#include "test1.h"
#include <LCD.C>
#include <input.c>
//#include <string.h>
//#include "internal_eeprom.c"
#include "nokia61.c"
unsigned char* get_data () //example
{
unsigned char buff[];
unsigned char data;
int i=0;
while (1)
{
data=getc();
if (data != 0x00)
buff[i++]= data;
else
break;
}
buff='\0';
return buff;
}
unsigned char* get_data2 () //mobile
{
unsigned char buff[];
unsigned char data;
int i=0;
int msg_found = 0;
while (1)
{
data=getc();
if (data == 0x03)
msg_found = 1;
else
if (data == 0x2E)
{
break;
}
else
if (msg_found)
buff[i++]= data;
}
buff='\0';
return buff;
}
void putstr(unsigned char* data)
{
unsigned char i;
i=0;
while (data != 0x00)
putc(data[i++]);
}
int comp (unsigned char* s , unsigned char* d , int len)
{
int i,count =0;;
for (i=0; i<len ;i++)
{
if( s == d)
count++;
}
if (count == len)
return 1;
else
return 0;
}
void copy (unsigned char* d , unsigned char* s , int len)
{
int i,count =0;;
for (i=0; i<len ;i++)
{
d = s;
}
}
void main(unsigned char* s)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
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);
setup_vref(FALSE);
SET_TRIS_A(0x00);
OUTPUT_A (0x00);
lcd_init();
{
unsigned char r[20];
unsigned char s1[7]= "Light1";
unsigned char s2[7]= "Light2";
unsigned char s3[7]= "Light3";
unsigned char l1[14] = "light1 on";
unsigned char l2[14] = "light2 on";
unsigned char l3[14] = "light3 on";
unsigned char smsc[13] = "+20122000020";
unsigned char phone[11] = "0122337666";
while (1)
{
strcpy(r,rsms());
print(r);
if (comp(r,s1,7))
{
OUTPUT_HIGH(PIN_A0);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l1 ,0x60);
}
else
if (comp(r,s2,7))
{
OUTPUT_HIGH(PIN_A1);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l2 ,0x60);
}
else
if (comp(r,s3,7))
{
OUTPUT_HIGH(PIN_A2);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l3 ,0x60);
}
init();
}
}
}
Nokia.c
#include <string.h>
#include "encry.h"
// **** Functions ****
unsigned char n61_recver();
unsigned char n61_recsenc();
unsigned char recsms (unsigned char);
void n61_sendack(unsigned char ,unsigned char);
// ** Code **
void init()
{
int i;
for (i=0; i<128;i++)
putc(0x55);
}
void print(unsigned char* data )
{
unsigned char i=0;
while(data)
lcd_putc(data[i++]);
}
static short timeout_error;
char timed_getc() {
long timeout;
timeout_error=FALSE;
timeout=0;
while(!kbhit()&&(++timeout<302))
delay_us(3);
if(kbhit())
return(getc());
else {
timeout_error=TRUE;
return(0);
}
}
void n61_sendframe(unsigned char type, unsigned char size, unsigned char *data)
{
unsigned char buf[80];
int at, n, len;
unsigned int *p;
unsigned char check1 = 0;
unsigned char check2 = 0;
at = 0;
// build header
buf[at++] = 0x1e; // message startbyte
buf[at++] = 0x00; // dest: phone
buf[at++] = 0x0c; // source: PC
buf[at++] = type;
buf[at++] = 0x00;
buf[at++] = size;
// add data
for(n = 0; n < size; ++n)
buf[at++] = data[n];
// if odd numbered, add filler byte
if(size % 2) {
buf[at++] = 0x00;
}
// calculate checksums
p = buf;
len = at;
for(n = 0; n < len; ++n)
{
check1 ^= buf[n++];
check2 ^= buf[n];
}
buf[at++] = check1;
buf[at++] = check2;
// send the message!
for(n = 0; n < at; ++n) {
putc(buf[n]);
}
}
void n61_sendack(unsigned char type, unsigned char seqnum)
{
char buf[10];
unsigned char n,check1,check2;
check1 = 0;
check2 = 0;
buf[0] = 0x1E;
buf[1] = 0x00;
buf[2] = 0x0C;
buf[3] = 0x7f;
buf[4] = 0x00;
buf[5] = 0x02;
buf[6] = type;
buf[7] = seqnum;
for(n = 0; n < 8; ++n)
{
check1 ^= buf[n++];
check2 ^= buf[n];
}
buf[8] = check1;
buf[9] = check2;
for(n = 0; n < 10; ++n)
putc(buf[n]);
}
unsigned char n61_ackwait(unsigned char seqnum)
{
unsigned char i,No;
unsigned char buf[10];
// while(1)
// {
if (timed_getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<10;i++)
buf = getc();
if ((buf[3] == 0x7f) && ( buf[7] == (seqnum & 0x07) ) && ( ( buf[7] & 0xF0) != 0x80 ) )
return 1;
else
return 0;
break;
}
// }
}
void n61_askforver (unsigned char seqnum)
{
unsigned char ver[7] = {0x00 ,0x01 ,0x00 ,0x03 ,0x00 ,0x01 ,0x00};
ver[6] = seqnum;
n61_sendframe(0xD1 ,7 ,ver);
if (n61_ackwait(seqnum))
{
n61_recver();
break;
}
}
void n61_senc (unsigned char seqnum)
{
unsigned char senc[6] = {0x00 ,0x01 ,0x00 ,0x01 ,0x01 ,0x00};
senc[5] = seqnum;
n61_sendframe(0x04 ,6 ,senc);
if (n61_ackwait(seqnum))
{
n61_recsenc();
break;
}
}
unsigned char n61_recver()
{
unsigned char i,No;
unsigned char buf[46];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<46;i++)
buf = getc();
if (buf[3] == 0xD2)
{
n61_sendack(0xD2 ,buf[43] & 0x07);
return 1;
}
else
{
n61_sendack(0xD2 ,buf[43] | 0x80);
return 0;
}
break;
}
}
}
unsigned char n61_recsenc()
{
unsigned char i,No;
unsigned char buf[20];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<20 ;i++)
buf = getc();
if (buf[3] == 0x04)
{
n61_sendack(0x04 ,buf[16] & 0x07);
return 1;
}
else
{
n61_sendack(0xD4 ,buf[16] | 0x80);
return 0;
}
break;
}
}
}
int n61_smssendfull(char *smsc ,char *dest ,char *msg ,unsigned char seqnum)
{
while (1)
{
unsigned char buf[80];
int n;
// standard frame data header
buf[0] = 0x00;
buf[1] = 0x01;
buf[2] = 0x00;
// send sms
buf[3] = 0x01;
buf[4] = 0x02;
buf[5] = 0x00;
// smsc
memset(buf + 6, 0, 12);
n61_bcd(buf + 6, smsc);
// TPDU ?
buf[18] = 0x15;
// misc
buf[19] = 0x00; // message ref
buf[20] = 0x00; // protocol ID
buf[21] = 0x00; // data coding scheme (non-flash)
// message size
buf[22] = strlen(msg);
// destination
memset(buf + 23, 0, 12);
n61_bcd2(buf + 23, dest);
// validity period
buf[35] = 0xA7;
// filler
buf[36] = 0;
buf[37] = 0;
buf[38] = 0;
buf[39] = 0;
buf[40] = 0;
buf[41] = 0;
// the string
n61_pack7(buf + 42, msg);
buf[ 42 + buf[22]] = 0x01;
buf[ 43 + buf[22]] = seqnum;
n61_sendframe (0x02 ,44 + buf[22] ,buf);
if (n61_ackwait (buf[43 + buf[22]]))
break;
}
}
unsigned char recint (unsigned char seqnumn)
{
unsigned char i,No;
unsigned char buf[18];
// for (i = 0 ;i<11 ; i++)
// {
if (timed_getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<18;i++)
buf = getc();
if (buf[3] == 0x02 && buf[5] == 0x09)
{
n61_sendack (0x02, (buf[14] & 0x07));
// print(recsms(seqnumn));
return 1;
break;
}
}
// }
return 0;
}
unsigned char recint2 ()
{
unsigned char i,No;
unsigned char buf[20];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<20 ;i++)
buf = getc();
if (buf[3] == 0x0D)
{
n61_sendack (0x0D, (buf[16] & 0x07));
return 1;
}
else
return 0;
break;
}
}
}
unsigned char* rsms ()
{
unsigned char type;
unsigned char size,seqnum;
unsigned char buf[60];
int i=0;
unsigned char del[8] ;
while(1)
{
if (getc() == 0x1E)
{
getc();
getc();
type = getc();
getc();
size = getc();
if (type == 0x02)
{
for(i=0 ;i<size ;i++)
{
buf = getc();
}
getc();
getc();
getc();
if (buf[3] == 0x10)
{
size = buf[22];
seqnum = buf[48];
n61_sendack (0x02, seqnum & 0x07);
// recint2();
// recint2();
delay_ms(906);
//delete msg
while (1)
{
init();
delay_ms(109);
del[0] = 0x00;
del[1] = 0x01;
del[2] = 0x00;
del[3] = 0x0A;
del[4] = buf[4];
del[5] = buf[5];
del[6] = 0x01;
del[7] = 0x60;
n61_sendframe (0x14 ,8 ,del);
if (n61_ackwait(seqnum & 0x07))
break;
}
// buf[42 + size] = '\0';
return(n61_unpack7(buf + 42 , size));
}
break;
}
break;
}
}
}
note : we are using pic16f877a+max232+nokia6210+lcd
our goal is to send sms to the pic and get the report back
here is the code:
Microcontroller Code in C
Main program.h
#include "test1.h"
#include <LCD.C>
#include <input.c>
//#include <string.h>
//#include "internal_eeprom.c"
#include "nokia61.c"
unsigned char* get_data () //example
{
unsigned char buff[];
unsigned char data;
int i=0;
while (1)
{
data=getc();
if (data != 0x00)
buff[i++]= data;
else
break;
}
buff='\0';
return buff;
}
unsigned char* get_data2 () //mobile
{
unsigned char buff[];
unsigned char data;
int i=0;
int msg_found = 0;
while (1)
{
data=getc();
if (data == 0x03)
msg_found = 1;
else
if (data == 0x2E)
{
break;
}
else
if (msg_found)
buff[i++]= data;
}
buff='\0';
return buff;
}
void putstr(unsigned char* data)
{
unsigned char i;
i=0;
while (data != 0x00)
putc(data[i++]);
}
int comp (unsigned char* s , unsigned char* d , int len)
{
int i,count =0;;
for (i=0; i<len ;i++)
{
if( s == d)
count++;
}
if (count == len)
return 1;
else
return 0;
}
void copy (unsigned char* d , unsigned char* s , int len)
{
int i,count =0;;
for (i=0; i<len ;i++)
{
d = s;
}
}
void main(unsigned char* s)
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
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);
setup_vref(FALSE);
SET_TRIS_A(0x00);
OUTPUT_A (0x00);
lcd_init();
{
unsigned char r[20];
unsigned char s1[7]= "Light1";
unsigned char s2[7]= "Light2";
unsigned char s3[7]= "Light3";
unsigned char l1[14] = "light1 on";
unsigned char l2[14] = "light2 on";
unsigned char l3[14] = "light3 on";
unsigned char smsc[13] = "+20122000020";
unsigned char phone[11] = "0122337666";
while (1)
{
strcpy(r,rsms());
print(r);
if (comp(r,s1,7))
{
OUTPUT_HIGH(PIN_A0);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l1 ,0x60);
}
else
if (comp(r,s2,7))
{
OUTPUT_HIGH(PIN_A1);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l2 ,0x60);
}
else
if (comp(r,s3,7))
{
OUTPUT_HIGH(PIN_A2);
init();
delay_ms(109);
n61_smssendfull(smsc ,phone, l3 ,0x60);
}
init();
}
}
}
Nokia.c
#include <string.h>
#include "encry.h"
// **** Functions ****
unsigned char n61_recver();
unsigned char n61_recsenc();
unsigned char recsms (unsigned char);
void n61_sendack(unsigned char ,unsigned char);
// ** Code **
void init()
{
int i;
for (i=0; i<128;i++)
putc(0x55);
}
void print(unsigned char* data )
{
unsigned char i=0;
while(data)
lcd_putc(data[i++]);
}
static short timeout_error;
char timed_getc() {
long timeout;
timeout_error=FALSE;
timeout=0;
while(!kbhit()&&(++timeout<302))
delay_us(3);
if(kbhit())
return(getc());
else {
timeout_error=TRUE;
return(0);
}
}
void n61_sendframe(unsigned char type, unsigned char size, unsigned char *data)
{
unsigned char buf[80];
int at, n, len;
unsigned int *p;
unsigned char check1 = 0;
unsigned char check2 = 0;
at = 0;
// build header
buf[at++] = 0x1e; // message startbyte
buf[at++] = 0x00; // dest: phone
buf[at++] = 0x0c; // source: PC
buf[at++] = type;
buf[at++] = 0x00;
buf[at++] = size;
// add data
for(n = 0; n < size; ++n)
buf[at++] = data[n];
// if odd numbered, add filler byte
if(size % 2) {
buf[at++] = 0x00;
}
// calculate checksums
p = buf;
len = at;
for(n = 0; n < len; ++n)
{
check1 ^= buf[n++];
check2 ^= buf[n];
}
buf[at++] = check1;
buf[at++] = check2;
// send the message!
for(n = 0; n < at; ++n) {
putc(buf[n]);
}
}
void n61_sendack(unsigned char type, unsigned char seqnum)
{
char buf[10];
unsigned char n,check1,check2;
check1 = 0;
check2 = 0;
buf[0] = 0x1E;
buf[1] = 0x00;
buf[2] = 0x0C;
buf[3] = 0x7f;
buf[4] = 0x00;
buf[5] = 0x02;
buf[6] = type;
buf[7] = seqnum;
for(n = 0; n < 8; ++n)
{
check1 ^= buf[n++];
check2 ^= buf[n];
}
buf[8] = check1;
buf[9] = check2;
for(n = 0; n < 10; ++n)
putc(buf[n]);
}
unsigned char n61_ackwait(unsigned char seqnum)
{
unsigned char i,No;
unsigned char buf[10];
// while(1)
// {
if (timed_getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<10;i++)
buf = getc();
if ((buf[3] == 0x7f) && ( buf[7] == (seqnum & 0x07) ) && ( ( buf[7] & 0xF0) != 0x80 ) )
return 1;
else
return 0;
break;
}
// }
}
void n61_askforver (unsigned char seqnum)
{
unsigned char ver[7] = {0x00 ,0x01 ,0x00 ,0x03 ,0x00 ,0x01 ,0x00};
ver[6] = seqnum;
n61_sendframe(0xD1 ,7 ,ver);
if (n61_ackwait(seqnum))
{
n61_recver();
break;
}
}
void n61_senc (unsigned char seqnum)
{
unsigned char senc[6] = {0x00 ,0x01 ,0x00 ,0x01 ,0x01 ,0x00};
senc[5] = seqnum;
n61_sendframe(0x04 ,6 ,senc);
if (n61_ackwait(seqnum))
{
n61_recsenc();
break;
}
}
unsigned char n61_recver()
{
unsigned char i,No;
unsigned char buf[46];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<46;i++)
buf = getc();
if (buf[3] == 0xD2)
{
n61_sendack(0xD2 ,buf[43] & 0x07);
return 1;
}
else
{
n61_sendack(0xD2 ,buf[43] | 0x80);
return 0;
}
break;
}
}
}
unsigned char n61_recsenc()
{
unsigned char i,No;
unsigned char buf[20];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<20 ;i++)
buf = getc();
if (buf[3] == 0x04)
{
n61_sendack(0x04 ,buf[16] & 0x07);
return 1;
}
else
{
n61_sendack(0xD4 ,buf[16] | 0x80);
return 0;
}
break;
}
}
}
int n61_smssendfull(char *smsc ,char *dest ,char *msg ,unsigned char seqnum)
{
while (1)
{
unsigned char buf[80];
int n;
// standard frame data header
buf[0] = 0x00;
buf[1] = 0x01;
buf[2] = 0x00;
// send sms
buf[3] = 0x01;
buf[4] = 0x02;
buf[5] = 0x00;
// smsc
memset(buf + 6, 0, 12);
n61_bcd(buf + 6, smsc);
// TPDU ?
buf[18] = 0x15;
// misc
buf[19] = 0x00; // message ref
buf[20] = 0x00; // protocol ID
buf[21] = 0x00; // data coding scheme (non-flash)
// message size
buf[22] = strlen(msg);
// destination
memset(buf + 23, 0, 12);
n61_bcd2(buf + 23, dest);
// validity period
buf[35] = 0xA7;
// filler
buf[36] = 0;
buf[37] = 0;
buf[38] = 0;
buf[39] = 0;
buf[40] = 0;
buf[41] = 0;
// the string
n61_pack7(buf + 42, msg);
buf[ 42 + buf[22]] = 0x01;
buf[ 43 + buf[22]] = seqnum;
n61_sendframe (0x02 ,44 + buf[22] ,buf);
if (n61_ackwait (buf[43 + buf[22]]))
break;
}
}
unsigned char recint (unsigned char seqnumn)
{
unsigned char i,No;
unsigned char buf[18];
// for (i = 0 ;i<11 ; i++)
// {
if (timed_getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<18;i++)
buf = getc();
if (buf[3] == 0x02 && buf[5] == 0x09)
{
n61_sendack (0x02, (buf[14] & 0x07));
// print(recsms(seqnumn));
return 1;
break;
}
}
// }
return 0;
}
unsigned char recint2 ()
{
unsigned char i,No;
unsigned char buf[20];
while(1)
{
if (getc() == 0x1E)
{
buf[0] = 0x1E;
for (i = 1 ;i<20 ;i++)
buf = getc();
if (buf[3] == 0x0D)
{
n61_sendack (0x0D, (buf[16] & 0x07));
return 1;
}
else
return 0;
break;
}
}
}
unsigned char* rsms ()
{
unsigned char type;
unsigned char size,seqnum;
unsigned char buf[60];
int i=0;
unsigned char del[8] ;
while(1)
{
if (getc() == 0x1E)
{
getc();
getc();
type = getc();
getc();
size = getc();
if (type == 0x02)
{
for(i=0 ;i<size ;i++)
{
buf = getc();
}
getc();
getc();
getc();
if (buf[3] == 0x10)
{
size = buf[22];
seqnum = buf[48];
n61_sendack (0x02, seqnum & 0x07);
// recint2();
// recint2();
delay_ms(906);
//delete msg
while (1)
{
init();
delay_ms(109);
del[0] = 0x00;
del[1] = 0x01;
del[2] = 0x00;
del[3] = 0x0A;
del[4] = buf[4];
del[5] = buf[5];
del[6] = 0x01;
del[7] = 0x60;
n61_sendframe (0x14 ,8 ,del);
if (n61_ackwait(seqnum & 0x07))
break;
}
// buf[42 + size] = '\0';
return(n61_unpack7(buf + 42 , size));
}
break;
}
break;
}
}
}