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

PIC16F877 with AD7730

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



Joined: 10 Jan 2011
Posts: 5

View user's profile Send private message

PIC16F877 with AD7730
PostPosted: Mon Jan 10, 2011 9:57 am     Reply with quote

I want to realize an electronic scale and AD7730 or LTC2400 and PIC16F877. Please if you can help me with a script for CCS, such as connecting the output AD7730 / LTC2400 to PIC16F877. I found this driver but do not know how to adapt it:
Code:

int32 Read_A2D(void)
{
   int32 A2D_COUNTS;
   int i;

   output_high(A2D_SCLK);
   delay_us(100);
   output_high(A2D_CSI);
   output_low(A2D_SCLK);
   output_low(A2D_CSI);

   while((input(A2D_SDO))==1); //wait for /EOC to go low...

   for(i=4;i>0;i--) //I don't need to use these first 4 bits
   {
      output_high(A2D_SCLK);
      delay_us(5);
      output_low(A2D_SCLK);
      delay_us(5);
   }

   for(i=20;i>0;i--)      //Set i = number of bits to read
   {                     
      output_high(A2D_SCLK);
      delay_us(5);
      shift_left(&A2D_COUNTS,4,input(A2D_SDO));
      output_low(A2D_SCLK);
      delay_us(5);
   }
   output_high(A2D_CSI);

   return A2D_COUNTS;
}


Thank you very much.
naduran



Joined: 18 Sep 2011
Posts: 2

View user's profile Send private message

Plese
PostPosted: Tue Sep 20, 2011 4:33 am     Reply with quote

Please, how to use this source. I want it to meet the PIC16F876A.
Thank you very much
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Sep 20, 2011 4:14 pm     Reply with quote

for a start you want to DEFINE the control pins - as they connect to your pic
for example:
Code:

#DEFINE A2D_SCLK  PIN_B0
#define  A2D_CSI    PIN_B1
#define  A2D_CSI    PIN_B2
#define A2D_SDO   PIN_B3
 


and decide if you need to use fast i/o - then do some set_tris() etc etc

in short - make some hardware decisions and tell your pic code about them
hint: you may need a weak pullup resistor on the A2D_SDO line too
naduran



Joined: 18 Sep 2011
Posts: 2

View user's profile Send private message

ADC LTC2400 ...
PostPosted: Wed Sep 21, 2011 3:52 am     Reply with quote

I tried to apply this code:
Code:

#include "16F876A.H"
#fuses HS,NOWDT,PUT,BROWNOUT,NOPROTECT,NOLVP


#use delay(clock=16000000,restart_wdt)
//#use delay(clock=16000000)

//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use rs232(baud=9600, UART1)

#define LTC_CS        PIN_A0      // LTC2400 pin 5
#define LTC_SCK      PIN_A2      // LTC2400 pin 7
#define LTC_SDO      PIN_A3      // LTC2400 pin 6

#byte CMCON0  = 0x19


int32 variabila1=0;
unsigned int ADCBuffer[3];
//float rezultat = 0;
int32 rezultat = 0;
float Vref = 4.0962;         
//float Vref = 5.0;

#define FULLSCALE 16777216    // 2^24

int1 ltc_clock()
{
   int1 in=0;
   output_high(LTC_SCK);
   in=input(LTC_SDO);
   delay_ms(5);
   output_low(LTC_SCK);
   delay_ms(5);
   return(in);
}


void LTC_2400_read()
{
   unsigned int i=0;
   output_low(LTC_SCK);       
   output_low(LTC_CS);       

   delay_ms(70);
      ltc_clock();   
      ltc_clock(); 
      ltc_clock();   
      ltc_clock(); 
     
      ADCbuffer[0]=0;
      ADCbuffer[1]=0;
      ADCbuffer[2]=0;
   for (i = 1; i <= 24; i++)  shift_left(ADCbuffer,3,ltc_clock());
   output_high(LTC_CS);
   delay_ms(50);
   variabila1 = make32(ADCBuffer[2],ADCBuffer[1],ADCBuffer[0]);
}

