#include<iom16v.h>
#include<macros.h>
#define data_led_0 PORTB&=~(1<<PB6)
#define data_led_1 PORTB|= (1<<PB6)
#define chip_led_0 PORTB&=~(1<<PB7)
#define chip_led_1 PORTB|= (1<<PB7)
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<<PB6)|(1<<PB7);
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<<PB2);
}
void SPI_Transmit(unsigned char data)
{
SPDR=data;
while(!(SPSR&(1<<SPIF)));
delay(5);
}
unsigned char SPI_Receive(void)
{
SPDR=0Xff;
while(!(SPSR&(1<<SPIF)));
return SPDR;
}
void en_write(void)
{
PORTB&=~(1<<PB2);
SPI_Transmit(0x06);
PORTB|= (1<<PB2);
}
void erasure_w25x16(void)
{
en_write();
PORTB&=~(1<<PB2);
SPI_Transmit(0xc7);
PORTB|=(1<<PB2);
delay(400);
}
void write_SPI(unsigned int address,unsigned char byte)
{
union data devide;
devide.dress=address;
erasure_w25x16();
PORTB&=~(1<<PB2);
SPI_Transmit(0x02);
SPI_Transmit(devide.address[1]);
SPI_Transmit(devide.address[2]);
SPI_Transmit(devide.address[3]);
SPI_Transmit(byte);
PORTB|=(1<<PB2);
}
unsigned char read_SPI(unsigned int address)
{
unsigned char byte;
union data devide;
devide.dress=address;
PORTB&=~(1<<PB2);
SPI_Transmit(0x03);
SPI_Transmit(devide.address[1]);
SPI_Transmit(devide.address[2]);
SPI_Transmit(devide.address[3]);
byte=SPI_Receive();
PORTB|=(1<<PB2);
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);
}
}
此帖出自
小平头技术问答
#include<macros.h>
#define data_led_0 PORTB&=~(1<<PB6)
#define data_led_1 PORTB|= (1<<PB6)
#define chip_led_0 PORTB&=~(1<<PB7)
#define chip_led_1 PORTB|= (1<<PB7)
unsigned char const table[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x67};
void delay(int ms)
{
unsigned char j;
for(;ms>0;ms--)
{
for(j=0;j<150;j++);
}
}
void display(unsigned char byte)
{
DDRD=0XFF;
DDRB|=(1<<PB6)|(1<<PB7);
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=0X5c;
PORTB|=(1<<PB2);
}
void SPI_Transmit(unsigned char data)
{
SPDR=data;
while(!(SPSR&(1<<SPIF)));
}
unsigned char SPI_Receive(void)
{
SPDR=0Xff;
while(!(SPSR&(1<<SPIF)));
return SPDR;
}
void wait_status(void)
{
unsigned char status;
PORTB&=~(1<<PB2);
SPI_Transmit(0x05);
do{
status=SPI_Receive();
}while(status&0x01);
PORTB|= (1<<PB2);
}
void en_write(void)
{
PORTB&=~(1<<PB2);
SPI_Transmit(0x06);
PORTB|= (1<<PB2);
}
void erasure_w25x16(void)
{
en_write();
PORTB&=~(1<<PB2);
SPI_Transmit(0xc7);
PORTB|=(1<<PB2);
wait_status();
}
void write_SPI(unsigned long address,unsigned char byte)
{
erasure_w25x16();
en_write();
PORTB&=~(1<<PB2);
SPI_Transmit(0x02);
SPI_Transmit((address&0xff0000)>>16);
SPI_Transmit((address&0x00ff00)>>8);
SPI_Transmit(address&0x0000ff);
SPI_Transmit(byte);
PORTB|=(1<<PB2);
wait_status();
}
unsigned char read_SPI(unsigned int address)
{
unsigned char byte;
PORTB&=~(1<<PB2);
SPI_Transmit(0x03);
SPI_Transmit((address&0xff0000)>>16);
SPI_Transmit((address&0x00ff00)>>8);
SPI_Transmit(address&0x0000ff);
byte=SPI_Receive();
PORTB|=(1<<PB2);
return byte;
}
void main(void)
{
unsigned char byte=0;
SPI_MasterInit();
write_SPI(0x00fded,47);
byte=read_SPI(0x00fded);
while(1)
{
byte=read_SPI(0x00fded);
display(byte);
}
}终于成功了,分享一下
一周热门 更多>