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

First time SPI questions
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
Guest
Guest







RS-232 invert
PostPosted: Sun Aug 22, 2004 6:54 pm     Reply with quote

Oooops. Of course you are right..... Gomen.
Guest
Guest







Things are moving along...but
PostPosted: Mon Aug 23, 2004 1:22 pm     Reply with quote

Well it was the missing '0' in clock speed and things have moved quite quickly since this was pointed out to me.

I am getting results in hyperterm now but I can't work out what they mean Very Happy

The data sheet is a little unhelpful when it comes to looking at the data returned after a sample, for instance it does not make it clear what is returned when I disable averaging! I am assuming that it's two bytes containing 12bits MSB first (really only a nibble) but I am showing results like the one's below from my little prog and I cannot figure out what how this correlates to the 1.5v battery I have connected.

0CDA
0CCC
0CCA
0CBC
0CCD

I am measuring a 1.5v battery, again I am a little unsure which way round the battery is supposed to go! from what I can tell AN0 should be batt+ and AN1 batt- (just search for IN+ in datasheet, you will see what I mean)

Well here is the program any help will be appreciated very much as I'm a little stuck.



#include <16F876.H>
#fuses HS, NOWDT, NOPROTECT,PUT,BROWNOUT,NOLVP
#use delay(clock = 20000000)
#use rs232(baud = 9600, xmit=PIN_C6, rcv=PIN_C7,PARITY=N,BITS=8,ERRORS)

#define EOC PIN_B6
#define CS PIN_B7

void main()
{
int16 Data;
int LSB,MSB;

printf("Start\n\r");
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
output_low(CS); //CS Low enables serial interface

//Setup Register
//01 = bits 7-6 select setup reg
//10 = bits 5-4 CKSEl1/2 Clock mode internally timed, no need for CNVST
//10 = bits 3-2 REFSEL1/2 Internal, reference always on, no wake up delay
//11 = bits 1-0 DEFSEL1/2 One byte follows, write to Unipolar register

spi_write(0b01101010);

//Setup Register
//111111 = bits 7-2 AN0-AN11 as unipolar differential pairs 0+1,2+3 etc.
//00 = bits 1-0 These are only applicaable for MAX1231

spi_write(0b11111100);

//Averaging Register
//001 = bits 7-5 Select Averaging register
//0 = bit 4 AVGON 0 = disable Averaging
//00 = bits 3-2 NAVG1/2 dont care
//00 = bits 1-0 NSCAN1/0 dont care

spi_write(0b00100000);


output_high(CS);

delay_ms(1);

while(1)
{
output_low(CS);

//Conversion register
//1 = bits 7 select conversion register
//0000 = bits 6-3 CHSEL3-0 Select Channel AN0
//11 = bits 2-1 SCAN1-0 No scan, Convert channel 0 once only
//0 = bits 0 TEMP, no temp sample.

spi_write(0b10000110);

//Signal we have finished and sample can begin

output_high(CS);

//Wait to the End Of Conversion pin to go low


while(input(EOC)){
}

//Enable serial interface

output_low(CS);

//Clock out the data

MSB = spi_read(0);
LSB = spi_read(0);
Data = MSB << 8;
Data = Data + LSB;

printf("%LX\n\r",Data);

//Disable serial interface and wait a little bit

output_high(CS);
delay_ms(1);
}
}
Guest
Guest







Any A/D Guru's out there?
PostPosted: Fri Aug 27, 2004 12:14 pm     Reply with quote

Hi,

Well after much playing I finally got it reading values but this being my first A/D and me being a little new to all this I don't understand the results

I used a bench power supply to get some readings, here they are

0.5v = 0x242
1v = 0x55E
1.5v = 0x896
2v = 0xBCB

and here is the link to the pdf
http://pdfserv.maxim-ic.com/en/ds/MAX1227-MAX1231.pdf

