|
#include
#include
#define data_led_0 PORTB&=~(1<
#define data_led_1 PORTB|= (1<
#define chip_led_0 PORTB&=~(1<
#define chip_led_1 PORTB|= (1<
unsigned char const table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x67};
union data{
unsigned long dress;
unsigned char address[4];
};
void delay(int ms)
{
unsigned char j;
for(;ms>0;ms--)
{
for(j=0;j<200;j++);
}
}
void display(unsigned char byte)
{
DDRD=0XFF;
DDRB|=(1<
PORTB=0XFF;
PORTD=0XFF;
data_led_1;
PORTD=table[byte/10];
data_led_0;
chip_led_1;
PORTD=0XFE;
chip_led_0;
delay(5);
data_led_1;
PORTD=table[byte%10];
data_led_0;
chip_led_1;
PORTD=0xfd;
chip_led_0;
delay(5);
}
void SPI_MasterInit(void)
{
DDRB=0Xef;
SPCR=0X53;
PORTB|=(1<
}
void SPI_Transmit(unsigned char data)
{
SPDR=data;
while(!(SPSR&(1<
delay(5);
}
unsigned char SPI_Receive(void)
{
SPDR=0Xff;
while(!(SPSR&(1<
return SPDR;
}
void en_write(void)
{
PORTB&=~(1<
SPI_Transmit(0x06);
PORTB|= (1<
}
void erasure_w25x16(void)
{
en_write();
PORTB&=~(1<
SPI_Transmit(0xc7);
PORTB|=(1<
delay(400);
}
void write_SPI(unsigned int address,unsigned char byte)
{
union data devide;
devide.dress=address;
erasure_w25x16();
PORTB&=~(1<
SPI_Transmit(0x02);
SPI_Transmit(devide.address[1]);
SPI_Transmit(devide.address[2]);
SPI_Transmit(devide.address[3]);
SPI_Transmit(byte);
PORTB|=(1<
}
unsigned char read_SPI(unsigned int address)
{
unsigned char byte;
union data devide;
devide.dress=address;
PORTB&=~(1<
SPI_Transmit(0x03);
SPI_Transmit(devide.address[1]);
SPI_Transmit(devide.address[2]);
SPI_Transmit(devide.address[3]);
byte=SPI_Receive();
PORTB|=(1<
return byte;
}
void main(void)
{
unsigned char byte=0;
SPI_MasterInit();
write_SPI(0x00fded,25);
delay(100);
byte=read_SPI(0x00fded);
while(1)
{
display(byte);
}
}
|
|