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

urgent please (Microchip SPI A/D MCP3202)

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







Re: urgent please (Microchip SPI A/D MCP3202)
PostPosted: Thu Aug 21, 2003 2:56 pm     Reply with quote

I have modified some of the routines from the CCS examples for another project and they work OK with PIC16F877A and PIC16F73s

I've used this at 4MHz and 20MHz with xtals and ceramic resinators (all for combinatinos).

I have fast_io enabled for all my I/O lines.

Rob Young
rwyoung@ieee.org

/**********************************************************
* int16 read_mcp3202(int8 channel)
*
* Purpose : Sample data from 2 channel ADC. ADC is 12-bits, unipolar.
* Parameters : channel - 1 or 2
* Calls : write_spi_byte()
* read_spi()
* Returns : Value read from ADC. Return data is LSB justified in 16 bits
* Notes :
* Tested : 02/03 RWY
**********************************************************/
int16 read_mcp3202(int8 channel)
{
int8 ctrl_bits;
int8 high;
int8 low;

output_low(SPI_CLK);
output_high(SPI_DIN);
output_low(ADC_CS);

if (channel==1)
ctrl_bits = 0b00001011; // channel 1 single ended
else
ctrl_bits = 0b00001111; // channel 2 single ended
write_spi_byte(ctrl_bits, 5); // send control bits (4 bits plus dummy clock)

high = read_spi(4);
low = read_spi(8);
output_high(ADC_CS);
return ( make16(high,low));
}

/**********************************************************
* void write_spi_byte(int8 data, int8 num_bits)
*
* Purpose : Write upto 8 bits of data to SPI device. SPI device chip select
* must be activated prior to calling this subroutine.
* Parameters : data - data to write
* num_bits - up to 8 bits
* Calls :
* Returns : none
* Notes :
* Tested : 02/03 RWY
*********************************************************/
void write_spi_byte(int8 data, int8 num_bits)
{
int8 i;

delay_us(1);
for(i=0;i<num_bits;i++)
{
output_low(SPI_CLK);
if ((data & 1) == 0)
output_low(SPI_DIN);
else
output_high(SPI_DIN);
data = data >> 1;
delay_us(1);
output_high(SPI_CLK);
delay_us(1);
}
}

/**********************************************************
* int8 read_spi(int8 num_bits)
*
* Purpose : Read upto 8 bits of data from SPI device. SPI device chip
* select must be activated prior to calling this subroutine.
* Parameters : num_bits - up to 8 bits
* Calls :
* Returns : data read from SPI device.
* Notes :
* Tested : 02/03 RWY
*********************************************************/
int8 read_spi(int8 num_bits)
{
int8 i;
int8 data;

data=0;
for(i=0; i<num_bits; i++)
{
output_low(SPI_CLK);
delay_us(1);
shift_left(&data,1,input(SPI_DOUT));
output_high(SPI_CLK);
delay_us(1);
}
return (data);
}
___________________________
This message was ported from CCS's old forum
Original Post ID: 144517139
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