• 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.

sending and receiving bytes

Status
Not open for further replies.

butters

Member
Code:
#include <p18f452.h>
#include <usart.h>
#include <delays.h>

void rx_handler (void);//declare the ISR function.HIGH
void pull_chord (void);//declare the ISR function.LOW


#define BUF_SIZE 25

/*
 * Step #1  The data is allocated into its own section.
 */

#pragma idata bigdata
char data[11][BUF_SIZE+1] = {
  { "String #0\n\r" },
  { "String #1\n\r" },
  { "String #2\n\r" },
  { "String #3\n\r" },
  { "String #4\n\r" },
  { "String #5\n\r" },
  { "String #6\n\r" },
  { "String #7\n\r" },
  { "String #8\n\r" },
  { "String #9\n\r" },
  { "Invalid key (0-9 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


//location of the ISR.(LOW)
#pragma code low_pull = 0x18
void low_pull (void)
{
  _asm goto pull_chord _endasm
}
#pragma code  //return to default code section





#pragma interruptlow pull_chord
void pull_chord (void)
{

/*  OMG WHAT TO PUT?     */

}



#pragma interrupt rx_handler
void rx_handler (void)
{
  unsigned char c;

  /* Get the character received from the USART */
 c = ReadUSART() ;
  if (c >= '0' && c <= '9')
    {
      c -= '0';
      /* Display value received on LEDs */
      PORTB = c;


	  //enables driver so it can transmit value to hyperterm
      PORTCbits.RC5 = 1;
      putsUSART (data[c]);// writes string of character from data[c]
         
      //disables
      PORTCbits.RC5 = 0;

}
  else
    {
	  //enable driver
      PORTCbits.RC5 = 1;

      putsUSART (data[11]);

      /* Display value received on LEDs */
      PORTB = c;

	  //disable driver
	  PORTCbits.RC5 = 0;
    }


    /* 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);


//lighting up LED at RB0
PORTCbits.RC5 = 1;

	//value sent
	putcUSART(0x31);//1st byte
Delay10KTCYx(50);

PORTCbits.RC5 = 0;


PORTCbits.RC5 = 1;

	//value sent
	putcUSART(0x41);//2nd byte
Delay10KTCYx(50);

PORTCbits.RC5 = 0;


PORTCbits.RC5 = 1;

	//value sent
	putcUSART(0x4f);//3rd byte
Delay10KTCYx(50);

PORTCbits.RC5 = 0;


PORTCbits.RC5 = 1;

	//value sent
	putcUSART(0x65);//4th byte
Delay10KTCYx(50);

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;

/* Enable all low priority interrupts */
  INTCONbits.GIEL = 1;

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

i am currently able to send 4 static bytes to hyperterminal via rs-485.. whereby if PORTCbits.RC5 = 1 is present, the rs-485 sending part would be activated and send the respective static hex values... and when PORTCbits.RC5 = 0, it would enable the rs-485 receiving part and would interrupt the main programme... and hence serve the interrupt...

my problem is that how do i send dynamic hex values...? do i need arrays?
 

Pommie

Well-Known Member
Most Helpful Member
What do you mean by "dynamic hex values"? What do you want to send and how should it appear at the PC end.

Mike.
 

Pommie

Well-Known Member
Most Helpful Member
There are two ways to do that depending on whether the message is in Ram or Rom.

Code:
void PutMessage(rom char *Message){		//send a ROM message
    while(*Message!=0)
        putcUSART(*Message++);
} 

void main(){
char RamBuffer[]="Hello World\0x0d\0x0a";	//Message in RAM
char i;
    PutMessage((rom char*)"fame\0x0d\0x0a");	//Message in ROM
    i=0;
    while(RamBuffer[i]!=0){
        putcUSART(RamBuffer[i++]);
    }
}
I added the new line characters.

Mike.
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top