CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

real time clock with 18f4550

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

real time clock with 18f4550
PostPosted: Fri Aug 20, 2010 6:26 pm     Reply with quote

I do a real time clock with 18f4550 and I use PICKIT2 for programming of the PIC.

This is my program.
Code:

#include "18f4550.h"
#fuses XT,PUT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4M)

#define INTS_PER_SECOND 122//225     // (4000000/(4*32*256))

#include <stdlib.h>
#include "flex_lcd.c"

//declaration des varriables
int8 secon=0,minu=0,heur=0,int_count,nb=0;
int8 days=0,month=0;
int16 year=0;
int8 verif;
char debut[46]="                veuillez rectifier l'horloge ";

#define IS_LEAP(year) (year%4 == 0)

//**************************
#int_rtcc         // cette fonction est appéle à chaque debordement de rtcc
void RTCC_isr(void)
{
  if(secon == 60) {minu++; secon=0;
    if(minu == 60) {heur++; minu=0;
      if(heur == 24) {days++; heur=0;
        if (  (days == 29 && month==2 && !IS_LEAP(Year))
           || (days == 30 && month==2)
           || (days == 31 && (month==4 || month==6 || month==9 || month==11 ))
           || (days == 32)
           ) {month++;days=0;}
        if(month == 13) {year++; month=0;}
  }}}
   else
   {
      if(--int_count==0)
      {
         ++secon;
         int_count=INTS_PER_SECOND;
      }
   }
}


void main()
{
int8 D_H,U_H,D_M,U_M,D_S,U_S,D_D,U_D,D_Mo,U_Mo,Y3;
int8 T[12];
int8 T1[8]={9,10,12,13,15,16};
int8 T2[6]={16,15,11,10,8,7};
int8 i=0;
char aux;

int_count=INTS_PER_SECOND;
enable_interrupts(GLOBAL);
enable_interrupts(INT_RTCC);
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_32);
T[0]=D_H;
T[1]=0;
T[2]=0;
T[3]=0;
T[4]=0;
T[5]=0;
T[6]=0;
T[7]=0;
T[8]=0;
T[9]=0;
T[10]=0;
T[11]=0;

//***********************ouverture du programme*********************
lcd_init();
delay_ms(50);
lcd_gotoxy(1,1);
printf(lcd_putc,"  BISMI ELLEH  ");
lcd_gotoxy(1,2);
printf(lcd_putc,"****************");
delay_ms(2000);

//************************intro
defilement*************************************
/*
for(i=0;i<40;i++)
{
lcd_gotoxy(1,1);
printf(lcd_putc,debut);
lcd_gotoxy(1,2);
printf(lcd_putc,"****************");
for(i=0;i<46;i++)
{
aux=debut[i];
debut[i]=debut[i+1];
debut[i+1]=aux;
}
delay_ms(200);
}
*/

//***********************pour passer à la rectification*************
while (!input(PIN_B0))
{
lcd_gotoxy(1,1);
printf(lcd_putc,debut);
lcd_gotoxy(1,2);
printf(lcd_putc,"Appuier sur  '+'");
for(i=0;i<46;i++)
{
aux=debut[i];
debut[i]=debut[i+1];
debut[i+1]=aux;
}
delay_ms(200);
}

lcd_gotoxy(1,1);
printf(lcd_putc,"HEURE   00:00:00");
//********
lcd_gotoxy(1,2);
printf(lcd_putc,"DATE  00/00/2000");
delay_ms(1000);
//******************************************************************************
//************************rectification de l'heure******************************

nb=0;

