CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

More I2C woes

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

More I2C woes
PostPosted: Sun Jul 22, 2007 2:06 pm     Reply with quote

I have a master and a slave, both identical boards. If I read a value from the slave, it is correct. But if I write to the slave, then read, all reads are returning -1's. I have the Slave hard coded to return a 42 so it has to be sending the right thing. The slave also blinks leds when it is returning a value and they are blinking. It looks like it is a master problem, but if I reset the slave board it starts returning the correct value (until it sees another write from the master) So that makes me think it is a slave problem. I stripped almost everything from the slave code so it looks like this
Code:

#INT_SSP
void ssp_interupt ()
{
    int i;
    BYTE incoming, state;

   state = i2c_isr_state();
   if(state < 0x80)                     //Master is sending data
   {
       if(state >0)//data not address
        {}   
    }
   if(state == 0x80)                     //Master is requesting data
   {
        donepouring=42;
      i2c_write(donepouring);
        output_low(PIN_B6);
        output_low(PIN_B7);
        delay_ms(100);
        output_high(PIN_B6);
        output_high(PIN_B7);
      actionflag=1; //0 = nothing, 1 = read, 2 = change address
//printf("donepouring=%d\r\n",donepouring);
   }
}


here is the master code. The first read is correct, then there is a write, then the rest of the reads are wrong. Is there something I need to do in the slave int code after a write to reset something?

Code:

        BoardAddress=0x50;

                output_low(PIN_B6);
                delay_ms(500);             
                i2c_start();   // start condition
                i2c_write(BoardAddress + 1);
                value2 = i2c_read(0);
                i2c_stop();
                output_high(PIN_B6);
                printf(" value before write=%d \r\n",value2);



        for(relaynumber=1;relaynumber<=8;relaynumber++)
        {
            output_low(PIN_B7);
            i2c_start();
            i2c_write(BoardAddress);     //Address
            i2c_write(20);              // command for pour           
            i2c_write(relaynumber);        // send relay num   
            i2c_write(4);         // send time to pour 
            i2c_stop();
            Output_high(PIN_B7);
            printf("sending command to turn on relay# %d\r\n",relaynumber);
            delay_ms(1500);

            value2=0;
            while(value2!=1)             // 0 means still working, 1 means finished
            {
                output_low(PIN_B6);
                delay_ms(500);             
                i2c_start();   // start condition
                i2c_write(BoardAddress + 1);
                value2 = i2c_read(0);
                i2c_stop();
                output_high(PIN_B6);
                printf(" value2=%d \r\n",value2);
            }
        }

_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 22, 2007 2:26 pm     Reply with quote

You didn't have to start a whole new thread on the continuing i2c problem.

Try the master code shown in this post. Use Ex_slave.c for you slave.
See if that works.
http://www.ccsinfo.com/forum/viewtopic.php?t=28097&start=9


Also, vs. 4.007 is too early to use for anything. I would permanently
set that version aside.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jul 22, 2007 4:01 pm     Reply with quote

my slave code is practically identical to ex_slave.
I looked at the post you mentioned and changed my read part to add the additional write and restart before the read like this
Code:

            value2=0;
            while(value2!=1)             // 0 means still working, 1 means finished
            {
                output_low(PIN_B6);
                delay_ms(500);             
                i2c_start();   // start condition          //new line
                i2c_write(BoardAddress);                //new line
                i2c_start();   // start condition
                i2c_write(BoardAddress + 1);
                value2 = i2c_read(0);
                i2c_stop();
                output_high(PIN_B6);
                printf(" value2=%d \r\n",value2);
            }

but still get the same result.
The reads work until I do a write, then the reads are bad. any other ideas?
Oh yeah, and I switched to 3.249 and still get the same thing.
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 22, 2007 4:49 pm     Reply with quote

