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

Help a newbie to spi?
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Sep 19, 2007 6:42 am     Reply with quote

The setup of the ADC clock is outside the specifications. With your clock speed of 4Mhz set it to either DIV_8 or DIV_16. Now the ADC is delivering unreliable results.

Here is a cleaned up version of your code with all unused variables and declarations removed:
Code:
#include <16f690.h>
#fuses XT,NOWDT,NOBROWNOUT
#DEVICE *=16 ADC=10
#use delay(clock=4000000)
//Redbox 27VDC @ 20A Charger
//Revision 1.0
//written on compiler version 3.245
//tested on compiler version 3.245


#define CS PIN_B7

//Arrays
const int16 Temp[42] =   
    {784, 775, 765, 755, 745, 735, 725, 714, 704, 693,
     682, 671, 660, 649, 637, 626, 615, 603, 592, 581,
     569, 558, 546, 535, 524, 512, 501, 490, 479, 468,
     457, 446, 436, 425, 415, 405, 395, 385, 375, 365,
     356, 346};
const int8 digipot[41] =
    {255, 250, 244, 238, 232, 226, 220, 214, 208, 202,
     196, 190, 184, 178, 172, 166, 154, 148, 142, 136,
     130, 124, 118, 112, 106, 100,  94,  88,  82,  76,
      70,  64,  58,  52,  46,  40,  34,  28,  22,  16,
       0};

void main()
{
  int8 a, pot;
  int16 therm;
 
  setup_adc_ports(sAN0|sAN1|sAN2);
  setup_adc(ADC_CLOCK_DIV_8);         // Changed DIV_2 to DIV_8 for clock speed compliance.
  setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H | SPI_CLK_DIV_16);

  //setup
  output_high(CS);
  therm=0;
  pot=0;

  do
  {
    set_adc_channel(2);
    delay_us(75);
    therm = read_adc();

    if (therm > Temp[0])
    {
      pot = digipot[0];
    }
    else if (therm <= Temp[41])
    {
      pot = digipot[40];
    }
    else for (a = 0; a <= 40; a++)
    {
      if ( (therm <= Temp[a]) && (therm > Temp[a+1]) )
      {
        pot = digipot[a];
        break;            // Optimization: exit loop when found.
      }
    }
   
    output_low(CS);
    spi_write(0x11); //command byte
    spi_write(pot);  //data byte
    output_high(CS);

    delay_ms(500);
  } while(1);   
}


Also note how the arrays are declared as const. This places the data in ROM and saves about 120 bytes of RAM.

If the above code is still not working then try to split the problem in smaller parts. Is it the ADC that's failing or is it the potentiometer?

A real time saver in debugging is to implement an rs232 output for debug messages on the PC. Then you can print the values read from the ADC and the value you are sending to the potentiometer.
Ttelmah
Guest







PostPosted: Wed Sep 19, 2007 7:33 am     Reply with quote

How are you actually 'testing' this?.
You say that you only have the test meter attached to the pot. This may be the problem.
Digital potentiometers, behave significantly differently from 'real' ones. If you apply a DVM, the result can be wildly 'skewed', if the test voltage takes the wiper outside the supply range. You need to ground the bottom terminal of the digipot, or attach it to a voltage inside the working range, and then ensure that the DVM, is applying a +ve test voltage to the wiper. If you look at the data sheet test setup, this is done exactly this way. The pot will behave abnormally, if this is not the case.

Best Wishes
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Wed Sep 19, 2007 7:44 am     Reply with quote

Thankyou for all the help you are giving ckielstra.

I will test the code you have modified for me. One thing that makes me think that there is nothing wrong with the code as such is that when the pot gets stuck at 38K and I freeze spray the thermistor the data coming out of the PIC does change but the potentiometer doesn't change value.

I have sat and watched (with a scope)what the data should be when the pot is at 100K. When the pot is stuck I freeze spray the thermistor and the data comming out of the PIC changes to the 100K data but the digipot doesn't respond.

I have tried this on 3 different digipot chips now and they all do the same, I really don't now where the problem is.


OK now update I have tested the code you sent me and there is no change, it still does the same Mad
I monitored the data coming out of the chip again and it definetly changes to set the pot to 100K but the pot just sits there at 38K and won't budge. If I reset the power whilst the thermistor is still cold it immediately jumps to 100K when power is restored.

Can I assume the code works now and that the problem lies with the digipot?

JFK
jfk1965



Joined: 21 Oct 2003
Posts: 58

View user's profile Send private message

PostPosted: Wed Sep 19, 2007 8:01 am     Reply with quote

Ttelmah wrote:
How are you actually 'testing' this?.
You say that you only have the test meter attached to the pot. This may be the problem.
Digital potentiometers, behave significantly differently from 'real' ones. If you apply a DVM, the result can be wildly 'skewed', if the test voltage takes the wiper outside the supply range. You need to ground the bottom terminal of the digipot, or attach it to a voltage inside the working range, and then ensure that the DVM, is applying a +ve test voltage to the wiper. If you look at the data sheet test setup, this is done exactly this way. The pot will behave abnormally, if this is not the case.

Best Wishes


Hi I have the digipot setup as per the test parameter diagriam in the datasheet. I'm measuring it by connecting the DVM between Ground and the wiper. I'll check the DVM for a positive test voltage.

JFK
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 Previous  1, 2
Page 2 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