Thread: Variable Timer assistance

  1. #1
    Registered User
    Join Date
    Jan 2009
    Posts
    2

    Variable Timer assistance

    Hi there, i'm very new to c programming and i have an assignment to create a datalogger. The datalogger is sample voltages at a set time (i've set it for every second) . Now i have completed this and got it to work on the microcontroller i am supposed to use however i've recently been told that i can also make the timer variable which i would like to experiment with however i have very little idea of how to do this. i imagine it would be along the lines of making an array and changing the frequency of sampling this way. any assistance would be appreciated.
    Code:
    #include <stdio.h>
    #include <LPC23xx.H>                    /* LPC23xx definitions                */
    #include "adc.h"
    
    /* Import external functions from Serial.c file                               */
    extern void Serial_init (void);
    
    
    
    int main (void)
     {
        int i = 0;
    	int clock = 0;
    	int ADC0value[ADC_NUM];
        int pushbutton;
    	int x[20];
    
    
        ADCInit( ADC_CLK );                         
        Serial_init();                        /* Init UART  */
      	FIO2DIR &= ~(1<<10);
    	
    
    	printf("Data Logger\n");
    		
      
        while(1)
        {
            for ( i = 0; i < ADC_NUM; )
            {
    		                         
             	T0MR0         = 1200000;                     
     		    T0MCR         = 3;                          
        		T0TCR         = 1;    
    		while(1)
      		  {
    
    		//wait for pushbutton to be pressed
    
    		
    			 printf("Press the button now to begin sampling\n");
    			 	while ((FIO2PIN & (1<<10)) != 0 )
    			{
    				;
    			}
    
    
    		pushbutton ++;
    		FIO2PIN = pushbutton;
    
    			while ((FIO2PIN & (1<<10)) == 0 )
    			{
    				;
    			}
    
    			
    		
    		   while(1)
    		   {
    	                    /* Timer0 Enable               */
    			
            if ((clock == 0) || (clock == 8))
            {
    			ADC0value[i] = ADC0Read(i);
                printf("ADC channel %d = %d\n", i, ADC0value[i]);
            }
    		
           
    
            clock++;
            if (clock == 8)
            {
                clock = 0;
            }
            
    		
           
           // wait for the time period to elapse
            while((T0IR & 0x01) == 0)
            {
                ;
            }
            
            T0IR |= 0x01 ;
    
    		}	
     	 }
    	 }
    	 }
    	 }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    2
    before anyone says i know i have declared int x[20] and not used it, i was playing around earlier and forgot to remove it lol.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
            clock++;
            if (clock == 8)
            {
                clock = 0;
            }
    As long as 8 is a constant value that is always 2 ^ n, then you can use binary logic to do this in one line:
    Code:
       clock = (clock + 1) & (8-1);
    That will keep clock in a range of 0..7.

    If clock is always 0..7, then if ((clock == 0) || (clock == 8)) will never be true for clock==8.

    We do not see where you are setting up your timer, so I can't tell you how to make it changeable.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. Question about printing a variable???
    By Hoser83 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2006, 01:57 PM
  3. How accurate is the following...
    By emeyer in forum C Programming
    Replies: 22
    Last Post: 12-07-2005, 12:07 PM
  4. Use of variable
    By alice in forum C Programming
    Replies: 8
    Last Post: 06-05-2004, 07:32 AM
  5. Replies: 2
    Last Post: 04-12-2004, 01:37 AM