ATmega16讀寫EEPROM AT25256(ICC編譯)程序
#include
#define BAUD 9600 //波特率4800
#define CRYSTAL 3686400 //晶振3M
#define BAUD_set (unsigned int)( (unsigned long)CRYSTAL/(16*(unsigned long)BAUD)-1 ) //波特率計(jì)算
#define BAUD_H (unsigned char)(BAUD_set>>8) //波特率寄存器
#define BAUD_L (unsigned char)(BAUD_set)
/* //25256 #define SPIF 7 /* ////////////////////////USRT1,USRT2 end/////////////////////// unsigned char SPI_MasterTransmit(unsigned char cData) //EEPROM寫使能 //EEPROM寫禁止 //寫狀態(tài)寄存器 /* //25256字節(jié)寫 //25256頁寫。試驗(yàn)結(jié)果,只能連續(xù)寫入三個(gè)字節(jié) //25256讀
//GPRS
#define GPRS_ONOFFH PORTA|=(1<
//通訊燈
#defineLED1H PORTC|=(1<
#define EEPROM_CSH PORTD|=(1<
//////////////////////USRT0,USTR1 begin//////////////////////////
/////////////////////////////////////串口begin///////////////////////////////////////////
//串口0初始化
void USART0_Init()
{
UCSR0B = (1 << RXCIE0) | /*接收完成中斷允許*/
// (1 << TXCIE0) | /*發(fā)送完成中斷允許*/
(1 << RXEN0) | /*接收允許*/
(1 << TXEN0); /*發(fā)送允許*/
UBRR0H = BAUD_H; //設(shè)置波特率
UBRR0L = BAUD_L;
UCSR0C = (1 << URSEL0) | /*選擇訪問UCSRC寄存器*/
(3 << UCSZ00); /*UCSZ2 UCSZ1 UCSZ0=3 8個(gè)數(shù)據(jù)位*/
}
//串口1初始化
void USART1_Init()
{
UCSR1B = (1 << RXCIE1) | /*接收完成中斷允許*/
// (1 << TXCIE1) | /*發(fā)送完成中斷允許*/
(1 << RXEN0) | /*接收允許*/
(1 << TXEN0); /*發(fā)送允許*/
UBRR1H = BAUD_H; //設(shè)置波特率
UBRR1L = BAUD_L;
UCSR1C = (1 << URSEL1) | /*選擇訪問UCSRC寄存器*/
(3 << UCSZ10); /*UCSZ2 UCSZ1 UCSZ0=3 8個(gè)數(shù)據(jù)位*/
}
//串口0查詢方式發(fā)送數(shù)據(jù)
void USART0_Transmit(unsigned char data)
{
while(!(UCSR0A&(1<
當(dāng)發(fā)送緩沖器空時(shí)被置1;發(fā)送緩沖器包含需要發(fā)送的數(shù)據(jù)時(shí)清零*/
UDR0=data; //將數(shù)據(jù)放入緩沖器,發(fā)送數(shù)據(jù)
}
//串口1查詢方式發(fā)送數(shù)據(jù)
void USART1_Transmit(unsigned char data)
{
while(!(UCSR1A&(1<
當(dāng)發(fā)送緩沖器空時(shí)被置1;發(fā)送緩沖器包含需要發(fā)送的數(shù)據(jù)時(shí)清零*/
UDR1=data; //將數(shù)據(jù)放入緩沖器,發(fā)送數(shù)據(jù)
}
//發(fā)送GPRS命令給手機(jī)模塊
void USART1_Tx_Str(char *s,unsigned char cnt)
{
while(cnt--)
{
USART1_Transmit(*s);
s++;
}
}
*/
/////////////////////////////////////////////串口end/////////////////////////////////////
/*
unsigned char usart0_receive(void)
{
while(!(UCSR0A&(1<
}
*/
///////////////////////////////////////////GPRS begin////////////////////////////////////
//延時(shí)ms
/*
void delayms(unsigned int ms)
{
unsigned int i,j;
for(i=0;i
}
//GPRS開機(jī)
void GPRS_On()
{
GPRS_RSTH; GPRS_ONOFFH; delayms(20);
GPRS_RSTL; delayms(10);
GPRS_ONOFFL;LED1L;LED2L;LED3L;delayms(1800);
GPRS_ONOFFH;LED1H;LED2H;LED3H;delayms(200);
LED1L;LED2L;
}
//GPRS關(guān)機(jī)
void GPRS_Off()
{
GPRS_ONOFFL;delayms(1800);
GPRS_ONOFFH;delayms(5000);
GPRS_RSTL;
}
//GPRS復(fù)位
void GPRS_Rst()
{
GPRS_Off();
GPRS_On();
}
*/
////////////////////////////////////////GPRS end/////////////////////////////////////////
////////////////////////////////////eeprom begin//////////////////////////////////////
void SPI_MasterInit(void)
{
EEPROM_WPH;
delayms(20);
SPCR =(1<
使能SPI,選擇主機(jī)模式,SCK=fosc/128,選擇SPI模式CPOL=0,CPHA=0
*/
}
{
//啟動(dòng)數(shù)據(jù)傳輸
SPDR = cData;
//等待數(shù)據(jù)傳輸結(jié)束
while(!(SPSR&(1<
// SPSR&=~(1<
//每次發(fā)送數(shù)據(jù)的同時(shí),也會(huì)接收到數(shù)據(jù)。
}
void EEPROM_WREN()
{
EEPROM_CSL; //拉低片選端
SPI_MasterTransmit(0x06); //0x06為寫使能的指令
EEPROM_CSH; //抬高片選端,結(jié)束
}
void EEPROM_WRDI()
{
EEPROM_CSL;//delayms(20);
SPI_MasterTransmit(0x04); //0x04為寫禁止的指令
EEPROM_CSH;//delayms(20);
}
void EEPROM_WRSR()
{
EEPROM_WREN();
EEPROM_CSL; //使能
SPI_MasterTransmit(0x01); //0x01為寫狀態(tài)寄存器的指令
SPI_MasterTransmit(0x82); //把0x82寫入狀態(tài)寄存器
EEPROM_CSH; //抬高片選,結(jié)束
}
讀狀態(tài)寄存器。讀SPI從機(jī)數(shù)據(jù)時(shí),主機(jī)要給從機(jī)發(fā)送任意數(shù)據(jù),才能收到從機(jī)返回的數(shù)據(jù).
即0x00可以是任何數(shù)據(jù)(但是不能是25256的指令,例如0x01,0x04之類的)
*/
unsigned char EEPROM_RDSR()
{
unsigned char StatusReg;
EEPROM_WREN();
// EEPROM_WRDI(); //如使用此句,不使用EEPROM_WREN(),則讀出0x80
EEPROM_CSL; //使能
SPI_MasterTransmit(0x05); //0x05為讀狀態(tài)寄存器指令
StatusReg=SPI_MasterTransmit(0x00); //給25256發(fā)送任意數(shù)據(jù)
/*
此處原為:
SPDR = 0x00;
while(!(SPSR&(1<
時(shí)出錯(cuò),讀出數(shù)據(jù)都為0xff。想不清楚為什么
*/
EEPROM_CSH;
return StatusReg;
}
void EEPROM_ByteWrite(unsigned int Address,unsigned char Data)
{
EEPROM_WREN();
EEPROM_CSL; //使能
SPI_MasterTransmit(0x02); //0x02為寫指令
SPI_MasterTransmit(Address/256); //地址高位
SPI_MasterTransmit(Address%256); //地址低位
SPI_MasterTransmit(Data); //寫入數(shù)據(jù)
EEPROM_CSH;delayms(50);
}
void EEPROM_PageWrite(unsigned int Address,unsigned char *Data,unsigned char NData)
{
unsigned char i;
EEPROM_WREN();
EEPROM_CSL;
SPI_MasterTransmit(0x02);//頁寫指令
SPI_MasterTransmit(Address/256); //地址高位
SPI_MasterTransmit(Address%256); //地址低位
for(i=0;i
SPI_MasterTransmit(*Data); //寫入數(shù)據(jù)
Data++;
}
EEPROM_CSH;
}
unsigned char EEPROM_Read(unsigned int Address)
{
unsigned char dataa;
EEPROM_CSL; //使能
SPI_MasterTransmit(0x03); //0x03為讀25256的指令
SPI_MasterTransmit(Address/256); //地址高位
SPI_MasterTransmit(Address%256); //地址低位
dataa=SPI_MasterTransmit(0x00);//發(fā)送任意數(shù)據(jù)
EEPROM_CSH;
return dataa; //返回讀到的數(shù)據(jù)
}
////////////////////////////////////eeprom end////////////////////////////////////////
//端口初始化
void Port_Init()
{
PORTA=0xff;
DDRA=0x30; //PA4,PA5輸出1
PORTB=0xbf;
DDRB=0xb7; //PB1輸出1,PB2輸出1,PB3輸入0,PB4輸出1,PB0輸出1(用于新終端開關(guān)電源控制),PB5PB7為輸出1
PORTC=0xff;
DDRC=0xd0; //PC4輸出1,PC6,PC7輸出1
PORTD=0xff;
DDRD=0x26; //PD0輸入0,PD1輸出1,PD5PD2輸出1
}
void main(void)
{
// unsigned char dataa,i,j;
SREG=0x80; //開放全局中斷
Port_Init();
USART0_Init();
USART1_Init();
// GPRS_On();
SPI_MasterInit();EEPROM_WRSR();
while(1)
{
USART0_Transmit(0x22);
USART0_Transmit(0x33);
EEPROM_ByteWrite(0x2030,0x48); //給25256地址0x2030寫入數(shù)據(jù)0x48
USART0_Transmit(EEPROM_Read(0x2010));
USART0_Transmit(EEPROM_Read(0x2030));//讀出地址0x2030中數(shù)據(jù),從串口0發(fā)送出來
USART0_Transmit(0x44);
USART0_Transmit(0x55);
USART0_Transmit(EEPROM_RDSR());
EEPROM_PageWrite(0x1001,"456789",6); //輸出結(jié)果0xff,0xff,0xff,0x37,0x38,0x39
USART0_Transmit(EEPROM_Read(0x1001));
USART0_Transmit(EEPROM_Read(0x1002));
USART0_Transmit(EEPROM_Read(0x1003));
USART0_Transmit(EEPROM_Read(0x1004));
USART0_Transmit(EEPROM_Read(0x1005));
USART0_Transmit(EEPROM_Read(0x1006));
USART0_Transmit(0x11);
}
}