250x250
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 에버노트
- anydo
- Swipes
- ReactNative
- 온도센서
- debounce
- IOT
- 온도조절기
- 라온익스
- npm moment
- react-native
- 네모안
- 가스비절약
- Raspberry Pi
- 기름보일러
- evernote
- Node
- rasppi3
- 가스요금폭탄
- xcode11
- 난방비절약
- todoist
- REACT
- __attribute__
- 린나이온도조절기
- EditText
- node.js
- 경동
- 나비엔
- nodejs
Archives
- Today
- Total
어허
ATmega128l spi (8bit) 사용 본문
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
'개발 > AVR' 카테고리의 다른 글
AVR ATmega128 EEPROM read/write (0) | 2012.10.15 |
---|---|
Atmel Studio 6 에서 USBISP v3.0 & USBJTAG v3.0 사용 불가 (0) | 2012.10.04 |
[AVR] 배열이나 변수 선언등을 flash 메모리에 하기 ! MEMPROG (0) | 2011.11.03 |
AVR studio 에서 UART 입,출력(printf, scanf)사용하기 ! (2) | 2011.02.15 |
[AVR] sfr 로 지정된거 bit단위로 access 하기 (0) | 2011.02.10 |
Comments