📄 spi总线.c
字号:
#include <iom162v.h>
#include <macros.h>
#include <stdio.h>
unsigned int i;
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0xFF;
PORTD = 0xFF;
DDRD = 0x02;
PORTE = 0x00;
DDRE = 0xFF;
}
void SpiInit(void )
{
DDRB|=(1<<PB7)|(1<<PB5)|(1<<PB4);// Set SCK, MOSI & SS as outputs
PORTB&=~(1<<PB7);
PORTB&=~(1<<PB5);
SPCR = 0x53;
}
void SpiWriteByte(unsigned char c)
{
SPDR = c;
while (!(SPSR & 0x80));
}
unsigned char SpiReadByte(void)
{
SPDR=0xFF;
while (!(SPSR & 0x80));
return SPDR;
}
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
SpiInit();
MCUCR=0x00;
EMCUCR=0x00;
SFIOR=0x00;
TIMSK= 0x00;
ETIMSK=0x00;
GICR= 0x00;
PCMSK1=0x00;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void main (void)
{
init_devices();
PORTB&=~(1<<PB4); // Spi enable for write a spi command
SpiWriteByte(0x55);
PORTB|=(1<<PB4);
while(1)
{ if(PINA&(1<<PA1))
{PORTB&=~(1<<PB4);
i=SpiReadByte();
PORTB|=(1<<PB4);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -