View previous topic :: View next topic |
Author |
Message |
bassplayer
Joined: 24 Jun 2004 Posts: 6
|
18F452 SPI COMS W/ MPS2308 |
Posted: Mon Nov 13, 2006 10:37 am |
|
|
I've been running the 18F452 in SPI mode w/ the MPS2308 to add extra input switches for months now and it works perfectly. I was just told to add extra outputs now to the design, but I'm having problems configuring the MPS2308 for output mode... there's something simple that I must be doing wrong but can't find it.
Here's the code: (I set up an extra CS signal that is not required in order to debug this software that is not working)
//Here I simply read a DIP switch and output the 8 bit results to both
//a MPS2308 setup for output and port B as an additional image
//The system is running on a 4 MHz crystal
//The dip switch is working the led output chip is NOT
#define TDIP_CLK PIN_C3
#define TDIP_DOUT PIN_C5
#define TDIP_DIN PIN_C4
#define TDIP_CS_SW PIN_A2
#define TDIP_CS_LED PIN_A3
unsigned int val;
void send_byte(int8 val_to_send)
{
int8 count;
for(count=0; count<8; count++)
{
output_bit(TDIP_DOUT, shift_left(&val_to_send,1,0));
delay_us(5);
output_low(TDIP_CLK);
delay_us(5);
output_high(TDIP_CLK);
}
}
int8 get_byte(void)
{
int8 count;
int8 val;
for(count=0;count<8;count++)
{
output_low(TDIP_CLK);
shift_left(&val,1,input(TDIP_DIN));
output_high(TDIP_CLK);
}
return(val);
}
void main()
{
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
set_tris_a(0x00);
set_tris_b(0x00);
set_tris_c(0x10);
output_b(0x55);
output_high(TDIP_CS_LED);
output_high(TDIP_CS_SW);
output_high(TDIP_CLK);
output_low(TDIP_CS_SW);
send_byte(0x40);
send_byte(6); //Pull up resistor register
send_byte(0xff); //enable pullups
output_high(TDIP_CS_SW);
output_low(TDIP_CS_LED);
send_byte(0x46);
send_byte(0x05); //configuration register
send_byte(0x3e); //sequential operation disabled
//enable address pins that are both HI
output_high(TDIP_CS_LED);
output_low(TDIP_CS_LED);
send_byte(0x46);
send_byte(0x00); //write to address 0x00
send_byte(0x00); //set for ALL outputs
output_high(TDIP_CS_LED);
output_low(TDIP_CS_LED);
send_byte(0x46);
send_byte(0X0A); //OLAT register
send_byte(0x81);
output_high(TDIP_CS_LED);
delay_ms(200);
while(1)
{
output_low(TDIP_CS_SW);
send_byte(0x41);
send_byte(9); //GPIO register
val = get_byte(); //get dip switches
output_high(TDIP_CS_SW);
output_low(TDIP_CS_LED);
send_byte(0x46);
send_byte(0x0A);
send_byte(val); //send dips to
output_high(TDIP_CS_LED);
output_b(val);
delay_ms(500);
}
}
As a last note, I still have confusion as to the relationship between the OLAT and GPIO registers. I've tried to write the output LED configuration to the GPIO registers as well, but I'm still not having any luck.
Will look forward to hearing the obvious problem that I am having.
Thanks!!! |
|
|
bassplayer
Joined: 24 Jun 2004 Posts: 6
|
Solved my own problem... |
Posted: Mon Nov 13, 2006 12:11 pm |
|
|
I've learned the important lesson of adding extra delays after writing to the MPS23S08 when using it as an output latch. When writing to the latch I only had to add an extra delay and everything works. Back to reading the datasheet. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Nov 13, 2006 12:11 pm |
|
|
Quote: | I've been running the 18F452 in SPI mode w/ the [b]MPS2308 |
Can you verify that part number ? I can't find it. Do you really
mean the MCP23S08 ? |
|
|
|