📄 2atmega48.c
字号:
Target : a48
Crystal: 8.0000Mhz
//市
#include <iom48v.h>
#include <macros.h>
#include <stdio.h>
//volatile
//变量定义
unsigned int Output_Load,AC_frequency,out_v,Bat_c,Shutdown_T,ADC_Value;
static unsigned char cyc_count,CycleDat_A,CycleDat_B,Cycle_dat,
PwmOFFtime,PwmONtime,PWMflag,sys_delay,PWM10ms_data;
static unsigned int Battery_Value,ADC_Sample_ADD,in_AC_cycle,Input_AcValue,
T_1000ms_timer,Output_Value,RX_index,CommStatus,BAT_Work_maxtime,Fault_waitTime;
static unsigned char Key_time,Key_statu,sys,BAT,OnInvDelay,In_Frequency,Test_time,
PWM,ACCut_count,delay_us_timer,delay_ms_timer,delay_s_timer,ReC[4];
unsigned char take_data,BCD_I,BCD_II,BCD_III,Out_Load,BCD_data, mytimer,Temp_Value,Power_Status,InitPWM_T
,j0,Send_S,TX_Count,UPS_STDis,Buzz_time,Buzz_count,Buzz_status,Load_PWM,OverLoad_Count,com_off;
unsigned char III[39]="#KEBO UPS-650HP VER 1.01 \r";
unsigned char FFF[22]="#230.0 2.8 12.00 50.0\r",MMM[47],ST[8];
//宏定义
//#define ADC_over ((ADCSRA&0x10)==0)
#define on_M3 (PORTB&=~BIT(2))
#define cut_M3 (PORTB|=BIT(2))
#define on_M5 (PORTB|=BIT(1))
#define cut_M5 (PORTB&=~BIT(1))
#define on_M4 (PORTB&=~BIT(4))
#define cut_M4 (PORTB|=BIT(4))
#define on_M6 (PORTB|=BIT(3))
#define cut_M6 (PORTB&=~BIT(3))
#define on_300v (PORTD&=~BIT(7))
#define off_300v (PORTD|=BIT(7))
#define on_charge (PORTD|=BIT(6))
#define off_charge (PORTD&=~BIT(6))
#define on_relay (PORTC|=BIT(3))
#define off_relay (PORTC&=~BIT(3))
#define on_RedLED (PORTC |=BIT(5))
#define off_RedLED (PORTC&=~BIT(5))
//#define on_RedLED (PORTD |=BIT(3))
//#define off_RedLED (PORTD&=~BIT(3))
//#define on_YLED (PORTD|=BIT(4))
//#define off_YLED (PORTD&=~BIT(4))
#define on_YLED (PORTD|=BIT(3))
#define off_YLED (PORTD&=~BIT(3))
#define on_GreLED (PORTD|=BIT(4))
#define off_GreLED (PORTD&=~BIT(4))
#define on_Buzz (PORTB |=BIT(5))
#define off_Buzz (PORTB&= ~BIT(5))
#define RS232_command_on(){CommStatus=RSTART;RX_index=0;UCSR0B|=BIT(RXCIE0);}
//Buzz_status*/
#define stop 0xff
#define normal 0x00
//常量定义
#define Key_down 0b00100000
#define turn_on 1
#define OUT230V 57435
#define PWM10ms 100
#define M3_ON 0
#define M4_ON 1
#define M5_ON 2
#define M6_ON 3
//系统状态定义(sys)
#define AC_Normal 0
#define bat_work 1
#define over_load 2
#define fault 8
#define Test 0x0f
#define RSTART 0
#define REND 1
#define WSTART 2
#define WEND 3
#define off 4
//电池状态( BAT )
#define bat_ok 1
#define bat_lower 2
#define bat_Protect 4
#define bat_overcharge 8
#define FRAMING_ERROR (1 << FE0)
#define PARITY_ERROR (1 << UPE0)
#define DATA_OVERRUN (1 << DOR0)
#define DATA_REGISTER_EMPTY (1<<UDRE0)
#define RX_COMPLETE (1<<RXC0)
//key_status
#define cl 1
#define do 2
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x3F;
PORTC = 0x00; //m103 output only
DDRC = 0b11101001;
PORTD = 0b10000100;
DDRD = 0b11011010;;
}
//TIMER0 initialize - prescale:8
// WGM: Normal
// desired value: 100uSec
// actual value: 100.000uSec (0.0%)
void timer0_init(void)
{
TCCR0B = 0x00; //stop
TCNT0 = 0x9d; //set count
TCCR0A = 0x00;
TCCR0B = 0x02; //start timer
}
#pragma interrupt_handler timer0_ovf_isr:17
void timer0_ovf_isr(void)
{
TCNT0 += 0x9d;
if(in_AC_cycle!=0)in_AC_cycle--;
if(OnInvDelay!=0)OnInvDelay--;
if(Cycle_dat<0xfe){Cycle_dat++;}
if(delay_us_timer!=0)delay_us_timer--;//100uS延时
if(sys==bat_work||sys==Test)
{
if(OnInvDelay==0)
{
if(PWMflag==0){run_pwm0();}
if(PWMflag==1){run_pwmx();}
if(PWMflag==2){run_pwm1();}
if(PWMflag==3){run_pwmx();}
}
}
else{cut_M3;cut_M4;cut_M5;cut_M6;}//如果出现过载,市电或电池故障则关断输出逆变。
}
//-------------------------------------
void run_pwm0(void)
{
if(PwmONtime==0){
off_300v;cut_M5;
PwmOFFtime=PWM10ms_data-PWM;
PWMflag++;}
else{
PwmONtime--;
on_M5;
on_M4;
cut_M3;
off_charge;
if(PwmONtime<PWM-3){on_300v;}
}
}
//------------------------------------
void run_pwmx(void)
{cut_M5;cut_M6;on_M3;on_M4;on_charge;
if(PwmOFFtime==0)
{PwmONtime=PWM;PWMflag++;if(PWMflag>3){PWMflag=0;}}
else{PwmOFFtime--;}}
void run_pwm1(void)
{
if(PwmONtime==0){off_300v;cut_M6;PwmOFFtime=PWM10ms_data-PWM;PWMflag++;}
else{PwmONtime--;cut_M4;on_M6;on_M3;off_charge;if(PwmONtime<PWM-3){on_300v;}}
}
//TIMER1 initialize - prescale:8
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFC; //setup
TCNT1L = 0x18;
OCR1AH = 0x03;
OCR1AL = 0xE8;
OCR1BH = 0x03;
OCR1BL = 0xE8;
ICR1H = 0x03;
ICR1L = 0xE8;
TCCR1A = 0x00;
TCCR1B = 0x02; //start Timer
}
//#pragma vector=TIMER1_OVF_vect
//__interrupt void timer1_user_isr(void)
//{
//watchdog timeout
//}
#pragma interrupt_handler timer1_ovf_isr:14
void timer1_ovf_isr(void)
{
TCNT1H = 0xFC; //reload counter high value
TCNT1L = 0x18; //reload counter low value
if(mytimer>20){mytimer=0;if(Buzz_time!=0)Buzz_time--;}mytimer++;
if(T_1000ms_timer!=0){T_1000ms_timer--;}
else
{T_1000ms_timer=1000;
BAT_Work_maxtime++;
Fault_waitTime++;
if(sys_delay!=0)sys_delay--;
if(Test_time!=0)Test_time--;
if (delay_s_timer!=0)delay_s_timer--;
if(Shutdown_T!=0)Shutdown_T--;
}
if(delay_ms_timer!=0)delay_ms_timer--;
if((PIND & BIT(5)))Key_time++;else{Key_statu=do;Key_time=0;}
//断电测试-------------
if(sys==AC_Normal){
if(sys_delay==0)
{
ADMUX = 0b01000100; //select adc input 4
ADCSRA = 0b11000010;
while((ADCSRA&0x10)==0);
{
ADCSRA|=(1<<ADIF);
ADC_Value=ADCL;
ADC_Value|=ADCH<<8;}
if((ADC_Value<470)&&(ADC_Value>348))
{ACCut_count++;}
else{ACCut_count=0;}
if(ACCut_count>4)
{OnInvDelay=50;PWMflag=0;Input_AcValue=0;To_Bat();ACCut_count=0;
}
}
}
Buzzer();
}
#pragma interrupt_handler int0_ovf_isr:6
void int0_ovf_isr(void)
{
cyc_count++;
if(cyc_count<2)
{CycleDat_A=Cycle_dat;}//半周保存
else{In_Frequency=Cycle_dat;CycleDat_B=Cycle_dat-CycleDat_A;
cyc_count=0;Cycle_dat=0;}//一周保存
}
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x1e;
UBRR0L = 0xCe; //set baud rate lo;0xCF;
UBRR0H = 0x00; //set baud rate hi;0x00;
UCSR0B = 0x98;
}
//--------------------------------
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
//***********************
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
ReC[RX_index]=data; //保存数据到数组里面
if (++RX_index>=6) {RX_index=0;UCSR0B&=~BIT(RXCIE0);}
if(data==13){UCSR0B&=~BIT(RXCIE0);CommStatus=REND;}
}
}
//ADC initialize
// Conversion time: 3uS
void adc_init(void)
{
ADCSRA = 0x00; //disable adc
ADMUX = 0xc0; //select adc input 0
ACSR = 0x80;
ADCSRB = 0x00;
ADCSRA = 0b00000110;
DIDR0=0b00010111;//禁止AD口数字输入
//ADCSRA:
//bit7 ADEN ADC使能=1
//bit6 ADSC 启动ADC开始转换=1
//bit5 ADATE 自己触发使能
//bit4 ADIF ADC中断标志
//bit3 ADIE ADC中断使能
//bit2:0 ADC 预分频选择位 111=128时钟分频
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer0_init();
timer1_init();
adc_init();
uart0_init();
//watchdog_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x01; //timer 0 interrupt sources
TIMSK1 = 0x01; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x04;//0b00000100; //pin change mask 2
PCICR = 0x04; //pin change enable
PRR = 0x00; //power controller
PWM10ms_data=PWM10ms;
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//
void main(void)
{
/* OSCCAL=*((unsigned char const*)0XFFF); 采用内部RC时校准用*/
init_devices();
PWM=40;
sys=AC_Normal;
CommStatus=1;
// UPS_STDis|=0b00010000;
T_1000ms_timer=0;
sys_delay=2;
Cycle_dat=255;
while (2)
{
WDR();
WDTCSR |= (1<<WDCE) | (1<<WDE);
WDTCSR = 0x39; //WATCHDOG ENABLED - dont forget to issue WDRs
Power_control();
AD_Sample();
System_Deal();
Inverter_Deal();
Communication();
Batter_status();
Overload_status();
}
}
void Buzzer(void)
{
if(sys==over_load)
{on_Buzz;}else{
if(Buzz_status==stop){off_Buzz;}
else{
if (Buzz_time == 0)
{
if(PINB&BIT(5))
{off_Buzz;Buzz_time=15;Buzz_count++;
if(Buzz_count>2){Buzz_count=0;
if(BAT==bat_ok){Buzz_time=255;}
else{Buzz_time=35;}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -