Help with CCP - capture mode?

Discussion in 'Embedded Systems and Microcontrollers' started by hemnath, Apr 11, 2013.

  1. hemnath

    Thread Starter New Member

    Dec 12, 2012
    20
    0
    I'm trying to display frequency in LCD. But its not working. Please help.
    I'm giving input from function generator to pin C2.

    Code ( (Unknown Language)):
    1. #include "18f2520.h" #include "f2420_regs.h" #fuses INTRC #use delay(clock=4000000)  #define RS PIN_A2 #define EN PIN_A1  void lcd_init(); void lcd_cmd(unsigned char); void lcd_data(unsigned char);  unsigned int16 value; unsigned int8 high_byte, low_byte;  void main() { lcd_init(); TRIS_C2  =   1;              //  PIN C2 configured as input for capture mode TRISB   =   0;  CCP1CON =   0b00000101;     //  Capture mode, every rising edge CCPR1H  =   0; CCPR1L   =   0;  T1CON   =   0b11001000;     //  16-Bit, 1:1 Prescale value  while(1) { TMR1H    =   0; TMR1L    =   0; CCP1IF   =   0;              //  Interrupt flag is cleared while(CCP1IF == 0);       //  waits till Interrupt flag gets cleared  TMR1ON  =   1;              //  Timer1 ON  CCP1IF   =   0;              //  Interrupt flag is cleared while(CCP1IF == 0);       //  waits till Interrupt flag gets cleared  TMR1ON  =   0;              //  Timer1 OFF  low_byte = CCPR1L; high_byte = CCPR1H;  value   =   ((high_byte * 256) + low_byte)*60;  CCP1IE  =   0; CCP1IF   =   0; lcd_cmd(0x01); lcd_cmd(0x80); printf(lcd_data,"%lu HZ",value); delay_ms(1000); } }  void lcd_init() { lcd_cmd(0x30);      // Configure the LCD in 8-bit mode, 1 line and 5x7 font //lcd_cmd(0x28); lcd_cmd(0x0c);      // display on and cursor off lcd_cmd(0x01);      // clear display screen lcd_cmd(0x06);      // increment cursor lcd_cmd(0x80);      // set cursor to 1st line }  void lcd_cmd(unsigned char c) { output_b(c); output_low(RS); output_high(EN); delay_ms(15); output_low(EN); }  void lcd_data(unsigned char z) { output_b(z); output_high(RS); output_high(EN); delay_ms(15); output_low(EN); }
     
  2. hemnath

    Thread Starter New Member

    Dec 12, 2012
    20
    0
    I'm trying to display frequency in LCD. But its not working. Please help.
    I'm giving input from function generator to pin C2.

    #include "18f2520.h" #include "f2420_regs.h" #fuses INTRC #use delay(clock=4000000) #define RS PIN_A2 #define EN PIN_A1 void lcd_init(); void lcd_cmd(unsigned char); void lcd_data(unsigned char); unsigned int16 value; unsigned int8 high_byte, low_byte; void main() { lcd_init(); TRIS_C2 = 1; // PIN C2 configured as input for capture mode TRISB = 0; CCP1CON = 0b00000101; // Capture mode, every rising edge CCPR1H = 0; CCPR1L = 0; T1CON = 0b11001000; // 16-Bit, 1:1 Prescale value while(1) { TMR1H = 0; TMR1L = 0; CCP1IF = 0; // Interrupt flag is cleared while(CCP1IF == 0); // waits till Interrupt flag gets cleared TMR1ON = 1; // Timer1 ON CCP1IF = 0; // Interrupt flag is cleared while(CCP1IF == 0); // waits till Interrupt flag gets cleared TMR1ON = 0; // Timer1 OFF low_byte = CCPR1L; high_byte = CCPR1H; value = ((high_byte * 256) + low_byte)*60; CCP1IE = 0; CCP1IF = 0; lcd_cmd(0x01); lcd_cmd(0x80); printf(lcd_data,"%lu HZ",value); delay_ms(1000); } } void lcd_init() { lcd_cmd(0x30); // Configure the LCD in 8-bit mode, 1 line and 5x7 font //lcd_cmd(0x28); lcd_cmd(0x0c); // display on and cursor off lcd_cmd(0x01); // clear display screen lcd_cmd(0x06); // increment cursor lcd_cmd(0x80); // set cursor to 1st line } void lcd_cmd(unsigned char c) { output_b(c); output_low(RS); output_high(EN); delay_ms(15); output_low(EN); } void lcd_data(unsigned char z) { output_b(z); output_high(RS); output_high(EN); delay_ms(15); output_low(EN); }
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
  4. Eric007

    Senior Member

    Aug 5, 2011
    1,044
    33
    :D lol...No one here has ever posted a code like this!

    Please edit it if you want some help...
     
  5. hemnath

    Thread Starter New Member

    Dec 12, 2012
    20
    0
    I have used code tags. But i dont know why it is posted like that. Some problem with the formating. Please look into this text file.
     
  6. Motardo

    New Member

    Sep 21, 2011
    20
    2
    What specifically do you mean when you say it's not working? One problem I see is the line:
    value = ((high_byte * 256) + low_byte)*60;

    You can't get Hz by multiplying the period by 60. You need to divide 1 by it and multiply by the timer frequency. So if your timer is running at 1MHz, then:
    period = ((high_byte * 256) + low_byte);
    value = 1000000 / period;
     
  7. hemnath

    Thread Starter New Member

    Dec 12, 2012
    20
    0
    I ll define the problem now.

    I'm giving the input pulse from function generator. say about 500 Hz and connected to PIN C2 of micro-controller. And i interfaced LCD to uC. But in the LCD display, it displays random values and keeps on changing. Did i configured the registers correctly? Please help with that
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Just to be sure. You are 100% certain that your LCD program segment works as they should. Have you tested them with a simple "hello world" program
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    BTW, the problem with the code tags is your source code does not contain a "CR/LF" or carriage return and a line feed at the end of each line. That's the windows standard that even notepad.exe respects (and demands). Unix and Macintosh use other methods.

    There's a nice free program called "Programmer's Notepad" that can make those conversions for you.

    And as always, hit the "Go Advanced" and "Preview Post" buttons before you submit.
     
  10. hemnath

    Thread Starter New Member

    Dec 12, 2012
    20
    0
    yes i'm 100% sure about the lcd program. I have been using this LCD routine and implemented in many of my projects and worked successfully. I'm new to CCP- capture mode. I feel difficult in configuring, when have to capture the timer values and clear the interrupt flags.
    Please guide me.

    Thanks in advance :)
     
Loading...