Make the master and slave code be identical to the code I posted and
to Ex_Slave.c. Run the slave PIC at 20 MHz. Make sure you have
pull-ups (4.7K) on both SDA and SCL. Compile with 3.249. Make sure
there is a ground connection between the two boards. Don't put in any
other code (outputs to B6, etc). Don't try to run the code in debugger
mode. Run it in stand-alone mode. It should work.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jul 22, 2007 6:25 pm     Reply with quote

I tried the posted example and I get back a B. The only thing my code is doing differently is I'm just writing the address then reading, I'm not writing the address, then a 0, then reading. Is the 0 something I need?

The slave gets it power and ground from the master, both run at 20 mhz, vcc is 5V.

Thanks
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Jul 22, 2007 9:52 pm     Reply with quote

The Ex_Slave.c code emulates a small EEPROM, so it expects the byte
address to follow the i2c read address. The byte address is used as an
index into a RAM array that emulates the eeprom data.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Mon Jul 23, 2007 8:18 am     Reply with quote

I think my I2C code is ok, I think it is something else. I have a bunch of functions in my code below main that do not get called yet. As soon as I deleted all of them it started working correctly.

I've had that happen in one other project where a function stopped about half the I/O pins from working. Deleting and retyping it fixed the problem. I don't know if there is an invisible character or what.

So I'll start pasting stuff back in until it breaks, then see if I can use a different editor to look at that code and see if there is anything weird.

Thanks for the help.
Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 1:45 pm     Reply with quote

This is getting weirder. The code posted below allows the I2C stuff to work fine, but my 2 leds on B6 and B7 don't work at all.

If I comment out the
enable_interrupts(INT_RDA) line and the INt_RDA function then the leds work but I2C does not.
If I uncomment the INT_RDA function (whole thing or just the empty brackets) and leave it enabled then Neither I2C or leds work.

Any ideas here on what could be going on? I'm using 3.249.
Thanks
Ringo





Code:

#include <16F876a.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=19200, xmit=PIN_c6, rcv=PIN_c7)
#include <stdlib.h>

#byte PIC_SSPADD=0x93
int     NODE_ADDR  =  80; //0x50
//#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3, address=0x52)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0x50)
//#define master                  1
#define RelayH                PIN_A0
#define RelayG                PIN_A1
#define RelayF                PIN_A2
#define RelayE                PIN_B0
#define RelayD                PIN_B1
#define RelayC                PIN_B2
#define RelayB                PIN_B3
#define RelayA                PIN_B4
#define Master_Select         PIN_C5
#define SERIAL_BUFFER_SIZE 55   //5 + 6*numLiquors
#define numLiquors          8
int SERIAL_BUFFER[SERIAL_BUFFER_SIZE];
#include "Chrfinds.c"

void ProcessCommands();
void Pour();

char FwCmd[]         = {"fw"};        // firmware version
char Fw[]            = {"1.0.0"};
//char ResetCmd[]      = {"reset"};     // reset cpu
//char BlinkCmd[]      = {"blink"};     // blinks user leds
char PourCmd[]       = {"pour"};     // blinks user leds
//char GainCmd[]       = {"setgain"};

int donepouring=0;
int relay[numLiquors];
int time[numLiquors];
int boardnum[numLiquors];
int Sensors=0;
BYTE address, buffer[0x10];
int bytecounter=0;
int actionflag=0; //0 = nothing, 1 = read, 2 = change address
int *curPos;  // Current Serial Buffer Ptr Position
int curCnt=0;
int debug=0;

