Home > other >  ATmega128 and CH374 function of USB device and PC communication. Have problems and seek solutions.
ATmega128 and CH374 function of USB device and PC communication. Have problems and seek solutions.

Time:10-04

This part of the relevant SPI initialization, burning program, PC can't identify in the equipment management,

It says "unknown [phenomenon: there will be a USB device" (device descriptor request failure), but then will automatically refresh after 1 second and then disappear this device shows]

Debugging is not right, for so many times is that several kinds of SPI is initialized, but to change is still wrong,

Hope great god taught,

The code is as follows:

/* CH374 chip software simulation SPI serial connection of the hardware abstraction layer V1.0 */

/* I/O opening procedures, provide the register level to read and write subroutines */

# include "HAL. H"

# include ".. "
\ includes h
# include "CH374INC. H"

?

# define SCS0 PORTB & amp;=~ (1 & lt; & lt; PB0)

//piece choose low signal buy

# define SCS1 PORTB |=(1 & lt; & lt; PB0)

//piece selected high signal buy





# define SCK0 PORTB & amp;=~ (1 & lt; & lt; PB1)



# define SCK1 PORTB |=(1 & lt; & lt; PB1)



# define MOSI0 PORTB & amp;=~ (1 & lt; & lt; PB2)



# define MOSI1 PORTB |=(1 & lt; & lt; PB2)



# define MISO0 PORTB & amp;=~ (1 & lt; & lt; PB3)



# define MISO1 PORTB |=(1 & lt; & lt; PB3)



# define INT00 PORTD & amp;=~ (1 & lt; & lt; PD0)

# define INT01 PORTD |=(1 & lt; & lt; PD0)



# define _BV (N) (1 & lt; & lt; (N))

# define SetBit (PORT, N) PORT |=_BV (N)

# define ClrBit PORT& (PORT, N);=~ _BV (N)

# define GetBit (PIN, N) (PIN> N) & amp; (0 x01)

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

To MCU based on the actual pin definition

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

# define SPI_PORT_1 PORTB

# define SPI_DDR_1 DDRB

# define SPI_PIN_1 PINB

# define MOSI 2

# define SCK 1

# define SS 0

# define MISO 3

# define SPI_M_PORT1_CONFIG

{SetBit (SPI_DDR_1, MOSI); SetBit (SPI_DDR_1, SCK); SetBit (SPI_DDR_1, SS); ClrBit (SPI_DDR_1, MISO); }

# define SPI_S_PORT1_CONFIG {ClrBit (SPI_DDR_1, MOSI); ClrBit (SPI_DDR_1, SCK); ClrBit (SPI_DDR_1, SS); SetBit (SPI_DDR_1, MISO); }





Void SPI_PORT_INIT (void)//IO port initialization

{

SetBit (SPI_PORT_1, MOSI);

SetBit (SPI_PORT_1, MISO);

SetBit (SPI_PORT_1, SCK);

SetBit (SPI_PORT_1, SS);

}

Void SPI_MasterInit (void)

{

SPI_M_PORT1_CONFIG;

SPCR=0 x52;//can make SPI host mode, set the clock rate to FCK/64

SPSR=0 x00;//tup SPI

}







Void CH374_PORT_INIT (void)/* due to the use of general simulation parallel I/O read and write timing, initialized so */

{

SPI_PORT_INIT ();

SPI_MasterInit ();

}







Void Spi374OutByte UINT8 (d)/* 8 bits of data */SPI output

{/* if it is hardware SPI interface, should be the first to write data into the SPI data register, and then queries the SPI status register for SPI byte transmission complete */

UINT8 I;

For (I=0; I & lt; 8; I + +) {

SCK0;//CH374_SPI_SCK=0;

If (d & amp; 0 x80)

MOSI1;//PORTB |=(1 & lt; & lt; 2);//CH374_SPI_SDI=1;

The else

MOSI0;//CH374_SPI_SDI=0;

D & lt; & lt;=1;/* data bits are high in the former */

SCK1;//CH374_SPI_SCK=1;/* CH374 along the sampling in rising clock input */

}

}





UINT8 Spi374InByte SPI input 8 bits of data (void)/* */

{/* if it is hardware SPI interface, check the SPI status register should be the first to wait for SPI byte transfer is completed, and then read the data from the SPI data register */

UINT8 I, d;

D=0;

/* if the SPI SDO and SDI share an I/O pins, then must set the I/O in this direction for input */

For (I=0; I & lt; 8; I + +) {

SCK0;//CH374_SPI_SCK=0;/* CH374 falling edge at the clock output */

D & lt; & lt;=1;/* data bits are high in the former */

If (MISO1) d++;//if (CH374_SPI_SDO) d + +;

SCK1;//CH374_SPI_SCK=1;

}

Return (d);

}



Void Spi374Start (UINT8 addr, UINT8 CMD)/* SPI started */

{

//CH374_SPI_SCS=1;

/* for bidirectional I/O pins simulation SPI interface, you must ensure that already set SPI_SCS, SPI_SCK, SPI_SDI for the output direction, SPI_SDO for input direction */



SCS0;//CH374_SPI_SCS=0;/* SPI piece selected effective */

Spi374OutByte (addr);

Spi374OutByte (CMD);

}



# define Spi374Stop () {SCS1; }

//{CH374_SPI_SCS=1; }/* end of SPI */

//void Spi374Stop (void)/* end of SPI */

//{

//CH374_SPI_SCS=1;/* SPI piece selected void */

//}



UINT8 Read374Byte (UINT8 mAddr)/* */reading data from the specified register

{

UINT8 d;

Spi374Start (mAddr CMD_SPI_374READ);

D=Spi374InByte ();

Spi374Stop ();

Return (d);

}



Void Write374Byte (UINT8 mAddr, UINT8 mData) to the designated register write data *//*

{

Spi374Start (mAddr CMD_SPI_374WRITE);

Spi374OutByte (mData);

Spi374Stop ();

}



Void Read374Block (UINT8 mAddr, UINT8 mLen, PUINT8 mBuf)/* read data from the specified source address block */

{

Spi374Start (mAddr CMD_SPI_374READ);

While (mLen -) * mBuf++=Spi374InByte ();

Spi374Stop ();
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related