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 CCS Technical Support

Micro project

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



Joined: 19 Mar 2014
Posts: 1

View user's profile Send private message Send e-mail

Micro project
PostPosted: Tue Apr 01, 2014 11:37 pm     Reply with quote

Our project is to design and implement a power monitor aiming to display elapsed time and current frequency of either IDL or Generator. An alarm should be set whenever the frequency is off by +/- 2Hz. The system should provide time logging information on a daily basis. An external RTC may be used.

I'm using 6 buttons so that the user can reset the time and date of the LCD :

RB0: Day, RB1: Month, RB2: Year, RB3: Seconds, RB4: Minutes, RB5: Hour.

This is our code :
Code:

#include "Project.h"
#include "LCD2x16.h"
///////////////////////////////////////////////////////////////////////////////
BOOLEAN Ext_Int_detect = FALSE;
#int_EXT    //interrupt service routine for INT0
void  EXT_isr(void)
{
   Ext_Int_detect = TRUE;
}
///////////////////////////////////////////////////////////////////////////////
unsigned int16 old_CCP1=0, CCP1_diff;
#int_CCP1   //interrupt service routine for Capture 1
void CCP1_isr(void)
{
   CCP1_diff = CCP_1 - old_CCP1;
   
   old_CCP1 = CCP_1; //prepare for next capture
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
void main()
{
   int8 day=1;//same as: byte counter;
   int8 month=1;
   int8 year=2000;
   int8 hour=0;
   int8 min=0;
   int8 sec=0;
   float freq;
   

   
   setup_timer_3(T3_DISABLED | T3_DIV_BY_1);
   
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_1 );   //prepare timer1 for CCP1
   setup_ccp1(CCP_CAPTURE_FE);   //set up input capture for frequency detect
   
   ext_int_edge(H_TO_L);
   enable_interrupts(INT_EXT);
   enable_interrupts(INT_CCP1); //enable interrupt for input capture
   enable_interrupts(GLOBAL);
   
   set_tris_c(0xBF);
   
   delay_ms(500);    //wait a bit
   
   lcd_init();       //initialize 16x2 LCD
   printf(lcd_putc, "Hello World- LCD!\nMicro Demo"); // print on LCD
   
   printf("\fHello World- UART\n\r");  //print over UART (terminal)
   
   delay_ms(1000);   //wait 1 sec
   lcd_putc('\f');   //clear LCD
   
   while(TRUE)
   {
     if (!input(PIN_B0))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B0));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         day++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", day);
      }
      if (!input(PIN_B1))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B1));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         
         month++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", month);
      }
      if (!input(PIN_B2))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B2));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         year++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", year);
      }
      if (!input(PIN_B3))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B3));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         sec++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", sec);
      }
      if (!input(PIN_B4))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B4));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         min++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", min);
      }
      if (!input(PIN_B5))
      {
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 pressed!");
         while (!input(PIN_B5));
         delay_ms(20);  //debounce delay
         printf("\n\rPB3 released!");
         hour++;
         lcd_gotoxy(1,1);
         printf(lcd_putc, "C: %03u", hour);
      }

      //display frequency:
      freq = 1/((float)CCP1_diff/1000000);
      lcd_gotoxy(8,1);
      printf(lcd_putc, "F=%.2f", freq);
     
      delay_ms(100); //main loop delay (optional)
   }


}

When we run the simulation in PROTEUS, the simulation doesn't work and says:
Processor is in reset.

We are using a pic18f4520.

What is the error ? And what can we do to fix it ?
temtronic



Joined: 01 Jul 2010
Posts: 9229
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Apr 02, 2014 5:45 am     Reply with quote

As a first time poster you should know that we are NOT a Proteus forum. Please read PIC101.Not too many here actually use it as it is full of bugs, errors and faulty DRCs.
That being said...consider starting off small!
Cut code for the simple '1hz flashing LED' program to confirm the PIC hardware/setup runs correctly, then the "Hello LCD" program.Good news is you've included the delay_ms(500) before the LCD_init()! This will allow the LCD to properly configure itself before you access it.
Divide the project into small sections, RTC, Buttons,LCD, Logging,etc.. Work on one section at a time,test, confirm it works THEN do the next section.
Be aware that we an help you with code running on REAL PICs only ! Proteus is a simulator well known to NOT correctly emulate PICs so we have NO idea how to fix Proteus internal code.

hth
jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Wed Apr 02, 2014 7:40 am     Reply with quote

Some comments that might apply (but you don't show enough for us to know...).
You are using the same pin to trigger an interrupt, as you are using for button0.
Fuses. The default is for PORTB, to wake up set as analog inputs, unless you have the NOPBADEN fuse. You don't have any code to disable this behaviour.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Apr 03, 2014 10:50 am     Reply with quote

Code:
int8 year=2000;
What is the maximum value that fits in a 8-bit integer?
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