//EX18AVR SPI PORTC/*//EX18AVR ATmega8A SPI master program//author :QI小米粥//data :2018.3.30#include#includevoid init(void) {SFIOR &=~BIT(2);//配置的上拉//PORTB |=BIT(4);//配置miso上拉电阻DDRB =BIT(2)|BIT(3)|BIT(5);//SS,MOSI,SCK设置位输出SPCR =BIT(6)|BIT(4)|BIT(0); }void master_t(char a) {PORTB &=~BIT(2);//拉低ssSPDR =a;//数据写入自动启动时钟SCKwhile(!(SPSR&BIT(7))) //完成中断产生后,可以继续往下走 {}PORTB |=BIT(2);//抬高ss清除spi}#pragma interrupt_handler inter_spi:11void inter_spi(void) {}void main(void) {init();master_t(0xf0);}//默认输入引脚也要上拉,并没有这么做//按着datasheet中的简单的操作来的//SS开始转换要拉低,结束转换后要抬高*///EX19AVR ATmega8A SPI slave program//author:QI小米粥//data:2018.3.30#include#includeint a;
void init(void)
{
DDRC =0xff;//slave的输出
DDRB |=BIT(0);
SFIOR &=~BIT(2);
DDRB &=~BIT(2);//SS输入
PORTB |=BIT(2);
DDRB |=BIT(4);//MISO设置为输出
SPCR |=BIT(6);//SPI使能
}
char slave_r(void)
{
while(!(SPSR&BIT(7)))//传输完成后产生,中断后继续往下走,
{}
return SPDR;
}
void main(void)
{
init();
PORTC=slave_r();
PORTB |=BIT(0);
}
网友评论