#INT_SSP
void ssp_interupt ()
{
    int i;
    BYTE incoming, state;

   state = i2c_isr_state();
   if(state < 0x80)                     //Master is sending data
   {
       if(state >0)//data not address
        {
            bytecounter++;
/*            if(bytecounter>15)// too many bytes, somethign is wrong
            {
                bytecounter=0;
                printf("i2c overflow\r\n");
                return;
            }
*/
          incoming = i2c_read();
          buffer[bytecounter] = incoming;

          if(bytecounter ==3 )                     //First received byte is address
            {
              if(buffer[1]==20)                             //pour command
              {
                  SERIAL_BUFFER[0]='p';
                  SERIAL_BUFFER[1]=' ';
                  SERIAL_BUFFER[2]=49;                       //board address
                  SERIAL_BUFFER[3]=':';
                  SERIAL_BUFFER[4]=buffer[2]+48;               // relay num
                  SERIAL_BUFFER[5]=':';
                  SERIAL_BUFFER[6]=buffer[3]+48;               // time
                  SERIAL_BUFFER[7]='\r';
                 bytecounter =0;
                 pour();
              }
            }

          if(bytecounter ==8 )                     //First received byte is address
            {
                for(i=1;i<=8;i++)
                    printf(" %x ",buffer[i]);
                printf("\r\n");
                printf("buffer[8]= %d \r\n",buffer[8]);
                if((buffer[1]==0) && (buffer[2]==0xa0)
                    && (buffer[3]==0) && (buffer[4]==0xaa)
                    && (buffer[5]==0) && (buffer[6]==0xa5)
                    && (buffer[7]==0))
                {
                    NODE_ADDR=buffer[8];
                    PIC_SSPADD=NODE_ADDR;// change address
                    write_EEPROM (0,NODE_ADDR);// save new address
                    actionflag=2; //0 = nothing, 1 = read, 2 = change address
                }
                bytecounter=0;
            }
        }

    }
   if(state == 0x80)                     //Master is requesting data
   {
//      i2c_write(1);
      i2c_write(donepouring);
      actionflag=1; //0 = nothing, 1 = read, 2 = change address
   }
}

////////////////////////////////////////////////////////////////////////////////
void main ()
{
    int flashcounter=0;
    int i;
    int data1;
    int temp_node_address;
    int BoardAddress,relaynumber,value2,x,whileloopcounter;
    enable_interrupts(INT_SSP);
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);
    Port_b_pullups(true);
    printf("Bartender, firmware v%s\r\n", FW);
    output_high(PIN_B6);
    output_high(PIN_B7);
//    delay_ms(1000);

 ////////////**************Test code for master*********/////////////////////

    while(0)
    {
        BoardAddress=0x50;
        for(relaynumber=1;relaynumber<=8;relaynumber++)
        {
            output_low(PIN_B7);
            i2c_start();
            i2c_write(BoardAddress);    //Address
            i2c_write(20);              // command for pour
            i2c_write(relaynumber);     // send relay num
            i2c_write(4);               // send time to pour
            i2c_stop();
            Output_high(PIN_B7);
            printf("sending command to turn on relay# %d\r\n",relaynumber);
            delay_ms(1500);

            whileloopcounter=0;
            value2=0;
            while(value2!=1)             // 0 means still working, 1 means finished
            {
                output_low(PIN_B6);
                delay_ms(500);
                i2c_start();   // start condition
//                i2c_write(BoardAddress);
//                i2c_write(0);
//                i2c_start();   // start condition
                i2c_write(BoardAddress + 1);
                value2 = i2c_read(0);
                i2c_stop();
                output_high(PIN_B6);
                printf("%d value2=%d \r\n",whileloopcounter,value2);
                whileloopcounter++;
            }
            printf("out of while loop\r\n");
        }
    }//end of while for test code for master

////////////**************Test code for master*********/////////////////////



    while (1)
    {


        if(actionflag==0) //0 = nothing, 1 = read, 2 = change address
        {
            output_high(Pin_b6);
            output_high(Pin_b7);
            actionflag=0;
        }
        if(actionflag==1) //0 = nothing, 1 = read, 2 = change address
        {
            output_low(Pin_b6);
            output_low(Pin_b7);
            delay_ms(125);
            output_high(Pin_b6);
            output_high(Pin_b7);
            delay_ms(125);
            actionflag=0;
        }
        if(actionflag==2) //0 = nothing, 1 = read, 2 = change address
        {
            actionflag=0;
            flashcounter=(NODE_ADDR-80);
            flashcounter/=2;
            flashcounter++;
            for(i=0;i<flashcounter;i++)
            {
                Output_high(PIN_B7);
                output_high(PIN_B6);
                delay_ms(250);
                Output_Low(PIN_B6);
                Output_Low(PIN_B7);
                delay_ms(250);
            }
            delay_ms(500);
            output_high(PIN_B6);
        }
    }
}

