up down counter with External EEprom interface using 16F877A

Status
Not open for further replies.

rahanuddin

Newbie
Joined
Jun 29, 2013
Messages
1
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
south korea
Activity points
1,281
Hello ! Dear Programmer ...
I am not Programming Expert so I Programmed a little 4 digit 7 segment up/down counter and i want to store counting data in eeprom like 24c08. but counting data is not save to EEProm. so i need help......

Code:
sbit digit1 at PORTB.B0;
sbit digit2 at PORTB.B1;
sbit digit3 at PORTB.B2;
sbit digit4 at PORTB.B3;
//===========================================
sbit DATA_pin at  PORTC.B1;
sbit LATCH_pin at PORTC.B2;
sbit CLCOK_pin at PORTC.B0;
//===========================================


//int   count = 0;
unsigned pstatus = 0;
unsigned char binary_pattern[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
unsigned int counter; //=0;
unsigned int a1,a2,a3,a4; // temporary variables to store data of adc
void clock_signal(void){
   CLCOK_pin = 1;
    delay_us(500);
   CLCOK_pin = 0;
    delay_us(500);
}
void latch_enable(void)
   {
    LATCH_pin = 1;
    delay_us(500);
    LATCH_pin = 0;
    }
void send_data(unsigned int data_out)
{
    int i;
    unsigned hold;
    for (i=0 ; i<8 ; i++)
    {
        DATA_pin = (data_out >> i) & (0x01);
        clock_signal();
    }
    latch_enable(); // Data finally submitted
}


void main()
{
  TRISC.B0 = 0; // Set  DATA_pin as output pin
  TRISC.B1 = 0; // Set  CLCOK_pin as output pin
  TRISC.B2 = 0; // Set LATCH_pin as output pin

  TRISC.F4= 1;   //SDA
  TRISC.F3= 1;  //SCL

   //**********************************************

   //**********************************************

  //===========================================
  TRISD = 0x30;                      // Configure RB3 & RB4 pins as inputs
  // pstatus = 1;
  TRISB=0x00;
  PORTB = 0X00;


  digit1 = 0;
  digit2 = 0;
  digit3 = 0;
  digit4 = 0;
  while(1)
      {


         a1 = counter / 1000;   // holds 1000's digit
         a2 = ((counter/100)%10); // holds 100's digit
         a3 = ((counter/10)%10);  // holds 10th digit
         a4 = (counter%10);  // holds unit digit value
        //  counter = I2C1_Rd(0); // Read the data (NO acknowledge)

         send_data(binary_pattern[a1]); // send 1000's place data to fourth digit
        digit1=1;   //  turn on forth display unit
        delay_ms(3);
        digit1=0;   //  turn off forth display unit
      
        send_data(binary_pattern[a2]);  // send 100's place data to 3rd digit
        digit2=1;    //  turn on 3rd display unit
        delay_ms(3);
        digit2=0;  //  turn off 3rd display unit
      
        send_data(binary_pattern[a3]);  // send 10th place data to 2nd digit
        digit3 = 1;  //  turn on 2nd display unit
        delay_ms(3);
        digit3 = 0;   //  turn off 2nd display unit
     
       send_data(binary_pattern[a4]);  // send unit place data to 1st digit
       digit4 = 1;  //  turn on 1st display unit
       delay_ms(3);
       digit4 = 0;  //  turn off 1st display unit

      
     //================================================
         if(PORTD.F4 ==0)
         {
         counter++;
         if(counter>9999)
         //counter =0;
         delay_ms(200);
            I2C1_Init(100000); // initialize I2C communication
            I2C1_Start(); // issue I2C start signal
            I2C1_Wr(0xA0); // send byte via I2C (device address + W)
            I2C1_Wr(0); // send byte (address of EEPROM location)
            I2C1_Wr(1); // send byte (address of EEPROM location)
            I2C1_Wr(counter); // send data (data to be written)
            I2C1_Stop(); // issue I2C stop signal
         }
       
         if(PORTD.F5 ==0)
         {
         counter--;
         if(counter>9999)
         //counter =0;
         delay_ms(200);
       
            I2C1_Init(100000); // initialize I2C communication
            I2C1_Start(); // issue I2C start signal
            I2C1_Wr(0xA0); // send byte via I2C (device address + W)
            I2C1_Wr(0); // send byte (address of EEPROM location)
            I2C1_Wr(1); // send byte (address of EEPROM location)
            I2C1_Wr(counter); // send data (data to be written)
            I2C1_Stop(); // issue I2C stop signal

         }


      }

           I2C1_Start(); // issue I2C start signal
           I2C1_Wr(0xA0); // send byte via I2C (device address + W)
           I2C1_Wr(0); // send byte (data address)
           I2C1_Wr(1); // send byte (address of EEPROM location)
           I2C1_Repeated_Start(); // issue I2C signal repeated start
           I2C1_Wr(0xA1); // send byte (device address + R)
           counter = I2C1_Rd(0); // Read the data (NO acknowledge)
           I2C1_Stop(); // issue I2C stop signal
}
 

Attachments

  • updown counter.JPG
    132.1 KB · Views: 190
Last edited by a moderator:

I2C is not the easiest protocol to begin with.
Are you getting anything on the LCD? If not then are you sure that anything is working?
I assume that you are using the MikroC compiler and therefore using their I2C library. However I don't see that you are checking for an error values that the functions may return.
However it would be really useful to know the complete development environment you are using.
If you really are beginning programming, then I would start with the basic 'flash a LED' as this will let you know that you have the basic MCU set up, that you can program the MCU from the PC etc.
For example, your schematic does not show anything connected to the OSC1 pin. You also do now show any of the CONFIG settings so I assume that you are using the default RC oscillator. However the RC oscillator requires a resistor and capacitor on OSC1 to work.
Also do you have bypass capacitors on your circuit?
Finally are you using just the simulator or are you using a real device?
Susan
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…