打印
[PIC®/AVR®/dsPIC®产品]

AVR的SPI入门

[复制链接]
873|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
vr, spi, pi, AVR
TB3215-Getting-Started-with-SPI-90003215A.pdf (939.63 KB)

#include <avr/io.h>
void SPI0_init(void);
void slaveSelect(void);
void slaveDeselect(void);
uint8_t SPI0_exchangeData(uint8_t data);
void SPI0_init(void)
{
PORTA.DIR |= PIN4_bm; /* Set MOSI pin direction to output */
PORTA.DIR &= ~PIN5_bm; /* Set MISO pin direction to input */
PORTA.DIR |= PIN6_bm; /* Set SCK pin direction to output */
PORTA.DIR |= PIN7_bm; /* Set SS pin direction to output */
SPI0.CTRLA = SPI_CLK2X_bm /* Enable double-speed */
| SPI_DORD_bm /* LSB is transmitted first */
| SPI_ENABLE_bm /* Enable module */
| SPI_MASTER_bm /* SPI module in Master mode */
| SPI_PRESC_DIV16_gc; /* System Clock divided by 16 */
}
uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0.DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm)) /* waits until data is exchanged*/
{
;
}
return SPI0.DATA;
}
void slaveSelect(void)
{
PORTA.OUT &= ~PIN7_bm; // Set SS pin value to LOW
}
void slaveDeselect(void)
{
PORTA.OUT |= PIN7_bm; // Set SS pin value to HIGH
}
int main(void)
{
uint8_t data = 0;
SPI0_init();
while (1)
{
slaveSelect();
SPI0_exchangeData(data);
slaveDeselect();
}
}


使用特权

评论回复
沙发
小明的同学|  楼主 | 2024-3-30 22:05 | 只看该作者
#include <avr/io.h>
#include <avr/interrupt.h>
void SPI0_init(void);
volatile uint8_t receiveData = 0;
volatile uint8_t writeData = 0;
void SPI0_init(void)
{
PORTA.DIR &= ~PIN4_bm; /* Set MOSI pin direction to input */
PORTA.DIR |= PIN5_bm; /* Set MISO pin direction to output */
PORTA.DIR &= ~PIN6_bm; /* Set SCK pin direction to input */
PORTA.DIR &= ~PIN7_bm; /* Set SS pin direction to input */
SPI0.CTRLA = SPI_DORD_bm /* LSB is transmitted first */
| SPI_ENABLE_bm /* Enable module */
& (~SPI_MASTER_bm); /* SPI module in Slave mode */
SPI0.INTCTRL = SPI_IE_bm; /* SPI Interrupt enable */
}
ISR(SPI0_INT_vect)
{
receiveData = SPI0.DATA;
SPI0.DATA = writeData;
SPI0.INTFLAGS = SPI_IF_bm; /* Clear the Interrupt flag by writing 1 */
}
int main(void)
{
SPI0_init();
sei(); /* Enable Global Interrupts */
while (1)
{
;
}
}

使用特权

评论回复
板凳
小明的同学|  楼主 | 2024-3-30 22:06 | 只看该作者
#include <avr/io.h>
void SPI0_init(void);
void slaveSelect(void);
void slaveDeselect(void);
uint8_t SPI0_exchangeData(uint8_t data);
void SPI0_init(void)
{
PORTA.DIR |= PIN4_bm; /* Set MOSI pin direction to output */
PORTA.DIR &= ~PIN5_bm; /* Set MISO pin direction to input */
PORTA.DIR |= PIN6_bm; /* Set SCK pin direction to output */
PORTA.DIR |= PIN7_bm; /* Set SS pin direction to output */
SPI0.CTRLA = SPI_CLK2X_bm /* Enable double-speed */
| SPI_DORD_bm /* LSB is transmitted first */
| SPI_ENABLE_bm /* Enable module */
| SPI_MASTER_bm /* SPI module in Master mode */
| SPI_PRESC_DIV16_gc; /* System Clock divided by 16 */
SPI0.CTRLB |= SPI_MODE_3_gc; /* Data Mode 3 */
}
uint8_t SPI0_exchangeData(uint8_t data)
{
SPI0.DATA = data;
while (!(SPI0.INTFLAGS & SPI_IF_bm)) /* waits until data is exchanged*/
{
;
}

return SPI0.DATA;
}
void slaveSelect(void)
{
PORTA.OUT &= ~PIN7_bm; // Set SS pin value to LOW
}
void slaveDeselect(void)
{
PORTA.OUT |= PIN7_bm; // Set SS pin value to HIGH
}
int main(void)
{
uint8_t data = 0;
SPI0_init();
while (1)
{
slaveSelect();
SPI0_exchangeData(data);
slaveDeselect();
}
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

139

主题

1433

帖子

2

粉丝