////////////////////////////////////////////////////////////////////////////////
void Pour()
{
    int i=0;
    int j=0;
    signed int sPos, ePos;
    int quarterseconds=0;
    int BoardAddress=0;
    int value;
    sPos = 0;
    donepouring=0;
    // Find end of command, and set to null:
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    SERIAL_BUFFER[ePos] = '\0';

    // Go find first space:
    sPos = chrFind(' ', SERIAL_BUFFER, 0);

    while (sPos != -1)
    {
        boardnum[i]=getNextNumBeforeColon(&sPos);//after the space
        relay[i] = getNextNumBeforeColon(&sPos);//after the colon
        time[i] = getNextNumBeforeSpace(&sPos);
        i++;
        sPos = chrFind(' ', SERIAL_BUFFER, sPos);

    }
    for(j=0;j<i;j++)
    {
        printf("pouring relay# %d for %d time\r\n",relay[j],time[j]);
        if(boardnum[j]==1) //this board
        {
            switch (relay[j])
            {
                case 1: output_high(RelayA); break;
                case 2: output_high(RelayB); break;
                case 3: output_high(RelayC); break;
                case 4: output_high(RelayD); break;
                case 5: output_high(RelayE); break;
                case 6: output_high(RelayF); break;
                case 7: output_high(RelayG); break;
                case 8: output_high(RelayH); break;
            }

            for(quarterseconds=0;quarterseconds<time[j];quarterseconds++)
                delay_ms(250);

            output_low(RelayA);
            output_low(RelayB);
            output_low(RelayC);
            output_low(RelayD);
            output_low(RelayE);
            output_low(RelayF);
            output_low(RelayG);
            output_low(RelayH);
            printf("done pouring\r\n");
            donepouring=1;
            return;
        }
        else // Slave Board, send out command
        {
            printf("Sending I2c command to board %d\r\n",boardnum[j]);
 //         send command via I2C
            BoardAddress=boardnum+0x40;
            i2c_start();
            i2c_write(BoardAddress);     //Address
            delay_ms(15);
            i2c_write(20);              // command for pour
            delay_ms(15);
            i2c_write(relay[j]);        // send relay num
            delay_ms(15);
            i2c_write(time[j]);         // send time to pour
            delay_ms(15);
            i2c_stop();

            delay_ms(500);              // give the board time to pour
            value=0;
            while(value==0)             // 0 means still working, 1 means finished
            {
                printf("asking slave board if it is finished\r\n");
                i2c_start();   // start condition
                i2c_write(BoardAddress + 1);
                value = i2c_read(0);
                i2c_stop();
                delay_ms(200);              // give the board time to pour
            }
        }
    }
}
/*  removing this line makes #int_rda an empty function, but it kills I2C comms
#int_rda
void serial_int_routine()
{}
/*
    disable_interrupts(INT_RDA); // RS232 OFF

    // Read incoming byte:
    *curPos = getc();

    // Make sure we don't overrun the buffer:
    if (curCnt < SERIAL_BUFFER_SIZE)
    {
       // End of command, set flag to process the command:
       if (*curPos =='\r')
       {
//          ProcessCommands();

          // Reset pointer to beginning of buffer:
          curPos = SERIAL_BUFFER;
          curCnt = 0;
       }
       else
       {
            // Increment buffer position:
            curPos++;
            curCnt++;
       }
     }
     else
     {
         curCnt = 0;
         curPos = SERIAL_BUFFER;
     }
     enable_interrupts(INT_RDA); // RS232 ON
}
*/

