Pumpkin, Inc.

Pumpkin User Forums

RTC interface

For issues specific to Microchip's PICmicro® MCUs, including compilers (e.g. HI-TECH PICC & PICC-18, Microchip MPLAB®-C18) and IDEs (e.g. Microchip MPLAB®).

RTC interface

Postby aek » Mon May 19, 2008 6:09 am

Since there's no Salvo code in your appliation, this isn't so much a Salvo query as a DS1307 or 16F877, which might be better served in e.g. the Microchip forums ...

However, this is also an interesting topic, as it illustrates the performance penalties you encounter when using something like delay_ms() ... Every one of your delay_ms(10) calls forces a 4MHz PIC16F877 to "spin its wheels" for 10,000 CPU cycles (!), during which the only other thing it can do is service interrupts. With so many delay_ms() calls, the responsiveness of an application that contains this code is likely to suffer from all these busy-wait calls ... for this code, you spend most of your time busy-waiting, not doing something useful.

If this were a Salvo application using OS_Delay(2) instead of delay_ms(10) (assumes 5ms Salvo timer ticks), then the rest of the application (i.e. the other tasks) could be running while the busy-waits are spinning their wheels.

The challenge when using Salvo is that OS_Delay() can only be used from within a Salvo task, and not from inside a called lower-level function (to allow this would require a task stack, and Salvo operates without task stacks). This tends to make Salvo task code "more inline", but gains you considerable performance advantages because there is no busy-waiting, so other tasks (i.e. processes) can continue to operate while you're waiting for things to happen in one process (the RTC interface).

In your code, only main() and serial() have calls to delay_ms(), so it ought to be fairly easy to transform all of this into a Salvo application using OS_Delay() ...

------------------

[This message has been edited by aek (edited May 19, 2008).]

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Re: RTC interface

Postby aek » Mon May 19, 2008 6:18 am

Oh, lest I forget, there's another very important advantage of using OS_Delay() over delay_ms() ... power savings.

With delay_ms(), the CPU is constantly running, with no power savings. With OS_Delay() -- because Salvo is purely event-driven -- the only clock that needs to be running is the one for the timer associated with Salvo delays -- the processor sleeps, wakes up for every tick, and goes back to sleep, only to wake and run code when each Salvo delay expires. That's a huge power-saving advantage ...

------------------

-------
aek
aek
 
Posts: 1888
Joined: Sat Aug 26, 2000 11:00 pm

Re: RTC interface

Postby sirch » Mon May 19, 2008 7:54 am

thank-u so much.

quote:
Originally posted by aek:
Oh, lest I forget, there's another very important advantage of using OS_Delay() over delay_ms() ... [b]power savings.

With delay_ms(), the CPU is constantly running, with no power savings. With OS_Delay() -- because Salvo is purely event-driven -- the only clock that needs to be running is the one for the timer associated with Salvo delays -- the processor sleeps, wakes up for every tick, and goes back to sleep, only to wake and run code when each Salvo delay expires. That's a huge power-saving advantage ...

[/B]



sirch
 
Posts: 2
Joined: Sat May 17, 2008 11:00 pm

Re: RTC interface

Postby sirch » Mon May 19, 2008 11:11 am

