Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[Help Needed] Implementing RTC and motor using PIC18F4520

Status
Not open for further replies.

jinang

Junior Member level 1
Joined
May 14, 2011
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,532
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)

}
}
 

Hai,

The program will be a big one, Here i post a program for RTC and LCD using pic 16f877a
So make necessary changes.It will be working one.


void write();
void read();
void read1();
void disp( int temp,int i,int j);
void main()
{

trisc.f4=0;
trisc.f3=0;
Lcd8_Config(&portb,&portd,0,2,1,7,6,5,4,3,2,1,0);
Delay_ms(100);
Lcd8_Cmd(lcd_cursor_off);
Lcd8_Cmd(lcd_clear);
LCD8_Out(1,1,"WELCOME");
Delay_ms(100);
I2C_Init(100000);
write();
while(1)
{
read();
read1();
}

}

void write()
{
I2C_Start(); // issue start signal
I2C_Wr(0xd0); // address DS1307
I2C_Wr(0); // start from word at address (REG0)
I2C_Wr(0x80); // write $80 to REG0. (pause counter + 0 sec)
I2C_Wr(0x00); //changed for 12 hour
I2C_Wr(0x40); // changed for 12 hour
I2C_Wr(0); // write 0 to minutes word to (REG1)
I2C_Wr(0x12); // write 12 to hours word (24-hours mode)(REG2)
I2C_Wr(0x05); // write 5 - Thursday (REG3)
I2C_Wr(0x28); // write 25 to date word (REG4)
I2C_Wr(0x10); // write 9 (September) to month word (REG5)
I2C_Wr(0x10); // write 08 to year word (REG6)
I2C_Stop(); // issue stop signal

I2C_Start(); // issue start signal
I2C_Wr(0xD0); // address DS1307
I2C_Wr(0); // start from word at address 0
I2C_Wr(0); // write 0 to REG0 (enable counting + 0 sec)
I2C_Stop();

}
void read()
{
I2C_Start();
I2C_wr(0xD0);
I2C_wr(0);
I2C_Repeated_Start();
I2C_wr(0xD1);
}

void disp( int temp,int i,int j)
{
int s1,s2;
s1=Bcd2Dec(temp);
Lcd8_Chr(i,j,(s1/10 +48));
j++;
Lcd8_Chr(i,j,(s1%10 +48));
}

void read1()
{
char sec,minu,hour,day,month,year,date;
sec=I2C_Rd(1);
disp(sec,1,7);
Lcd8_Out(1,6,":");
minu=I2C_Rd(1);
disp(minu,1,4);
Lcd8_Out(1,3,":");
hour=I2C_Rd(1);
disp(hour,1,1);
day=I2C_Rd(1);
date=I2C_Rd(1);
disp(date,1,9);
Lcd8_Out(1,11,"/");
month=I2C_Rd(1);
disp(month,1,12);
Lcd8_Out(1,14,"/");
year=I2C_Rd(0);
disp(year,1,15);
I2C_Stop();
}
 

thank you for the code, but our RTC is functioning well also. it's just that when we insert some code for the motor operation, it made some delays in the RTC operation. will there be a way to limit the error? thank you :)
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top