Sunday, December 1, 2019

Timer using AVR

Introduction 
In this code, two external interrupts are used which are INT0 and INT1 correspondings pins of PD2andPD3 to Start and Stop the Timer.

Image result for avr32 int0

when Minute Flag reaches to 20, PORTC.0 becomes LOW and remains that state for 40 another 40 minutes. 

List of Items;
1. AVR32A  
2. LCD Display
3. Jumper wires

C code;

#include <avr/io.h>                        //avr header
#include <util/delay.h>                    // utility for delay  
# include <avr/interrupt.h>        // interrupt handling header
#define D4 eS_PORTD4            // Four wire data for LCD
#define D5 eS_PORTD5            //    do
#define D6 eS_PORTD6            //    do
#define D7 eS_PORTD7            // do

#define RS eS_PORTD0            // Rs for LCD
#define EN eS_PORTD1            // Enable LCD

#include "lcd.h"                // LCD headder file


unsigned char dig[]    =    {0,0,0,0}; // SSD
unsigned char ssd[]={0xc0,0x79,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; // SSD
char buffer[50];                // buffer for LCD data
  
void decodessd(unsigned int val);    // Decode SSD
void display(unsigned int rounds, unsigned int dp , unsigned int dp1); // SSD

volatile unsigned char s=0, m=0, iled=0, LED_indicator=0;  // when optimizing it can remove unused variables, be it needs in intrrupt
volatile unsigned int ms=0, S_flag=0, dS_flag=0 , M_flag=0, Minutes=0 , enable=1; // variables

//==========================================================================================

int main(void){                    //
    DDRB = 0xff;                // data direction register set as output
    DDRD = 0b11110011;            //  -do-
    DDRC = 255;                    //
    DDRA = 0xff;                //  
    //DDRD = 1<<2 | 1<<3; // set pin direction as input for PD2 and PD3
    //DDRD = 0<<2 | 0<<3; // set pin direction as input for PD2 and PD3
    OCR0 = 15;  // value going to compare at output compare registor
    TIMSK = 1<< OCIE0;  // enable compare match interrupt
    TCCR0 = 1<<WGM01 | 1<<CS01 | 1<<CS00;  // Timer Counter Control Regi 0 prescale 64
    MCUCR = 1<<ISC11 | 1<<ISC10; // The falling edge of INT1 generates an interrupt request.
    MCUCR |= 1<<ISC10 | 1<<ISC01; // The falling edge of INT2 generates an interrupt request.
    GICR = 1<<INT0 | 1<<INT1;    //enable general int. regi. 0 and 1
  
    // (1/8)us x 64 x 125 = 1ms for 8 MHz crystal
  
    sei(); // enable globle interrupt

    Lcd4_Init();
  
        Lcd4_Clear();
        _delay_ms(100);
        Lcd4_Set_Cursor(1,5);
        Lcd4_Write_String("TIMER");
    PORTC = 0xff;
//--------------------------------------------------------------------------------------  
    while(1)
    {

        Minutes=M_flag;
//        decodessd(Minutes*1000 + S_flag*10 + dS_flag);
//        display(50,1,2); // display 10 rounds, put decial at 0 and 2
      
        Lcd4_Set_Cursor(2,0);
        sprintf(buffer,"UP/DOWN Minutes: %2d", Minutes);
        Lcd4_Write_String(buffer);
        Lcd4_Set_Cursor(1,20);
        sprintf(buffer,"      Seconds  : %2d", S_flag);
        Lcd4_Write_String(buffer);
        Lcd4_Set_Cursor(2,20);
        sprintf(buffer," 60min-OFF 210min-ON");
        Lcd4_Write_String(buffer);
        if (M_flag < 239){
                PORTC = 1<<0;
            }
        if (M_flag >240){
                PORTC = 0<<0;
                }
       if (M_flag ==300){
                PORTC = 1<<0;
                ms=0;
                dS_flag = 0;
                S_flag= 0;
                M_flag= 0;
                }
    
      
      
    }
//-----------------------------------------------------------------------------------------      
  
    return 0;
}
//==================this is not used, keep futhure upgrade for SSD================================

void decodessd(unsigned int val)
    {
        dig[0]= val/1000;
        dig[1]=(val % 1000)/100;
        dig[2]=(val % 100)/10;
        dig[3]= val% 10;}
      
//=========================================================================================

void display(unsigned int rounds, unsigned int dp , unsigned int dp1)
{

unsigned int r;
unsigned char i;
  
    for (r=0;r!=rounds;r++){

        for(i=0;i!=4;i++){
            PORTA = 1<<(4+i);
          
            //if(i==dp){
            //PORTB = ssd[dig[i]]+128;
          
            //}else if(i==dp1){
            //PORTB = ssd[dig[i]]+128;
          
            //}
            //else{
                PORTB = ssd[dig[i]];
                }
            //_delay_us(1);  
            }  
        }  


//===============TIMER0 Compare vector to calculate the time ===========================================================================

ISR(TIMER0_COMP_vect){
  
    if (enable== 1){
      
  
        if(++ms == 100){  // ++ms = firest do the increment then compare
            ms=0;                 //  ms++ means first do the comparition and then increment
      
            if(++dS_flag == 10){  // count seconds
                dS_flag = 0;
          
                S_flag++;
              
            }
  
            if(S_flag ==60){
                S_flag= 0;
              
                M_flag++;
            }
        }
    }
  
}

//==============When INT0 trigger do this========================================================================

ISR (INT0_vect){
                ms=0;
                dS_flag = 0;
                S_flag= 0;
                M_flag= 0; }

//=============When INT1 trigger do this=========================================================================

ISR (INT1_vect){
    if(enable == 1){
            enable = 0; }
  
    else{
            enable = 1; }

        }      

No comments:

Search This Blog