어허

ATmega128l spi (8bit) 사용 본문

개발/AVR

ATmega128l spi (8bit) 사용

AKDK 2012. 10. 30. 16:53
728x90


여기저기 겁나게 널려있는 코드지만 일단 내코드 빽업용
datasheet에 자세하게 잘 나와 있으니 그거 가지고 공부 할 것!

GPIO로 직접 클락 만들고 데이터 쏘던 32bit SPI만 쓰다가
칩 내부 모듈 사용은 처음 *_*
오 ~

신기방기
신통방통

GPIO로 제어할 때 보다 속도도 훨씬 빠르고
코드도 뭐 ,,,, 나름 api 쪽 코드가 없으니 간편해 진것 같고
나쁘진 않은데 왠지 정이 안간다 ;;

다음 숙제는 GPIO로 UART api 만들기 ! 물론, 이건 EP021A로

ㅇ ㅏ내코드 뭔가 더러워

#define SPI8_SS			(Bit0)
#define SPI8_SCK		(Bit1)
#define SPI8_MOSI		(Bit2)
#define SPI8_MISO		(Bit3)

/***************************************************
 *
 *
 *	spi8 function part
 *
 *
 ***************************************************/


void spi8_Init(void)
{
	DDRB = SPI8_SS | SPI8_SCK | SPI8_MOSI;			// SCK, MOSI, SS output,
	DDRB &= ~(SPI8_MISO);							// MISO input	
	PORTB = 0x01;									// clear bits MOSI, MISO, SCK & ^SS.

	
	/* Enable SPI, Master, set clock rate fck/4*/
	SPCR = (1<<SPE) | (1<<MSTR);
	SPSR |= 0x01;				// Double SPI Speed Bit  => fck/2
	//PRINT_MSG("\r\n SPCR : 0x%02X", SPCR);
	//PRINT_MSG("\r\n SPSR : 0x%02X", SPSR);
}

uint8_t SPI8_WriteByte(uint8_t Data)
{
	PORTB &= ~(SPI8_SS);		// chipsel control, active low ( slave에서 원하는 대로 준거임 )
	SPDR = Data;
	while(!(SPSR & 0x80));		// SPIF(0x80)이 1로 될때까지 대기, 
	Data = SPDR;				// this line not dummy. do not erase this line!
	PORTB |= SPI8_SS;			// chipsel high
}
uint8_t SPI8_ReadByte(void)
{
	uint8_t	temp = 0;

	PORTB &= ~(SPI8_SS);
	SPDR = 0x00;				// this line not dummy. do not erase this line!
	while(!(SPSR & 0x80));
	temp = SPDR;				// return SPDR; 로 바로 리턴해도 된다. 필요에 따라 마음대로
	PORTB |= SPI8_SS;
	return temp;
}

uint32_t rr8(uint32_t Addr)
{
	uint32_t	val;
	int i = 0;

	/* Address setting sq0~3 */
	dout[0] = SPI8_READ | sq0;
	dout[1] = (Addr >> 24) & 0xff;

	dout[2] = SPI8_READ | sq1;
	dout[3] = (Addr >> 16) & 0xff;

	dout[4] = SPI8_READ | sq2;
	dout[5] = (Addr >> 8) & 0xff;

	dout[6] = SPI8_READ | sq3;
	dout[7] = Addr & 0xff;

	/* real Read & Write sq4~7 */
	dout[8] = SPI8_READ | sq4;
	dout[9] = 0;

	dout[10] = SPI8_READ | sq5;
	dout[11] = 0;

	dout[12] = SPI8_READ | sq6;
	dout[13] = 0;

	dout[14] = SPI8_READ | sq7;
	dout[15] = 0;
	
	for(i = 0; i < SPI_BYTES; i++)
	{
		if(i < 8) {
			SPI8_WriteByte(dout[i]);
		} else {
			SPI8_WriteByte(dout[i]);
			din[++i] = SPI8_ReadByte();
		}
	}

	val = (uint32_t)din[9]<<24
		| (uint32_t)din[11]<<16
		| (uint32_t)din[13]<<8
		| (uint32_t)din[15];
			
	return val;
}

void wr8(uint32_t Addr, uint32_t Data)
{
	int i = 0;

	/* Address setting sq0~3 */
	dout[0] = SPI8_WRITE | sq0;
	dout[1] = (Addr >> 24) & 0xff;

	dout[2] = SPI8_WRITE | sq1;
	dout[3] = (Addr >> 16) & 0xff;

	dout[4] = SPI8_WRITE | sq2;
	dout[5] = (Addr >> 8) & 0xff;

	dout[6] = SPI8_WRITE | sq3;
	dout[7] = Addr & 0xff;

	/* real Read & Write sq4~7 */
	dout[8] = SPI8_WRITE | sq4;
	dout[9] = (Data >> 24) & 0xff;

	dout[10] = SPI8_WRITE | sq5;
	dout[11] = (Data >> 16) & 0xff;

	dout[12] = SPI8_WRITE | sq6;
	dout[13] = (Data >> 8) & 0xff;

	dout[14] = SPI8_WRITE | sq7;
	dout[15] = Data & 0xff;
	
	for(i = 0; i < SPI_BYTES; i++)
	{
		SPI8_WriteByte(dout[i]);
	}
}
728x90
"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."
공유하기 링크
Comments