Search This Blog

Tuesday, March 23, 2010

Timers example in ATmega16, codevision TIMER1

TIMER1 is 16 bit timer.

Here is an example with variable time of timer.
ADC Volt - PORTA.0
LCD PORT C
PUSH BUTTONS PORTB.0-7
LED/OUTPUT PORTD.0-7

TIMER1 PRESCALER
TCCR1A=0b00000000;
TCCR1B=0b00000010;             //8

COUNTER VALUE 
TCNT1=55535;  

CALCULATION 
IF 65535-X = Y AND
WE ASSIGN TCNT1=Y;
TIMER CODE WILL BE EXECUTED AFTER EVERY X MICRO-SECS  
 --------------------------------------------------------------------
#include mega16.h                  //8 M Hz
#include delay.h
#include stdio.h
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include lcd.h

// I2C Bus functions
#asm
   .equ __i2c_port=0x12 ;PORTD
   .equ __sda_bit=3
   .equ __scl_bit=2
#endasm
#include

#define ADC_VREF_TYPE 0x20
// Read the 8 most significant bits of the AD conversion result
unsigned char read_adc(unsigned char adc_input)
{
    ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
    // Delay needed for the stabilization of the ADC input voltage
    delay_us(10);
    // Start the AD conversion
    ADCSRA|=0x40;
    // Wait for the AD conversion to complete
    while ((ADCSRA & 0x10)==0);
    ADCSRA|=0x10;
    return ADCH;
}

// global variables
//unsigned int freq_array[]={50,100,150,200,250,300,350,400,450,500,550,600,650,700,750,800,850,900,950,1000} ;
//unsigned int no_of_pulses[]={2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,40} ;
//unsigned int pc_array[]={1,2,5,10,20,50,100,200,500,1000} ;
void PC_OUTPUTS(void);
void DELAY(void);
void ADC_DELAY(unsigned int);
void READ_VALUES(void);
void CODE(void);
unsigned int actual_delay_time,volt;
     
void WRITE_TO_EEPROM(void);
unsigned int FREQ_COUNTER=2;
unsigned int PC_COUNTER=1;
unsigned char adc_volt;                                   
unsigned char str_buffer[20];
unsigned int BTN_0=0;      
unsigned int BTN_1=0;
unsigned int BTN_2=0;
unsigned int BTN_3=0;
unsigned int BTN_4=0;

unsigned int ON_OFF_FLAG=1;       //CAL ON/OFF flag

//Timer 0 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
        TCNT1=actual_delay_time;
        PORTD.7=~PORTD.7;
}


void main(void)
{


// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0xFF;
DDRB=0x00;          //input   push buttons

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xff;           //output         lcd

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0xff;           //output     led

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

     
TCNT1=55535;
TCCR1A=0b00000000;
TCCR1B=0b00000010;   //SELECT CLOCK HRER CS
       
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

//Timer(s)/Counter(s) Interrupt(s) initialization
//TIMSK=0x00;
TIMSK=0b00000100;
// Analog Comparator initialization
// Analog Comparator:
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA6;
SFIOR&=0x1F;

PORTD.2=0;
PORTD.3=0;
// I2C Bus initialization
i2c_init();
//WRITE_TO_EEPROM();    //this will reset both var to 0
READ_VALUES();
//INIT LCD
lcd_init(20);
lcd_clear();
lcd_putsf("ETM");
delay_ms(1000);

#asm("sei")
//START FROM THE LAST SAVED
DELAY();
PC_OUTPUTS();

// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/256k
#pragma optsize-
WDTCR=0x1C;
WDTCR=0b00001111;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif


while (1)
      { 
         //read voltage from PORTA.0  0 - 255   
         WDTCR=0b00001111;
         adc_volt=read_adc(0);                                 //100 us
         if (ON_OFF_FLAG==1)
         {     
           DELAY();
         }
         else
         {
                 PORTD.7=0;
         }
         WDTCR=0b00001111;          
         CODE(); 
         delay_ms(20);          
      };                                                        //END OF WHILE LOOP              
}                                                               //END OF MAIN

