|
#include <iom16v.h>
#include <macros.h>
/*CS引脚:PA1*/
#define SPI_CS PA1
#define SPI_CS_Output (DDRB|=(1<<SPI_CS)) //置CS引脚为输出脚
#define SPI_CS_High (PORTB|=(1<<SPI_CS)) //CS引脚输出高电平
#define SPI_CS_Low (PORTB&=~(1<<SPI_CS)) //CS引脚输出低电平
/*SCLK引脚:PB7*/
#define SPI_SCLK PB7
#define SPI_SCLK_Output (DDRB|=(1<<SPI_SCLK)) //置SCLK引脚为输出脚
#define SPI_SCLK_High (PORTB|=(1<<SPI_SCLK)) //SCLK引脚输出高电平
#define SPI_SCLK_Low (PORTB&=~(1<<SPI_SCLK)) //SCLK引脚输出低电平
/*MISO(数据输入引脚):PB6*/
#define SPI_MISO PB6
#define SPI_MISO_Input (DDRB&=~(1<<SPI_MISO)) //置MISO引脚为输入脚
#define SPI_MISO_Pullup (PORTB|=(1<<SPI_MISO)) //MISO引脚为上拉输入
/*MOSI(数据输出引脚):PB5*/
#define SPI_MOSI PB5
#define SPI_MOSI_Output (DDRB|=(1<<SPI_MOSI)) //置MOSI引脚为输出脚
#define SPI_MOSI_High (PORTB|=(1<<SPI_MOSI)) //MOSI引脚输出高电平
#define SPI_MOSI_Low (PORTB&=~(1<<SPI_MOSI)) //MOSI引脚输出低电平
unsigned int State_SW8=0;
unsigned int bl1,bl2;
void port_init(void)
{
PORTA = 0x09;
DDRA = 0x09;
PORTB = 0xE0;
DDRB = 0x03;
PORTC = 0xD0; //m103 output only
DDRC = 0xf0;
PORTD = 0xF8;
DDRD = 0xF8;
}
/*******************************************************************************************************************
** 函数名称: void SPI_SendByte() Name: void SPI_SendByte()
** 功能描述: 通过SPI接口发送一个字节 Function: send a byte by SPI interface
** 输 入: INT8U byte: 发送的字节 Input: INT8U byte: the byte that will be send
** 输 出: 无 Output: NULL
********************************************************************************************************************/
void SPI_SendByte(unsigned int byte)
{
//unsigned int temp;
//SPDR = byte; /* 发送数据放入SPI数据寄存器 */
//while(0 == (SPSR & 0x80)); /* 等待SPIF置位,即等待数据发送完毕 */
/* wait for SPIF being set, that is, wait for finishing of
data being send */
//temp = SPI_SPDR;
}
/*******************************************************************************************************************
** 函数名称: INT8U SPI_RecByte() Name: INT8U SPI_RecByte()
** 功能描述: 从SPI接口接收一个字节 Function: receive a byte from SPI interface
** 输 入: 无 Input: NULL
** 输 出: 收到的字节 Output: the byte that be received
********************************************************************************************************************/
unsigned int SPI_RecByte(void)
{
SPDR = 0xFF;
while(0 == (SPSR & 0x80)); /* 等待SPIF置位,即等待收到数据 */
/* wait for SPIF being set, that is, wait for being received
data */
return SPDR; /* 读取收到的字节 read the byte received */
}
#pragma interrupt_handler int1_isr:iv_INT1
void int1_isr(void)
{
//external interupt on INT1
State_SW8=1;
}
void init_devices(void)
{
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x0A;
GICR = 0x80;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
/****************************************************************************
* 名 称:SPI_Initialization()
* 功 能:SPI初始化函数
* 入口参数:无
* 出口参数:无
****************************************************************************/
void SPI_Initialization(void)
{
SPI_CS_Output;
SPI_CS_High;
SPI_SCLK_Output;
SPI_SCLK_High;
SPI_MISO_Input;
SPI_MISO_Pullup;
SPI_MOSI_Output;
SPI_MOSI_High;
SPCR =0xd5;
SPSR=0x00;
}
/****************************************************************************
* 名 称:SPI_LowSpeedMode()
* 功 能:SPI低速模式
* 入口参数:无
* 出口参数:无
****************************************************************************/
void SPI_LowSpeedMode(void)
{
SPCR = 0;
SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0) | (1 << SPR1);
}
/****************************************************************************
* 名 称:SPI_HighSpeedMode()
* 功 能:SPI倍速模式
* 入口参数:无
* 出口参数:无
****************************************************************************/
void SPI_HighSpeedMode(void)
{
SPCR = 0;
SPCR = (1 << SPE) | (1 << MSTR);
SPSR |= (1 << SPI2X);
}
/****************UART初始化*********
*波特率:9600 数据位:8 奇偶校验:无 *
*禁止收发中断 *
*************************************/
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x2F; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x18;
}
/*************************************************************
* 名称:main()
* 功能:
***************************************************************/
int main(void)
{
int i;
init_devices();
beed(10);
SPI_Initialization();
Delay(200);
SPI_SCLK_High;
/*for(i=0;i<=21;i++)
{ //大于74个时钟
SPI_SendByte(0xff);
}
SPI_CS_Low;
SPI_SendByte(0x40);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x00);
SPI_SendByte(0x95);
while(bl1==0xff)
{
bl1= SPI_RecByte();
} ;*/
beed(3);
Delay(200);
SPDR = 0x00;
return 0;
}
void Delay(int counter)
{
int i;
while(counter--)
{
for(i=0; i<(187*7); i++);
}
}
void beed(unsigned int i)
{
while(i--)
{
PORTA &=~(1<<3);
Delay(2);
PORTA |=(1<<3);
Delay(20);
}
}
不知道为什么不断的循环 |
|