BASIC4MCU | 질문게시판 | atmega128 내부 EEPROM 사용하기
페이지 정보
작성자 전자학습자 작성일2020-01-29 11:37 조회5,816회 댓글0건본문
안녕하세요. 설 명절 잘 보내셨는지요
선생님께서 도와주신대로 테스트를 해보았습니다.
제가 원했던 동작대로 작동했습니다.
새로생긴 궁금증을 질문드립니다.
아래는 선생님 도움을 받아 제가 써본 코딩입니다.
시간 T1, T2, T3을 설정하여 T1은 LED1, T2는 LED2, T3는 LED3을 각각
설정된 시간만큼 순차적으로 점등시킵니다.
버튼 PINE&1, PINE&2, PINE&4, PINE&8 이있고
LED PORTB=1 , PORTB=2, PORTB=4 가 있습니다.
버튼 PINE&1을 1회 누르면 T1의 값을 셋팅하는 상태가 되고 이상태에서 PINE&2를 누르면 0.5초씩 증가 PINE&4를 누르면 0.5초씩 감소
버튼 PINE&1을 2회 누르면 T2를, 3회누르면 T3의 시간을 조절할수있습니다.
4회누르면 시간조절을 하지않고 대기합니다.
질문 1. T1,T2,T3 각각 조절한 시간을 전원을 껏다켜도 그대로 유지하기위해
atmega128a 내부 EEPROM을 사용해서 저장하려고 아래코딩에 빨간색으로 표시한부분을 넣어주었습니다.
하지만 전원을 껏다 켜도 조절했던 값이 저장이안되고 0이됩니다.
aaa,bbb,ccc 변수를 전역변수로 선언해서 그런것일까요?? 전역변수는 초기화하지않아도 0으로 자동 초기화된다고 배웠습니다.
그렇다면 어떻게 처리해야할까요??
질문 2. LED는 PINE&8 이 눌러져야 켜지게되어있는데 PINE&2를 연속으로 누르다보면 누를때마다 LED1이 깜빡 깜빡하는 현상이있습니다.
이건 하드웨어적인 문제일까요?
#define F_CPU 16000000L
#include <avr/io.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/eeprom.h>
int aaa,bbb, ccc, count;
ISR (TIMER0_OVF_vect)
{
static int sec=99;
if ((PINE&8)==0)
{
count=0;
sec=0;
}
if (++count>=100)
{
count=0;
if(sec<aaa+bbb+ccc)sec++;
}
if (sec<aaa)
{
PORTB=1;
}
else if (sec<aaa+bbb)
{
PORTB=2;
}
else if (sec<aaa+bbb+ccc)
{
PORTB=4;
}
else PORTB=0;
}
int main(void)
{
int d=0;
char aa[10] = "";
char bb[10] = "";
char cc[10] = "";
float a = atof(aa);
float b = atof(bb);
float c = atof(cc);
int addressA=20, addressB=50, addressC=80;
eeprom_update_byte ((uint8_t*)addressA,aaa);
eeprom_update_byte ((uint8_t*)addressB,bbb);
eeprom_update_byte ((uint8_t*)addressC,ccc);
aaa=eeprom_read_byte ((uint8_t*)addressA);
bbb=eeprom_read_byte ((uint8_t*)addressB);
ccc=eeprom_read_byte ((uint8_t*)addressC);
LCD_init();
LCD_goto_XY(0,1);
LCD_write_string("T1");
LCD_goto_XY(0,6);
LCD_write_string("T2");
LCD_goto_XY(0,12);
LCD_write_string("T3");
LCD_goto_XY(1,0);
sprintf(aa,"%.1f",a);
LCD_write_string(aa);
LCD_goto_XY(1,6);
sprintf(bb,"%.1f",b);
LCD_write_string(bb);
LCD_goto_XY(1,11);
sprintf(cc,"%.1f",c);
LCD_write_string(cc);
DDRE = 0x00;
DDRF = 0X01;
PORTE = 0x00;
DDRB = 0x07;
TCCR0 = 0x04; //분주비 64 설정
TIMSK |= (1 << TOIE0);
sei();
while(1)
{
if((PINE&1)==0){
if(d<4) {d++;
_delay_ms(100);}
else {
d=0;
LCD_init();
LCD_goto_XY(0,1);
LCD_write_string("T1");
LCD_goto_XY(0,6);
LCD_write_string("T2");
LCD_goto_XY(0,12);
LCD_write_string("T3");
LCD_goto_XY(1,0);
sprintf(aa,"%.1lf",a);
LCD_write_string(aa);
LCD_goto_XY(1,6);
sprintf(bb,"%.1lf",b);
LCD_write_string(bb);
LCD_goto_XY(1,11);
sprintf(cc,"%.1lf",c);
LCD_write_string(cc);
}
}
if(d==1) {
LCD_init();
LCD_goto_XY(0,1);
LCD_write_string("T1");
LCD_goto_XY(1,0);
sprintf(aa,"%.1f",a);
LCD_write_string(aa);
if((PINE&2)==0)
{
aaa=aaa+5;
a=((float)aaa)/10;
_delay_ms(10);
}
if(((PINE&4)==0) && a>0)
{
aaa=aaa-5;
a=((float)aaa)/10;
_delay_ms(10);
}
}
if(d==2) {
LCD_init();
LCD_goto_XY(0,6);
LCD_write_string("T2");
LCD_goto_XY(1,6);
sprintf(bb,"%.1lf",b);
LCD_write_string(bb);
if((PINE&2)==0)
{
bbb=bbb+5;
b=((float)bbb)/10;
_delay_ms(10);
}
if(((PINE&4)==0) && b>0)
{
bbb=bbb-5;
b=((float)bbb)/10;
_delay_ms(10);
}
}
if(d==3) {
LCD_init();
LCD_goto_XY(0,12);
LCD_write_string("T3");
LCD_goto_XY(1,11);
sprintf(cc,"%.1lf",c);
LCD_write_string(cc);
if((PINE&2)==0)
{
ccc=ccc+5;
c=((float)ccc)/10;
_delay_ms(10);
}
if(((PINE&4)==0) && c>0)
{
ccc=ccc-5;
c=((float)ccc)/10;
_delay_ms(10);
}
}
};
return 0;
}
댓글 0
조회수 5,816등록된 댓글이 없습니다.