当K1按下时,往EEPROM写入21个数。当k2按下时,向EEPROM地址不固定写21个数,k3是显示地址的内容。
EEPROM的编程一定要包含EEPROM.h 文件
Proteus的仿真结果。
Studio6.2的运行结果。
程序清单:
/*
* GccApplication13.c
*
* Created: 2014-11-10 21:26:08
* Author: Administrator
*/
#define F_CPU 4000000UL
#include <util/delay.h>
#include <avr/eeprom.h>
#include <avr/io.h>
#include <stdlib.h>
#include <stdint.h>
#define Write_1_21_Key_DOWN() ((PINB & 0x01)==0x00)
#define Write_Random_Key_DOWN() ((PINB & 0x08) == 0x00 )
#define Loop_Show_Key_DOWN() ((PINB & 0x40)== 0x00)
#define BEEP() (PORTD ^= 0x80)
//将字节变量eepromx 分配于EEPROm存储器(地址不透明)
uint8_t eepromx __attribute__((section("eeprom")));
uint8_t eeprom_array[] EEMEM = {0x0a,0x0b,0x0c,0x0d,0x0e,0x0f,0x1a,0x1b,0x1c,0x1d,0x1e,0x1f,
0x2a,0x2b,0x2c,0x2d,0x2e,0x2f,0x2a,0x2b,0x2c,0x2d,0x2e,0x2f};
const uint8_t SEG_CODE[] = {0x3f,0x06,0x58,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
uint8_t Display_Buffer[] = {0,0};
void Show_Count_ON_DSY()
{
PORTD = 0xFF;
PORTC = SEG_CODE[Display_Buffer[1]];
PORTD = 0xFE;
_delay_ms(2);
PORTD = 0xFF;
PORTC = SEG_CODE[Display_Buffer[0]];
PORTD = 0xFD;
_delay_ms(2);
}
void Play_BEEP()
{
uint16_t i;
for(i=0;i<300;i++)
{
BEEP();_delay_us(200);
}
}
int main(void)
{
uint8_t Current_Data,LOOP_SHOW_FLAG=0;
uint16_t i,Current_Read_Addr = 0x0001;
DDRC = 0xFF;PORTD = 0xFF;
DDRD = 0xFF;PORTD = 0xFF;
DDRB = 0x00; PORTB = 0xFF;
srand(200);
while(1)
{
start:
if(Loop_Show_Key_DOWN())
{
Current_Read_Addr = 0x0001;
eeprom_busy_wait();
Current_Data = eeprom_read_byte((uint8_t*)Current_Read_Addr);
if(Current_Data !=0xFF) LOOP_SHOW_FLAG=1;
Play_BEEP();
while(Loop_Show_Key_DOWN());
}
if(Write_1_21_Key_DOWN())
{
LOOP_SHOW_FLAG = 0;
for(i=1;i<=20;i++)
{
eeprom_busy_wait();
eeprom_write_byte((uint8_t *)i,(uint8_t)i);
}
eeprom_busy_wait();
eeprom_write_byte(&eepromx,0x15);
Play_BEEP();
while(Write_1_21_Key_DOWN());
}
if(Write_Random_Key_DOWN())
{
LOOP_SHOW_FLAG = 0;
for(i=1;i<=20;i++)
{
eeprom_busy_wait();
eeprom_write_byte((uint8_t*)i,rand()%100);
}
eeprom_busy_wait();
eeprom_write_byte(&eepromx,rand());
}
if(LOOP_SHOW_FLAG)
{
eeprom_busy_wait();
if(Current_Read_Addr !=21)
Current_Data = eeprom_read_byte((uint8_t*)Current_Read_Addr);
else
Current_Data = eeprom_read_byte(&eepromx);
Display_Buffer[1] = Current_Data/10;
Display_Buffer[0] = Current_Data % 10;
for(i=0;i<160;i++)
{
Show_Count_ON_DSY();
if(Write_1_21_Key_DOWN() || Write_Random_Key_DOWN())
{
LOOP_SHOW_FLAG = 0;
PORTD = 0xFF;
goto start;
}
}
Current_Read_Addr = Current_Read_Addr %21 +1;
}
//TODO:: Please write your application code
}
}
|