 |
 |
| View previous topic :: View next topic |
| Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 3:58 pm |
|
|
Do a test. Restrict the read_analog() function to just reading AN0.
Make the changes shown in bold below. See what happens.
| Quote: | void read_analog()
{
int channel;
int16 value;
channel = 0;
// for(channel=0;channel<=5;channel++)
{
set_adc_channel(channel);
delay_us(15);
value = Read_ADC();
analog_array[channel] = value;
}
} |
|
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Wed Aug 29, 2007 4:05 pm |
|
|
The code you just posted appears to work.
This one also seems to work.
| Code: |
void read_analog()
{
int channel;
int16 value;
disable_interrupts(INT_SSP);
for(channel=0;channel<=5;channel++)
{
delay_us(15);
set_adc_channel(channel);
delay_us(15);
value = 42*channel;
value = Read_ADC();
analog_array[channel] = value;
}
enable_interrupts(INT_SSP);
} |
I had already added the disable and enable but that did not help. Adding the delay_us before setting the channel seems to help the most. I'm gonna run it a few hundred time to make sure it is working, but so far it looks good.
Any idea why I would need to delay before setting a channel?
Ringo _________________ Ringo Davis |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 4:18 pm |
|
|
I don't know, but adding a delay and observing that it works isn't good
enough. The reason for the failure should be completely determined.
If you don't understand the reason, then any "fix" is just a bandaid that
could fail in the future. |
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Wed Aug 29, 2007 4:52 pm |
|
|
I completely agree.
I have written several programs that read the adc, but never one that reads them this quickly. It is usually a function that reads a specific channel, so I don't know that the delay is not always needed.
I guess the question is how do I determine what is causing the lockup when I don't have the delay? Any suggestions on figuring that out?
Thanks
Ringo _________________ Ringo Davis |
|
 |
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Wed Aug 29, 2007 9:07 pm |
|
|
Well I used to use the 18LF452 with version 4.012 with 40 A/D reads each of 2 channels in a row, 10 times per second, never had an issue with the compiler or the pic.
I don't know what's going on though in your case. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Aug 29, 2007 9:38 pm |
|
|
I have some more time to look at your code. Can you post the missing
routines and #define statements, so that the following errors will be
fixed when I compile the code:
| Quote: |
Error 12: Undefined identifier chrFind
Error 12: Undefined identifier getNextLongValue
Error 12: Undefined identifier getNextValue
Error 12: Undefined identifier ACK
|
|
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Thu Aug 30, 2007 7:11 am |
|
|
Here is the latest code, I really appreciate you looking at it.
| Code: |
#include "18F452.h"
#fuses hs,nowdt,noprotect,put,nolvp
#device *=16
#device adc=10
#device HIGH_INTS=TRUE
#use delay(clock=20000000)
#byte PIC_SSPADD=0xC8
int Default_address = 100;
int NODE_ADDR = 100;//0x64
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=100,slow,force_hw)
#include <stdlib.h>
#define TX_PIN PIN_C6
#define RX_PIN PIN_C7
#use rs232(baud=19200,xmit=TX_PIN,rcv=RX_PIN,errors,bits=8,parity=N)
#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)
#define emulate 0
/* I2C commands
38 change Delay_between_readings <address>,<38>,<time in ms(0-255)>
39 Set Sonar Mode to in <address>,<39>
40 Set Sonar Mode to cm <address>,<40>
41 Set Sonar Mode to us <address>,<41>
42 request sonar array <address>,<42> , then read back 20 bytes Sonar1Low,Sonar1High, Sonar2low,Sonar2high....
43 request analog array <address>,<43> , then read back 12 bytes Analog1Low,Analog1High, Analog2low,Analog2high....
44 request all array <address>,<44> , then read back 32 bytes Sonar1Low,Sonar1High,Sonar2Low...Analog1Low,Analog1High....
*/
BYTE address, buffer[0x10];
int bytecounter=0;
int Delay_between_readings =25;
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};
int Sonar_present[]={0,0,0,0,0,0,0,0,0,0};
int16 analog_array[]={1,2,3,4,5,6};
int16 Sonar_array[]={258,0,260,0,1024,0,0,0,0,0};
int debug=0;
int EEPROM_Base=0;
int16 loopcounter=0;
// commands
#define SERIAL_BUFFER_SIZE 250
#define I2C_BUFFER_SIZE 250
#define READ_MODE 0
#define WRITE_MODE 1
//global variables
int SERIAL_BUFFER[SERIAL_BUFFER_SIZE];
int *curPos; // Current Serial Buffer Ptr Position
int curCnt=0;
int Sonar_data_mode=0;//0=cm, 1=in, 2=us
int streamflag =0;
int Sensors=0;
int I2C_request_type=42;
int actionflag=0;
int SonarEnabled=1;
//#include "utilities.c"
void InitPic();
int32 timeEcho(int trigger, int echo);
int FaketimeEcho(int trigger, int echo);
void read_analog();
void ProcessCommands();
void SetupNewEEPROM();
void ReadAllEEPROM();
void ConfigureBaudRate();
void ConfigureSonarMode();
void ConfigureSonarDelay();
char FwCmd[] = {"fw"}; // firmware version
char Fw[] = {"1.0.6"};
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 DelayCmd[] = {"delay"}; // time in ms to delay between sonar readings
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
char AnalogCmd[] = {"analog"};
char AllSonarCmd[] = {"allsonar"};
char Streamsonar[] = {"streams"};
char Streamanalog[] = {"streama"};
char Streamevthing[] = {"streame"};
#INT_SSP HIGH
void ssp_interupt ()
{
int i;
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80) //Master is sending data
{
incoming = i2c_read();
buffer[bytecounter]=incoming;
bytecounter++;
if(bytecounter ==2 ) //First received byte is address
{
if(buffer[1]==39)//1=in
{
Sonar_data_mode=1;
bytecounter=0;
printf("Mode set to inches\r\n");
}
else if(buffer[1]==40)//0=cm
{
Sonar_data_mode=0;
bytecounter=0;
printf("Mode set to cm\r\n");
}
else if(buffer[1]==41)// 2=us
{
Sonar_data_mode=2;
bytecounter=0;
printf("Mode set to raw\r\n");
}
else if(buffer[1]==42) // request sonar array
{
I2C_request_type=42;
bytecounter=0;
printf("requesting sonar array\r\n");
}
else if(buffer[1]==43) // request analogr array
{
I2C_request_type=43;
bytecounter=0;
printf("requesting analog array\r\n");
}
else if(buffer[1]==44) // request all array
{
I2C_request_type=44;
bytecounter=0;
printf("requesting combined array\r\n");
}
else {}
}
if(bytecounter ==3 ) //First received byte is address
{
if(buffer[1]==38) // change Delay_between_readings
{
Delay_between_readings=buffer[2];
bytecounter=0;
printf("Delay_between_readings set to %d\r\n",Delay_between_readings);
}
}
if(bytecounter ==8 ) //First received byte is address
{
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 (10,NODE_ADDR);// save new address
actionflag=2; //0 = nothing, 1 = read, 2 = change address
}
bytecounter=0;
}
if(bytecounter >8 )
{
bytecounter=0;
Printf("i2c overflow\r\n");
}
}
if(state == 0x80) //Master is requesting data
{
printf("reading\r\n");
actionflag=1; //0 = nothing, 1 = read, 2 = change address
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
signed int
chrFind(char c, char *source, signed int pos)
{
while (source[pos] != '\0')
{
if (source[pos] == c)
{
return pos;
}
else
{
pos++;
}
}
return -1;
}
signed int
revChrFind(char c, char *source, signed int pos)
{
while (pos > 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]));
}
// 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;
}
}
}
void main()
{
int led1on=0;
int i,j,trig,echo,SonarNumber,data1;
int mycounter=0;
int32 returnValue;
int32 range,data;
int fakeraw;
int oppositeSonarNumber=0;
curPos = SERIAL_BUFFER;
curCnt = 0;
InitPic();
enable_interrupts(global); // Enable IRQs
// data1 = read_EEPROM (0);
// if ((data1==255) ||(data1==0))//blank eeprom
// SetupNewEEPROM();
// else
// ReadAllEEPROM();
for(i=Default_address;i<=NODE_ADDR;i+=2)
{
output_high(PIN_B6);
output_high(PIN_B7);
delay_ms(250);
output_low(PIN_B6);
output_low(PIN_B7);
delay_ms(250);
}
output_high(PIN_B6);
printf(">");
streamflag=0;
Sonar_data_mode=1;//0=cm, 1=in, 2=us
while (1)
{
switch(actionflag)//0 = nothing, 1 = read, 2 = change address
{
case 0:
//nothing
break;
case 1:
printf("I saw a request for data\r\n");
output_high(PIN_B7);
if((I2C_request_type==42) || (I2C_request_type==44))//sonar
{
printf("sending 20 bytes of data \r\n");
for(mycounter=0;mycounter<10;mycounter++)
{
i2c_write(Sonar_array[mycounter] & 0x00ff);
delay_ms(10);
i2c_write((Sonar_array[mycounter] & 0xff00) >> 8);
delay_ms(10);
}
}
if((I2C_request_type==43) || (I2C_request_type==44))//analog
{
printf("sending 12 bytes of data \r\n");
Read_analog();
for(j=0;j<6;j++)
{
delay_ms(10);
i2c_write(Analog_array[j] & 0x00ff);
delay_ms(10);
i2c_write((Analog_array[j] & 0xff00) >> 8);
// printf(" adc value is %ld\r\n",Analog_array[j]);
}
}
// I2C_request_type=0;
output_LOW(PIN_B7);
actionflag=0;
break;
case 2:
actionflag=0;
break;
default:
break;
}// end of action_flag
//printf("entering read_analog\r\n");
//delay_ms(1000);
// Read_analog();
//printf("out of read_analog\r\n");
//
if(SonarEnabled==1)
{
for(SonarNumber=0;SonarNumber<10;SonarNumber++)
{
trig=SonarNumber*2;
echo=trig+1;
// if(emulate==1)
range=faketimeEcho(trig,echo);
// else
// range=timeEcho(trig,echo);
Sonar_array[SonarNumber]=range;
delay_ms(Delay_between_readings);
//
// oppositeSonarNumber=SonarNumber+5;
// trig=oppositeSonarNumber*2;
// echo=trig+1;
// if(emulate==1)
// range=faketimeEcho(trig,echo);
// else
// range=timeEcho(trig,echo);
// Sonar_array[oppositeSonarNumber]=range;
// delay_ms(Delay_between_readings);
}
}
//
//
if(streamflag==1 ||streamflag==3 )//sonar
{
for(SonarNumber=0;SonarNumber<10;SonarNumber++)
{
if(range!=999)
{
printf("%d %lu ",SonarNumber,Sonar_array[SonarNumber]);
}
}
printf("\r\n");
}
if(streamflag==2||streamflag==3)//analog
{
Read_analog();
for(i=0;i<=5;i++)
printf("%ld ",analog_array[i]);
printf("\r");
}
//
loopcounter++;
if(loopcounter>10000)
{
loopcounter=0;
if(led1on==1)
{
led1on=0;
output_high(Pin_B7);
}
else
{
led1on=1;
output_low(Pin_B7);
}
}
}
}
void InitPic()
{
short int dummy_variable;
setup_adc_ports(AN0_AN1_AN2_AN3_AN4_AN5);
setup_adc( ADC_CLOCK_INTERNAL );
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_RDA); // Serial Data Available IRQ
enable_interrupts(INT_SSP);
// enable_interrupts(INT_TIMER2); // Timer 2 Overflow
dummy_variable = input(PIN_A0);//an0
dummy_variable = input(PIN_A1);//an1
dummy_variable = input(PIN_A2);//an2
dummy_variable = input(PIN_A3);//an3
output_low(PIN_A4);// not connected
dummy_variable = input(PIN_A5);//an4
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);
dummy_variable = input(PIN_C3);
dummy_variable = input(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);//an5
dummy_variable = input(PIN_E1);
output_low(PIN_E2);
}
/*
void read_analog()
{
int channel;
int16 value;
channel = 0;
// for(channel=0;channel<=5;channel++)
{
set_adc_channel(channel);
delay_us(15);
value = Read_ADC();
analog_array[channel] = value;
}
}
*/
////////////////////////////////////////////////////////////////////////////////////////////////
void read_analog()
{
int channel;
int16 value;
//disable_interrupts(INT_SSP);
for(channel=0;channel<=5;channel++)
{
// printf("setting--");
delay_us(15);
set_adc_channel(channel);
delay_us(15);
value = 42*channel;
// printf("reading--");
value = Read_ADC();
// printf("storing--");
analog_array[channel] = value;
}
// printf("returning\r\n");
//enable_interrupts(INT_SSP);
}
////////////////////////////////////////////////////////////////////////////////////////////////
#int_rda HIGH
void serial_int_routine()
{
int32 raw,data;
int trig,echo;
// Read incoming byte:
// printf("intRDA \r\n");
*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
{
// printf("i saw a %d\r\n",*curPos);
// Increment buffer position:
curPos++;
curCnt++;
}
}
else
{
curCnt = 0;
curPos = SERIAL_BUFFER;
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
void ProcessCommands()
{
signed int epos;
int num,i;
int32 raw,data;
int trig,echo;
// printf("Processing commands\r\n");
// Find end of command, and set to null:
// ePos = chrFind('\r', SERIAL_BUFFER, 0);
//printf("EPOS: %d", ePos);
// printf("\r\n");
if(strncmp (FwCmd, serial_buffer, 2)==0)
printf("%s\r\n", 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 (DelayCmd, serial_buffer, 4) == 0)
{
ConfigureSonarDelay();
}
else if(strncmp (ResetCmd, serial_buffer, 5)==0)
{
reset_cpu();
// Ack(ePos);
}
else if(strncmp (maxez1Cmd, serial_buffer, 5)==0)
{
//maxez1 x
num = serial_buffer[7];
trig=num*2;
echo=trig+1;
raw=timeEcho(trig,echo);
printf("%ld\r\n",raw);
//Ack(ePos);
}
else if(strncmp (AnalogCmd, serial_buffer, 5)==0)
{
read_analog();
for(i=0;i<=5;i++)
printf("%ld ",analog_array[i]);
printf("\r");
}
else if(strncmp (AllSonarCmd, serial_buffer, 5)==0)
{
//maxez1 x
for(num=0;num<=9;num++)
{
trig=num*2;
echo=trig+1;
raw=timeEcho(trig,echo);
printf("%ld ",raw);
}
//Ack(ePos);
}
else if(strncmp (Streamsonar, serial_buffer, 7)==0)
{
streamflag =1;
printf("Ack\r\n>");
}
else if(strncmp (Streamanalog, serial_buffer, 7)==0)
{
streamflag =2;
printf("Ack\r\n>");
}
else if(strncmp (Streamevthing, serial_buffer, 7)==0)
{
streamflag =3;
printf("Ack\r\n>");
}
else if(strncmp (StopCmd, serial_buffer, 4)==0)
{
streamflag =0;
printf("Ack\r\n>");
}
else
{
// printf("%s", *serial_buffer);
printf("Nack\r\n>");
// Nack(ePos);
}
// printf("\r\n");
// int_flag=0;
}
////////////////////////////////////////////////////////////////////////////////////////////////
void SetupNewEEPROM(void)
{
int address=0;//1 = blank eeprom test
int data=1;
write_EEPROM (address,data);
address=1;//baudrate
data=3;//0=2400,2=9600,3=19200,4=57600,5=115200
write_EEPROM (address,data);
// address=2;//Encoder style
// data=0;// 0=single. 1 = quad
// write_EEPROM (address,data);
// WritePid();
write_EEPROM (10,100);// save new address
write_EEPROM (11,1);//Sonar_data_mode 0=cm, 1=in, 2=us
}
////////////////////////////////////////////////////////////////////////////////////////////////
void ReadAllEEPROM(void)
{
int address,baud;
address = 1; //baudrate
baud = read_EEPROM (1);
// address = 2; //encoder type
// encoder_type= read_EEPROM (EEPROM_Base+2);
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 (11);
NODE_ADDR=read_EEPROM (10);
PIC_SSPADD=NODE_ADDR;
}
////////////////////////////////////////////////////////////////////////////////////////////////
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 ConfigureSonarDelay()
{
signed int sPos, ePos;
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)
{
Delay_between_readings = getNextValue(&sPos);
}
printf("Delay changed to %dms\r\n",Delay_between_readings);
}
////////////////////////////////////////////////////////////////////////////////////////////////
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);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////
int FaketimeEcho(int trigger, int echo)
{
int i;
i=rand();
i/=10;
i+=150;
return i;
}
////////////////////////////////////////////////////////////////////////////////////////////////
//void timeEcho(int trigger, int echo, signed int ePos)
int32 timeEcho(int trigger, int echo)
{
int timeout=0;
int16 ccs_trig,ccs_echo;
int16 io_tris;
int16 io_port;
int16 bitmask;
int16 RawData=0;
int8 retval;
unsigned int flight_time=0;
//printf("timeEcho Trigger Pin: %d Echo Pin: %d\r\n", trigger, echo);
//trigger goes high for 10us minimum
//wait for echo to go high
//time how long echo is high
//100us to 25ms
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(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);
}
|
_________________ Ringo Davis |
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Thu Aug 30, 2007 11:12 am |
|
|
I think I found the problem.
I had the line
ePos = chrFind('\r', SERIAL_BUFFER, 0);
commented out. So the end of my serial string was never getting set to NULL. Things like AtoI should not have been working but they were sometimes and not others. I just it just depended on what was next to the string in memory. It is weird that it was locking up when those functions were not getting called, but fixing this line seems to have fixed all the problems. I've still have a little bit of code left to uncomment, but so far it looks good. Thanks for the help.
Ringo _________________ Ringo Davis |
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Thu Aug 30, 2007 12:27 pm |
|
|
Next question. changing the I2C address. I have done this before, but it doesn't appear to be working.
I define the address like this
| Code: |
#byte PIC_SSPADD=0xC8
int NODE_ADDR = 100;//0x64
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=100,slow,force_hw)
|
Then I change it like this
| Code: |
NODE_ADDR = getNextValue(&sPos);
printf("New Address: %u\r\n", NODE_ADDR);
PIC_SSPADD=NODE_ADDR;// change address
write_EEPROM (10,NODE_ADDR);// save new address
|
Even if I reboot, or remove power completely, it still responds only to the old address. I save the address in eeprom, then read it on power up. I print out the values of Node_addr and PIC_SSPADD and they are both the new address, but it responds to the old address.
So it goes like this.
read pic_sspadd and node_addr, they both are 100
Change the address to 102
read pic_sspadd and node_addr, they both are 102
Send command to 100, it works
send command to 102, does not work
Power cycle
read pic_sspadd and node_addr, they both are 102
Send command to 100, it works
send command to 102, does not work
am I forgetting to do something? This code works on a pic16F876 (using a different SSPADD).
Thanks
Ringo _________________ Ringo Davis |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 30, 2007 12:35 pm |
|
|
| Quote: | | #byte PIC_SSPADD=0xC8 |
Hint: How many digits are used in the SFR addresses for the 18F-series ? |
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Thu Aug 30, 2007 12:39 pm |
|
|
I guess this is where I look dumb, but what do you mean by how many digits for the SFR?
Here is the entire header section
| Code: |
#include "18F452.h"
#fuses hs,nowdt,noprotect,put,nolvp
#device *=16
#device adc=10
#device HIGH_INTS=TRUE
#use delay(clock=20000000)
#byte PIC_SSPADD=0xC8
int Default_address = 100;
int NODE_ADDR = 100;//0x64
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=100,slow,force_hw)
|
_________________ Ringo Davis |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 30, 2007 12:40 pm |
|
|
Look in the 18F452 data sheet at this table:
| Quote: | | TABLE 4-1: SPECIAL FUNCTION REGISTER MAP |
|
|
 |
Ringo42
Joined: 07 May 2004 Posts: 263
|
|
Posted: Thu Aug 30, 2007 12:42 pm |
|
|
The Spec shows the address like this
FC8h
and I'm using C8
DOHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
I get it,
Should be 0xFC8
For some reason I kept over looking the F. _________________ Ringo Davis |
|
 |
|
|
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
|