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
}