//-----------------------------------------------------------------------------
void ProcessCommands()
{
    int brightest=0;
    signed int sPos, ePos;
    printf("\r\nentering Process commands\r");
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    SERIAL_BUFFER[ePos] = '\0';
    sPos = chrFind(' ', SERIAL_BUFFER, 0);
//  printf("\n spos=%d\r\n",sPos);
    if(strncmp ( PourCmd, serial_buffer, 3)==0)
    {
        printf("Pouring\r\n");
        pour();
    }
    else
    {
        printf("NACK\r\n");
    }
}

_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Jul 29, 2007 2:42 pm     Reply with quote

I don't have to comment out the entire serial interupt, jus the line
//#INT_RDA
which makes it an interupt.
the function is still there.
with
//#INT_RDA
I get no leds, but I2C works perfectly
with
#INT_RDA
The leds work fine but I2C works 2 to 4 times (random) then always returns a -1, even when it is hard coded to reurn a 1 like this i2c_write(1);

Any ideas?
Thanks
Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Aug 02, 2007 10:36 pm     Reply with quote

It looks like I can't have both of these at the same time

#use I2C(master, sda=PIN_c4, scl=PIN_c3)
#use rs232(baud=19200, xmit=PIN_c6, rcv=PIN_c7)

I can get serial to work, or I2C, but as soon as I include both statements the interrupts quit. Has anybody seen this before. I'm using a 16f876A.
Thanks,
Ringo
_________________
Ringo Davis
Ttelmah
Guest







PostPosted: Fri Aug 03, 2007 7:03 am     Reply with quote

In what you have posted, you are enabling a RS232 interrupt, but show no handler. This will cause everything to hang, if data is seen on the serial line....

Best Wishes
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Fri Aug 03, 2007 7:43 am     Reply with quote

What I posted before was slave code. I took out all the serial stuff and now I2C works. But now I'm working on master code which needs both Serial and I2C. I've been stripping stuff out to try to get it working. Here is the current version. As soon as I put in the Use I2C line the serial int quits working.
Code:

#include <16F876a.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#byte PIC_SSPADD=0x93
int     NODE_ADDR  =  80; //0x50
//#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C3, address=0x52)
#use I2C(master, sda=PIN_c4, scl=PIN_c3)
#use rs232(baud=19200, xmit=PIN_c6, rcv=PIN_c7)

#define RelayH                PIN_A0
#define RelayG                PIN_A1
#define RelayF                PIN_A2
#define RelayE                PIN_B0
#define RelayD                PIN_B1
#define RelayC                PIN_B2
#define RelayB                PIN_B3
#define RelayA                PIN_B4
#define Master_Select         PIN_C5
#define SERIAL_BUFFER_SIZE 55   //5 + 6*numLiquors
#define numLiquors          8
int SERIAL_BUFFER[SERIAL_BUFFER_SIZE];
#include <stdlib.h>
#include "Chrfinds.c"

void ProcessCommands();
void Pour();

int relay[numLiquors];
int timearray[numLiquors];
int boardnum[numLiquors];
int relaynum=0;
int time=0;
int donepouring=1;

BYTE address;
BYTE buffer[20];
int actionflag=0; //0 = nothing, 1 = read, 2 = change address
int *curPos;  // Current Serial Buffer Ptr Position
int curCnt=0;
int debug=0;
////////////////////////////////////////////////////////////////////////////////
void main ()
{
    int x;
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);

printf("\rBartender Master board\r\n");
    for(x=0;x<5;x++)
    {
        output_high(PIN_B6);
        output_low(PIN_B7);
        delay_ms(100);
        output_low(PIN_B6);
        output_high(PIN_B7);
        delay_ms(100);
    }
 
//    while (0)
    {
        time=1;
        for(relaynum=1;relaynum<=8;relaynum++)
            pour();
    }


//    while(0)
//    {
//        int BoardAddress=0x50;
//        int relaynumber;
//        int whileloopcounter;
//        int value2;
//       
//        for(relaynumber=1;relaynumber<=8;relaynumber++)
//        {
//            output_low(PIN_B7);
//            i2c_start();
//            i2c_write(BoardAddress);    //Address
//            i2c_write(20);              // command for pour
//            i2c_write(relaynumber);     // send relay num
//            i2c_write(4);               // send time to pour
//            i2c_stop();
//            Output_high(PIN_B7);
//            printf("sending command to turn on relay# %d\r\n",relaynumber);
//            delay_ms(1500);
//
//            whileloopcounter=0;
//            value2=0;
//            while(value2!=1)             // 0 means still working, 1 means finished
//            {
//                output_low(PIN_B6);
//                delay_ms(500);
//                i2c_start();   // start condition
//                i2c_write(BoardAddress + 1);
//                value2 = i2c_read(0);
//                i2c_stop();
//                output_high(PIN_B6);
//                printf("%d value2=%d \r\n",whileloopcounter,value2);
//                whileloopcounter++;
//            }
//            printf("out of while loop\r\n");
//        }
//    }//end of while for test code for master




    while (1)
    {
        if(actionflag==1) //0 = nothing, 1 = read, 2 = change address
        {
            output_low(Pin_b6);
            output_low(Pin_b7);
            delay_ms(125);
            output_high(Pin_b6);
            output_high(Pin_b7);
            delay_ms(125);
            actionflag=0;
        }
        if(actionflag==3) //0 = nothing, 1 = read, 2 = change address
        {
            output_low(Pin_b6);
            output_high(Pin_b7);
            delay_ms(125);
            output_low(Pin_b6);
            output_high(Pin_b7);
            delay_ms(125);
            pour();
            actionflag=0;
        }

    }
}

////////////////////////////////////////////////////////////////////////////////
void Pour()
{
int x;
//time=4;
//relaynum=3;
        printf("\r\npouring relay# %d for %d time\r\n",relaynum,time);
       switch (relaynum)
        {
            case 1: output_high(RelayA); break;
            case 2: output_high(RelayB); break;
            case 3: output_high(RelayC); break;
            case 4: output_high(RelayD); break;
            case 5: output_high(RelayE); break;
            case 6: output_high(RelayF); break;
            case 7: output_high(RelayG); break;
            case 8: output_high(RelayH); break;
        }

output_high(PIN_b6);
output_high(PIN_b7);
//delay_ms(1000);
        for(x=0;x<time;x++)
            delay_ms(250);
output_low(PIN_b6);
output_low(PIN_b7);
       
        output_low(RelayA);
        output_low(RelayB);
        output_low(RelayC);
        output_low(RelayD);
        output_low(RelayE);
        output_low(RelayF);
        output_low(RelayG);
        output_low(RelayH);
//        printf("done pouring\r\n");
        donepouring=1;
//        return;


}       

void ProcessCommands()
{
    signed int sPos, ePos;
    int i=0;
    printf("\r\nprocess commands\r\n");
    sPos = 0;
    // Find end of command, and set to null:
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    SERIAL_BUFFER[ePos] = '\0';

    // Go find first space:
    sPos = chrFind(' ', SERIAL_BUFFER, 0);

    while (sPos != -1)
    {
        boardnum[i]=getNextNumBeforeColon(&sPos);//after the space
        relaynum = getNextNumBeforeColon(&sPos);//after the colon
        time = getNextNumBeforeSpace(&sPos);
        i++;
        sPos = chrFind(' ', SERIAL_BUFFER, sPos);
    pour();

    }
   
}

#int_rda //HIGH
void serial_int_routine()
{
//    disable_interrupts(INT_RDA); // RS232 OFF
    output_high(PIN_b6);// these 3 lines only put in for troubleshooting. Delay would not be in real code
    delay_ms(100);
    output_low(PIN_b6);
    // Read incoming byte:
    *curPos = getc();
    // Make sure we don't overrun the buffer:
    if (curCnt < SERIAL_BUFFER_SIZE)
    {
       // End of command, set flag to process the command:
       if (*curPos =='\r')
       {
          ProcessCommands();
          // Reset pointer to beginning of buffer:
          curPos = SERIAL_BUFFER;
          curCnt = 0;
          output_low(PIN_b6);
       }
       else
       {
            // Increment buffer position:
            curPos++;
            curCnt++;
       }
     }
     else
     {
         curCnt = 0;
         curPos = SERIAL_BUFFER;
     }
     enable_interrupts(INT_RDA); // RS232 ON
}


