How to set TMRO & TMR3 as Counter?.


Closed Thread
Results 1 to 22 of 22

Hybrid View

  1. #1
    ra68gi's Avatar
    ra68gi Guest


    Did you find this post helpful? Yes | No

    Default

    Hi nature tech,
    Its been a long time since i last posted on this forum. I have written a program for a rotary encoder. But i have written it in C. I have used pic16f72 and have made 1x, 2x, 4x measurements. In 1x it increments counter only once for every pulse. I had used a 1000ppr(pulse per rev) encoder & the count value displayed on a LCD. So the resolution is 360degrees/1000. With 2x you will get double & with 4x you get 360/4000.
    I have used different interrupt sources like portb int0, ccp,timer0, timer1.
    If you are interested i will send you the source code.
    The compiler Iam using is free version & you can write upto 2k code. And all this with instant interrupts.
    If you are interested let me know & i will post it.

    Raghunathan.

  2. #2
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Mikro C
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  3. #3
    ra68gi's Avatar
    ra68gi Guest


    Did you find this post helpful? Yes | No

    Default

    No. It's BoostC.
    If you are new to C & interested to learn it quickly, see this link..http://forum.sourceboost.com/index.php?showtopic=2399. C is more structured & universal language. I migrated to C recently & its power is awsome as regards to microcontroller programming. If you intend to impliment rtos on small ram pics, that too is available called nova rtos. Have a try.
    Regards

    Raghunathan.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Sounds good... yet another C compiler

    Well, i'm not going to move on it as long as i'll don't have any customer asking for OR if a free and legit full-version appear in my mail-box .

    I have and use too much different compiler now (C, BASIC)... hard to get comfortable with all of them...

    My favourites
    1. PicBasicPro
    2. SwordFish
    3. Hi-Tech C
    Last edited by mister_e; - 15th May 2007 at 04:17.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Jun 2005
    Location
    Penang
    Posts
    40


    Did you find this post helpful? Yes | No

    Default Hi all.

    Hi ra68gi,

    I think i wanna have a look on your code as well.Is your code is an adaptation from "instant interrupt" which you mix with asm or else?Well,1x, 2x,4x resolution sounds great.It might be a choice which i will skip using PLD or CPLD to do the job.If this doesnt work then i will opt for 2 PIC18F4331 which is connected through I2C that will enable me to control 2 motors almost at the same time.

    Talk about C,yes it's a great stuff.You just have to include the related <.h> file and tweak a bit in you code,and there you go.I think you could download sourceboost free?Or limited somehow.

  6. #6
    ra68gi's Avatar
    ra68gi Guest


    Did you find this post helpful? Yes | No

    Default

    Yes, you get 2k version absolutely free with all the frills like a beautiful simulator / debugger with no other limitation other then 2k code limit. Floating point routines is also available. Consider the pic basic compiler(2k page limit pbc) which is sold at $100. And for just $70 you get a full version BoostC compiler( no limit).

    All compilers have instant interrupts its only pic basic compilers that don't have instant interrupts (God knows why)ie. the interrupt latency is much higher than generated by other compilers. more over the pbc commands are non re-enterent, meaning that only after it completes executing the whole command( say pause 1000) will it enter the ISR. Which is very bad & thats why Darrel has created the instant interrupt routines for PBC.

    coming to the project..
    Both clockwise & anti clockwise the LCD is made to display only positive values. If you wish you can rewrite the code to get -ve value displayed.
    Any thing is possible with this compiler.

    The 1x code is commented well but not the 4x( i didn't have time).
    Ask me if you need the 2x code. I suppose the 4x will give you all the information to build the 2x.

    The 1x code is as follows...
    Code:
    --------------------------------------------------------------------------
    // this test builds & works properly.	
    #include "system.h"
    #include "lcd_driver.h"
    #include "stdio.h"
    // Set clock frequency to 4MHz.
    
    #pragma CLOCK_FREQ 4000000
    
    //set configuration fuse.
    #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF
    
    #define LCD_ARGS 	2,	/* Interface type: mode 0 = 8bit, 1 = 4bit(low nibble), 2 = 4bit(upper nibble) */ \
    		0, 				/* Use busy signal: 1 = use busy, 0 = use time delays */\
    		PORTC, TRISC, 	/* Data port and data port tris register */ \
    		PORTB, TRISB, 	/* Control port and control port tris register */ \
    		2,				/* Bit number of control port is connected to RS */ \
    		7,				/* Bit number of control port is connected to RW */ \
    		3 				/* Bit number of control port is connected to Enable */
    
    
    
    signed long counter;
    unsigned long counter1;
    unsigned int value1;
    bit update_flag;
    
    /*Interrupt service routine (ISR).On ccp capture interrupt, program 
      will jump to this code location.  */	
    void interrupt( void )
    {
      update_flag = 1 ;
    
       if(portb.0 == 0)   //check the quadrature signal
      {
       counter ++ ;      // increment counter
      }             
       else         //else decrement counter.
      {
       counter -- ;
      }  
       pir1.2 = 0;       //clear ccp1if 
    }
     
    
      
    
    /* function to display counter. */
    void display (unsigned int x)
    {
    unsigned int xn ;
    unsigned int xd ;
    char buf[ 10 ];
        lcd_clear();
        lprintf("count=");
        sprintf( buf, "%d", x );
        lprintf( buf );
    }
    
    
    /* The main code configures the intcon, pie1, t1con & ccpcon1 
       registers.*/
    
    void main()
    {
    	trisb = 1;		 //configure portb.0 as input & rest as 
    	portb = 0;		 //clear port B
    	trisc = 0b00000100; //RC2/ccp1 pin as input.
    
    	
    // enable interrupts: interrupt control register.
    	intcon.6=1;     //enable peripheral interrupts    
    	intcon.7=1;     //enable global interrupt
    	
    //peripheral interrupt enable register 1.
        pie1.2 = 1;    //enable ccp1 interrupt.
        
        
    
        
    /*ccpcon1: capture/compare/pwm control register1. 
        
        bit3-0.....ccpxm3:ccpxm0: ccpx mode select bits.
        we will use...
        0100 = capture mode, every falling edge.
        0101 = capture mode, every rising edge.
        we can choose any one of the above mode.
        but we will use 0101 in our project.
        to get less count use 0110( divide by 4).
        or 0111 for divide by 16.  
    */
    
    /* setup LCD */
        lcd_setup();
     
            
        update_flag = 1;  // start counter from 0
        ccp1con = 0b00000101;  //set capture mode.   
        counter = 0;  //reset counter.
     while(1)
     {      
        while (update_flag == 1)   
       {
         if(counter < 0 )
         { 
          counter1 = (-1)* counter;
           display(counter1) ;
           }
        else
          { 
           display(counter) ;
          }  
        update_flag = 0 ;    
       }	 
      }
    }
    --------------------------------------------------------------------------
    code for the 4x...

    Code:
    // this test builds & works properly.
    	
    #include "system.h"
    #include "lcd_driver.h"
    #include "stdio.h"
    // Set clock frequency to 20MHz.
    
    #pragma CLOCK_FREQ 20000000
    
    //set configuration fuse.
    #pragma DATA _CONFIG, _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_OFF
    
    #define LCD_ARGS 	2,	/* Interface type: mode 0 = 8bit, 1 = 4bit(low nibble), 2 = 4bit(upper nibble) */ \
    		0, 				/* Use busy signal: 1 = use busy, 0 = use time delays */\
    		PORTC, TRISC, 	/* Data port and data port tris register */ \
    		PORTB, TRISB, 	/* Control port and control port tris register */ \
    		2,				/* Bit number of control port is connected to RS */ \
    		7,				/* Bit number of control port is connected to RW */ \
    		3 				/* Bit number of control port is connected to Enable */
    
    
    
    signed long counter;
    unsigned int value1;
    bit update_flag;
    
    /*Interrupt service routine (ISR).On ccp capture interrupt, program 
      will jump to this code location.  */	
    void interrupt( void )
    {
      update_flag = 1 ;
    
       if(intcon.1)   //RB0/int interrupt(rising edge)
     {
       if(portc.1 == 0)
        {
          counter ++ ;      // increment counter
        }             
       else         //else decrement counter.
        {
          counter -- ;
        }  
       intcon.1 = 0;       //clear intf 
     }
            else
       if(intcon.2)   //tmr0if (falling edge)
     {
       if(portc.1 == 1)
        {
           counter ++ ;
        }
       else
        {
           counter -- ;
        }
       tmr0 = 255 ;
       intcon.2 = 0;
      }
           else
        if(pir1.0)    //tmr1if
     {
        if(portc.3 == 1)
         {
            counter ++ ;
         }
        else
         {
            counter -- ;
         }
        t1con.0 = 0 ;
        tmr1l = 255 ;
        tmr1h = 255 ;
        t1con.0 = 1 ;
        pir1.0 = 0 ;
     }
           else
        if(pir1.2)     //ccp1if
     {
        if(portc.3 == 0)
         {
            counter ++ ;
         }                                           
        else
         {
            counter -- ;
         }
        pir1.2 = 0;
     }
    }        
     
    
      
    
    /* function to display counter. */
    void display (unsigned long x)
    {
        char buff [ 10 ] ;
        sprintf32(buff,"%d",x);
        lcd_clear();
        lprintf("count=");   
        lprintf (buff);
        
    }
    
    
    /* The main code configures the intcon, pie1, t1con & ccpcon1 
       registers.*/
    
    void main()
    {
    	trisb = 1;		 //configure portb.0 as input & rest as 
    	portb = 0;		 //clear port B
    	trisc = 0b00000111; //RC2/ccp1, RC0, RC1 as inputs.
    
    //configure option register bits 
    //portb pull-ups(rbpu)(disable)...bit7  
    //interrupt on rising edge(RB0/int)(intedg)...bit6
    //tmr0 clock source(tocs) is transition on RA4...bit5
    //tmr0 source edge select(tose) is high to low transition...bit4
    //bit3-bit0...don't care.
        option_reg.6 = 0b11111111;
        	
    // enable interrupts: interrupt control register.
    //	intcon.6=1;  enable peripheral interrupts    
    //	intcon.7=1;  enable global interrupt
    //  intcon.5=1;  enable tmr0 overflow interrupt.
    //  intcon.4=1;  enable inte (RB0/int)
    //  intcon.1=0;  intf ( RB0/int flag)
     
    //     intcon = 0b11110000 ;	
    //i will enable tmr0 & RB0/int along with ccp and tmr1 interrupt.
    
    
    //timer1 module: configure t1con.
    /*TIMER1 INCREMENTS ONLY IN RISING EDGE*/
    //bit7-6....00
    //bit5-4....00...prescaler 1:1
    //bit3......0....t1oscen cleared.(makes RC0 as clk input)
    //bit2......0....synchronize external clock input.
    //bit1......1....external clock on pin RC0.
    //bit0......1....start timer1. 
    //     t1con = 0b00000011;
    //we will start all interrupts at one go a bit latter. 
       
    //peripheral interrupt enable register 1.
         pie1.2 = 1;        //enable ccp1 interrupt.
         pie1.0 = 1;        //enable tmr1 interrupt.
        
    /*ccpcon1: capture/compare/pwm control register1. 
        
        bit3-0.....ccpxm3:ccpxm0: ccpx mode select bits.
        we will use...
        0100 = capture mode, every falling edge.
        0101 = capture mode, every rising edge.
        we can choose any one of the above mode.
        but we will use 0101 in our project.
        to get less count use 0110( divide by 4).
        or 0111 for divide by 16.  
    */
    
    //  LCD setup 
        lcd_setup();
       
        update_flag = 1;  // start counter from 0
        ccp1con = 0b00000100;  //set capture mode in falling edge.
        tmr0 = 255;  // a pulse will generate an overflow interrupt.
        tmr1l = 255;
        tmr1h = 255;    // a single pulse input will gen an interrupt.
        t1con = 0b00000011;  // start timer1 as pulse counter. 
        intcon = 0b11110000 ;  //enable RB0/int, tmr0 interrupt   
        counter = 0;  //start measurement.
     while(1)
     {      
        while (update_flag == 1)   
       {
         if(counter < 0 )
         { 
           counter = (-1)*counter;
           }
         update_flag = 0 ;
         display(counter) ;
       }	 
     }
    }
    --------------------------------------------------------------------------
    Regards
    Raghunathan.
    Last edited by ra68gi; - 15th May 2007 at 18:54. Reason: unable to see #include <system.h>

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    All compilers have instant interrupts its only pic basic compilers that don't have instant interrupts (God knows why)
    Yeah... but god did it for us...

    Instant interrupts - Revisited
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Pin won't stay high during Repeat...Until loop??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 16th August 2009, 23:57
  2. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  3. Counter not counting !!!
    By lerameur in forum mel PIC BASIC Pro
    Replies: 24
    Last Post: - 20th February 2009, 22:15
  4. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  5. error on compiling
    By parker in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 16th June 2005, 14:31

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts