想把数据存放在25LC160中,然后取出显示,但是倒腾了几天都没弄出来,数据手册也看了几遍,找不出问题,麻烦各位大神解释解释,非常感谢。
#include<iom88.h>
#include<avr_macros.h>
#define uchar unsigned char
#define uint unsigned int
#define SPI_CS PORTD_Bit5
#define SPI_SCK PORTB_Bit5
#define SPI_HOLD PORTD_Bit7
#define SPI_SI PORTB_Bit3
#define SPI_SO PORTB_Bit4
#define SPI_WP PORTD_Bit6
#define SPI_RE_SO (PINB&0X10)
uchar table[] = {0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90};//0~9
void delay(uchar x)
{
uchar kk;
for(;x>0;x--)
for(kk=100;kk>0;kk--);
}
/**********************************************************
读字节数据子函数
MCU从SPI读出数据
**********************************************************/
uchar SPI_READ()
{
uchar dat;
uchar i;
SPI_CS = 0;
DDRB &= 0XEF;
for(i=0;i<8;i++)
{
SPI_SCK = 0;
delay(1);
dat = dat<<1;
if(SPI_RE_SO)
dat |= 0x01;
SPI_SCK = 1;
}
return dat;
}
/**********************************************************
写字节数据子函数
MCU写数据到SPI,
**********************************************************/
void SPI_WRITE(uchar dat)
{
uchar i;
SPI_CS = 0;
for(i=0;i<8;i++)
{
SPI_SCK = 0;
delay(1);
if(dat&0x80)
SPI_SI = 1;
else
SPI_SI = 0;
SPI_SCK = 1;
dat = dat<<1;
}
}
void WREN() //写允许
{
SPI_CS = 0;
delay(1);
SPI_WRITE(0x06);
SPI_CS = 1;
}
/**********************************************************
写字节数据子函数
将数据写入指定地址
**********************************************************/
void Write_byte(uint add,uchar dat)
{
uchar add_h,add_l;
SPI_CS = 0;
add_h = add>>8;
add_l = add&0x00ff;
SPI_WRITE(0x02);
SPI_WRITE(add_h);
SPI_WRITE(add_l);
SPI_WRITE(dat);
SPI_CS = 1;
}
uchar Read_byte(uint add) //将数据从16bit地址读出
{
uchar add_h,add_l;
uchar value;
SPI_CS = 0;
add_h = add>>8;
add_l = add&0x00ff;
SPI_WRITE(0x03);
delay(1);
SPI_WRITE(add_h);
SPI_WRITE(add_l);
delay(1);
value = SPI_READ();
SPI_CS = 1;
return value;
}
void WRDI() //写禁止
{
SPI_CS = 0;
delay(1);
SPI_WRITE(0x04);
SPI_CS = 1;
}
uchar RDSR() //读状态寄存器
{
uchar value;
SPI_CS = 0;
delay(1);
SPI_WRITE(0x05);
value = SPI_READ();
SPI_CS = 1;
return value;
}
void WRSR(uchar stat) //写状态寄存器
{
SPI_CS = 0;
delay(1);
SPI_WRITE(0x01);
SPI_WRITE(stat);
SPI_CS = 1;
}
void SPI_init(void)
{
SPI_CS = 1;
delay(1);
SPI_SCK = 1;
WREN();
delay(20);
WRSR(0X02);
delay(20);
}
void main(void)
{
uchar temp=0;
DDRD = 0XFF;
DDRB = 0X2C;
DDRC = 0xff;
PORTD &= 0XEF;
SPI_HOLD = 1;
SPI_CS = 0;
SPI_WP = 1;
SPI_init();
while(1)
{
Write_byte(0x0001,0x55);
temp = Read_byte(0x0001);
PORTC = table[temp%10];
}
} |