当前位置: 代码迷 >> 单片机 >> SPI+EEPROM读写有关问题
  详细解决方案

SPI+EEPROM读写有关问题

热度:115   发布时间:2016-04-28 15:42:48.0
SPI+EEPROM读写问题
开发环境:Silicon Laboratories IDE+C8051F340+EEPROM IN24LC16
目的:想向EEPROM中写入一串数据,然后再读出来,并显示在UART0上
状况:用Watch调试查看时发现在读的时候中间变量test_byte并没有发生变化,最终导致读出的结果不对,不知道是没有写进入,还是写错了,抑或读的不对,希望各位大侠解答

#include <C8051F340.h>                 // SFR declarations
#include <stdio.h>                     // printf is declared here
sfr16 TMR2     = 0xCC;                 // Timer2 low and high bytes together
#define uchar unsigned char
#define UINT unsigned int
#define BAUDRATE           115200  
#define SYSCLK             24000000   
#define  F_SCK_MAX         2000000    
#define  T_NSS_DISABLE_MIN 500      
#define  EEPROM_CMD_RDSR   0x05
sbit LED_1 = P3^4;             
sbit LED_2 = P3^5;
void PCA0_Init (void);
void OSCILLATOR_Init (void);
void PORT_Init (void);
void TIMER2_Init (void);
void UART0_Init (void);
void SPI0_Init (void);
void Init_Device (void);
void Delay_us (uchar time_us);
void Delay_ms (uchar time_ms);
void EEPROM_Write (UINT address, uchar value);
uchar EEPROM_Read (UINT address);
void main (void)
{
   UINT  address =0x0000;   
   uchar  test_byte;    
   UINT  i ;
   bit  flag = 1;
   Init_Device ();  
  LED_1 = 1;
  LED_2 = 1;
   for (i = 0; i < 7; i++)
   {
      test_byte = 0xAA;
  IE &= 0x7F ;
      EEPROM_Write (address, test_byte);
  IE |= 0x80 ;
  address += 1;
      
      printf ("%02x ", (UINT)test_byte);
   }
   test_byte = 0x00 ;

   // Verify 
   for (i = 0x0000; i < address; i+=2 )
   {
      IE &= 0x7F ;
      test_byte = EEPROM_Read (i);
  IE |= 0x80 ;
      if (test_byte == 0xAA)
      {
         LED_1 = 0;
         printf ("right at %u\n", i);
      }
  else {
   printf("wrong at %u\n",i);
flag = 0;
}

   }