jinang
Junior Member level 1
Hi! I am working on a project that is capable of moving a motor (in a time based manner) every after 30 minutes. The time will be based on an RTC in such a way that when I turn on the system, it looks at the time in the RTC to know which position the motor should be in and then after 30 minutes, it will move to the next position again. Codes below written in MikroC.
The problem we encountered with our code (given below) is that it doesn't run properly because the length of the code makes the RTC exhibit some delays. also, the 'for' loop (text in bold) does not recognize the assignment of value for the variable 'counter1'. The LED at that pin (RA1) continue to blink continuously in the entire operation instead of blinking n number of times depending on the condition satisfied in the if statements prior to that.
Would greatly appreciate any help given because I've been working for weeks for this already. Thank you.
char seconds, minutes, hours, day, month, year; // Global date/time variables
unsigned short move = 0;
unsigned short reset = 0;
unsigned short i = 0;
unsigned short counter2 = 10;
unsigned short counter1;
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Config(&PORTC, 4, 3); // Initialize full master mode
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xA1);
seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal
}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
EEPROM_Write(0x20, seconds);
EEPROM_Write(0x21, minutes);
EEPROM_Write(0x22, hours);
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (EEPROM_Read(0x22) / 10) + 48);
Lcd_Chr(1, 7, (EEPROM_Read(0x22) % 10) + 48);
Lcd_Chr(1, 9, (EEPROM_Read(0x21) / 10) + 48);
Lcd_Chr(1,10, (EEPROM_Read(0x21) % 10) + 48);
Lcd_Chr(1,12, (EEPROM_Read(0x20) / 10) + 48);
Lcd_Chr(1,13, (EEPROM_Read(0x20) % 10) + 48);
}
void Pin_Selection () {
//determine which LED yung maglight
if ((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x01;
else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x03;
else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x07;
else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x0F;
else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x1F;
else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x3F;
else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x7F;
else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0xFF;
else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
PORTE = 0x01;
else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
PORTE = 0x03;
else
PORTE = 0x07;
}
void Counter_Assignment() {
//determine pang-ilang counter
if((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 0;
else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 1;
else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 2;
else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 3;
else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 4;
else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 5;
else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 6;
else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 7;
else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 8;
else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 9;
else
counter1 = 10;
}
//------------------ Performs project-wide init
void Init_Main() {
TRISD = 0;
PORTD = 0xFF;
TRISD = 0xFF;
TRISB = 0x00;
TRISE = 0x00;
TRISA = 0x00;
ADCON1 = 0x0F;
T0CON = 0xF8;
Delay_ms(200);
Soft_I2C_Config(&PORTC, 4, 3); // Initialize Soft I2C communication
Lcd_Init(&PORTD); // Initialize LCD
Delay_ms(200);
Lcd_Cmd(Lcd_CLEAR); // Clear LCD display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Time:");
Lcd_Chr(1,8,':');
Lcd_Chr(1,11,':');
}
//----------------- Main procedure
void main() {
Delay_ms(200);
Init_Main(); // Perform initialization
for (reset=0; reset<=counter2; reset++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F2 = 1;
Delay_ms(500);
PORTA.F2 = 0;
}
while(1) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
Pin_Selection();
Counter_Assignment();
for (move=0; 0<=move<=counter1; move++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F1 = 1;
Delay_ms(500);
PORTA.F1 = 0;
move++;
}
while (((EEPROM_Read(0x21)==29) && (EEPROM_Read(0x20)<=59) ) || ((EEPROM_Read(0x21)==59) && (EEPROM_Read(0x20)<=59) ) )
{
Pin_Selection();
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F1 = 1;
Delay_ms(500);
PORTA.F1 = 0;
for (i = 0; 0<=i<=1800; i++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}}
// read EEPROM_Read(0x22)
}
}
The problem we encountered with our code (given below) is that it doesn't run properly because the length of the code makes the RTC exhibit some delays. also, the 'for' loop (text in bold) does not recognize the assignment of value for the variable 'counter1'. The LED at that pin (RA1) continue to blink continuously in the entire operation instead of blinking n number of times depending on the condition satisfied in the if statements prior to that.
Would greatly appreciate any help given because I've been working for weeks for this already. Thank you.
char seconds, minutes, hours, day, month, year; // Global date/time variables
unsigned short move = 0;
unsigned short reset = 0;
unsigned short i = 0;
unsigned short counter2 = 10;
unsigned short counter1;
//--------------------- Reads time and date information from RTC (PCF8583)
void Read_Time() {
Soft_I2C_Config(&PORTC, 4, 3); // Initialize full master mode
Soft_I2C_Start(); // Issue start signal
Soft_I2C_Write(0xA0); // Address PCF8583, see PCF8583 datasheet
Soft_I2C_Write(2); // Start from address 2
Soft_I2C_Start(); // Issue repeated start signal
Soft_I2C_Write(0xA1);
seconds = Soft_I2C_Read(1); // Read seconds byte
minutes = Soft_I2C_Read(1); // Read minutes byte
hours = Soft_I2C_Read(1); // Read hours byte
day = Soft_I2C_Read(1); // Read year/day byte
month = Soft_I2C_Read(0); // Read weekday/month byte
Soft_I2C_Stop(); // Issue stop signal
}
//-------------------- Formats date and time
void Transform_Time() {
seconds = ((seconds & 0xF0) >> 4)*10 + (seconds & 0x0F); // Transform seconds
minutes = ((minutes & 0xF0) >> 4)*10 + (minutes & 0x0F); // Transform months
hours = ((hours & 0xF0) >> 4)*10 + (hours & 0x0F); // Transform hours
year = (day & 0xC0) >> 6; // Transform year
day = ((day & 0x30) >> 4)*10 + (day & 0x0F); // Transform day
month = ((month & 0x10) >> 4)*10 + (month & 0x0F); // Transform month
EEPROM_Write(0x20, seconds);
EEPROM_Write(0x21, minutes);
EEPROM_Write(0x22, hours);
}
//-------------------- Output values to LCD
void Display_Time() {
Lcd_Chr(1, 6, (EEPROM_Read(0x22) / 10) + 48);
Lcd_Chr(1, 7, (EEPROM_Read(0x22) % 10) + 48);
Lcd_Chr(1, 9, (EEPROM_Read(0x21) / 10) + 48);
Lcd_Chr(1,10, (EEPROM_Read(0x21) % 10) + 48);
Lcd_Chr(1,12, (EEPROM_Read(0x20) / 10) + 48);
Lcd_Chr(1,13, (EEPROM_Read(0x20) % 10) + 48);
}
void Pin_Selection () {
//determine which LED yung maglight
if ((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x01;
else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x03;
else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x07;
else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x0F;
else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x1F;
else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0x3F;
else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
PORTB = 0x7F;
else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
PORTB = 0xFF;
else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
PORTE = 0x01;
else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
PORTE = 0x03;
else
PORTE = 0x07;
}
void Counter_Assignment() {
//determine pang-ilang counter
if((EEPROM_Read(0x22)==10) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 0;
else if((EEPROM_Read(0x22)==10) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 1;
else if((EEPROM_Read(0x22)==11) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 2;
else if((EEPROM_Read(0x22)==11) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 3;
else if((EEPROM_Read(0x22)==12) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 4;
else if((EEPROM_Read(0x22)==12) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 5;
else if((EEPROM_Read(0x22)==13) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 6;
else if((EEPROM_Read(0x22)==13) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 7;
else if((EEPROM_Read(0x22)==14) && (00<=EEPROM_Read(0x21)<=30))
counter1 = 8;
else if((EEPROM_Read(0x22)==14) && (30<=EEPROM_Read(0x21)<=59))
counter1 = 9;
else
counter1 = 10;
}
//------------------ Performs project-wide init
void Init_Main() {
TRISD = 0;
PORTD = 0xFF;
TRISD = 0xFF;
TRISB = 0x00;
TRISE = 0x00;
TRISA = 0x00;
ADCON1 = 0x0F;
T0CON = 0xF8;
Delay_ms(200);
Soft_I2C_Config(&PORTC, 4, 3); // Initialize Soft I2C communication
Lcd_Init(&PORTD); // Initialize LCD
Delay_ms(200);
Lcd_Cmd(Lcd_CLEAR); // Clear LCD display
Lcd_Cmd(Lcd_CURSOR_OFF); // Turn cursor off
Lcd_Out(1,1,"Time:");
Lcd_Chr(1,8,':');
Lcd_Chr(1,11,':');
}
//----------------- Main procedure
void main() {
Delay_ms(200);
Init_Main(); // Perform initialization
for (reset=0; reset<=counter2; reset++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F2 = 1;
Delay_ms(500);
PORTA.F2 = 0;
}
while(1) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
Pin_Selection();
Counter_Assignment();
for (move=0; 0<=move<=counter1; move++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F1 = 1;
Delay_ms(500);
PORTA.F1 = 0;
move++;
}
while (((EEPROM_Read(0x21)==29) && (EEPROM_Read(0x20)<=59) ) || ((EEPROM_Read(0x21)==59) && (EEPROM_Read(0x20)<=59) ) )
{
Pin_Selection();
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
PORTA.F1 = 1;
Delay_ms(500);
PORTA.F1 = 0;
for (i = 0; 0<=i<=1800; i++) {
Read_Time(); // Read time from RTC(PCF8583)
Transform_Time(); // Format date and time
Display_Time(); // Prepare and display on LCD
}}
// read EEPROM_Read(0x22)
}
}