Can anyone explain this in simple terms Confused because I can work out why 2v is not twice the value of 1v (approx)
Ttelmah
Guest







Re: Any A/D Guru's out there?
PostPosted: Fri Aug 27, 2004 3:39 pm     Reply with quote

Guest wrote:
Hi,

Well after much playing I finally got it reading values but this being my first A/D and me being a little new to all this I don't understand the results

I used a bench power supply to get some readings, here they are

0.5v = 0x242
1v = 0x55E
1.5v = 0x896
2v = 0xBCB

and here is the link to the pdf
http://pdfserv.maxim-ic.com/en/ds/MAX1227-MAX1231.pdf

Can anyone explain this in simple terms Confused because I can work out why 2v is not twice the value of 1v (approx)

First thing, is just how accurate the bench supply is?. These normally measure what they are delivering, which is a whole 'wire length' away from the sensor circuit, and not with a terribly accurate meter anyway (unless it has recently been properly calibrated...).
You need to be looking at measuring the voltage actually present at the chip, rather than at the supply. It looks as though there is a significant offset voltage somewhere between the ground of the voltage source you are using, and the ADC's ground reference. The 'steps' between 0.5,1, and 1.5v, are pretty good, but the 0.5v reading, behaves as if the chip thinks it's ground is about 0.15v +, compared to the incoming voltage. You need to be _very_ carefult when designing board layouts to keep the analog circuitry seperate from the digital stuff as much as possible.

Best Wishes
rhodotron



Joined: 20 Apr 2008
Posts: 3

View user's profile Send private message

PostPosted: Tue May 20, 2008 2:27 pm     Reply with quote

I have to write a software to communicate to a dac mcp4822 and I am a bit confused on how start writing my code.
May be, my question is a lot stupid but I see few coding possibility.
One is to use the #use spi().
But I see also some example with setup spi() which apparently do the same. (not yet sure of that...)

when should we use one or the other option ?

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 20, 2008 2:44 pm     Reply with quote

This CCS driver should work for the MCP4822. The two chips are
very similar.
Quote:
c:\program files\picc\drivers\mcp4922.c
rhodotron



Joined: 20 Apr 2008
Posts: 3

View user's profile Send private message

PostPosted: Tue May 20, 2008 3:40 pm     Reply with quote

thanks pcm but about my question on #use spi and setup_spi what do you think ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue May 20, 2008 3:57 pm     Reply with quote

Apparently you want to use hardware SPI. In that case, I think it's
easier to use the setup_spi() and spi_write().

Look at the timing diagrams in the MCP4822 data sheet. It lists the
SPI modes that can be used. Here are the constants:
Code:

#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

Carefully follow the timing diagram in Figure 5-1 Write Command,
in the MCP4822 data sheet. Everything that you do in code must
conform to that timing diagram. Part of it must be done by directly
controlling i/o pins (CS and LDAC). The SCLK and SDI signals
are generated by the hardware SPI module in the PIC. The SCLK and
SDO pins are used on the PIC to send these signals to the DAC.
ckielstra



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

View user's profile Send private message

PostPosted: Tue May 20, 2008 7:43 pm     Reply with quote

rhodotron wrote:
thanks pcm but about my question on #use spi and setup_spi what do you think ?
By coincidence I had a conversation with CCS support regarding this.

The problem with setup_spi is that the way the parameters are combined using the OR command makes it difficult to do parameter checking. Several times in this forum we have seen people using an invalid combination of parameters. That's why I asked CCS if it is possible to implement parameter checks.

Today I received the following response: 'We are moving (slowly) to higher level, easier to use functions. In the case of setup_spi() we are recommending instead to use the #USE SPI... interface.'

I tried playing a bit with #use SPI in version 4.072d and couldn't get it to work for a hardware SPI unit. The code did compile in several combinations but the resulting code was incomplete and doesn't work.

For now I recommend to stick with the tested function setup_spi.
To be continued...
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