spi接口发送.c

来自「ICC mega8例子」· C语言 代码 · 共 66 行

C
66
字号
//ICC-AVR application builder : 2009-3-12 16:45:27
// Target : M8
// Crystal: 8.0000Mhz

#include <iom8v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x00;
 PORTC = 0x00; //m103 output only
 DDRC  = 0x00;
 PORTD = 0x00;
 DDRD  = 0x00;
}

//call this routine to initialize all peripherals
void init_devices(void)
{
 //stop errant interrupts until set up
 CLI(); //disable all interrupts
 port_init();

 MCUCR = 0x00;
 GICR  = 0x00;
 TIMSK = 0x00; //timer interrupt sources
 SEI(); //re-enable interrupts
 //all peripherals are now initialized
}

//*******************************spi_master*****************************
/***********************************************************
用    途:SPI主从式发送
Taget   :mega8
crystal :8M
介    绍:PB2-SS,PB3-MOSI,PB4-MISO,PB5-SCK
         从机的SS要接地
**********************************************************/
void spi_master_init()
{
 //DDR_SPI=(1<<DD_MOSI)|(1<<DD_SCK)
 DDRB=(1<<3)|(1<<5)|(1<<2);
 //使能SPI,master,fck/16
 SPCR=(1<<SPE)|(1<<MSTR)|(1<<SPR1);
}
//发送数据
void spi_master_putc(unsigned char cData)
{
 SPDR=cData;//发送数据
 while(!(SPSR&(1<<SPIF)))
 {;}
}
//*********************************************************************

void main()
{
 port_init();
 init_devices();
 
 spi_master_init();
 while(1)
 {
  spi_master_putc(0x54);
 }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?