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 CCS Technical Support

PIN D1 on 18f452
Goto page 1, 2  Next
 
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

PIN D1 on 18f452
PostPosted: Fri Mar 23, 2007 11:52 pm     Reply with quote

I'm trying to time sonars using diffferent pins. They all work except Pin D1 (set as an input).
I'm using
Code:

#define set_bit_var(x)   bit_set(*(int8 *)(x >> 3), x & 7)
#define clear_bit_var(x) bit_clear(*(int8 *)(x >> 3), x & 7)
#define read_bit_var(x)  bit_test(*(int8 *)(x >> 3), x & 7)
int16 pin_table[] = {   PIN_C1, PIN_C0,
                        PIN_D0, PIN_C2,
                        PIN_D2, PIN_D1,
                        PIN_C5, PIN_D3,
                        PIN_D5, PIN_D4,
                        PIN_D7, PIN_D6,
                        PIN_B1, PIN_B0,
                        PIN_B3, PIN_B2,
                        PIN_B5, PIN_B4,
                        PIN_E2, PIN_E1};

int8 const bitmask_table[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};

to set it up then in my function I have



Code:

int32 timeEcho(int trigger, int echo){
    ccs_trig = pin_table[trigger];
    ccs_echo = pin_table[echo];
   
    // Get the i/o port address
    io_port = ccs_trig >> 3;
    bitmask = bitmask_table[ccs_trig & 7];
    io_tris=io_port+TRIS_OFFSET;   // 0x12 is the offset
   
    *(io_tris) &= ~bitmask; // Set TRIS = output
   
    // start off with the trigger low, just in case
    clear_bit_var(ccs_trig);
    delay_us(25);
   
    // pulse the trigger to send the ping
    set_bit_var(ccs_trig);
    delay_us(25);
    clear_bit_var(ccs_trig);
   
    // Get the i/o port address
    io_port = ccs_echo >> 3;
    bitmask = bitmask_table[ccs_echo & 7];
    *(io_tris) |= bitmask; // Set TRIS = input
   
    // Read pin (ret. 0 or 1)
    retval = !!(*io_port & bitmask);
   
    // Wait for the ping to actually be sent- it may be slightly delayed
    while(read_bit_var(ccs_echo) == 0)//700 us delay
    {
        delay_us(10);
        timeout++;
        if(timeout >150)//timeout
        {
//            Nack(ePos);
            return;
        }
    }
   
    //Start timing the return flight
   
    set_timer0(0); // timer0 is .4us per timer tick
    while(read_bit_var(ccs_echo)==1)
    {
        RawData = get_timer0();
        //timeout after 25ms
        if(RawData > 62500) // .4*62500=25ms
        {
                return(65000);
         }
          else
            return(flight_time);
}


So in my main loop I do
Code:

    while (1)
    for(i=0;i<10;i++)
    {delay_ms(100);
        trig=i*2;
        echo=trig+1;
        returnValue=timeEcho(trig,echo);
        printf("value %d = %ld\r\n",i,returnValue);
    }


They all work except for the 3rd pair. I can verify that the d2 is acting correctly with my o-scope, and I see the correct pulse come back D1, but the function always returns junk. Is there something special about D1 I'm not catching?
I've tried
setup_psp(PSP_DISABLED);
but that did not help. what am I missing?
Thanks
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Mar 24, 2007 1:04 am     Reply with quote

1. Check if the wire is really connected to pin D1.
Also check if the ground wire is connected.

2. Check the .H file for your PIC and see if the #define statement
for pin D1 is correct.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 7:59 pm     Reply with quote

I know the pic is connected correctly, I'm using the oscope directly on the pin. I see the pin go high and low so I know it is moving but the software is not seeing it.

I assembled a 2nd pcb and it does the same thing.

I'm assuming the .H file is corrrect because if I do this:
Code:

    while(1)
    {
     printf("D1 = %d\r",input(pin_d1));   
    }

I see it change ok. So it is something weird in my code.
of the 10 sets of Trig and echo pins this is the only one that does not work. Any Ideas?
Thanks
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 8:15 pm     Reply with quote

Remove the external circuit that is currently connected to the pin pair
of D2 and D1.

Jumper over the circuit from pins E2 and E1 onto the D2 and D1 pair.

See if it now starts working.

If it does, it means there's a problem with the external circuits on
D2 and D1.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 8:30 pm     Reply with quote

I'm actually only using 1 sonar and moving it around from pair to pair so I know that is not it. It works on the others, just not this one.
Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 8:34 pm     Reply with quote

I started printing the value from the Pin_Table and it looks correct accroding to the .H file

Here is what it prints
trig=31761 ech=31760
value 0 = 999
trig=31768 ech=31762
value 1 = 999

trig=31770 ech=31769
value 2 = 999

trig=31765 ech=31771
value 3 = 999
trig=31773 ech=31772
value 4 = 999
trig=31775 ech=31774
value 5 = 999
trig=31753 ech=31752
value 6 = 999
trig=31755 ech=31754
value 7 = 999
trig=31757 ech=31756
value 8 = 999

31769 is PIN_D1 so it has the correct value, what else could be going wrong here?
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 9:36 pm     Reply with quote

Write a test program that only tests the D2-D1 pin pair. In this program,
use the CCS i/o functions instead of the functions in the timeEcho()
routine. See if it starts working.

Also, the timeEcho() function has several variables in it and there are no
declarations given for these variables in your post.
These variables are local to that function and ought to be declared
inside the function (as locals). Can you post the declarations ?
Also, there's a #define statement for the TRIS_OFFSET that's missing.
In other words, I'm looking for compilable code that I can test.

Can you also post your compiler version ?
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 9:40 pm     Reply with quote

I'm using 4.007

I wrote a new function called Sonar that has some pins hard coded into it. I started with a pair that work in the other function and now they don't work in this function. I see everything on the O-scope, so I know things are happening. If you look at teh sonar code it always returns a 298.

Here is the complete code
Code:

#include "18f452.h"
#fuses hs,nowdt,noprotect,put,nolvp
#device *=16
#device adc=10
#device HIGH_INTS=TRUE
#use delay(clock=20000000)

#include <stdlib.h>

#define EEPROM_Base 0x0
#define TRIS_OFFSET 0x12
#define LED1_PIN    PIN_B6
#define LED2_PIN    PIN_B7
#define TX_PIN      PIN_C6
#define RX_PIN      PIN_C7

#define    BAUD_RATE                    19200

#use rs232(baud=BAUD_RATE,xmit=TX_PIN,rcv=RX_PIN,errors,bits=8,parity=N)
#use I2C(master, sda=PIN_c4, scl=PIN_c3,slow)
//#use fast_io(b)
//#define   Global_Address               0
///Macros
#define LED1_ON output_high(LED1_PIN)
#define LED2_ON output_high(LED2_PIN)
#define LED1_OFF output_low(LED1_PIN)
#define LED2_OFF output_low(LED2_PIN)

// These macros allow accessing a CCS pin number which
// is passed as a variable.    Note:  These use a lot of ROM.
#define set_bit_var(x)   bit_set(*(int8 *)(x >> 3), x & 7)
#define clear_bit_var(x) bit_clear(*(int8 *)(x >> 3), x & 7)
#define read_bit_var(x)  bit_test(*(int8 *)(x >> 3), x & 7)
int16 pin_table[] = {   PIN_C1, PIN_C0,
                        PIN_D0, PIN_C2,
                        PIN_D2, PIN_D1,
                        PIN_C5, PIN_D3,
                        PIN_D5, PIN_D4,
                        PIN_D7, PIN_D6,
                        PIN_B1, PIN_B0,
                        PIN_B3, PIN_B2,
                        PIN_B5, PIN_B4,
                        PIN_E2, PIN_E1};

int8 const bitmask_table[] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
short Sonar_present[]={0,0,0,0,0,0,0,0,0,0};

// commands
#define SERIAL_BUFFER_SIZE 250
#define I2C_BUFFER_SIZE    250
#define space              0x4f

#define READ_MODE 0
#define WRITE_MODE 1
#define Max_num_servos 2
#define methodTableLength 24

//global variables
//int SERIAL_BUFFER[SERIAL_BUFFER_SIZE]; // communications buffer
int portval;
int Sonar_data_mode=0;

int SERIAL_BUFFER[SERIAL_BUFFER_SIZE];
int *curPos;  // Current Serial Buffer Ptr Position
int curCnt;

//int I2C_CMD_BUFFER[I2C_BUFFER_SIZE];

char FwCmd[]         = {"fw"};        // firmware version
char Fw[]            = {"1.0.0"};
char ResetCmd[]      = {"reset"};     // reset cpu
char BlinkCmd[]      = {"blink"};     // blinks user leds
                                       // blink <ledId>:<freq>..<ledId>:<freq>
char CfgUnitsCmd[]   = {"cfg units"};  // //Sonar_data_mode 0=cm, 1=in, 2=us
char CfgBaudCmd[]    = {"cfg baud"};  // sets baud rate.0=9600. 1=19200. 2=57600. 3=115200
char GetEncoderCmd[] = {"getenc"};    // returns left wheel encoder value & right wheel encoder value
char DebugCmd[]      = {"debug"};
char maxez1Cmd[]     = {"maxez1"};     // Maxbotics EZ1 sonar using 2 line Pulse width mode
char SensorCmd[]     = {"sensor"};    // returns the raw A/D (8 bit) reading from sensor ports 0-5
char StopCmd[]       = {"stop"};      // sends string to speech synthesizer.(max 45 characters)
char RestoreCmd[]    = {"restore"};   // Restores all default values and reboots

int loops=50;
short debug                = 0;

short int ledState1 = 1;
int ledBlinkFreq1 = 250;
int ledCnt1 = 0;
int ledEnable1 = 1;

short int ledState2 = 0;
int ledBlinkFreq2 = 250;
int ledCnt2 = 0;
int ledEnable2 = 1;
void InitPIC();
void BlinkLeds();
void SetupNewEEPROM();
void ReadAllEEPROM();
void ProcessCommands();
void Heartbeat();
void atoiTest();
int32 timeEcho(int trigger, int echo);
int32 sonar(int num);
////////////////////////////////////////////////////////////////////////////////////////////////

void main()
{
      int data1,i,trig,echo;
      int32 returnValue;
        int temp;
    // Initialize Serial Buffer Position Pointer:
    curPos = SERIAL_BUFFER;
    curCnt = 0;
   
    printf("-------EzExpander, firmware v%s\r\n", FW);
    printf("Copyright 2006-2007, RoboticsConnection.com\r\n>");   
   
    InitPic();
   
    LED1_OFF;
    LED2_OFF;
       
//    data1 = read_EEPROM (EEPROM_Base+0);
   
    //blank eeprom
//    if ((data1==255) ||(data1==0))
//      SetupNewEEPROM();
//    else
//      ReadAllEEPROM();

    enable_interrupts(GLOBAL);
    // test all sonars to see what id present
//    while(1)
//    {
//     printf("D1 = %d\r",input(pin_d1));   
 //   }
   
   
    while (1)
    for(i=0;i<10;i++)
    {delay_ms(100);
        trig=i*2;
        echo=trig+1;
        returnValue=timeEcho(trig,echo);
//        printf("Timed Echo= %d = %ld",i,returnValue);
        printf("  sonar%d=%ld\r\n",i,sonar(i));
        printf("\r\n");
        if((returnValue==65000) || (returnValue==255))
            Sonar_present[i]=0;       
        else
            Sonar_present[i]=1;
    }
    for(i=0;i<10> 0)
    {
        if (source[pos] == c)
        {
            return pos;
        }
        else
        {
            pos--;
        }
    }
    return -1;
}

////////////////////////////////////////////////////////////////////////////////////////////////

signed int
getNextId(signed int *sPos)
{
    int id;
    signed int cPos;
   
    // Eat up extra whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
   
    cPos = chrFind(':', SERIAL_BUFFER, *sPos);
   
    if (cPos == -1)
    {
        return -1;
    }
    // We're tricking the atoi() calls below to easily
    // convert multi-digit numbers to integers.
    // Heh, heh, heh...
    SERIAL_BUFFER[cPos] = '\0';
   
    id = atoi(&(SERIAL_BUFFER[*sPos]));
    *sPos = cPos+1;
    return id;
}
////////////////////////////////////////////////////////////////////////////////////////////////

signed int32
getNextLong(signed int *sPos)
{
    signed int32 id;
    signed int cPos;
   
    // Eat up extra whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
   
    cPos = chrFind(':', SERIAL_BUFFER, *sPos);
   
    if (cPos == -1)
    {
        return -1;
    }
    // We're tricking the atoi() calls below to easily
    // convert multi-digit numbers to integers.
    // Heh, heh, heh...
    SERIAL_BUFFER[cPos] = '\0';
   
    id = (signed int32)atol(&(SERIAL_BUFFER[*sPos]));
   
    *sPos = cPos+1;
   
    return id;
}
////////////////////////////////////////////////////////////////////////////////////////////////

int32
getNextLongValue(signed int *sPos)
{
    // Eat up extra whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
   
    return (int32)atoi32(&(SERIAL_BUFFER[*sPos]));
}

////////////////////////////////////////////////////////////////////////////////////////////////

signed int
getNextValue(signed int *sPos)
{
    // Eat up whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
   
    //printf(" sPos: %d\r\n", *sPos);
   
    // Since there should be a space after the value,
    // atoi will convert the number up to that space.
    return atoi(&(SERIAL_BUFFER[*sPos]));
}
////////////////////////////////////////////////////////////////////////////////////////////////

Int16
getNextInt16Value(signed int *sPos)
{
    // Eat up extra whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
    //return (int16)atoi32(&(SERIAL_BUFFER[*sPos]));
    return (int16)atoi(&(SERIAL_BUFFER[*sPos]));
}
////////////////////////////////////////////////////////////////////////////////////////////////

int16
getNextId16(signed int *sPos)
{
    int16 id;
    signed int cPos;
   
    // Eat up extra whitespace.
    while (isspace(SERIAL_BUFFER[*sPos]))
    {
        *sPos += 1;
    }
   
    cPos = chrFind(':', SERIAL_BUFFER, *sPos);
   
    if (cPos == -1)
    {
        return -1;
    }
    // We're tricking the atoi() calls below to easily
    // convert multi-digit numbers to integers.
    // Heh, heh, heh...
    SERIAL_BUFFER[cPos] = '\0';
   
    id = atoi(&(SERIAL_BUFFER[*sPos]));
    *sPos = cPos+1;
    return id;
}

// Modified to parse for 'r:NNNN' sequence
// which can be optionally passed in for debugging purposes
// Ack() now returns the number following the 'r:' in every ACK
void Nack(signed int ePos)
{
    int32 rc = 1;
    signed int pos1 = -1;
    signed int pos2 = -1;
   
    // Check to see if there is an 'r:0000'
    // Start from end of string
    pos1 = revChrFind('r', SERIAL_BUFFER, ePos);
   
    if (pos1 != -1)
    {
        pos2 = chrFind('-', SERIAL_BUFFER, pos1);
        if (pos2 != -1)
        {
            pos2++;
            rc = getNextLongValue(&(pos2));
            printf("NACK %ld", rc);
            return;
        }
        printf("NACK");
    }
    else
    {
        printf("NACK");
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////

// Modified to parse for 'r:NNNN' sequence
// which can be optionally passed in for debugging purposes
// Ack() now returns the number following the 'r:' in every ACK
void Ack(signed int ePos)
{
    int32 rc = 1;
    signed int pos1 = -1;
    signed int pos2 = -1;
   
    // Check to see if there is an 'r:0000'
    // Start from end of string
    pos1 = revChrFind('r', SERIAL_BUFFER, ePos);
   
    if (pos1 != -1)
    {
        pos2 = chrFind('-', SERIAL_BUFFER, pos1);
        if (pos2 != -1)
        {
            pos2++;
            rc = getNextLongValue(&(pos2));
            printf("ACK %ld", rc);
            return;
        }
        printf("ACK");
    }
    else
    {
        printf("ACK");
    }
}

// Simply looks for a 'r:NNNN' number, and returns the NNNNumber if present
void SilentAck(signed int ePos)
{
    int32 rc = 1;
    signed int pos1 = -1;
    signed int pos2 = -1;
   
    // Check to see if there is an 'r:0000'
    // Start from end of string
    pos1 = revChrFind('r', SERIAL_BUFFER, ePos);
   
    if (pos1 != -1)
    {
        pos2 = chrFind('-', SERIAL_BUFFER, pos1);
        if (pos2 != -1)
        {
            pos2++;
            rc = getNextLongValue(&(pos2));
            printf(" %ld", rc);
            return;
        }
    }
}




////////////////////////////////////////////////////////////////////////////////////////////////

#int_TIMER2
void TIMER2_isr()
{
    static long int loop_counter=0;
   
    ledCnt1++;
    ledCnt2++;
    // Update LEDs:
    if (ledCnt1 == ledBlinkFreq1)
    {
      if (ledEnable1)
     {
         if (ledState1)
        {
           ledState1 = 0;
          led1_off;
        }
         else
        {
           ledState1 = 1;
          led1_on;
        }
     }
     ledCnt1 = 0;
    }
   
    if (ledCnt2 == ledBlinkFreq2)
    {
      if (ledEnable2)
     {
         if (ledState2)
        {
           ledState2 = 0;
          led2_off;
        }
         else
        {
           ledState2 = 1;
          led2_on;
        }
     }
     ledCnt2 = 0;
    }
}

////////////////////////////////////////////////////////////////////////////////////////////////

#int_rda HIGH
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 PrintAsBinary(int8 Value)// could be int16
{
    int8 i;
   
    i = sizeof(Value) * 5;    // Get number of bits.
    do
    {
        // Check MSB against 1 and print the value.
        if (shift_right(&Value, sizeof(Value), 0) == 1)
            putc('1');
        else
            putc('0');
        putc(' ');
    } while (--i);
}

////////////////////////////////////////////////////////////////////////////////////////////////

void QuerySensor(void)
{
    signed int sPos, ePos;
    int channel;
    long int ADC_Return_Value;
    char nackFlag=true;
   
    // 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)
    {
        channel = getNextValue(&sPos);
       
        // Only return values for pins wit6hin range
        if ((0 <= channel) && (channel <5>> 3;
    bitmask = bitmask_table[ccs_trig & 7];
    io_tris=io_port+TRIS_OFFSET;   // 0x12 is the offset
   
    *(io_tris) &= ~bitmask; // Set TRIS = output
   
    // start off with the trigger low, just in case
    clear_bit_var(ccs_trig);
    delay_us(25);
   
    // pulse the trigger to send the ping
    set_bit_var(ccs_trig);
    delay_us(25);
    clear_bit_var(ccs_trig);
   
    // Get the i/o port address
    io_port = ccs_echo >> 3;
    bitmask = bitmask_table[ccs_echo & 7];
    *(io_tris) |= bitmask; // Set TRIS = input
   
    // Read pin (ret. 0 or 1)
    retval = !!(*io_port & bitmask);
   
    // Wait for the ping to actually be sent- it may be slightly delayed
    while(read_bit_var(ccs_echo) == 0)//700 us delay
    {
        delay_us(10);
        timeout++;
        if(timeout >150)//timeout
        {
//            Nack(ePos);
            return(999);
        }
    }
   
    //Start timing the return flight
   
    set_timer0(0); // timer0 is .4us per timer tick
    while(read_bit_var(ccs_echo)==1)
    {
        RawData = get_timer0();
        //timeout after 25ms
        if(RawData > 62500) // .4*62500=25ms
        {
            if(Sonar_data_mode == 2)//raw
            {
//                printf("%lu",65000);
                return(65000);
            }
            else
            {
//                printf("%u",255);
                return(255);
            }
        }
    }
   
    // Raw data is 0.4us.
    // sonar = 147us/inch.
    // 1/.4 = 2.5 ticks/us
    // 147us/inch*2.5ticks/us=367 ticks/inch
   
    if(Sonar_data_mode == 0)//cm
    {
        flight_time=RawData/145; // 367/2.54 (inches to Cm)
  //      printf("%u",(unsigned int)flight_time);
        return(flight_time);
    }
    else if(Sonar_data_mode == 1)//inch
    {   
        flight_time=RawData/367;
  //      printf("%u",(unsigned int)flight_time);
        return(flight_time);
    }
    else
    {
        // Raw Mode
//        printf("%lu",RawData);
        return(RawData);
    }
//    SilentAck(ePos);
}

////////////////////////////////////////////////////////////////////////////////////////////////
int32 sonar(int num)
{
#use STANDARD_IO(D)
    int timeout=0;
    int16 trig,echo;
    int16 RawData=0;
    int16 flight_time=0;
    int dummy;
   
    //trigger goes high for 10us minimum
    //wait for echo to go high
    //time how long echo is high
    //100us to 25ms

    dummy=(input(PIN_D6));// make sure it is an input

   
    // start off with the trigger low, just in case
    output_low(PIN_D7);
    delay_us(25);
   
    // pulse the trigger to send the ping
    output_high(PIN_D7);
    delay_us(25);
    output_low(PIN_D7);
   
    dummy=(input(PIN_D6));// make sure it is an input
    // Wait for the ping to actually be sent- it may be slightly delayed
    while(input(PIN_D6) == 0)//700 us delay
    {
        delay_us(10);
        timeout++;
        if(timeout >150)//timeout
        {
//            Nack(ePos);
            return(998);
        }
    }
   
    //Start timing the return flight
   
    set_timer0(0); // timer0 is .4us per timer tick
//    while(input(echo)==1)
    while(input(PIN_D6)==1)
    {
        RawData = get_timer0();
        //timeout after 25ms
        if(RawData > 62500) // .4*62500=25ms
        {
//            if(Sonar_data_mode == 2)//raw
//            {
///                printf("%lu",65000);
 //               return(65000);
 //           }
 //           else
  //          {
  //              printf("%u",255);
                return(255);
  //          }
        }
    }
   
    // Raw data is 0.4us.
    // sonar = 147us/inch.
    // 1/.4 = 2.5 ticks/us
    // 147us/inch*2.5ticks/us=367 ticks/inch
   
/*    if(Sonar_data_mode == 0)//cm
    {
        flight_time=RawData/145; // 367/2.54 (inches to Cm)
        printf("%u",(unsigned int)flight_time);
        return(flight_time);
    }
    else if(Sonar_data_mode == 1)//inch
*/
    {   
        flight_time=RawData/367;
//        printf("%u",(unsigned int)flight_time);
        return(flight_time);
    }
  /*
    else
    {
        // Raw Mode
        printf("%lu",RawData);
        return(RawData);
    }
 */   
//    SilentAck(ePos);
}

////////////////////////////////////////////////////////////////////////////////////////////////

void SetupNewEEPROM(void)
{
   int address=0;//1 = blank eeprom test
   int data=1;

   write_EEPROM (EEPROM_Base+address,data);

   address=1;//baudrate
   data=3;//0=2400,2=9600,3=19200,4=57600,5=115200
   write_EEPROM (EEPROM_Base+address,data);

   write_EEPROM (EEPROM_Base+11,1);//Sonar_data_mode 0=cm, 1=in, 2=us
}
////////////////////////////////////////////////////////////////////////////////////////////////

void ReadAllEEPROM(void)
{
    int address,baud;
    address = 1;  //baudrate
    baud = read_EEPROM (EEPROM_Base+1);
   
   
    switch (baud)
    {
        case 0:
            set_uart_speed(2400);
        break;
        case 1:
            set_uart_speed(4800);
        break;
        case 2:
            set_uart_speed(9600);
        break;
        case 3:
            set_uart_speed(19200);
        break;
        case 4:
            set_uart_speed(57600);
        break;
        case 5:
            set_uart_speed(115200);
        break;
    }
   
    Sonar_data_mode=read_EEPROM (EEPROM_Base+11);
}


////////////////////////////////////////////////////////////////////////////////////////////////

void ConfigureBaudRate()
{
    int baud = 3;
    int address = 1;
    int32 NewBaud;
    signed int sPos, ePos;

    // 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);
    sPos++;
    sPos = chrFind(' ', SERIAL_BUFFER, sPos);

    if (sPos == -1)
    {
        baud = read_EEPROM (EEPROM_Base+address);
        switch (baud)
        {
            case 0: printf("2400"); break;
            case 1: printf("4800"); break;
            case 2: printf("9600"); break;
            case 3: printf("19200");break;
            case 4: printf("57600");break;
            case 5: printf("115200");break;
        }
        return;
    }

    NewBaud = getNextLongValue(&sPos);

    if(NewBaud==2400)baud=0;
    else if(NewBaud==4800)baud=1;
    else if(NewBaud==9600) baud=2;
    else if(NewBaud==19200) baud=3;
    else if(NewBaud==57600) baud=4;
    else if(NewBaud==115200) baud=5;
    else if(NewBaud==0) baud=0;
    else if(NewBaud==1) baud=1;
    else if(NewBaud==2) baud=2;
    else if(NewBaud==3) baud=3;
    else if(NewBaud==4) baud=4;
    else if(NewBaud==5) baud=5;

    switch (baud)
    {
        case 0: set_uart_speed(2400); break;
        case 1: set_uart_speed(4800); break;
        case 2: set_uart_speed(9600); break;
        case 3: set_uart_speed(19200);break;
        case 4: set_uart_speed(57600);break;
        case 5: set_uart_speed(115200);break;
        default :set_uart_speed(19200);break;
    }
    write_EEPROM (EEPROM_Base+address,baud);
    ACK(ePos);
}
////////////////////////////////////////////////////////////////////////////////////////////////

void ConfigureSonarMode()
{
    signed int sPos, ePos;
    int address=11;
    signed int mode;

    // Find end of command, and set to null:
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    SERIAL_BUFFER[ePos] = '\0';

    sPos = chrFind(' ', SERIAL_BUFFER, 0);
    sPos = chrFind(' ', SERIAL_BUFFER, ++sPos);

    if (sPos != -1)
    {
        //Sonar_data_mode 0=cm, 1=in, 2=us
        mode = getNextValue(&sPos);

        if((mode >= 0) && (mode <= 2))
        {
            write_EEPROM (EEPROM_Base+address, mode);
            Sonar_data_mode=mode;
            ACK(ePos);
        }
    }
    else
    {
        printf("%d\r",Sonar_data_mode);
    }
}
////////////////////////////////////////////////////////////////////////////////////////////////

void BlinkLeds(void)
{
    signed int sPos, ePos, led, freq;

    // Find end of command, and set to null:
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    SERIAL_BUFFER[ePos] = '\0';

    sPos = chrFind(' ', SERIAL_BUFFER, 0);

    while (sPos != -1)
    {
        led = getNextId(&sPos);
        if (led == -1)
        {
            Nack(ePos);
            return;
        }

        freq = getNextValue(&sPos);
        if (freq == -1)
        {
            Nack(ePos);
            return;
        }

        if (led == 1)
        {
            if (freq == 0)
            {
                ledEnable1 = 0;
                led1_off;
            }
            else
            {
                ledBlinkFreq1 = freq;
                ledEnable1 = 1;
            }
        }
        else if (led == 2)
        {
            if (freq == 0)
            {
                ledEnable2 = 0;
                led2_off;
            }
            else
            {
                ledBlinkFreq2 = freq;
                ledEnable2 = 1;
            }
        }
        sPos = chrFind(' ', SERIAL_BUFFER, sPos);
    }
    Ack(ePos);
}




////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessCommands()
{
    signed int epos;
   
    // Find end of command, and set to null:
    ePos = chrFind('\r', SERIAL_BUFFER, 0);
    //printf("EPOS: %d", ePos);
   
    printf("\r\n");
    if(strncmp (BlinkCmd, serial_buffer, 5)==0)
        BlinkLeds();
    else if(strncmp (FwCmd, serial_buffer, 2)==0)
        printf("%s", FW);
    else if(strncmp (CfgBaudCmd, serial_buffer, 7)==0)
        ConfigureBaudRate();
    else if(strncmp (CfgUnitsCmd, serial_buffer, 7)==0)
        ConfigureSonarMode();
//    else if(strncmp (I2cProgCmd, serial_buffer, 4)==0)
//        ChangeI2CAddress();
    else if (strncmp (DebugCmd, serial_buffer, 4) == 0)
    {
        printf("Entering Debug mode.  Reset to exit\r\n");
        debug=1;
    }
    else if (strncmp (RestoreCmd, serial_buffer, 7) == 0)
    {
        InitPic();
        SetupNewEEPROM();
        delay_ms(20);
        reset_cpu();
    }
    else if(strncmp (ResetCmd, serial_buffer, 5)==0)
    {
        reset_cpu();
        Ack(ePos);
    }
    else
    {
        // printf("%s", *serial_buffer);
        Nack(ePos);
    }
   
    printf("\r\n>");
//    int_flag=0;
}

////////////////////////////////////////////////////////////////////////////////////////////////

void InitPic()
{
     short int dummy_variable;
    setup_timer_2(T2_DIV_BY_4,128,16);// 18.6 khz 0-512 int every 8ms //
    setup_timer_0(RTCC_Internal|RTCC_div_2);
 
    //enable_interrupts(INT_TIMER2);
    enable_interrupts(INT_RDA);    // Serial Data Available IRQ
    enable_interrupts(INT_TIMER2); // Timer 2 Overflow
    enable_interrupts(global);     // Enable IRQs
   

    dummy_variable = input(PIN_A0);
    dummy_variable = input(PIN_A1);
    dummy_variable = input(PIN_A2);
    dummy_variable = input(PIN_A3);
    dummy_variable = input(PIN_A4);
    dummy_variable = input(PIN_A5);

    dummy_variable = input(PIN_B0);
    output_low(PIN_B1);
    dummy_variable = input(PIN_B2);
    output_low(PIN_B3);
    dummy_variable = input(PIN_B4);
    output_low(PIN_B5);
    dummy_variable = input(PIN_C0);
    output_low(PIN_C1);
    dummy_variable = input(PIN_C2);
    output_low(PIN_C3);
    output_low(PIN_C4);
    output_low(PIN_C5);
    output_low(PIN_C6);
    //output_low(PIN_C7);RX
    output_low(PIN_D0);
    dummy_variable = input(PIN_D1);
    output_low(PIN_D2);
    dummy_variable = input(PIN_D3);
    dummy_variable = input(PIN_D4);
    output_low(PIN_D5);
    dummy_variable = input(PIN_D6);
    output_low(PIN_D7);
    dummy_variable = input(PIN_E0);
    dummy_variable = input(PIN_E1);
    output_low(PIN_E2);
   
       
}
/*    switch(num)
    {
        case 0:
            trig=PIN_C1;
            echo=PIN_C0;
        break;
        case 1:
            trig=PIN_D0;
            echo=PIN_C2;
        break;
        case 2:
            trig=PIN_D2;
            echo=PIN_D1;
        break;
        case 3:
            trig=PIN_C5;
            echo=PIN_D3;
        break;
        case 4:
            trig=PIN_D5;
            echo=PIN_D4;
        break;
        case 5:
            trig=PIN_D7;
            echo=PIN_D6;
        break;
        case 6:
            trig=PIN_B1;
            echo=PIN_B0;
        break;
        case 7:
            trig=PIN_B3;
            echo=PIN_B2;
        break;
        case 8:
            trig=PIN_B5;
            echo=PIN_B4;
        break;
        case 9:
            trig=PIN_E2;
            echo=PIN_E1;
        break;
    }
  */

_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Apr 01, 2007 9:45 pm     Reply with quote

Quote:
I'm using 4.007

This is not good. Stop right there and try it with vs. 3.249.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Mon Apr 02, 2007 8:18 am     Reply with quote

Unfortunately I don't have that version. When I bought the compiler they shipped version 4.x. If this is a compiler bug will CCS give me an older version to use?
Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Mon Apr 02, 2007 9:53 am     Reply with quote

I wrote 10 separate functions (sonar0 through Sonar9) that call out the pin names instead of using the pin table and all those work as long as I don't call the function that uses the pin table. If I call TimedEcho then one of the Sonarx's will quit working. Very weird.
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 02, 2007 10:28 am     Reply with quote

Quote:

Unfortunately I don't have that version. When I bought the compiler they
shipped version 4.x. If this is a compiler bug will CCS give me an older
version to use?

When you initially buy the compiler, you are given "download rights" for
a limited time. During that time, you should have downloaded vs. 3.249.
Look at the compiler download page:
http://www.ccsinfo.com/compilerupdates.php
You can see that vs. 3.249 is available, on the right side of the page.

If you forgot to do this and you're a registered owner of the compiler,
then email CCS support and ask them if they will email it to you.
Also, in my opinion, vs. 4.007 is too early a version to use.
See this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=30349&highlight=bug
There's a problem with the built-in PWM functions, and the 16F876 is
a very common and very basic chip. That's with vs. 4.017.

So whatever you do, stop using 4.007 and either get 3.249, or ask CCS
to upgrade you to 4.031, which appears to be more of a working version.
When you email them, give them your user Reference number.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Wed Apr 04, 2007 9:53 pm     Reply with quote

I emailed Tech support about getting 3.249 and they have not responded. I guess they are blowing me off hoping I'll spend the $250 for an annual update. Unfortunately it's not in the budget right now.
Thanks
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Apr 04, 2007 10:16 pm     Reply with quote

If you bought the compiler and didn't download vs. 3.249 in time, due
to an oversight or to not understanding that it was needed, I still believe
that CCS owes it to you. I suggest that you call their support dept. on
the telephone and (politely) ask them for it. Make sure you give them
your user reference number.
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Thu Apr 05, 2007 7:05 am     Reply with quote

What is the user reference number? is that something on the invoice? I'm at the wrong computer right now, when I get back to my desk I'll look at the emailed invoice.
Thanks
Ringo
_________________
Ringo Davis
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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