Need Help, Unable to Receive Messages

Discussion in 'Programmer's Corner' started by RicMata, Jan 7, 2009.

  1. RicMata

    Thread Starter New Member

    Jan 7, 2009
    1
    0
    Hi Guys,
    Hope you can help me on this one, I'm working on this particular school project. I'm able to send messages to a device, however, I can't receive any.

    Here's my program code, please pardon my code. I'm still learning.

    Master Code:
    ==========


    unsigned short data[8] = "";
    unsigned short address[3] = {160,161,162};
    char rs_msg[5] = "";
    int rxflag = 0;
    int txflag = 0;
    int x = 0, i = 0;
    int iIndex = 0;
    int pc_msg = 0;


    void enable_interrupt()
    {
    PIE1.RCIE = 1; // Enable interrupt
    INTCON.PEIE = 1; // on byte received
    PIE1.TXIE = 0; // via USART (RS485)
    INTCON.GIE = 1; // Set into receive status
    }

    void disable_interrupt()
    {
    PIE1.RCIE = 0; // disable interrupt
    INTCON.PEIE = 0; // on byte received
    PIE1.TXIE = 0; // via USART (RS485)
    INTCON.GIE = 0;
    }

    void send_pc()
    {
    int y = strlen(rs_msg);
    int ind = 0;

    while (y > 0){

    if(rs_msg[ind] != '<' && rs_msg[ind] != '>' )
    {

    Usart_Write(rs_msg[ind]);
    Lcd_Custom_Chr(3,1,rs_msg[ind]);
    delay_ms(300);
    }

    y--;
    ind++;
    }
    }

    void clear_rs_data()
    {

    int ind = strlen(rs_msg);
    lcd_Custom_chr_cp(ind+48);

    while(ind >= 0){
    rs_msg[ind] =0;
    ind--;
    }
    x = 0;
    }

    void clear_bufferData()
    {
    int ind = strlen(data);
    lcd_Custom_chr_cp(ind+48);

    while(ind >= 0){
    data[ind] =0;
    ind--;

    }
    x=0;
    }

    void interrupt(void)
    {
    RS485Master_Receive(data);

    if(data[0])
    {
    rxflag = 1;
    }
    }
    void showRoomNo()
    {
    unsigned short j = 0;

    data[4] = 0; //Clear message received flag
    delay_ms(100);
    j = data[3]; //Number of data bytes received

    for (i = 0; i < j; i++) {
    rs_msg = data[x];
    x++;
    }
    Lcd_Custom_Cmd(LCD_CLEAR);
    Lcd_Custom_Out(2,2,"Room Number: ");
    Lcd_Custom_Out(2,16,rs_msg);
    delay_ms(100);

    }

    void main () {

    char sReminder[21] = "";

    TRISC.F2 = 0;
    TRISD.F1 = 0;


    Lcd_Custom_Config(&portb,1,2,3,4, &portb, 7,6,5);
    Usart_Init(9600); // Initialize usart module
    RS485Master_Init(&PORTC, 2);
    Lcd_Custom_Out(1, 1,"Master");
    delay_ms(1000);
    Lcd_Custom_Cmd(Lcd_Clear);

    while ( 1)
    {
    enable_interrupt();

    if (rxflag == 1)
    {

    while (rs_msg[x-1] != '>')
    {

    if (rxflag == 1) {

    showRoomNo();
    }
    }
    disable_interrupt();
    send_pc();

    Lcd_Custom_Out(4,1,"sent to pc");
    rxflag = 0;
    clear_rs_data();
    clear_bufferData();

    }

    else
    {
    Usart_write(pc_msg);
    }
    }
    }


    Slave Code:
    =========


    //--------------------Declaration of Global variables------------------->
    char msg[3]="1";
    char roomNo[5]="<1>";
    int temp[21];
    int i=0;
    int k=0;
    int rxflag=0;
    int j=0;
    int index=0;
    unsigned short data[8] = ""; // buffer for receiving/sending messages

    //------------------End of declarations------------------>

    //------------------Function declarations---------------->

    void interrupt();
    void clear_temp();
    void ipakita();
    void clear_bufferData();
    void clear_roomNo();
    void send_roomNo();
    void display_rs();
    void erase_rs_data();

    //--------------End of function declaration-------------->

    //------------------Function operations------------------>

    void interrupt (void)
    {
    RS485Slave_Receive(data);

    if (data[3] > 0)
    rxflag = 1;
    }

    void enable_interrupt ()
    {
    PIE1.RCIE = 1;
    INTCON.PEIE = 1;
    PIE1.TXIE = 0;
    INTCON.GIE = 1;
    }

    void disable_interrupt ()
    {
    PIE1.RCIE = 0; // Enable interrupt
    INTCON.PEIE = 0; // on byte received
    PIE1.TXIE = 0; // via USART (RS485)
    INTCON.GIE = 0;
    }

    void send_roomNo ()
    {
    int temp1=0;
    int temp2=0;
    int temp3 =0;
    int ind =0;
    int res1 =0;
    int res2 =0;
    char room[4]="";
    temp3 = strlen(roomNo);
    res1 = temp3/3;
    res2 = temp3%3;


    while (temp2 < res1) {

    for (temp1 =0;temp1<3;temp1++) {
    room[temp1] = roomNo[ind];
    ind++;
    }
    RS485Slave_Send(room, 3);
    delay_ms(500);
    temp2++;
    }
    if (res2 > 0 && res2 < 3) {

    for (temp1 =0;temp1<res2;temp1++) {
    room[temp1] = roomNo[ind];
    ind++;
    }

    RS485Slave_Send(room, res2);
    delay_ms(500);

    }
    }

    void showRoomNo()
    {
    unsigned short j = 0;

    delay_ms(108);
    j = data[3]; // Number of data bytes received

    for (k = 0; k < j; k++) {
    msg[k]=data[k];
    data[k] = 0;
    Lcd_Custom_Out(4, 10,msg);
    }
    }

    void erase_rs_data ()
    {

    int ind= 0;
    ind= strlen(msg);

    while(ind >= 0){
    msg[ind] =0;
    ind--;
    }
    }

    void clear_temp ()
    {
    int val=0;
    for (val=0;val<i;val++){
    temp[val]=0;
    }
    i=0;
    }

    void clear_bufferData ()
    {

    int val=0;

    for (val=0;val<9;val++){
    data[val]=0;
    }
    }

    void clear_roomNo ()
    {

    int val=0;

    for (val=0;val<=i;val++){
    roomNo[val]=0;
    }
    }


    //---------------End of function operations-------------->

    void main () {

    char sReminder01[21] = " Time to Take";
    char sReminder02[21] = " Medicine";
    TRISB = 0x00; //set portB as input
    TRISC.F2 = 0; //set portC pin2 as output
    TRISD.f0 = 1;
    TRISD.f1 = 1;

    Lcd_Custom_Config(&portb, 1,2,3,4, &portb, 7,6,5); //initialize lcd
    Usart_Init(9600);
    RS485Slave_Init(&PORTC, 2, 160); // Initialize MCU as Slave with address 160
    Lcd_Custom_Out(1,1, "Slave 1");
    delay_ms(1000); // Initialize baud rate
    Lcd_Custom_Cmd(Lcd_Clear);

    while (1)
    {
    enable_interrupt();


    if(portd.f0 == 0 || portd.f1 == 0)
    {

    Lcd_Custom_Out(1,1, "Sending Message...");

    send_roomNo();

    Lcd_Custom_Cmd(Lcd_Clear);

    }


    if (rxflag == 1)
    {
    Lcd_Custom_Out(2,5,sReminder01);
    Lcd_Custom_Out(3,7,sReminder02);
    rxflag=0;
    }
    clear_bufferData();
    erase_rs_data();
    }
    }
     
Loading...