⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi_final.c

📁 spi驱动程序
💻 C
字号:
/********************************************************************************************
*Copyright (c) 2006, moons'
* All rights reserved.

*Filename: spi.c

*Author:WangXinXin LiMing
*Date:2006.8.24
*version:1.0
********************************************************************************************/
#include <iom128.h>
#define uint unsigned int
#define uchar unsigned char
uint i ;
uint j ;
uint k ;
uchar ok = 1 ;
uchar Burst_Array_Read[2800] ;
uchar Read[1056] ;
uchar a = 0xCD ;
uchar b = 0x00 ;
/***************************************** overflow ****************************************/
void overflow() 
{}
/***************************************** SPI_Init ****************************************/
uchar SPI_Init()
{  
   DDRD  |= (1<<5);
   DDRB   = (1<<PB2)|(1<<PB1)|(1<<PB0) ;   // SCK OUT  ;  MOSI OUT
   SPCR   = 0x51  ;                        // MODE3 ;  fosc/8
   DDRF  &= ~0x02 ;
   PORTF |= 0x02  ;
   SPSR  |= 0x01  ;
   return SPSR    ;
}
/************************************** SPI_Write_Byte *************************************/
void SPI_Write_Byte(char cData)
{
  SPDR = cData ;
  while(! (SPSR & (1<<SPIF))) ;
}
/************************************** SPI_Read_Byte **************************************/
uchar SPI_Read_Byte(void)
{
  SPDR = 0x11 ;  
  while(!(SPSR&(1<<SPIF)));             
  return SPDR ;
}
/*************************************** Flash_Read ****************************************/
void Flash_Read(uint PageNo,uint offset)
{
    uchar P1,P2,P3 ;
    P1 = ( ( PageNo >> 5 ) ) ;
    P2 = ( ( PageNo & 0x001F ) << 3 ) ;
    P3 = 0x00 ;                     //don't care number
    PORTB &= ~0x01   ;  
    PORTD &= ~0x20   ;             // PD5 DOWN
    SPI_Write_Byte(0x53)  ;        //opcode
    SPI_Write_Byte(P1)  ;         //24 bits address
    SPI_Write_Byte(P2)  ;
    SPI_Write_Byte(P3)  ;
     for(k=0;k<offset;k++)
    {
        Read[k] = SPI_Read_Byte()    ;
    }
    PORTD |= 0x20    ;              // PD5 UP
    PORTB |= 0x01    ; 
    while (!( PINF & 0x02 )  ) ;
}    
/********************************** Flash_Burst_Array_Read *********************************/
uchar Flash_Burst_Array_Read(uint PageNo,uint offset,uchar * RdData, uint TotalBytes) 
{
    uchar A1,A2,A3 ;
    A1 = ( PageNo >> 5 ) ;
    A2 = ( ( ( PageNo & 0x001F ) << 3 ) | ( offset >> 8 ) ) ;
    A3 = ( offset & 0x00FF ) ;
    PORTB &= ~0x01   ;  
    PORTD &= ~0x20   ;             // PD5 DOWN
    SPI_Write_Byte(0xe8)  ;        //opcode
    SPI_Write_Byte(A1)  ;          //24 bits address
    SPI_Write_Byte(A2)  ;
    SPI_Write_Byte(A3)  ;
    SPI_Write_Byte(0x00) ;         //32 don't care bits
    SPI_Write_Byte(0x00) ;
    SPI_Write_Byte(0x00) ;
    SPI_Write_Byte(0x00) ;
    for(j=0;j<TotalBytes;j++)
    {
        Burst_Array_Read[j] = SPI_Read_Byte()    ;
    }
    PORTD |= 0x20    ;              // PD5 UP
    PORTB |= 0x01    ; 
    while (!( PINF & 0x02 )  ) ;
    return * RdData ;
}
/********************************** Flash_Write_origin *************************************/
uchar Flash_Write_origin(uint PageNo,uint offset,uchar * WrData, uint TotalBytes) 
{ 
    uchar P1,P2,P3,B1,B2,B3 ;
    PORTD &= ~0x20 ;               // PD5 DOWN
    PORTB &= ~0x01 ;  
    SPI_Write_Byte(0x84)  ;        //opcode
    B1 = 0x00 ;  //don't care number
    B2 = ( offset >> 8 ) ; 
    B3 = ( offset & 0x00FF ) ;
    P1 = ( ( PageNo >> 5 ) );
    P2 = ( ( PageNo & 0x001F ) << 3 ) ;
    P3 = 0x00 ; //don't care number
    SPI_Write_Byte(B1) ;         //11 bits address
    SPI_Write_Byte(B2) ;
    SPI_Write_Byte(B3) ;
    for(i=0;i<TotalBytes;i++)
    {
    SPI_Write_Byte(* WrData) ;
    }
    while (!( PINF & 0x02 )  ) ;    
    PORTD |= 0x20  ;                 // PD5 UP
    PORTB |= 0x01  ;  
////////////////////////////////////////////////////////////////////////////////////////
    PORTB &= ~0x01   ;  
    PORTD &= ~0x20   ;                // PD5 DOWN
    SPI_Write_Byte(0x83)  ;          //opcode
    SPI_Write_Byte(P1)  ;            //13 bits address
    SPI_Write_Byte(P2)  ;
    SPI_Write_Byte(P3)  ;
    PORTD |= 0x20     ;              // PD5 UP
    PORTB |= 0x01     ;  
    while (!( PINF & 0x02 ) ) ;
    return ok ;
}
/************************************** Flash_Write ****************************************/
uchar Flash_Write(uint PageNo,uint offset,uchar * WrData, uint TotalBytes) 
{    
    if((PageNo+((offset+TotalBytes)/1056))>8191) overflow() ;
    if( TotalBytes>1056 )
    {
        Flash_Read(PageNo,offset) ;           // read previous data 
        Flash_Write_origin(PageNo,offset,&a,1056-offset) ;
        PageNo =  PageNo+1 ;
        TotalBytes = TotalBytes-1056 ;
        
        for( ;TotalBytes>1056;TotalBytes = TotalBytes-1056 )
        { 
           Flash_Read(PageNo,offset) ;       // read previous data 
           Flash_Write_origin(PageNo,0,&a,1056) ;
           PageNo =  PageNo+1 ;
        }   
        Flash_Write_origin(PageNo,0,&a,TotalBytes+offset) ;  
    }  
    if( (TotalBytes<1056) & (TotalBytes+offset>1056) )
    {
       Flash_Read(PageNo,offset) ;          // read previous data 
       Flash_Write_origin(PageNo,offset,&a,1056) ;
       Flash_Write_origin(PageNo,0,&a,(TotalBytes+offset-1056)) ;
    }
    if( TotalBytes+offset<1056 )
    {
       Flash_Read(PageNo,offset) ;          // read previous data 
       Flash_Write_origin(PageNo,offset, &a, TotalBytes) ;
    }  
    return ok ;
}
/****************************************** main *******************************************/
main()
{
   SPI_Init() ;
   Flash_Write(0,100,&a,2500) ;
   Flash_Burst_Array_Read(0,100,&b,2500)  ;
   while(1) ; 
}

⌨️ 快捷键说明

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