void CODE()
{
        //CAL ON OFF
        if (PINB.4 == 0 && BTN_4==0)
         {
            if (ON_OFF_FLAG==1) 
            {
                ON_OFF_FLAG=0;
                lcd_gotoxy(14,1);
                lcd_putsf("   OFF");  
                PORTD.7=0;
                TCCR1A=0b00000000;
                TCCR1B=0b00000000;   //STOP TIMER     
            }   
            else
            {
                ON_OFF_FLAG=1; 
                lcd_gotoxy(14,1);
                lcd_putsf("    ON");
                PC_OUTPUTS();  
                TCCR1A=0b00000000;
                TCCR1B=0b00000010;   //START TIMER           prescaler=8
            }
            BTN_4=1;
         }
         if (PINB.4 == 1)
            BTN_4=0;              
                   
        //INCREASE NO OF PULSES PORTB 0 PIN       
        if (PINB.0 == 0 && BTN_0==0)
         {
            if (FREQ_COUNTER < 40)
             {
               FREQ_COUNTER=FREQ_COUNTER+2;
               WRITE_TO_EEPROM();
             }
             BTN_0=1; 
         }
        if (PINB.0 == 1)
            BTN_0=0;       
        
        //DECREASE NO OF PULSES PORTB 1 PIN     
        if (PINB.1 == 0 && BTN_1==0)
         {    
            if (FREQ_COUNTER > 2)
             {
                FREQ_COUNTER=FREQ_COUNTER - 2;
                WRITE_TO_EEPROM();
             }
             BTN_1=1; 
         } 
        if (PINB.1 == 1)
            BTN_1=0;       
             
        //INCREASE IN PC COUNT PORTB 2 PIN     
        if (PINB.2 == 0 && BTN_2==0)
         {
            if (PC_COUNTER < 10)
             {      
                PC_COUNTER++;
                PC_OUTPUTS();
                WRITE_TO_EEPROM();
             }
             BTN_2=1;
         }
        if (PINB.2 == 1)
            BTN_2=0;       
          
        //DECREAE IN PC COUNT PORTB 2 PIN     
        if (PINB.3 == 0 && BTN_3==0)
         {
            if (PC_COUNTER > 1)
             {
                PC_COUNTER--;
                PC_OUTPUTS();
                WRITE_TO_EEPROM();
             }
             BTN_3=1;  
         }
        
        if (PINB.3 == 1)
            BTN_3=0;       
      
        //BATTERY INDICATION  PORTB 5 PIN - LO   6 - OK  7 - CH
        if (PINB.6 == 0)
         {
            lcd_gotoxy(14,0);
            lcd_putsf("BAT OK");
         }
         else if (PINB.7 == 0)
         {
            lcd_gotoxy(14,0);
            lcd_putsf("BAT CH");
         }  
         else if  (PINB.5 == 0)
         {
            lcd_gotoxy(14,0);
            lcd_putsf("BAT LO");
         }
         else
         { 
            lcd_gotoxy(14,0);
            lcd_putsf("      ");   
         }
}

//------------------------------------------------------------------------------
// Procedure:    PC_OUTPUTS
// Inputs:        none
// Return:        none
// Description:    Vary the 5 PC signals
//------------------------------------------------------------------------------

void PC_OUTPUTS(void)
{
       switch (PC_COUNTER)
       {
         case 1:      
             PORTD.0=0;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=0;
             PORTD.6=1;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     1   PC");
             break;  
         case 2: 
             PORTD.0=0;
             PORTD.1=1;
             PORTD.4=0;  
             PORTD.5=1;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     2   PC");
             break;
         case 3:
             PORTD.0=1;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=1;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     5   PC");
             break;
         case 4:
             PORTD.0=0;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=1;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     10  PC");
             break;
         case 5:
             PORTD.0=0;
             PORTD.1=1;
             PORTD.4=1;  
             PORTD.5=0;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     20  PC");
             break;   
          case 6:      
             PORTD.0=1;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=0;
             PORTD.6=0; 
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     50  PC");
             break;  
         case 7: 
             PORTD.0=0;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=0;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     100 PC");
             break;
         case 8:
             PORTD.0=0;
             PORTD.1=1;
             PORTD.4=0;  
             PORTD.5=0;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     200 PC");
             break;
         case 9:
             PORTD.0=1;
             PORTD.1=0;
             PORTD.4=0;  
             PORTD.5=0;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     500 PC");
             break;
         case 10:
             PORTD.0=0;
             PORTD.1=0;
             PORTD.4=0;  
             PORTD.5=0;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             lcd_putsf("PULSE MAG     1000PC");
             break; 
         default:
             PORTD.0=0;
             PORTD.1=0;
             PORTD.4=1;  
             PORTD.5=1;
             PORTD.6=0;
             lcd_gotoxy(0,1);
             //sprintf(str_buffer,"ERROR PC - %u",PC_COUNTER) ;
             lcd_putsf("INC/DEC");
             PC_COUNTER=1;

       };
}

//------------------------------------------------------------------------------
// Procedure:    WRITE_TO_EEPROM
// Inputs:        none
// Return:        none
// Description:    Writes both variable to eeprom
//------------------------------------------------------------------------------
void WRITE_TO_EEPROM()
{
    i2c_start();
    i2c_write(0xA0);    
    i2c_write(0x00);    
    i2c_write((unsigned char)FREQ_COUNTER);
    i2c_write((unsigned char)PC_COUNTER);   
    i2c_stop();
    delay_ms(10);
}