void main()
{
  disable_interrupts(GLOBAL);
  CMCON0=7;
  setup_ccp1(CCP_OFF);
  setup_timer_2(T2_DISABLED,0,1);
  delay_ms(10);
  output_high(LTC_CS);
 
while(TRUE)
{
   LTC_2400_read();
   rezultat =  (int32)variabila1 / Vref; // / FULLSCALE;
   printf("%Lu \r\n", rezultat);
   delay_ms(250);
}
}


I put the LTC2400 input to GND, we next result in HyperTerminal:

4095752
15
4095779
91
29
4095785
4095775
4095789
4095788
4095752
5
4095780
66
79
83
16
4095762
21
4095790
48
3

I put the LTC2400 input to Vcc (5V), we have the following result:

4095793
4095757
4095747
43
54
4095726
4095769
1
11
56
86
120
4095688
7
45
7
37
4095739

I do not understand the cause of this instability.
Thank you.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Sep 21, 2011 7:39 am     Reply with quote

whats your schematic ???
how is the ADC connected to what model PIC and
your measurement source?
does the LTC part require a weak pull up resistor on SDO ??
how are you managing the WDT ??

and with a char array defined for adcbuffer - THIS LOOKS very ODD indeed

shift_left(ADCbuffer,3,ltc_clock());
octomescu



Joined: 10 Jan 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Sep 21, 2011 8:09 am     Reply with quote

My schematic is:


Are already blocked. I do not know what to do. Sad
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Sep 21, 2011 8:25 am     Reply with quote

what PIC are you using ???

did you just COPY a previous forum posting

http://www.ccsinfo.com/forum/viewtopic.php?t=27871&highlight=ltc2400


which was done for an F628??



have you closely read the data sheet for the LTC converter ??
you might look more closely at page 12
octomescu



Joined: 10 Jan 2011
Posts: 5

View user's profile Send private message

PostPosted: Wed Sep 21, 2011 9:07 am     Reply with quote

asmboy wrote:
what PIC are you using ???

did you just COPY a previous forum posting

http://www.ccsinfo.com/forum/viewtopic.php?t=27871&highlight=ltc2400


which was done for an F628??



have you closely read the data sheet for the LTC converter ??
you might look more closely at page 12



Yes ok, but do not understand why not work with 16F876. I looked in the data sheet, but do not understand much. Crying or Very sad
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Wed Sep 21, 2011 2:42 pm     Reply with quote

have you tried putting a 5 k ohm - 1 turn trim pot from Vcc 5v to ground and running the wiper to the input of the ADC so you can set precise 1/2 vcc etc values ??

also this mixed casting line
Code:

rezultat =  (int32)variabila1 / Vref; // / FULLSCALE;
is just more uncertainty

why not try
just printing out variabila1 instead of doing the divide by float stuff ??

my first rule of "project not working":
MAKE IT AS SIMPLE as possible - to be sure the hardware works as intended.

you are at an extra disadvantage because you copied other code
other than writing it yourself from scratch.

also THIS:

you are ONLY clocking out 28 bits of a 32 bit total 'word'
also you ignore the EOC bit - but please read bottom of page 11 of data sheet starting with:
Quote:

In order to shift the conversion result out of the device, CS
must first be driven LOW. EOC is seen at the SDO pin of the
device once CS is pulled LOW. EOC changes real time from
HIGH to LOW at the completion of a conversion. This
signal may be used as an interrupt for an external
microcontroller. Bit 31 (EOC) can be captured on the first
rising edge of SCK. Bit 30 is shifted out of the device on the
first falling edge of SCK. The final data bit (Bit 0) is shifted
out on the falling edge of the 31st SCK and may be latched
on the rising edge of the 32nd SCK pulse. On the falling
edge of the 32nd SCK pulse, SDO goes HIGH indicating a
new conversion cycle has been initiated. This bit serves as
EOC (Bit 31) for the next conversion cycle. Table 2 summarizes
the output data format.


i would write this driver to in a whole different way.
to make sure a conversion is ready and kosher,
also from the data sheet-
Quote:

Bit 31 (first output bit) is the end of conversion (EOC)
indicator. This bit is available at the SDO pin during the
conversion and sleep states whenever the CS pin is LOW.
This bit is HIGH during the conversion and goes LOW
when the conversion is complete.


i would check the SDO line state before even starting a readout - and then read all 32 bits before raising not_cs - but that just me - striving for completeness ;-))
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