?? init.c
字號:
//ICC-AVR application builder : 2005-4-25 9:42:27
// Target : M8
// Crystal: 8.00Mhz
/*
* Copyright (c) 2005, 廣州力源電器公司開發部
* All rights reserved.
* 文件名稱:init.c 負責初始化MCU各內部資源及外部端口
* 當前版本: 1.0
* 作者: 張洪強
* 完成日期: 2005.7.25
*/
#include <avr/io.h>
//#include <avr/iom8.h>
#include <avr/interrupt.h>
#include <avr/signal.h>
#include <avr/wdt.h>
#include <avr/eeprom.h>
#include "init.h"
void port_init(void)
{
PORTB = 0x0F; //PB0-4接鍵盤,設為輸入上拉
DDRB = 0xF0; //PB4-7接控制輸出,設為輸出不上拉
PORTC = 0x3C; //PC0,PC1接模擬信號進行AD轉換,設為輸入不上拉
DDRC = 0x3C; //PC2,PC3,PC4,PC5接4位7段LED的位選端,設為輸出上拉
PORTD = 0xFF; //PORTD口接7段LED的段選端,全部設為輸出上拉
DDRD = 0xFF;
}
//TIMER0 initialize - prescale:1024
// desired value: 50Hz
// actual value: 50.080Hz (0.2%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xB2; //set count
TCCR0 = 0x05; //start timer
}
//#pragma interrupt_handler timer0_ovf_isr:10
SIGNAL(SIG_OVERFLOW0)
{
static unsigned char i = 0;
static unsigned int total = 0,max_value = 0,min_value = 0xff;
TCNT0 = 0xB2; //reload counter value
if ( TIMER_EN )
{
TIMER_NUM++;
}
if ( adc_data_temp > max_value )
{
max_value = adc_data_temp;
}
if ( adc_data_temp < min_value )
{
min_value = adc_data_temp;
}
total += adc_data_temp;
if ( i == 49 )
{
adc_data[0] =( total - min_value - max_value ) / 48.0;
adc_data_temp = 0;
max_value = 0;
min_value = 0xff;
total = 0;
}
else if ( i == 99 )
{
adc_data[1] =( total - min_value - max_value ) / 48.0;
adc_data_temp = 0;
max_value = 0;
min_value = 0xff;
total = 0;
}
if ( i < 50 )
{
ADMUX &= ~( 1 << MUX0 );
}
else
{
ADMUX |= ( 1 << MUX0 );
}
i++;
i %= 100;
ADCSR |= ( 1<< ADSC ); //啟動新的AD轉換
}
//Watchdog initialize
// prescale: 512K
void watchdog_init(void)
{
wdt_reset(); //this prevents a timout on enabling
wdt_enable(WDTO_1S); //WATCHDOG ENABLED - dont forget to issue WDRs,0.52S
}
//ADC initialize
// Conversion time: 117uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0xC0; //select adc input 0,選擇AVCC為參考電源
ACSR = 0x80; //禁止模擬比較器
ADCSR = 0xCE; //使能ADC,ADC轉換開始,允許ADC中斷,32分頻
}
//call this routine to initialize all peripherals
void init_devices(void)
{ //stop errant interrupts until set up
cli(); //disable all interrupts
port_init();
watchdog_init();
timer0_init();
adc_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
if ( UP_DOWN_POWER != 0x55aa )
{
/*
KEY_A_V_STATE = 0;
KEY_AUTO_MAN_STATE = 0;
KEY_CC_CV_STATE = 0;
POWER_STATE = 0;
*/
eeprom_busy_wait();
KEY_A_V_STATE = eeprom_read_byte ( (char*)0x01 );
eeprom_busy_wait();
KEY_AUTO_MAN_STATE = eeprom_read_byte ( (char*)0x03 );
eeprom_busy_wait();
KEY_CC_CV_STATE = eeprom_read_byte ( (char*)0x05 );
eeprom_busy_wait();
POWER_STATE = eeprom_read_byte ( (char*)0x07 );
}
if ( POWER_STATE )
{
PORTB |= ( 1 << POWER ); //開啟電源
}
else
{
PORTB &= ~( 1 << POWER ); //關閉電源
}
if ( KEY_A_V_STATE )
{
PORTB |= ( 1 << OUT_A ); //顯示電流
}
else
{
PORTB &= ~( 1 << OUT_A ); //顯示電壓
}
if ( KEY_CC_CV_STATE )
{
PORTB |= ( 1 << OUT_CV ); //穩壓
}
else
{
PORTB &= ~( 1 << OUT_CV ); //穩流
}
if ( KEY_AUTO_MAN_STATE )
{
PORTB |= ( 1 << OUT_AUTO ); //自動
}
else
{
PORTB &= ~( 1 << OUT_AUTO ); //手動
}
UP_DOWN_POWER = 0x55aa;
sei(); //re-enable interrupts
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -