[Atmel] 用AtmelStudio6.2跑mega16例程(50)EEPROM

[复制链接]
1812|1
 楼主| ddllxxrr 发表于 2014-11-10 22:45 | 显示全部楼层 |阅读模式
当K1按下时,往EEPROM写入21个数。当k2按下时,向EEPROM地址不固定写21个数,k3是显示地址的内容。

EEPROM的编程一定要包含EEPROM.h 文件


Proteus的仿真结果。


Studio6.2的运行结果。


程序清单:

  1. /*
  2. * GccApplication13.c
  3. *
  4. * Created: 2014-11-10 21:26:08
  5. *  Author: Administrator
  6. */

  7. #define F_CPU 4000000UL
  8. #include <util/delay.h>
  9. #include <avr/eeprom.h>
  10. #include <avr/io.h>
  11. #include <stdlib.h>
  12. #include <stdint.h>

  13. #define Write_1_21_Key_DOWN() ((PINB & 0x01)==0x00)
  14. #define Write_Random_Key_DOWN() ((PINB & 0x08) == 0x00 )
  15. #define Loop_Show_Key_DOWN() ((PINB & 0x40)== 0x00)

  16. #define BEEP() (PORTD ^= 0x80)

  17. //将字节变量eepromx 分配于EEPROm存储器(地址不透明)
  18. uint8_t eepromx __attribute__((section("eeprom")));

  19. uint8_t eeprom_array[] EEMEM = {0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
  20. 0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f};

  21. const uint8_t SEG_CODE[] = {0x3f,0x06,0x58,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
  22. uint8_t Display_Buffer[] = {0,0};

  23. void Show_Count_ON_DSY()
  24. {       
  25.     PORTD = 0xFF;
  26.         PORTC = SEG_CODE[Display_Buffer[1]];
  27.         PORTD = 0xFE;
  28.         _delay_ms(2);
  29.         PORTD = 0xFF;
  30.         PORTC = SEG_CODE[Display_Buffer[0]];
  31.         PORTD = 0xFD;
  32.         _delay_ms(2);       
  33. }

  34. void Play_BEEP()
  35. {
  36.         uint16_t i;
  37.         for(i=0;i<300;i++)
  38.         {
  39.                 BEEP();_delay_us(200);
  40.         }
  41. }


  42. int main(void)
  43. {
  44.     uint8_t Current_Data,LOOP_SHOW_FLAG=0;
  45.         uint16_t i,Current_Read_Addr = 0x0001;
  46.         DDRC = 0xFF;PORTD = 0xFF;
  47.         DDRD = 0xFF;PORTD = 0xFF;
  48.         DDRB = 0x00; PORTB = 0xFF;
  49.         srand(200);
  50.         while(1)
  51.     {
  52.         start:
  53.                 if(Loop_Show_Key_DOWN())
  54.                 {
  55.                         Current_Read_Addr = 0x0001;
  56.                         eeprom_busy_wait();
  57.                         Current_Data = eeprom_read_byte((uint8_t*)Current_Read_Addr);
  58.                         if(Current_Data !=0xFF) LOOP_SHOW_FLAG=1;
  59.                         Play_BEEP();
  60.                         while(Loop_Show_Key_DOWN());
  61.                 }
  62.                 if(Write_1_21_Key_DOWN())
  63.                 {
  64.                         LOOP_SHOW_FLAG = 0;
  65.                         for(i=1;i<=20;i++)
  66.                         {
  67.                                 eeprom_busy_wait();
  68.                                 eeprom_write_byte((uint8_t *)i,(uint8_t)i);
  69.                         }
  70.                         eeprom_busy_wait();
  71.                         eeprom_write_byte(&eepromx,0x15);
  72.                         Play_BEEP();
  73.                         while(Write_1_21_Key_DOWN());
  74.                        
  75.                        
  76.                 }
  77.                
  78.                 if(Write_Random_Key_DOWN())
  79.                 {
  80.                         LOOP_SHOW_FLAG = 0;
  81.                         for(i=1;i<=20;i++)
  82.                         {
  83.                                 eeprom_busy_wait();
  84.                                 eeprom_write_byte((uint8_t*)i,rand()%100);
  85.                         }
  86.                         eeprom_busy_wait();
  87.                         eeprom_write_byte(&eepromx,rand());
  88.                        
  89.                 }
  90.                
  91.                 if(LOOP_SHOW_FLAG)
  92.                 {
  93.                         eeprom_busy_wait();
  94.                         if(Current_Read_Addr !=21)
  95.                         Current_Data = eeprom_read_byte((uint8_t*)Current_Read_Addr);
  96.                         else
  97.                         Current_Data = eeprom_read_byte(&eepromx);
  98.                         Display_Buffer[1] = Current_Data/10;
  99.                         Display_Buffer[0] = Current_Data % 10;
  100.                        
  101.                         for(i=0;i<160;i++)
  102.                         {
  103.                                 Show_Count_ON_DSY();
  104.                                 if(Write_1_21_Key_DOWN() || Write_Random_Key_DOWN())
  105.                                 {
  106.                                        
  107.                                         LOOP_SHOW_FLAG = 0;
  108.                                         PORTD = 0xFF;
  109.                                         goto start;
  110.                                 }
  111.                         }
  112.                         Current_Read_Addr = Current_Read_Addr %21 +1;
  113.                 }
  114.                
  115.                 //TODO:: Please write your application code
  116.     }
  117. }



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
cdwess 发表于 2015-4-16 23:32 | 显示全部楼层
这ATmel的库真不好用啊,还不如直接寄存器操作
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2404

主题

7001

帖子

68

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