Or implement ACK polling, which is the faster method.The AVR needs to wait the time specified in the datasheet before trying next IIC transfer.
Code dot - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 unsigned short i, DD0, DD1, DD2, DD3, DD4; char temp=0; unsigned short mask(unsigned short num) { switch (num) { case 0 : return 0x3F; case 1 : return 0x06; case 2 : return 0x5B; case 3 : return 0x4F; case 4 : return 0x66; case 5 : return 0x6D; case 6 : return 0x7D; case 7 : return 0x07; case 8 : return 0x7F; case 9 : return 0x6F; } //case end } unsigned int counter = 0; char second = 0; char hour = 12; char minute = 0; void interrupt(){ if(PIR1.TMR1IF){ counter++; TMR1H = 0xD8; TMR1L = 0xF0; if(counter >= 25) { counter = 0; second++; EEPROM_Write(8,second); EEPROM_Write(4,minute); EEPROM_Write(2,hour); if (second >= 61){ second = 0; minute++; if (minute >= 60){ minute = 0; hour++; if (hour >= 13){ hour = 1; second += 22; } } } } PIR1.TMR1IF = 0; if (temp > 0) temp--; } } void main() { CMCON |= 7; TRISB = 0b00000000; TRISA = 0b00110000; PORTB = 0x00; PORTA = 0x00; hour = EEPROM_Read(2); minute = EEPROM_Read(4); TMR1H = 0xD8; TMR1L = 0xF0; T1CON.T1CKPS1 = 1; T1CON.T1CKPS0 = 0; PIE1.TMR1IE = 1; INTCON.PEIE = 1; INTCON.GIE = 1; T1CON.TMR1ON = 1; do { if (PORTA.F4 == 1 && temp == 0) { PORTB.F7=0; if (PORTA.F4==0) { hour++; if (hour >= 13) hour = 1; } else { minute++; if (minute >= 60) minute = 0; } temp = 12;//doesn't get another button click until a half of second } DD0 = minute%10; DD0 = mask(DD0); DD1 = (minute/10); DD1 = mask(DD1); DD2 = hour%10; DD2 = mask(DD2); DD3 = (hour/10); DD3 = mask(DD3); if (counter > 13) DD4 = 1; else DD4 = 0;//blick the LED onece a second for (i = 0; i<=20; i++) { PORTB = DD0; PORTA = 1; delay_ms(3); PORTB = DD1; PORTA = 2; delay_ms(3); PORTB = DD2; PORTA = 4; delay_ms(3); if(DD3 != 63) { PORTB = DD3; PORTA = 8; delay_ms(3); } PORTB = 0x00; PORTA = 4; PORTB.F7 = DD4; // turn on/off blicking LED delay_ms(3); } } while(1); // endless loop }
this is my full codes.
===============================
Code dot - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 unsigned short i, DD0, DD1, DD2, DD3, DD4; char temp=0; unsigned short mask(unsigned short num) { switch (num) { case 0 : return 0x3F; case 1 : return 0x06; case 2 : return 0x5B; case 3 : return 0x4F; case 4 : return 0x66; case 5 : return 0x6D; case 6 : return 0x7D; case 7 : return 0x07; case 8 : return 0x7F; case 9 : return 0x6F; } //case end } unsigned int counter = 0; char second = 0; char hour = 12; char minute = 0; void interrupt(){ if(PIR1.TMR1IF){ counter++; TMR1H = 0xD8; TMR1L = 0xF0; if(counter >= 25) { counter = 0; second++; EEPROM_Write(8,second); EEPROM_Write(4,minute); EEPROM_Write(2,hour); if (second >= 61){ second = 0; minute++; if (minute >= 60){ minute = 0; hour++; if (hour >= 13){ hour = 1; second += 22; } } } } PIR1.TMR1IF = 0; if (temp > 0) temp--; } } void main() { CMCON |= 7; TRISB = 0b00000000; TRISA = 0b00110000; PORTB = 0x00; PORTA = 0x00; hour = EEPROM_Read(2); minute = EEPROM_Read(4); TMR1H = 0xD8; TMR1L = 0xF0; T1CON.T1CKPS1 = 1; T1CON.T1CKPS0 = 0; PIE1.TMR1IE = 1; INTCON.PEIE = 1; INTCON.GIE = 1; T1CON.TMR1ON = 1; do { if (PORTA.F4 == 1 && temp == 0) { PORTB.F7=0; if (PORTA.F4==0) { hour++; if (hour >= 13) hour = 1; } else { minute++; if (minute >= 60) minute = 0; } temp = 12;//doesn't get another button click until a half of second } DD0 = minute%10; DD0 = mask(DD0); DD1 = (minute/10); DD1 = mask(DD1); DD2 = hour%10; DD2 = mask(DD2); DD3 = (hour/10); DD3 = mask(DD3); if (counter > 13) DD4 = 1; else DD4 = 0;//blick the LED onece a second for (i = 0; i<=20; i++) { PORTB = DD0; PORTA = 1; delay_ms(3); PORTB = DD1; PORTA = 2; delay_ms(3); PORTB = DD2; PORTA = 4; delay_ms(3); if(DD3 != 63) { PORTB = DD3; PORTA = 8; delay_ms(3); } PORTB = 0x00; PORTA = 4; PORTB.F7 = DD4; // turn on/off blicking LED delay_ms(3); } } while(1); // endless loop }
program is stuck at 'EEPROM_Write(4,minute);' line.
It is seen as program is running, but do not flow.
pls help me.
It's a built-in mikroC library function.Can you show us the schematics and the EEPROM_Write routine.
u8 I2C_24C_EEPROM_Write_Page (u8 I2c_Address, u16 Mem_Address, char * pBuffer)
{
u8 err=0;
err=I2c_Start(EEPROM_I2Cx);
if (err) return err;
err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
if (err) return err;
I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
while (* pBuffer) I2c_WriteByte(EEPROM_I2Cx, * pBuffer++);
I2c_Stop(EEPROM_I2Cx);
delay_ms(10);
return I2c_Success;
}
u8 I2C_24C_EEPROM_Write_Page_ByLen (u8 I2c_Address, u16 Mem_Address, char * pBuffer, u16 Len)
{
u8 err;
u8 PagesWritten = 0;
u8 BytesToWrite;
while (Len)
{
if (Len<MaxPageSizeBytes)
{
BytesToWrite = Len;
Len = 0;
}
else
{
BytesToWrite = MaxPageSizeBytes;
PagesWritten++;
Len-= MaxPageSizeBytes;
}
err=I2c_Start(EEPROM_I2Cx);
if (err) return err;
err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
if (err) return err;
I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
I2c_WriteByte(EEPROM_I2Cx, 0x00FF&Mem_Address);
while (BytesToWrite--) I2c_WriteByte(EEPROM_I2Cx, * pBuffer++);
I2c_Stop(EEPROM_I2Cx);
delay_ms(I2c_EEPROM_WriteTime_ms);
Mem_Address+= MaxPageSizeBytes;
}
return I2c_Success;
}
u8 I2C_24C_EEPROM_Read_Byte (u8 I2c_Address, u16 Mem_Address, char * pBuffer)
{
u8 err;
err=I2c_Start(EEPROM_I2Cx);
if (err) return err;
err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
if (err) return err;
I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
I2c_ReStart(EEPROM_I2Cx);
I2c_NACK(EEPROM_I2Cx);
I2c_WriteAddress(EEPROM_I2Cx, I2c_Address|1);
* pBuffer = I2c_ReadByte(EEPROM_I2Cx);
I2c_Stop(EEPROM_I2Cx);
I2c_ACK(EEPROM_I2Cx);
return I2c_Success;
}
u8 I2C_24C_EEPROM_Read_Page (u8 I2c_Address, u16 Mem_Address, char * pBuffer, u8 pBufferSize)
{
u8 err=0;
I2c_ACK(EEPROM_I2Cx);
err=I2c_Start(EEPROM_I2Cx);
if (err) return err;
err=I2c_WriteAddress(EEPROM_I2Cx, I2c_Address);
if (err) return err;
I2c_WriteByte(EEPROM_I2Cx, (0xFF00&Mem_Address)>>8);
I2c_WriteByte(EEPROM_I2Cx, 0xFF&Mem_Address);
I2c_ReStart(EEPROM_I2Cx);
I2c_WriteAddress(EEPROM_I2Cx, I2c_Address|1);
I2c_ACK(EEPROM_I2Cx);
while (pBufferSize)
{
* pBuffer++ = I2c_ReadByte(EEPROM_I2Cx);
pBufferSize--;
if (!pBufferSize) I2c_NACK(EEPROM_I2Cx);
}
I2c_Stop(EEPROM_I2Cx);
return I2c_Success;
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?