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

📄 main.c

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

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

/**************************   DECLARE USER FUNCTIONS    **********************************/
unsigned long fact(int x);

/******************************************************************************************
*   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

    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    

    fact(3);

    for(;;)                   // endless main loop
    {
  
        Value = ADC_read();   //reads a value from ADC Depending on ADC Mux setting
        Value = Value >> 2;   // 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)
          WriteLCD(MaxValue);        // Write the MaxValue to the LCD Buffer if pressed UP
        else if (key == BUTTON_B)
          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.


/********************************************************************/
/*** 					X factorial  (X!)											***/
/*  This function will recursively call itself until the parameter
	is 1 (the termination condition).

	The recursion algorithm for factorial is:
		  FACT_X = X * (X-1) * (X-2) * (X-3) ..... * 1   ,or
		  FACT_X = X * (X-1)! for X0 and 0! =1;

********************************************************************/

unsigned long fact(int x)
{
    if ( x == 1 )
	return(1);
    else
	return( x * fact( x-1 ) );
}

⌨️ 快捷键说明

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