Any help will be greatly appreciated.
Thanks, Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Fri Aug 03, 2007 8:34 am     Reply with quote

Here is an even more stripped down version that still does not work. The int_rda function is copied straight from a working program, so I know the code is good. Every once in a while the program will work, but changing something as simple as adding a character in a printf will make it not work.
Code:

#include <16F876a.h>
#device *=16
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#byte PIC_SSPADD=0x93
int     NODE_ADDR  =  80; //0x50

//#use I2C(master, sda=PIN_c4, scl=PIN_c3)
#use rs232(baud=19200, xmit=PIN_c6, rcv=PIN_c7,ERRORS)

#define RelayH                PIN_A0
#define RelayG                PIN_A1
#define RelayF                PIN_A2
#define RelayE                PIN_B0
#define RelayD                PIN_B1
#define RelayC                PIN_B2
#define RelayB                PIN_B3
#define RelayA                PIN_B4
#define Master_Select         PIN_C5
#define SERIAL_BUFFER_SIZE 25 //55  //5 + 6*numLiquors
#define numLiquors          8

void processCommands();

int SERIAL_BUFFER[SERIAL_BUFFER_SIZE];

int actionflag=0; //0 = nothing, 1 = read, 2 = change address
int *curPos;  // Current Serial Buffer Ptr Position
int curCnt=0;

void main ()
{
    int x;
    enable_interrupts(INT_RDA);
    enable_interrupts(GLOBAL);

printf("\r\nBartender Master board\r\n");
    for(x=0;x<5;x++)
    {
        output_high(PIN_B6);
        output_low(PIN_B7);
        delay_ms(100);
        output_low(PIN_B6);
        output_high(PIN_B7);
        delay_ms(100);
    }
 
    while (1)
    { }
}


#int_rda
void serial_int_routine()
{
//    disable_interrupts(INT_RDA); // RS232 OFF
    output_high(PIN_b6);// these 3 lines only put in for troubleshooting. Delay would not be in real code
    delay_ms(100);
    output_low(PIN_b6);
    // Read incoming byte:
    *curPos = getc();
    // Make sure we don't overrun the buffer:
    if (curCnt < SERIAL_BUFFER_SIZE)
    {
       // End of command, set flag to process the command:
       if (*curPos =='\r')
       {
          // Reset pointer to beginning of buffer:
          curPos = SERIAL_BUFFER;
          curCnt = 0;
          output_low(PIN_b6);
          ProcessCommands();
       }
       else
       {
            // Increment buffer position:
            curPos++;
            curCnt++;
       }
     }
     else
     {
         curCnt = 0;
         curPos = SERIAL_BUFFER;
     }
     enable_interrupts(INT_RDA); // RS232 ON
}


void processCommands()
{
 printf("process commands\r\n");   
   
}

_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 03, 2007 4:00 pm     Reply with quote

Quote:

int *curPos; // Current Serial Buffer Ptr Position

void main ()
{
int x;
enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);

printf("\r\nBartender Master board\r\n");
for(x=0;x<5;x++)
{
output_high(PIN_B6);
output_low(PIN_B7);
delay_ms(100);
output_low(PIN_B6);
output_high(PIN_B7);
delay_ms(100);
}

while (1)
{ }
}


#int_rda
void serial_int_routine()
{
// disable_interrupts(INT_RDA); // RS232 OFF
output_high(PIN_b6);
delay_ms(100);
output_low(PIN_b6);
// Read incoming byte:
*curPos = getc();

It can't work because 'curpos' is never initialized to point to an array.
On the first character, it will start over-writing RAM, probably at address
0x00, which is the start of the SFR registers in the PIC.

I would get rid of your #int_rda code and just use the code in Ex_sisr.c.
Don't do any processing inside the RDA isr. Do it all in main(). In other
words, keep everything ultra-simple.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group