//------------------------------------------------------------------------------
// Procedure:    READ_VALUES
// Inputs:        none
// Return:        none
// Description:    Read freq counter, PC Counter var from eeprom
//------------------------------------------------------------------------------
void READ_VALUES()
{
    i2c_start();
    i2c_write(0xA0);    
    i2c_write(0x00);
    i2c_start();
    i2c_write(0xA1);         
    FREQ_COUNTER = i2c_read(0);
    delay_ms(10);
   
    i2c_start();
    i2c_write(0xA0);    
    i2c_write(0x01);
    i2c_start();
    i2c_write(0xA1);         
    PC_COUNTER = i2c_read(0);
   
    i2c_stop();
    delay_ms(10);
}
Search Amazon.com for
//------------------------------------------------------------------------------
// Procedure:    ADC_DELAY
// Inputs:        DELAY (STEP) TIME
// Return:        none
// Description:    DELAY +/- 2.5%
//------------------------------------------------------------------------------
void ADC_DELAY(unsigned int delay_time)
{
   int a;
   a=delay_time / 1000;
   if (a < 1)
   a=1;
   volt=(unsigned int)adc_volt;          
   
    if (volt > 0 && volt < 128)
    {  
     if (delay_time < 501) 
       actual_delay_time= delay_time - (( 50 - ( 2 * volt ) / 10 ) * a)/2;    
     else
       actual_delay_time= delay_time - ( 50 - ( 2 * volt ) / 10 ) * a;
    }  
    if (volt > 127 && volt < 256)
    {  
        volt=volt - 127;
         if (delay_time < 501)
           actual_delay_time= delay_time  + ( ( ( 4 * volt ) / 10 ) * a)/2 ;
         else
           actual_delay_time= delay_time  + ( ( 4 * volt ) / 10 ) * a ;
    } 
    if (volt == 0)
       actual_delay_time= delay_time - (5  * (delay_time / 100) );   
      

      
      lcd_gotoxy(0,2);
      sprintf(str_buffer,"%u,%u,%u     ",delay_time,actual_delay_time,adc_volt)  ;
      lcd_puts(str_buffer);     
     
      actual_delay_time=65535-actual_delay_time;
      TCNT1=actual_delay_time;    
     
      WDTCR=0b00001111;                                  
}

//------------------------------------------------------------------------------
// Procedure:    DELAY
// Inputs:        none
// Return:        none
// Description:    Vary the Delay of signal
//------------------------------------------------------------------------------
void DELAY(void)
{
     switch (FREQ_COUNTER)
       {
         case 2: 
             ADC_DELAY(10000);
             //delay_ms(10);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 2 ");
             break;  
         case 4:                      
             ADC_DELAY(5000); 
            //delay_ms(5);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 4 ");
             break;
         case 6:
             ADC_DELAY(3333);
             //delay_us(3333); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 6 ");
             break;
         case 8:
             ADC_DELAY(2500);
             //delay_us(2500);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 8 ");
             break;
         case 10:
             ADC_DELAY(2000);
             //delay_ms(2); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 10 ");
             break;   
         case 12:
             ADC_DELAY(1667);      
             //delay_us(1667); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 12 ");
             break;  
         case 14:
             ADC_DELAY(1428);
             //delay_us(1428);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 14 ");
             break;
         case 16:
             ADC_DELAY(1250);
             //delay_us(1250);   
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 16 ");
             break;
         case 18:
             ADC_DELAY(1111);
             //delay_us(1111);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 18 ");
             break;
         case 20:
             ADC_DELAY(1000);
             //delay_ms(1);  
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 20 ");
             break;   
         case 22:
             ADC_DELAY(909);     
             //delay_us(909);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 22 ");
             break;  
         case 24:  
             ADC_DELAY(833);
             //delay_us(833); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 24 ");
             break;
         case 26:
             ADC_DELAY(769);
             //delay_us(769); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 26 ");
             break;
         case 28:
             ADC_DELAY(714);
             //delay_us(714);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 28 ");
             break;
         case 30:
             ADC_DELAY(667);
             //delay_us(667);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 30 ");
             break;   
         case 32:    
             ADC_DELAY(625);
             //delay_us(625);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 32 ");
             break;  
         case 34:
             ADC_DELAY(588);
             //delay_us(588);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 34 ");
             break;
         case 36:  
             ADC_DELAY(556);
             //delay_us(556);   
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 36 ");
             break;
         case 38: 
             ADC_DELAY(526);
            // delay_us(526);
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 38 ");
             break;
         case 40:   
             ADC_DELAY(500);
             //delay_us(500); 
             lcd_gotoxy(0,0);
             lcd_putsf("PULSES 40 ");
             break; 
         default:
             ADC_DELAY(500);
             //delay_us(500);
             lcd_gotoxy(0,0);
             //sprintf(str_buffer,"ERROR-%u",FREQ_COUNTER)  ;
             lcd_putsf("INC/DEC"); 
             FREQ_COUNTER=2;
       };
}
 

No comments:

Post a Comment