hello friends!
I'm getting prob with interfacing DS1307 to 16f877.Here is my code .PLZ help me...
//RTC program
code:
void serial();
int data;
void ssp_ack();
unsigned char receive(unsigned char addr);
unsigned char parameter[15],tx;
void transformm();
void main()
{ //DS1307 INIT STARTS

delay_ms(20);
STATUS.RP0=1;
//INTCON=0xD0;
TRISC.F3=1; //chng
TRISC.F4=1; //chng
SSPSTAT=0x80;//CKE=0 and smp=1; chng
SSPADD=10;
STATUS.RP0=0;
SSPCON=0X2E; //SSPEN ENABLE, MODE=1000(8)

//WRITE CYCLE STARTS
STATUS.RP1=0;
STATUS.RP0=0;
PIR1.SSPIF=0;
STATUS.RP0=1;
SSPCON2.SEN=1;
STATUS.RP0=0;
ssp_ack();


SSPBUF=0xD0; //CONTROLIN

ssp_ack();
STATUS.RP0=0;
STATUS.RP1=0;
SSPBUF=0x00; //reg pointer
ssp_ack();
SSPBUF=0x06; //set seconds

ssp_ack();
SSPBUF=0x06; //set minutes

ssp_ack();
SSPBUF=0x06; //set hr
ssp_ack();
SSPBUF=0x06; //set day

ssp_ack();
SSPBUF=0x06; //set date

ssp_ack();
SSPBUF=0x06;

ssp_ack();
SSPBUF=0x06;

ssp_ack();
SSPCON2.PEN=1;//STOP bit transmit

while(!PIR1.SSPIF);
PIR1.SSPIF=0;
delay_ms(20);
receive(0);
parameter[0]=receive(0); //sec
parameter[1]=receive(1); //min
parameter[2]=receive(2); //hr
parameter[3]=receive(3); //day
parameter[4]=receive(4); //date
parameter[5]=receive(5); //month
parameter[6]=receive(6); //year
SSPCON2.ACKDT=1;//Set NACK value
SSPCON2.ACKEN=1;
SSPCON2.RCEN=1;
ssp_ack();
delay_ms(20);
transformm();
serial();
}


unsigned char receive(unsigned char addr)
{ unsigned char x;
a=2;
tx=0;
STATUS.RP1=0;
STATUS.RP0=0;
PIR1.SSPIF=0;
STATUS.RP0=1;
SSPCON2.RSEN=1; //start bit transmit
STATUS.RP0=0;
ssp_ack();

SSPBUF=0xD0; //CONTROLIN

ssp_ack();
STATUS.RP0=0;
STATUS.RP1=0;
SSPBUF=0x00; //reg pointer
ssp_ack();

STATUS.RP0=1;
SSPCON2.RSEN=1; //start bit transmit
STATUS.RP0=0;
ssp_ack();

SSPBUF=0xD1; //CONTROLOUT
ssp_ack();
STATUS.RP0=1;
SSPCON2.RCEN=1;
ssp_ack();
x=SSPBUF;
SSPCON2.PEN=1; //STOP bit transmit

while(!SSPIF);
PIR1.SSPIF=0;
return(x);
}

void ssp_ack()
{
while(!PIR1.SSPIF); //CHNG
//serial(a);
PIR1.SSPIF=0;

}

//serial comm.
void serial()
{ int i=0;
//TRISC=0x80;//SELECT TX(RC6) AS INPUT AND RX(RC7) AS OUTPUT
for(i=0;i<7;i++)
{
SPBRG=0x19;// 25 DECIMAL FOR 9600 BAUD RATE
delay_ms(10);
TXSTA.F2=1; //BRGH=1 HIGH SPEED
delay_ms(10);
TXSTA.F4=0;// ASYNCRONOUS TRANSMIT
delay_ms(10);
RCSTA.F7=1;// SPEN SERIAL PORT ENABLE
delay_ms(10);
INTCON=0x90;// DISABLE TRANSMIT INTERRUPTS
delay_ms(10);
TXSTA.F6=0;// DISABLE 9BIT TRANSMISSION
delay_ms(10);
TXSTA.F5=1; // TRANSMIT ENABLE
delay_ms(10);
TXSTA.F0=0;// 9TH BIT OF DATA
delay_ms(10);
TXREG=parameter[i];
while(TXSTA.F1==0);
delay_ms(10);

}
}


void transformm()
{ parameter[0] = ((parameter[0] & 0xF0) >> 4)*10 + (parameter[0] & 0x0F);
parameter[1] = ((parameter[1] & 0xF0) >> 4)*10 + (parameter[1] & 0x0F);
parameter[2] = ((parameter[2] & 0xF0) >> 4)*10 + (parameter[2] & 0x0F);
parameter[6] = ( parameter[6] & 0xC0) >> 6;
parameter[3] = ((parameter[3] & 0x30) >> 4)*10 + (parameter[3] & 0x0F);
parameter[5] = ((parameter[5] & 0x10) >> 4)*10 + (parameter[5] & 0x0F);
}


[This message has been edited by aek (edited May 19, 2008).]

sirch
 
Posts: 2
Joined: Sat May 17, 2008 11:00 pm


Return to PICmicro MCUs

Who is online

Users browsing this forum: No registered users and 2 guests

cron