比神乐 发表于 2024-11-30 21:04

【AT-START-L021测评】+25LC040读写

代码:
#include "at32l021_board.h"
#include "at32l021_clock.h"
#include "OLED.h"
/** @addtogroup AT32L021_periph_examples
* @{
*/
#define uint unsigned int
#define u8unsigned char

#define READ_INSTRUCTION0x03 // ???(??25LC040,????????????)
#define WRITE_INSTRUCTION 0x02 // ???(??,??????????)
#define WREN_INSTRUCTION0x06 // ?????

unsigned char readData,readData1,readData2;

void _nop_(void)
{
        int i;
        for(i=0;i<20;i++);
}
void SPI_Init(void) {
    // ???SPI??,????????
}

void SPI_SendByte(unsigned char byte) {
    // ??SPI??????
                u8 i;
                for(i=0;i<8;i++)
                {
                        //SCK=0;
                        at32_lc040_off(SCK1);
                        _nop_();
                        _nop_();
                        if((byte&0x80)==0x80)
                                at32_lc040_on(SI);
                        else
                                at32_lc040_off(SI);
                        _nop_();
                        _nop_();
                        byte<<=1;
                        //SCK=1;
                        at32_lc040_on(SCK1);
                        _nop_();
                        _nop_();
                }
}
u8 SPI_SendByte1(unsigned char byte) {
    // ??SPI??????
                u8 i,date=0;
                for(i=0;i<8;i++)
                {
                        date<<=1;
                        //SCK=0;
                        at32_lc040_off(SCK1);
                        _nop_();
                        _nop_();
                        if((byte&0x80)==0x80)
                                at32_lc040_on(SI);
                        else
                                at32_lc040_off(SI);
                        _nop_();
                        _nop_();
                        byte<<=1;
                        //SCK=1;
                        at32_lc040_on(SCK1);
                        _nop_();
                        _nop_();
                        //SO=1;
                        if(at32_soin_state())
                                date|=1;
                }
                return date;
}

unsigned char SPI_ReceiveByte(void) {
    // ??SPI??????
                u8 i,dat=0;
        for(i=0;i<8;i++)
        {
                dat<<=1;
                //SCK=1;                        //第一位
                at32_lc040_on(SCK1);
                _nop_();
                        _nop_();
        //SCK=0;
                at32_lc040_off(SCK1);
                _nop_();
                        _nop_();
                //SO=1;
                if(at32_soin_state())
                        dat|=1;
               
                _nop_();
                        _nop_();
               
        }
               
               
    return dat; // ??????????????
}

void SPI_CS_Low(void) {
    // ???????
    //CS = 0; // ?????????P1.0??
                at32_lc040_off(CS1);
}

void SPI_CS_High(void) {
    // ???????
    //CS = 1; // ?????????P1.0??
                at32_lc040_on(CS1);
}


// ????
void EEPROM_WriteByte(unsigned char address, unsigned char date) {
    SPI_CS_Low(); // ??????
    SPI_SendByte(WREN_INSTRUCTION); // ???????
    SPI_CS_High(); // ??????,???????(??????)
      
    SPI_CS_Low(); // ????????
    SPI_SendByte(WRITE_INSTRUCTION); // ?????
    //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)
    SPI_SendByte(address); // ???????
    SPI_SendByte(date); // ????
    SPI_CS_High(); // ??????,?????
      
    // ???????????,?????EEPROM????
}

unsigned char EEPROM_ReadByte(unsigned char address) {
    unsigned char date;
      
    SPI_CS_Low(); // ??????
    SPI_SendByte(READ_INSTRUCTION); // ?????
    //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)
    SPI_SendByte(address ); // ???????
    date = SPI_SendByte1(0 ); // ????
    SPI_CS_High(); // ??????,?????
      
    return date;
}

void EEPROM_ReadByte1(unsigned char address) {
    //unsigned char date;
      
    SPI_CS_Low(); // ??????
    SPI_SendByte(READ_INSTRUCTION); // ?????
    //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)
    SPI_SendByte(address ); // ???????
    readData = SPI_ReceiveByte(); // ????
                readData1 = SPI_ReceiveByte();
       readData2 = SPI_ReceiveByte();
    SPI_CS_High(); // ??????,?????
      
   // return date;
}

void MS_delay(uint MS)
{
    uint x,y;
    for(y=MS;y>0;y--)
    {
            for(x=796;x>0;x--);
    }
}
/** @addtogroup 021_GPIO_led_toggle GPIO_led_toggle
* @{
*/

/**
* @briefmain function.
* @paramnone
* @retval none
*/
int main(void)
{
        u8 bai,shi,ge;
uint i=0;
        system_clock_config();

at32_board_init();
        LCD_Init();
LCD_CLS();
        //LCD_P8x16Str(45,5,(u8*)"EASEAR");
        //LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
        //Draw_BMP(0,0,100,3,(u8*)Dot);
        SPI_Init(); // ???SPI??
      
    // ??:?????EEPROM
    EEPROM_WriteByte(0x00, 169); // ???0x0000??0xAB
                for(i=0;i<30000;i++);
    EEPROM_WriteByte(0x01, 208); // ???0x0000??0xAB
                for(i=0;i<30000;i++);
                EEPROM_WriteByte(0x02, 135); // ???0x0000??0xAB
                for(i=0;i<30000;i++);
                readData=EEPROM_ReadByte(0); // ???0x0000????
    bai= readData/100+'0';
                shi=readData%100/10+'0';
                ge=readData%10+'0';
                LCD_P8x16Char(0,0,bai);
                LCD_P8x16Char(8,0,shi);
                LCD_P8x16Char(16,0,ge);
                readData1 = EEPROM_ReadByte(0x01); // ???0x0000????
    bai= readData1/100+'0';
                shi=readData1%100/10+'0';
                ge=readData1%10+'0';
                LCD_P8x16Char(0,2,bai);
                LCD_P8x16Char(8,2,shi);
                LCD_P8x16Char(16,2,ge);
                        _nop_();
                readData2 = EEPROM_ReadByte(0x02); // ???0x0000????
                bai= readData2/100+'0';
                shi=readData2%100/10+'0';
                ge=readData2%10+'0';
                LCD_P8x16Char(0,4,bai);
                LCD_P8x16Char(8,4,shi);
                LCD_P8x16Char(16,4,ge);
while(1)
{
    at32_led_toggle(LED2);
//                at32_oled_on(SCL);
//                at32_oled_on(SDA);
//                at32_oled_on(RST);
//                at32_oled_on(DC);
//    delay_ms(10);
//                at32_oled_off(SCL);
//                at32_oled_off(SDA);
//                at32_oled_off(RST);
//                at32_oled_off(DC);
                delay_ms(1000);
    //at32_led_toggle(LED3);
    //delay_ms(200);
    ///at32_led_toggle(LED4);
    //delay_ms(200);
}
}
运行效果图:

页: [1]
查看完整版本: 【AT-START-L021测评】+25LC040读写