用LPC1768的IO口模擬SPI時序,將程序貼上,
#define CH376_SPI_SCK_init() ? gpio_init(LPC_GPIO0,15,1,1)
#define CH376_SPI_SCK_high() ? gpio_output_value(LPC_GPIO0,15,1)
#define CH376_SPI_SCK_low() ? ?gpio_output_value(LPC_GPIO0,15,0)
#define CH376_SPI_SCS_init() ? gpio_init(LPC_GPIO0,16,1,1)
#define CH376_SPI_SCS_high() ? gpio_output_value(LPC_GPIO0,16,1)
#define CH376_SPI_SCS_low() ? ?gpio_output_value(LPC_GPIO0,16,0)
#define CH376_SPI_MISO_init() ?gpio_init(LPC_GPIO0,17,0,1)
#define CH376_SPI_MISO() ? ? ? gpio_read_value(LPC_GPIO0,17)
#define CH376_SPI_MOSI_init() ?gpio_init(LPC_GPIO0,18,1,1)
#define CH376_SPI_MOSI_high() ?gpio_output_value(LPC_GPIO0,18,1)
#define CH376_SPI_MOSI_low() ? gpio_output_value(LPC_GPIO0,18,0)
#define CH376_SPI_RST_init() ? gpio_init(LPC_GPIO0,19,1,1)
#define CH376_SPI_RST_high() ? gpio_output_value(LPC_GPIO0,19,1)
#define CH376_SPI_RST_low() ? ?gpio_output_value(LPC_GPIO0,19,0)
#define CH376_SPI_INT_init() ? gpio_init(LPC_GPIO0,20,0,1)
#define CH376_SPI_INT_state() ?gpio_read_value(LPC_GPIO0,20)
void mDelaymS(uint16_t cnt)
{
? ? uint16_t i,j;
? ? for(i=0;i
? ? {
? ? ? ? ?for(j=0;j<5000;j++); ? ? ??
? ? }
}
void delay_us(uint16_t cnt)
{
uint16_t i,j;
? ? for(i=0;i
? ? { ? ? ? ? ? ? ? ? ? ? ? ? ??
? ? ? ? ?for(j=0;j<10;j++); ? ? ??
? ? }
}
voidCH376_PORT_INIT( void )
{
? ?CH376_SPI_SCS_high();
? CH376_SPI_SCK_high();
}
voidxEndCH376Cmd( void )
{?
CH376_SPI_SCS_high();?
}
voidSpi376OutByte(uint8_t d )
{ ?
?//ssp_write_read_byte(LPC_SSP0,d);?
? uint8_t i;
for(i=0;i<8;i++)
{
CH376_SPI_SCK_low();
if(d&0x80)
{
CH376_SPI_MOSI_high();
}
else
{
? ? CH376_SPI_MOSI_low();
}
d<<=1;
CH376_SPI_SCK_high();
}
}
uint8_t Spi376InByte(void)
{
uint8_t i,d=0;
for(i=0;i<8;i++)
{
CH376_SPI_SCK_low();
d<<=1;
if(CH376_SPI_MISO())
{
?d++;
}
CH376_SPI_SCK_high();
}
return d;
}
/*******************************************************************************
向CH376寫命令
*******************************************************************************/
voidxWriteCH376Cmd( uint8_t mCmd )
{
CH376_SPI_SCS_high(); ??
delay_us(3);
CH376_SPI_SCS_low();
mDelaymS( 10 );
Spi376OutByte( mCmd ); ?
delay_us(2);
}
/*******************************************************************************
向CH376寫數(shù)據(jù)
*******************************************************************************/
voidxWriteCH376Data( uint8_t mData )
{
Spi376OutByte( mData );
delay_us(10); ?
}
/*******************************************************************************
CH376讀數(shù)據(jù)
*******************************************************************************/
uint8_txReadCH376Data( void )
{
uint8_t ?i=0;
delay_us( 10 );
i=Spi376InByte();
? return (i);
}
/*******************************************************************************
查詢INT中斷
*******************************************************************************/
uint8_tQuery376Interrupt( void )
{
uint8_t i;
i = (CH376_SPI_INT_state() ? 0 : 1 );
return( i );?
}
/*******************************************************************************
初始化CH376
*******************************************************************************/
uint8_tmInitCH376Host( void )
{
uint8_tres;
mDelaymS(100);
/*CH376_SPI_RST_high();
mDelaymS(100);
CH376_SPI_RST_low();
mDelaymS(100);*/?
CH376_PORT_INIT( ); ? ? ? ? ??
xWriteCH376Cmd( CMD11_CHECK_EXIST );?
xWriteCH376Data( 0x55 );
res = xReadCH376Data( );
//printf("res =%02x \n",(unsigned short)res);
xEndCH376Cmd( );
if ( res != 0xAA ) return( ERR_USB_UNKNOWN ); ?
xWriteCH376Cmd( CMD11_SET_USB_MODE );?
xWriteCH376Data( 0x06 );?
delay_us( 20 );
res = xReadCH376Data( );
//printf("res =%02x \n",(unsigned short)res);
xEndCH376Cmd( );
if ( res == CMD_RET_SUCCESS ) ?
{
? ?return( USB_INT_SUCCESS );?
}
else?
{
? ?return( ERR_USB_UNKNOWN );
}
}
初始化返回值一直是0xff,不知道怎么回事,淚崩!??!