Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Alert Alarm System

Status
Not open for further replies.

butters

Member
hi all... im currently doing on this project(attached).. basically my part is the communication between microcontroller and remote system and acknowledgement (slides 7 and 8)... basically i have tried various examples but i couldnt find a way to do it.. pointers and helps are much appreciated...


Code:
#include <p18f4520.h>
#include <usart.h>
#include <delays.h>
#include <portb.h>

unsigned char c;
unsigned char c1;
unsigned char c2;
unsigned char c3;
unsigned char c4;
unsigned char c5;

/*
  unsigned char c[6];
  unsigned char i;
  unsigned char b;
*/
void rx_handler (void);//declare the ISR function.HIGH



#define BUF_SIZE 25

/*
 * Step #1  The data is allocated into its own section.
 */
#pragma idata bigdata
char data[8][BUF_SIZE+1] = {
  { "You Typed: 0\n\r" },
  { "You Typed: 1\n\r" },
  { "You Typed: 2\n\r" },
  { "You Typed: 3\n\r" },
  { "You Typed: 4\n\r" },
  { "You Typed: 5\n\r" },
  { "Invalid key (0-5 only)\n\r" }
};


#pragma idata


//location of the ISR.(HIGH)
#pragma code rx_interrupt = 0x8
void rx_int (void)
{
  _asm goto rx_handler _endasm
}
#pragma code  //return to default code section



#pragma interrupt rx_handler
void rx_handler (void)
{




/*
b = getcUSART();

if(b == '1')
{ 
	c[0] = b;
	i = 1;
}
else
{
	c[i] = b;
	i++;
}
*/




  //Get the character received from the USART 
while(!DataRdyUSART());
 c  = getcUSART();
while(!DataRdyUSART());
 c1 = getcUSART();
while(!DataRdyUSART());
 c2 = getcUSART();
while(!DataRdyUSART());
 c3 = getcUSART();
while(!DataRdyUSART());
 c4 = getcUSART();




    //Clear the interrupt flag 
    PIR1bits.RCIF = 0;
}




void main (void)
{
ADCON1 = 0x0F;
TRISCbits.TRISC5 = 0;

//LED
TRISB = 0;


  OpenUSART (USART_TX_INT_OFF &
             USART_RX_INT_ON &
             USART_ASYNCH_MODE &
             USART_EIGHT_BIT &
             USART_CONT_RX &
             USART_BRGH_HIGH, 25);


PORTBbits.RB3 = 1;

PORTCbits.RC5 = 1;
Delay10KTCYx(50);


while (BusyUSART());
	//value sent
	putcUSART(0x31);

while (BusyUSART());
	//value sent
	putcUSART(0x32);

while (BusyUSART());
//value sent 
	putcUSART(0x33);

while (BusyUSART());
	//value sent
	putcUSART(0x34);

while (BusyUSART());
	//value sent
	putcUSART(0x35);



Delay10KTCYx(50);

PORTBbits.RB3 = 0;
PORTCbits.RC5 = 0;




/* Enable interrupt priority */
  RCONbits.IPEN = 1;

  /* Make receive interrupt high priority */
  IPR1bits.RCIP = 1;

  /* Enable all high priority interrupts */
  INTCONbits.GIEH = 1;

  /* Loop forever */
 while (1)
    ;
}

and here's a code i have been working on for the communication between the remote system and PIC...
 

Attachments

  • File1-AAS4.pdf
    816.9 KB · Views: 301
Last edited:
Well, it looks to be doing something completely different.

Try getting it working without interrupts first, something like,
Code:
char GetByte(){
    while(!DataRdyUSART());
    return(getcUSART());
}


    
    while(GetByte()!=0xce);     //wait for header byte
    checksum=0xce;              //initialise checksum
    for(i=0;i<4;i++){           //now get next 4 bytes
        c[i]=GetByte();
        checksum+=c[i];
    }
    if(checksum==0xff){

        //got a valid package so send acknowledge
        //and use the data
    }

