www.pudn.com > 25xx256-EEPROM-programming-reference.rar > main.c, change:2007-09-07,size:8656b


 // C_SPI_BB_VF_LED+UART 
 // MAIN.c   
 //*************************************************************************** 
 // File Name    : main.c  
 // Dependencies : spi_bb_dec.h = header in which are declared the necessary  
 //                               functions for a "bit-bang" method , to 
 //                               interface a "MicroChip"s SPI memory  
 //                spi_bb_def.c = definitions of the above functions  
 //                REG952.h     = SFRs definitions offered by "Keil"  
 //                STARTUP950   = start-up code for LPC952 ( "Keil" )   
 // Processor    : P89LPC952   
 // Hardware     : MicroChip's SPI EEPROM = 25xx256 on MCB950 EVB .  
 // I.D.E.       : uVision3 - Keil  
 // Company      : MicroChip Technology , Inc.  
 //........................................................................... 
 //                SOFTWARE  LICENSE AGREEMENT  
 //........................................................................... 
 // "Microchip Technology Inc. (“Microchip”) licenses this software to you  
 // solely for use with Microchip Serial EEPROM products.  
 // The software is owned by Microchip and/or its licensors,and is protected  
 // under applicable copyright laws.  All rights reserved. 
 // SOFTWARE IS PROVIDED “AS IS.”  MICROCHIP AND ITS LICENSOR EXPRESSLY  
 // DISCLAIM ANY WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING  
 // BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS  
 // FOR A PARTICULAR PURPOSE,OR NON-INFRINGEMENT. IN NO EVENT SHALL MICROCHIP  
 // AND ITS LICENSORS BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR  
 // CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT,  
 // COST OF PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY  
 // CLAIMS BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE  
 // THEREOF),ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS." 
 //*************************************************************************** 
 // History      :  V1.0 - Initial Release  
 //........................................................................... 
 // File Description :  This is the main function for ANxxxx . Demonstrates  
 //                     a possible Hw & Sw interface between P89LPC952 and  
 //                     MicroChip's SPI EEPROM = 25xx256 .  
 // Writes a source string in the 25xx256 , reads it back in the destination 
 // string , compares the 2 strings , presenting 2 types of messages :  
 // error = flashes for ever 0x00 -0xff once / second  
 // o.k.  = shifts left one LED as sign of success(or gradualy lights the LEDs).  
 // During the sequence , the code displays the string's characters on LEDs 
 // and on UART1 line , for a visual control .  
 // The code shows how to access the SPI memory through 2 methods :  
 // random mode & page mode , depending of the value of the "access_mode"  
 // variable - RND_MODE or PG_MODE    
 //*************************************************************************** 
        #include "REG952.h" 
        #include "c_spi_bb_dec.h"   
        void main(void)    
      {  
        #define  ADR0  0x0040          // initialization of the address 
        #define  STRSZ     16          // string length  
        #define  RND_MODE   0          // random mode  
        #define   PG_MODE   1          // page mode 
 //...........................................................................  
        unsigned int adr_cnt        ;  // address counter  
        unsigned char ch_cnt        ;  // character counter inside  strings   
        unsigned char     ok        ;  // displays the "success" message 
        unsigned int n              ;  // auxiliary variable used to display  
        unsigned int duty           ;  // the variable duty PWM 
        unsigned char access_mode = RND_MODE ;  // access mode = RANDOM MODE 
//      unsigned char access_mode =  PG_MODE ;  // access mode = PAGE   MODE   
 //...........................................................................  
        unsigned char *src_str = "C_SPI_BB_VFLEDTX" ;   
                                       // source string which will be  
                                       // written in the e2prom(C=language,SPI= 
                                       // memory type , BB = bit-bang method ,  
                                       // VFLEDTX = verif through LEDs&COM1  
        unsigned char dst_str[STRSZ];  // destination string,read from the eep    
 //¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ 
                                       //  call all initialization routines  
        ini_intr()                  ;  //  interrupts'  init  
        ini_wdt()                   ;  //  W.D.T.'s     init  
        ini_osc()                   ;  //  oscillator's init    
        ini_gpio()                  ;  //  GPIO's       init , change for SPI  
        ini_i2c()                   ;  //  I2C bit-bang init  
        ini_com1()                  ;  //  UART1's      init  
        ini_tim()                   ;  //  Timers'      init  
        ini_spi()                   ;  //  init related GPIOs for SPI function  
        ini_memspi()                ;  //  init the SPI memory , no wr prot  
 //¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ 
 // WRITE SOURCE STRING IN EEPROM  
 //............................................................................ 
        if(access_mode==RND_MODE)      // IF random access mode  
      { ch_cnt=0  ;  adr_cnt=ADR0   ;  // intialize necessary variables  
        while(ch_cnt<STRSZ)            // for all chars in the source string  
{spi_rndwr(adr_cnt,src_str[ch_cnt]) ;  // perform random byte write   
        ch_cnt++ ; adr_cnt++ ;  }   }  // increment char + address counters  
        else                           // IF page access mode  
{spi_pgwr(src_str,ADR0,STRSZ)   ;   }  // perform page write from source str,   
                                       // starting from ADR0 address ,  
                                       // for STRSZ bytes  
 //¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ 
 // READ EEPROM IN THE DESTINATION STRING  
 // ........................................................................... 
        if(access_mode==RND_MODE)      // IF random access mode  
      { ch_cnt=0 ; adr_cnt=ADR0     ;  // intialize necessary variables  
        while(ch_cnt<STRSZ)            // for all chars in the source string  
 { spi_rndrd(adr_cnt,dst_str+ch_cnt);  // perform random byte read    
   adr_cnt++;ch_cnt++    ;    }     }  // increment char + address counters  
        else                           // IF page access mode  
   {spi_pgrd(dst_str,ADR0,STRSZ) ;  }  // perform page read in dest str,   
                                       // starting from ADR0 address ,  
                                       // for STRSZ bytes   
 //¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ 
 // COMPARE SOURCE & DESTINATION STRINGS  
 //............................................................................ 
 for(ch_cnt=0;ch_cnt<STRSZ;ch_cnt++)      // STRSZ length comparison cycle   
{ if((*(dst_str+ch_cnt))!=(*(src_str+ch_cnt))) // compare source <> destination 
    { while(1)                            // IF error flash for ever  
    { P5=00;dly1s();P5=255;dly1s();}}     // P5's LEDs once / second  
else{ P5=*(dst_str+ch_cnt)          ;     // IF no error , P5 = char       
      tx_com1(*(dst_str+ch_cnt))    ;     // send the char on UART1  
      dly1s()    ;               }  }     // once / second  
 //¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬¬ 
 //   ok = 0x01                     ;  // init the "display_success" variable .    
 //   while(1)                         //display forever a shifter,as sign of ok   
 // 	 { 
      for(ok=0x01;ok;ok<<=1) 
	     {P5=ok;dly250ms();}    
 //		                    }                             
 // An alternative to this "success" sequence is to gradualy light the 8 LEDs  
 // through a PWM with self-variable duty-cycle . Below , the possible code . 
  #define  speed   0x010             // the speed of changing the PWM ratio 
  #define MAXDUTY  3000              // MAXDUTY steps for the PWM  
  while(1)   
  { n++ ;  
    if(n<duty) { P5=0xff ; }  
    else       { P5=0x00 ; }      
    if(n>MAXDUTY) { duty +=speed ; n=0x0000 ; }  
    if(duty>MAXDUTY) { duty=0x0000 ; } } }  
 // ***************************************************************************