⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 AVR DRAGON的仿真版配套源程序,包括定时器,LCD驱动,按键处理,AD转换等等.对AVR初学者甚为有用
💻 C
字号:
//*****************************************************************************
//  File........: main.c
//  Author(s)...: ATMEL Norway
//  Target(s)...: ATmega169 in Butterfly or STK502
//  Compiler....: IAR EWAAVR 2.28a
//  Description.: Main loop and initialization
//  Revisions...: 1.0
//  YYYYMMDD - VER. - COMMENT                                       - SIGN.
//  20030326 - 1.0  - Created                                       - AR
//*****************************************************************************

//  Include files
#include <avr/interrupt.h>
#include "Main.h"
#include "LCD_driver.h"
#include "ADC.h"
#include "button.h"

/******************************************************************************************
*   Function name:  main
*   returns:        None
*   parameters:     None
*   Purpose:        Main function which contains the endless main loop
******************************************************************************************/
int main(void)
{
    //Variables
    unsigned int MaxValue, MinValue, Value;
    char key;
    
    //Initialization
    LCD_Init();               // General LCD Initialization Routine from AVR064
    ADC_init(LIGHT_SENSOR);   // Set ADC Mux to measure on LDR resistor    
    Button_Init();            // Initialize Button reading

    //PWM initialization draft
    TCCR1A = (1<<COM1A0);     // Toggle OC1A on compare match
    TCCR1B = (1<<WGM12);      // Clear Timer on Compare Match (CTC) Top value in OCR1A
   
    OCR1AH = 0x11;              // Set a initial value in the OCR1A-register
    OCR1AL = 0xc0;              //This will set the frequency.

    sbi(TCCR1B, CS10);        // start Timer1, prescaler(1)    
    // end of Timer initialization 


    Value = ADC_read();       //Do initial Measure to initialize Max and Min Values
    Value = Value >> 2;       // Reduce to 8bit accuracy.
    MaxValue = Value;         //to a sensible start value
    MinValue = Value;     

    sei();     //enable global interrupt    

    for(;;)                   // endless main loop
    {
  
        Value = ADC_read();   //reads a value from ADC Depending on ADC Mux setting      
        Value = Value << 1;  // Reduce to 8bit accuracy.
        
        OCR1A = Value;        // Update OCR1A with new value from the LDR        
        Value = Value >> 3;  // Reduce to 8bit accuracy.
          
        if ( Value > MaxValue )   // simple Max Min check
          MaxValue = Value;
        if ( Value < MinValue ) 
          MinValue = Value;

        key = getkey();       //read joystick 

        if (key == BUTTON_A)
        {
          LCD_WriteDigit('O', 5);
          LCD_WriteDigit('N', 6);
          LCD_WriteDigit(' ', 7);
          sbi(DDRB, PB5);            //set PORTB bit 5 as output (OC1A pin)  
          LCDCRA |= (1<<LCDIE);	//Enable LCD_Start_frame interrupt first when

        }

//          WriteLCD(MaxValue);        // Write the MaxValue to the LCD Buffer if pressed UP
        else if (key == BUTTON_B)
        {
          cbi(DDRB, PB5);            //set PORTB bit 5 as input (OC1A pin)  

          LCD_WriteDigit('O', 5);
          LCD_WriteDigit('F', 6);
          LCD_WriteDigit('F', 7);
          LCDCRA |= (1<<LCDIE);	//Enable LCD_Start_frame interrupt first when

//          WriteLCD(MinValue);        // Write the MinValue to the LCD Buffer if pressed DOWN
        }
//        else
//          WriteLCD(Value);           // Write the Value to the LCD Buffer otherwise
    }
}


/******************************************************************************************
*   Function name:  WriteLCD
*   returns:        none 
*   parameters:     Unsigned char 
*   Purpose:        Writes Hex value of temp as BCD value on display
******************************************************************************************/

void WriteLCD( unsigned int Hex )
{
    unsigned char Dec_L = 0;          //clear decimal-bytes
    unsigned char Dec_M = 0;          //we need to convert the HEX value to BCD
    unsigned char Dec_H = 0;          

    while(Hex)                  // loop until the Hex is zero
    {
        Dec_L = Hex;            // store decimal-byte to Dec_L
        Hex -= 10;              // subtract 10 from the Hex-byte
        if(!(SREG & 0x01))      // if carry flag is not set 
        {
            Dec_M++;            // incrase Dec_M-byte
            if(Dec_M > 9)       // if Dec_M-byte over 100 decimal
            {
                Dec_M = 0;      // clear Dec_M
                Dec_H++;        // increase Dec_H
            }
            if(!Hex)            // if the Hex is zero
                Dec_L = 0;      // clear Dec_L
        }
        else
            Hex = 0;   
    }

    // Write out Ascii values to LCD Display using functions in LCD_driver.c 
    Dec_L += 0x30;              // add 0x30 to get the rigth ascii-value
    LCD_WriteDigit(Dec_L, 7);
    
    Dec_M += 0x30;              // add 0x30 to get the rigth ascii-value
    LCD_WriteDigit(Dec_M, 6);

    Dec_H += 0x30;              // add 0x30 to get the rigth ascii-value
    LCD_WriteDigit(Dec_H, 5);
  
    LCDCRA |= (1<<LCDIE);	//Enable LCD_Start_frame interrupt first when
}                               //we have new data to write.


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -