搜索
bottom↓
回复: 0
打印 上一主题 下一主题

SPDR = 0x00;变成while了

[复制链接]

出0入0汤圆

跳转到指定楼层
1
发表于 2010-12-31 20:15:11 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#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);
        }
}
不知道为什么不断的循环

阿莫论坛20周年了!感谢大家的支持与爱护!!

一只鸟敢站在脆弱的枝条上歇脚,它依仗的不是枝条不会断,而是自己有翅膀,会飞。
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-3-29 15:38

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表