|
|
View previous topic :: View next topic |
Author |
Message |
chrispol
Joined: 18 Oct 2009 Posts: 14
|
Embedded ethernet board spi and mcp3301 |
Posted: Wed Mar 10, 2010 2:09 pm |
|
|
I can't seem to communicate with a mcp3301 on spi, below is a test app
Code: | #include <18F4620.h>
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES H4 //High speed osc with HW enabled 4X PLL
#FUSES NOPROTECT //Code not protected from reading
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#use delay(clock=40000000)
#define MCP3301_CS PIN_B4
#define MCP3301_SDO PIN_C4
#define MCP3301_CLK PIN_C3
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
void init_mcp3301()
{
output_high(MCP3301_CLK);
output_high(MCP3301_CS);
setup_spi( SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4 );
}
BYTE read_adc_byte(BYTE number_of_bits)
{
BYTE i,data;
data=0;
for(i=0;i<number_of_bits;++i)
{
output_low(MCP3301_CLK);
delay_us(25);
shift_left(&data,1,input(MCP3301_SDO));
output_high(MCP3301_CLK);
delay_us(25);
}
return(data);
}
signed long read_mcp3301()
{
signed long out_value;
BYTE byte_low=0,byte_high=0;
output_low(MCP3301_CLK);
output_low(MCP3301_CS);
byte_high=read_adc_byte(8);
byte_low=read_adc_byte(5);
output_high(MCP3301_CS);
out_value = make16(byte_high,byte_low);
if(bit_test(out_value,12))
{bit_set(out_value,15);}
else
{bit_clear(out_value,15);}
return out_value & 0x9FFF;
}
void main()
{
signed long adc_value;
port_b_pullups(TRUE);
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
init_mcp3301();
for(;;)
{
//TcpIpTask();
adc_value = read_mcp3301();
printf("adc_value: %Ld \r\n",adc_value); // Signed output
delay_ms(1000);
}
} |
Anyone have any ideas? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Mar 10, 2010 2:42 pm |
|
|
You're mixing hardware SPI with software SPI. You need to choose
which one you want to use.
Most of your code is written for software SPI. If that's what you want
to use, then you should delete the setup_spi() line.
If you want to use hardware SPI, the MCP3301 data sheet explains
how to do it in this section:
Quote: |
7.3 Using the MCP3301 with
Microcontroller (MCU) SPI Ports
|
If that doesn't help, then post a list of the connections between the PIC
and the MCP3301 chip. Post the actual pin numbers, not the pin names. |
|
|
|
|
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
|