📄 spi_master.c
字号:
/********************************************************
************************作者:汝正阳*********************
*******功能:SPI主机模式,查询方式接收,并在LED上显示****
********************** 单片机型号:M8********************
************************2009年4月12日********************
***********说明:作为主机可作为接受但必须发送数据********
*********************************************************/
#include <iom8v.h>
#include <macros.h>
#define uint unsigned int
void delay(unsigned int ms) //定义过后可用uint
{
unsigned int i,j;
for(i=0;i<ms;i++)
{
for(j=0;j<1141;j++);
}
}
void port_init(void)//端口初始化
{
PORTB |= 0x00; //设置PB口
DDRB|=(1<<DDB3)|(1<<DDB5)|(1<<DDB2);//SCK,MOSI,SS输出
DDRC=0Xff;
PORTC=0Xff;
}
void spi_init(void)
{
SPCR = 0X78; //setup SPI 主机模式
SPSR = 0x00; //setup SPI sck=7.3728MHz/16
}
void spi_txd(char m)//发送程序
{
SPDR=m;
while(!(SPSR&(1<<SPIF)))//等待发送结束
;
}
unsigned char spi_read(void)
{
SPDR = 0x00; //作为主机必须发送数据,这句不可少
while(!(SPSR & BIT(SPIF)));
return SPDR;
}
void main (void)
{
unsigned char n;
port_init();
spi_init();
while(1)
{
PORTB=(0<<PB2);
n=spi_read();
PORTB=(1<<PB2);
PORTC=~n;
delay(30);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -