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

[复制链接]
1494|0
 楼主| 比神乐 发表于 2024-11-30 21:04 | 显示全部楼层 |阅读模式
代码:
  1. #include "at32l021_board.h"
  2. #include "at32l021_clock.h"
  3. #include "OLED.h"
  4. /** @addtogroup AT32L021_periph_examples
  5.   * @{
  6.   */
  7. #define uint unsigned int
  8. #define u8  unsigned char

  9. #define READ_INSTRUCTION  0x03 // ???(??25LC040,????????????)  
  10. #define WRITE_INSTRUCTION 0x02 // ???(??,??????????)  
  11. #define WREN_INSTRUCTION  0x06 // ?????  

  12. unsigned char readData,readData1,readData2;

  13. void _nop_(void)
  14. {
  15.         int i;
  16.         for(i=0;i<20;i++);
  17. }
  18. void SPI_Init(void) {  
  19.     // ???SPI??,????????  
  20. }  
  21.   
  22. void SPI_SendByte(unsigned char byte) {  
  23.     // ??SPI??????
  24.                 u8 i;
  25.                 for(i=0;i<8;i++)
  26.                 {
  27.                         //SCK=0;
  28.                         at32_lc040_off(SCK1);
  29.                         _nop_();
  30.                         _nop_();
  31.                         if((byte&0x80)==0x80)
  32.                                 at32_lc040_on(SI);
  33.                         else
  34.                                 at32_lc040_off(SI);
  35.                         _nop_();
  36.                         _nop_();
  37.                         byte<<=1;
  38.                         //SCK=1;
  39.                         at32_lc040_on(SCK1);
  40.                         _nop_();
  41.                         _nop_();
  42.                 }
  43. }  
  44. u8 SPI_SendByte1(unsigned char byte) {  
  45.     // ??SPI??????
  46.                 u8 i,date=0;
  47.                 for(i=0;i<8;i++)
  48.                 {
  49.                         date<<=1;
  50.                         //SCK=0;
  51.                         at32_lc040_off(SCK1);
  52.                         _nop_();
  53.                         _nop_();
  54.                         if((byte&0x80)==0x80)
  55.                                 at32_lc040_on(SI);
  56.                         else
  57.                                 at32_lc040_off(SI);
  58.                         _nop_();
  59.                         _nop_();
  60.                         byte<<=1;
  61.                         //SCK=1;
  62.                         at32_lc040_on(SCK1);
  63.                         _nop_();
  64.                         _nop_();
  65.                         //SO=1;
  66.                         if(at32_soin_state())
  67.                                 date|=1;
  68.                 }
  69.                 return date;
  70. }  
  71.   
  72. unsigned char SPI_ReceiveByte(void) {  
  73.     // ??SPI??????  
  74.                 u8 i,dat=0;
  75.         for(i=0;i<8;i++)
  76.         {
  77.                 dat<<=1;
  78.                 //SCK=1;                        //第一位
  79.                 at32_lc040_on(SCK1);
  80.                 _nop_();
  81.                         _nop_();
  82.         //SCK=0;
  83.                 at32_lc040_off(SCK1);
  84.                 _nop_();
  85.                         _nop_();
  86.                 //SO=1;
  87.                 if(at32_soin_state())
  88.                         dat|=1;
  89.                
  90.                 _nop_();
  91.                         _nop_();
  92.                
  93.         }
  94.                
  95.                
  96.     return dat; // ??????????????  
  97. }  
  98.   
  99. void SPI_CS_Low(void) {  
  100.     // ???????  
  101.     //CS = 0; // ?????????P1.0??
  102.                 at32_lc040_off(CS1);
  103. }  
  104.   
  105. void SPI_CS_High(void) {  
  106.     // ???????  
  107.     //CS = 1; // ?????????P1.0??
  108.                 at32_lc040_on(CS1);
  109. }


  110. // ????  
  111. void EEPROM_WriteByte(unsigned char address, unsigned char date) {  
  112.     SPI_CS_Low(); // ??????  
  113.     SPI_SendByte(WREN_INSTRUCTION); // ???????  
  114.     SPI_CS_High(); // ??????,???????(??????)  
  115.       
  116.     SPI_CS_Low(); // ????????  
  117.     SPI_SendByte(WRITE_INSTRUCTION); // ?????  
  118.     //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)  
  119.     SPI_SendByte(address); // ???????  
  120.     SPI_SendByte(date); // ????  
  121.     SPI_CS_High(); // ??????,?????  
  122.       
  123.     // ???????????,?????EEPROM????  
  124. }  
  125.   
  126. unsigned char EEPROM_ReadByte(unsigned char address) {  
  127.     unsigned char date;  
  128.       
  129.     SPI_CS_Low(); // ??????  
  130.     SPI_SendByte(READ_INSTRUCTION); // ?????  
  131.     //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)  
  132.     SPI_SendByte(address ); // ???????  
  133.     date = SPI_SendByte1(0 ); // ????  
  134.     SPI_CS_High(); // ??????,?????  
  135.       
  136.     return date;  
  137. }  

  138. void EEPROM_ReadByte1(unsigned char address) {  
  139.     //unsigned char date;  
  140.       
  141.     SPI_CS_Low(); // ??????  
  142.     SPI_SendByte(READ_INSTRUCTION); // ?????  
  143.     //SPI_SendByte((address >> 8) & 0xFF); // ???????(??25LC040,?????)  
  144.     SPI_SendByte(address ); // ???????  
  145.     readData = SPI_ReceiveByte(); // ????
  146.                 readData1 = SPI_ReceiveByte();
  147.          readData2 = SPI_ReceiveByte();
  148.     SPI_CS_High(); // ??????,?????  
  149.       
  150.    // return date;  
  151. }  
  152.   
  153. void MS_delay(uint MS)
  154. {
  155.     uint x,y;
  156.     for(y=MS;y>0;y--)
  157.     {
  158.             for(x=796;x>0;x--);
  159.     }
  160. }
  161. /** @addtogroup 021_GPIO_led_toggle GPIO_led_toggle
  162.   * @{
  163.   */

  164. /**
  165.   * [url=home.php?mod=space&uid=247401]@brief[/url]  main function.
  166.   * @param  none
  167.   * @retval none
  168.   */
  169. int main(void)
  170. {
  171.         u8 bai,shi,ge;
  172.   uint i=0;
  173.         system_clock_config();

  174.   at32_board_init();
  175.         LCD_Init();
  176.   LCD_CLS();
  177.         //LCD_P8x16Str(45,5,(u8*)"EASEAR");
  178.         //LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
  179.         //Draw_BMP(0,0,100,3,(u8*)Dot);
  180.         SPI_Init(); // ???SPI??  
  181.       
  182.     // ??:?????EEPROM  
  183.     EEPROM_WriteByte(0x00, 169); // ???0x0000??0xAB
  184.                 for(i=0;i<30000;i++);
  185.     EEPROM_WriteByte(0x01, 208); // ???0x0000??0xAB
  186.                 for(i=0;i<30000;i++);
  187.                 EEPROM_WriteByte(0x02, 135); // ???0x0000??0xAB
  188.                 for(i=0;i<30000;i++);
  189.                 readData=EEPROM_ReadByte(0); // ???0x0000????  
  190.     bai= readData/100+'0';
  191.                 shi=readData%100/10+'0';
  192.                 ge=readData%10+'0';
  193.                 LCD_P8x16Char(0,0,bai);
  194.                 LCD_P8x16Char(8,0,shi);
  195.                 LCD_P8x16Char(16,0,ge);
  196.                 readData1 = EEPROM_ReadByte(0x01); // ???0x0000????  
  197.     bai= readData1/100+'0';
  198.                 shi=readData1%100/10+'0';
  199.                 ge=readData1%10+'0';
  200.                 LCD_P8x16Char(0,2,bai);
  201.                 LCD_P8x16Char(8,2,shi);
  202.                 LCD_P8x16Char(16,2,ge);
  203.                         _nop_();
  204.                 readData2 = EEPROM_ReadByte(0x02); // ???0x0000????  
  205.                 bai= readData2/100+'0';
  206.                 shi=readData2%100/10+'0';
  207.                 ge=readData2%10+'0';
  208.                 LCD_P8x16Char(0,4,bai);
  209.                 LCD_P8x16Char(8,4,shi);
  210.                 LCD_P8x16Char(16,4,ge);
  211.   while(1)
  212.   {
  213.     at32_led_toggle(LED2);
  214. //                at32_oled_on(SCL);
  215. //                at32_oled_on(SDA);
  216. //                at32_oled_on(RST);
  217. //                at32_oled_on(DC);
  218. //    delay_ms(10);
  219. //                at32_oled_off(SCL);
  220. //                at32_oled_off(SDA);
  221. //                at32_oled_off(RST);
  222. //                at32_oled_off(DC);
  223.                 delay_ms(1000);
  224.     //at32_led_toggle(LED3);
  225.     //delay_ms(200);
  226.     ///at32_led_toggle(LED4);
  227.     //delay_ms(200);
  228.   }
  229. }
运行效果图:
1.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部