while(nb<6)
{
   if(input(PIN_B1))
   {
   nb++;
   lcd_gotoxy(1,1);
   printf(lcd_putc,"HEURE   %d%d:%d%d:%d%d",T[0],T[1],T[2],T[3],T[4],T[5]);
   delay_ms(1);
   }
   
   if(input(PIN_B0))
   {
   //*********************D_H************************
      if (nb==0)
      {
      if (T[nb]==2)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
   //*********************U_H************************
      if (nb==1)
      {
      //****
      if(T[0]==2)
      {
      if (T[nb]==3)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
      //****
      if(T[0]==1)
      {
      if (T[nb]==9)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
     
      }

   //*********************D_M************************
         if (nb==2)
      {
      if (T[nb]==5)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
   //*********************U_M************************
         if (nb==3)
      {
      if (T[nb]==9)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
   //*********************D_S************************
         if (nb==4)
      {
      if (T[nb]==5)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
   //*********************U_S************************
         if (nb==5)
      {
      if (T[nb]==9)
      {
      T[nb]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb]=T[nb]+1;
      while(input(PIN_B0));
      }
      }
     
     
   }
   lcd_gotoxy(T1[nb],1);
   printf(lcd_putc,"%d",T[nb]);
   delay_ms(100);
   lcd_gotoxy(T1[nb],1);
   printf(lcd_putc,"_");
   delay_ms(100);
}

//******************************************************************************
//************************rectification de l'heure******************************

nb=0;
lcd_gotoxy(1,1);
printf(lcd_putc,"HEURE   %d%d:%d%d:%d%d",T[0],T[1],T[2],T[3],T[4],T[5]);
lcd_gotoxy(1,2);
printf(lcd_putc,"DATE  00/00/2000");
delay_ms(100);

while(nb<6)
{
   if(input(PIN_B1))
   {
   nb++;
   lcd_gotoxy(1,2);
   printf(lcd_putc,"DATE  %d%d/%d%d/20%d%d",T[11],T[10],T[9],T[8],T[7],T[6]);
   delay_ms(1);
   }
   
   if(input(PIN_B0))
   {
   //*********************Y4************************
      if (nb==0)
      {
      if (T[nb+6]==9)
      {
      T[nb+6]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb+6]=T[nb+6]+1;
      while(input(PIN_B0));
      }
      }
   //*********************Y3************************
         if (nb==1)
      {
      if (T[nb+6]==9)
      {
      T[nb+6]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb+6]=T[nb+6]+1;
      while(input(PIN_B0));
      }
      }
   //*********************calcul de l'année************************
   year=2000+10*T[7]+T[6];
   verif=year%4;

   //**************************************************************
   //*********************U_Mo************************
      if (nb==2)
      {
      if (T[nb+6]==9)
      {
      T[nb+6]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb+6]=T[nb+6]+1;
      while(input(PIN_B0));
      }
      } 
   
   //*********************D_Mo************************
    if (nb==3)
      {
      if (T[8]<=9 && T[8]>2 )
      {
         if (T[nb+6]==0)
         {
         T[nb+6]=0;
         while(input(PIN_B0));
         }
         else
         {
         T[nb+6]=T[nb+6]+1;
         while(input(PIN_B0));
         }
      }
      if (T[8]<=2 )
      {
         if (T[nb+6]==1)
         {
         T[nb+6]=0;
         while(input(PIN_B0));
         }
         else
         {
         T[nb+6]=T[nb+6]+1;
         while(input(PIN_B0));
         }
      }
     
      }
   //********************calcul month****************
   month=T[9]*10+T[8];

   //*********************U_D************************
         if (nb==4)
      {
      if (T[nb+6]==9)
      {
      T[nb+6]=0;
      while(input(PIN_B0));
      }
      else
      {
      T[nb+6]=T[nb+6]+1;
      while(input(PIN_B0));
      }
      }
   
   
   //*********************D_D************************
      if (nb==5)
      {
      if (T[10]<=9 && T[10]>1 )
      {
         if (T[nb+6]==2)
         {
         T[nb+6]=0;
         while(input(PIN_B0));
         }
         else
         {
         T[nb+6]=T[nb+6]+1;
         while(input(PIN_B0));
         }
      }
      if (T[10]<=1 )
      {
         if (T[nb+6]==3)
         {
         T[nb+6]=0;
         while(input(PIN_B0));
         }
         else
         {
         T[nb+6]=T[nb+6]+1;
         while(input(PIN_B0));
         }
      }
   }
 }
   
lcd_gotoxy(T2[nb],2);
printf(lcd_putc,"%d",T[nb+6]);
delay_ms(100);
lcd_gotoxy(T2[nb],2);
printf(lcd_putc,"_");
delay_ms(100);
}

secon=T[4]*10+T[5];
minu=T[2]*10+T[3];
heur=T[0]*10+T[1];
days=T[11]*10+T[10];

//**********************fin rectification de l'heure****************************

   while(true)
   {
      D_H=heur/10;
      U_H=heur-10*D_H;
      D_M=minu/10;
      U_M=minu-10*D_M;
      D_S=secon/10;
      U_S=secon-10*D_S;
      D_D=days/10;
      U_D=days-10*D_D;
      D_Mo=month/10;
      U_Mo=month-10*D_Mo;
      Y3=(year-2000);
     
lcd_gotoxy(1,1);
printf(lcd_putc,"HEURE   %d%d:%d%d:%d%d",D_H,U_H,D_M,U_M,D_S,U_S);
//********
lcd_gotoxy(1,2);
printf(lcd_putc,"DATE  %d%d/%d%d/20%02d",D_D,U_D,D_Mo,U_Mo,Y3);
delay_ms(100);
//******************

   }
}

When I simulate this program with ISIS it works fine but when I try it,
it doesn't work correctly.
I have no idea and its very urgent.
I upload all my files here:
http://www.mediafire.com/?zrrcprvmu4cret3
Thanks.

[/code]
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 4:35 am     Reply with quote

Obvious things to look at if code runs in the simulator, but not for real, are:
1) Your clock. Have you actually got a 4MHz crystal attached, have you got the right loading capacitors for this, etc. etc.?. How long are the wires to the crystal. Any chance they could be picking up other signals, etc.
2) Your supply. Nicely smoothed 5v, with a small decoupling capacitor close to the PIC.
3) How is MCLR connected?.

Have you proved the processor runs, and runs at the right frequency, with a simple 'display hello', then display a counter, type program?.

Best Wishes
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 9:56 am     Reply with quote

The problem is not that the program doesn't work in the board but that the frequency of running at the right frequency.
If you see the file that I uploaded you can see that I try it with 16f877a and it run very well in reality. But when I use the 18f4550 in the same circuit it run but not in the same frequency.
So help please.

Also if you have remarks for the program I wish that I ameliorate my program.

THANKS.
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 11:44 am     Reply with quote

Also I open the file in MPLAB and I see this

http://www.mediafire.com/i/?8f1eq81yz3gh337

and I can't see the 4Mhz input but 48Mhz.

What can I do so??
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 3:00 pm     Reply with quote

The PLL division ration is not a problem.
You have the XT fuse selected, which doesn't _use_ the PLL for the CPU clock.
The bits are set to '1' by default, hence the maximum PLL value is selected.
What I don't see is the CPUDIV1 fuse. Also you have the USB voltage regulator enabled (again default), which you should really turn off, if you are not using the USB.

Best Wishes
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

PostPosted: Sat Aug 21, 2010 8:13 pm     Reply with quote

Thanks a lot. I have added "#FUSES CPUDIV1 , NOVREGEN " to my code and it runs at the right frequency.
I want to learn more about fuses if you can give me an URL or some thing to read which is useful.

Thanks again, and if you can give me advices to ameliorate my code if it's possible.

THANKS.
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Sun Aug 22, 2010 4:12 am     Reply with quote

No URL.
With the compiler, there is a 'fuses.txt' file, that gives descriptions for the CCS names.
Then the processor data sheet is 'God'....
If (for example), you look at the oscillator diagram, you will see that the external clock comes in, and routes in three possible directions. When 'XT, HS, EC, or EC_IO' are selected (the last two needing an external oscillator), the clock goes directly to the CPUDIV multiplexor, and then on to the processor, without involving the PLL.

Best Wishes
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

PostPosted: Sun Aug 22, 2010 9:49 am     Reply with quote

THANKS A LOT
But what about ameliorating my code??
Humberto



Joined: 08 Sep 2003
Posts: 1215
Location: Buenos Aires, La Reina del Plata

View user's profile Send private message

PostPosted: Sun Aug 22, 2010 12:45 pm     Reply with quote

Your code its too large and require extra hardware to test it, hence it would be improbable to get some help from somebody.
Try to implement some basic procedure like an RS232 mssge monitoring, some bliking LEDs, etc in order to debug your
project in real time.

Regarding the FUSES, I hope that this would help you:
http://www.ccsinfo.com/forum/viewtopic.php?t=24336&highlight=fuses

Humberto
farouk_baya



Joined: 18 Jun 2010
Posts: 27

View user's profile Send private message

PostPosted: Sun Aug 22, 2010 5:05 pm     Reply with quote

I tried this program in the board and it works fine.
But I think it is so long, so how can I enhance it ?
THANKS
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group