Mike.
 
Code:
while(!DataRdyUSART());
 c  = getcUSART();
while(!DataRdyUSART());
 c1 = getcUSART();
while(!DataRdyUSART());
 c2 = getcUSART();
while(!DataRdyUSART());
 c3 = getcUSART();
while(!DataRdyUSART());
 c4 = getcUSART();

if(c == ':' && c == 0xCE)
{
	respond_to_command();
}
else if()


i was planning to attend to each incoming bytes individually...
 
I already did. What was wrong with the code I posted. It does exactly what is stated in the pdf.

Mike.
 
Well, it looks to be doing something completely different.

Try getting it working without interrupts first, something like,
Code:
char GetByte(){
    while(!DataRdyUSART());
    return(getcUSART());
}


    
    while(GetByte()!=0xce);     //wait for header byte
    checksum=0xce;              //initialise checksum
    for(i=0;i<4;i++){           //now get next 4 bytes
        c[i]=GetByte();
        checksum+=c[i];
    }
    if(checksum==0xff){

        //got a valid package so send acknowledge
        //and use the data
    }

Mike.

care to explain in details about ur code?
thanks.
 
I made a function GetByte() so that you could do,
Code:
    c=GetByte();

//instead of,

    while(!DataRdyUSART());
    c  = getcUSART();

I used that function to wait until 0xce, the start of packet byte, is received.
I then read the next 4 bytes into an array. So they are in c[0] to c[3].
I also added the bytes up to calculate the checksum.

Mike.
 
The checksum is the sum of all 5 bytes received, 0xce+add+cmd+data+chksum, and so it should equal 0xff. Actually, having just looked at the pdf again the checksum shouldn't include the 0xce. So, change to,

Code:
    while(GetByte()!=0xce);     //wait for header byte
    checksum=[COLOR="Red"]0[/COLOR];                 //initialise checksum
    for(i=0;i<4;i++){           //now get next 4 bytes
        c[i]=GetByte();         //store in array
        checksum+=c[i];         //and add to checksum
    }
    if(checksum==0xff){

        //got a valid package so send acknowledge
        //and use the data
    }

Mike.
 
sorry.. i think got it already.

i got it already.. becoz i confused with the statement that states that add ALL DATA, including checksum.. i thought ALL DATAS inclusive of the start byte.
 
Address. And the checksum will be 0xff (add+cmd+data+chksum) with the modified code.

Mike.
 
Code:
while(!DataRdyUSART());
    s  = getcUSART();



    while(s !=0xCE);     //wait for header byte
    checksum=0;              //initialise checksum
    for(i=0;i<4;i++){           //now get next 4 bytes
        c[i]=s;
		checksum+=c[i];         //and add to checksum
				    }	


if(checksum==0xff)
{

//OK

unsigned char m;
m = (0xFF - (c[2]+ 0x06 + c[1]));


while (BusyUSART());
	//value sent
	putcUSART(0xCE);

while (BusyUSART());
	//value sent
	putcUSART(c[2]);

while (BusyUSART());
//value sent 
	putcUSART(0x06);

while (BusyUSART());
	//value sent
	putcUSART(c[1]);

while (BusyUSART());
	//value sent
	putcUSART(m);

        //got a valid package so send acknowledge
        //and use the data
    
}


else{

//NOT OK

unsigned char m;
m = (0xFF - (c[2]+ 0x15 + c[1]));


while (BusyUSART());
	//value sent
	putcUSART(0xCE);

while (BusyUSART());
	//value sent
	putcUSART(c[2]);

while (BusyUSART());
//value sent 
	putcUSART(0x15);

while (BusyUSART());
	//value sent
	putcUSART(c[1]);

while (BusyUSART());
	//value sent
	putcUSART(m);


mike, i did something like this for the acknowledgement. but i keep getting "CE CE 15 CE 4E" as an acknowledgement if i send "CE 05 41 01 B8"
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top