Sabtu, 06 Juni 2015

Tutorial EEPROM

#include <mega8535.h>
#include <delay.h>
int eeprom *pointer_eeprom;
int data=0;
int ubah;
int sat,pul,rat,rib;
void write_data_to_eeprom()
{
 *pointer_eeprom=data;
}        
    
void read_data_to_eeprom()
{     
  data=*pointer_eeprom;
} 
void convert_to_format7segment()
{
           if (ubah==0){ubah=0xc0;}
           if (ubah==1){ubah=0xf9;}
           if (ubah==2){ubah=0xa4;}
           if (ubah==3){ubah=0xb0;}
           if (ubah==4){ubah=0x99;}
           if (ubah==5){ubah=0x92;}
           if (ubah==6){ubah=0x82;}
           if (ubah==7){ubah=0xf8;}
           if (ubah==8){ubah=0x80;}
           if (ubah==9){ubah=0x90;}
}
void display_7segment()
{
           PORTC=rib;//send data kedigit5
           PORTD.5=0;
           PORTD.6=0;
           PORTD.7=0;
           delay_ms(5);
           PORTC=rat;//send data kedigit6
           PORTD.5=1;
           PORTD.6=0;
           PORTD.7=0;
           delay_ms(5);
           PORTC=pul;//send data kedigit7
           PORTD.5=0;
           PORTD.6=1;
           PORTD.7=0;
           delay_ms(5);
           PORTC=sat;//send data kedigit8
           PORTD.5=1;
           PORTD.6=1;
           PORTD.7=0;
           delay_ms(5);
}
// Declare your global variables here
void main(void)
           {
 PORTA=0x00;
           DDRA=0x00;
           
           PORTB=0x00;
           DDRB=0x00;
           
           PORTC=0x00;
           DDRC=0xFF;
           
           PORTD=0x07;
           DDRD=0xE0;
           
           TCCR0=0x00;
           TCNT0=0x00;
           OCR0=0x00;
           
           TCCR1A=0x00;
           TCCR1B=0x00;
           TCNT1H=0x00;
           TCNT1L=0x00;
           ICR1H=0x00;
           ICR1L=0x00;
           OCR1AH=0x00;
           OCR1AL=0x00;
           OCR1BH=0x00;
           OCR1BL=0x00;
           
           ASSR=0x00;
           TCCR2=0x00;
           TCNT2=0x00;
           OCR2=0x00;
           
           MCUCR=0x00;
           MCUCSR=0x00;
           
           TIMSK=0x00;
 UCSRB=0x00;
 ACSR=0x80;
           SFIOR=0x00;
 SFIOR&=0x0F;
// SPI initialization
           // SPI disabled
   SPCR=0x00;
 TWCR=0x00;
 pointer_eeprom=0; //addres eeprom tobe write and read
 read_data_to_eeprom();
 while (1)
 {
  if (PIND.0==0)
  {
   data=data+5; // data=data+2; add 5
   delay_ms(200);
   if (data <=0)
   {
    data=0;
   }
   write_data_to_eeprom();
  }
  else if (PIND.1==0)
  {
   data=data-5;
   delay_ms(200);
   if (data <=0)
   {
    data=9999;
   }
   write_data_to_eeprom();
  }
  else if (PIND.2==0)
  {
   data=0;
  }
  sat = data % 10;// sat = modulus
  pul = data / 10;
  pul = pul % 10;
  rat = data / 100;
  rat = rat % 10;
  rib = data / 1000;
  rib=rib%10;
  ubah=sat;
  convert_to_format7segment();
  sat=ubah;
  ubah=pul;
  convert_to_format7segment();
  pul=ubah;
  ubah=rat;
  convert_to_format7segment();
  rat=ubah;
  ubah=rib;
  convert_to_format7segment();
  rib=ubah; 
  display_7segment(); 
 }
}


http://www.mytutorialcafe.com/microcontroller%20AVR%20Tutorial%20and%20Project.htm
         

Tidak ada komentar: