|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
PIC Issue |
Posted: Thu Aug 16, 2007 6:56 pm |
|
|
I am using a PIC18 series as a slave talking to a master through I2C. The PIC acquires data from a bunch of sensors and prepares a 30 byte data packet for the Master.
To acquire the data, the PIC sequentially power sensors, reads its value, then powers another sensor, reads its value and so on…
To make sure the master gets a complete data packet, I use the following method.
Code: |
void Buff1()
{
BF=1; // Set Flag
i=0; // Clear Variable
for (i=0;i<33;i++)
{
LongPack1[i]= LongPack[i];
}
BF=0; // Clear Flag
}
|
So if the Flag is set I use LongPack[30], if the flag is cleared I use LongPack1[30].
For some reason when I check the bit count of the values of the sensors I receive at the Master. I seem to be off by 100bits. In other words I get 394 bits as the value of a sensor instead of 498 bits.
I checked the VCC of the ADC on the PIC, and it varies between 4.89V and 4.97V.
The other problem I seem to face is that ever 60secs, then 70 secs, then 80 secs, then 90 secs, I receive the bit count of the sensors equal to 40.
Any idea why these two problems could be taking place!
Thanks. |
|
|
Guest
|
|
Posted: Thu Aug 16, 2007 7:37 pm |
|
|
Code: |
// This is how I read the sensors.
Address_Sensor(SensorNum); // Enables a Sensor
delay_us(50000);
set_adc_channel( MuxID-1 ); // Sets the adc channel 0 for Mux Set 1
delay_us(1000);
SensorOutput[SensorNum-1] =(float) Read_ADC(); // Reads the sensor output from the adc and saves the value in an Array
delay_us(10);
//Since I have a 10 bit ADC I convert the 5 sensor values from float to int16.
for( SensorNum=MaxIndex-2;SensorNum<MaxIndex+3;SensorNum++)
{
DataPack[i]=(int16)SensorOutput[SensorNum];
i++;
}
DataPack[5]=MaxIndex;
// In the method below I break the five 16bit sensor values into ten 8 bit values. I do this so I can send the 5 sensor values as 10 bytes to the master using I2C.
for( index=0;index<10;index++) // First 10 bytes of the Packet contain the 5 Sensor Values
{
ShortPack[index]=make8(DataPack[i],1);
index++;
ShortPack[index]=make8(DataPack[i],0);
i++;
}
|
Is there any way in the above code I could have changed the sensor values from a bit count of around 490s to around 400!! |
|
|
Steve H. Guest
|
|
Posted: Fri Aug 17, 2007 8:07 am |
|
|
A few things...
for (i=0;i<33;i++)
{
LongPack1[i]= LongPack[i];
this loop is for 0 to 32 - yet your output variable is: LongPack[30]???
Here you convert an int16 value to a float (which is only single precision and uses a lot of program memory and storage space),
SensorOutput[SensorNum-1] =(float) Read_ADC();
Yet later you convert it back to an int16,
(int16)SensorOutput[SensorNum]
This code is not making any sense to me - perhaps not to you either.
I would suggest that you will need to use a: "divide and conquer" approach on this.
That is start small - write a program that gets one sensor value correctly - make sure that that is all OK. Use a voltmeter to confirm all readings and make sure that everything makes sense.
Then try this same program on each sensor individually.
Then extend this to reading one sensor 30 times and build/test your packet.
Only after all this is done - then you can extend your program to a scan - and probably at that point - very effortlessly.
It's best to walk before you run.
HTH - Steve H. |
|
|
|
|
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
|