?? master.c
字號:
//包含所需頭文件
#include <ioM16v.h>
#include <macros.h>
/*------宏定義------*/
#define uchar unsigned char
#define uint unsigned int
uchar k=1;
/*------函數定義------*/
//SPI函數
void spi_write(uchar sData);
uchar spi_read(void);
void delay(uint z)
{
uint i,j;
for(i=0;i<z;i++)
for(j=0;j<1000;j++);
}
//端口初始化
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
void spi_init(void)
{
//spi初始化
PORTB |= (1<<PB4) | (1<<PB5) | (1<<PB6) | (1<<PB7);
DDRB|=BIT(PB7)|BIT(PB5)|BIT(PB4);
SPCR = 0x50;
SPSR = 0x00;
}
//功能:使用SPI發送一個字節
void spi_write(uchar sData)
{
SPDR = sData;
while(!(SPSR & BIT(SPIF)));
//sData=SPDR;//讀從機發回來的數據
}
//功能:使用SPI接收一個字節
uchar spi_read(void)
{
SPDR = 0x00;
while(!(SPSR & BIT(SPIF)));
return SPDR;
}
void init_devices(void)
{
CLI(); //禁止所有中斷
MCUCR = 0x00;
MCUCSR = 0x80;//禁止JTAG
GICR = 0x00;
port_init();
spi_init();
SEI();//開全局中斷
}
//主函數
void main(void)
{
uint a=1000;
init_devices();
//在這繼續添加你的代碼
do{
PORTB&=~BIT(PB4);
spi_write(k*25);
PORTB|=BIT(PB4);
delay(100);
k++;
}
while(!(spi_read()==255));
while(1)
{
;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -