Jan 17, 2014 10:42
/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.9 Professional
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
Project :
Version :
Date : 15.01.2014
Author : 12val12
Chip type : ATmega16
Program type : Application
Clock frequency : 2,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include
#include
#define ADC_VREF_TYPE 0xC0
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
#asm("cli") ;
delay_us(4);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
#asm("sei") ;
return ADCW;
}
// Declare your global variables here
void print_int (unsigned int V, int I) ;
unsigned char data[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned int VJ=0;
int IJ=0;
unsigned int X=0;
unsigned char zn=0;
unsigned char c8=0 ;
unsigned char c7=0;
unsigned char c6=0;
unsigned char c5=0;
// unsigned char c4=0;
unsigned char c3=0;
unsigned char c2=0;
unsigned char c1=0;
unsigned char c0=0;
long int V1,I1,I2;
// float V1; float I1; float I2;
// Timer 2 overflow interrupt service routine
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
//
PORTD=0;PORTB.7=0;PORTB.6=0;
if ( zn==0) {PORTD=0b01000000;PORTC=data[0];}
if ( zn==1) {PORTD=0b00100000;PORTC=data[1];}
if ( zn==2) {PORTD=0b00010000;PORTC=data[2];}
if ( zn==3) {PORTD=0b00001000;PORTC=data[3];}
if ( zn==4) {PORTD=0b00000100;PORTC=data[4];} //знак тока
if ( zn==5) {PORTD=0b00000010;PORTC=data[5];}
if ( zn==6) {PORTD=0b00000001;PORTC=data[6];}
if ( zn==7) {PORTB.7=1;PORTC=data[7];}
if ( zn==8) {PORTB.6=1;PORTC=data[8];}
// if (segm==0) {PORTD=0b01000000;PORTC=255;} //проверка горения сегментов
if (++zn>8) { zn=0; } // ij++ ; в цикл для проверки кол ва прорисовок
}
static flash unsigned char digitI[] = // общий катод (для ОА побитно инвертировать)
{ // A -0 bit... B-1bit ..... G-6bit H(точка)-7бит
(0b00111111), //0
(0b00000110), //1
(0b01011011), //2
(0b01001111), //3
(0b01100110), //4
(0b01101101), //5
(0b01111101), //6
(0b00000111), //7
(0b01111111), //8
(0b01101111) //9
};
static flash unsigned char digitIp[] = // общий катод (для ОА побитно инвертировать) c точкой
{ // A -0 bit... B-1bit ..... G-6bit H(точка)-7бит
(0b10111111), //0
(0b10000110), //1
(0b11011011), //2
(0b11001111), //3
(0b11100110), //4 р
(0b11101101), //5
(0b11111101), //6
(0b10000111), //7
(0b11111111), //8
(0b11101111) //9
};
// void print_longint (unsigned long int s)
// {
// c8=0 ; c7=0;c6=0; c5=0; c4=0; c3=0; c2=0;c1=0; c0=0;
//
// // while (s>=1000000000){s-=1000000000;c9++;}
// while (s>=100000000){s-=100000000;c8++;}
// data[8]=digitI[c8];
// while (s>=10000000){s-=10000000;c7++;}
// data[7]=digitIp[c7]; //с фиксированной точкой
// while (s>=1000000){s-=1000000;c6++;}
// data[6]=digitI[c6];
// while (s>=100000){s-=100000;c5++;}
// data[5]=digitI[c5]; ;
// while (s>=10000){s-=10000;c4++;}
// data[4]=digitI[c4];
// while (s>=1000){s-=1000;c3++;}
// data[3]=digitIp[c3]; //с фиксированной точкой
// while (s>=100){s-=100;c2++;}
// data[2]=digitI[c2];
// while (s>=10){s-=10;c1++;}
// data[1]=digitI[c1];
// c0=s;
// data[0]=digitI[c0];
// }
//
void print_int (unsigned int V, int I)
{
c8=0 ;c7=0;c6=0; c5=0; //c4=0;
c3=0; c2=0;c1=0; c0=0;
while (V>=1000){V-=1000;c8++;}
data[8]=digitI[c8]; // подготовка данных в сегменты согласно цифре в разряде
if (c8 ==0) data[8]=0b00000000;// старший разряд если ноль не рисуем ничего
while (V>=100){V-=100;c7++;}
data[7]=digitIp[c7]; //с фиксированной точкой
while (V>=10){V-=10;c6++;}
data[6]=digitI[c6];
c5=V;
data[5]=digitI[c5]; ;
// while (s>=10000){s-=10000;c4++;}
// data[4]=digitI[c4];
if (I<0) {I=0-I; data[4]=0b01000000;} else{data[4]=0b00000000;};
if (I1>99000) data[4]=0b00000011;
while (I>=1000){I-=1000;c3++;}
data[3]=digitIp[c3]; //с фиксированной точкой
while (I>=100){I-=100;c2++;}
data[2]=digitI[c2];
while (I>=10){I-=10;c1++;}
data[1]=digitI[c1];
c0=I;
data[0]=digitI[c0];
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0b11000000;
PORTB=0x00;
// Port C initialization
// OUT
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0xFF;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 62,500 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
// ASSR=0x00;
// TCCR2=0x03;
// TCNT2=0x00;
// OCR2=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 250,000 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x02;
TCNT2=0x00;
OCR2=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 31,250 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
// ASSR=0x00;
// TCCR2=0x04;
// TCNT2=0x00;
// OCR2=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 7,813 kHz
// Mode: Normal top=FFh
// // OC2 output: Disconnected
// ASSR=0x00;
// TCCR2=0x06;
// TCNT2=0x00;
// OCR2=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: 1,953 kHz
// Mode: Normal top=FFh
// OC2 output: Disconnected
// ASSR=0x00;
// TCCR2=0x07;
// TCNT2=0x00;
// OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 125,000 kHz
// ADC Voltage Reference: AREF pin
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
// Global enable interrupts
#asm("sei") ;
V1=read_adc(0)*100;
I1= read_adc(1)*100;;
I2= read_adc(2)*100;;
while (1) /// тело 1
{
// for (ij=999999999;ij>0;ij--)
// {
V1=V1*0.99+ read_adc(0) ;//цифровой выходной RC фильтр
//VJ=( V1*5.83-(V1*0.00015*V1))*0.002; // подгонка под делитель компенс нелинейности .. 22к... 1k (до 50 Вольт) 22k - 0.25 Вт
VJ= (V1*0.0588)-(V1*0.00014*V1*0.00018);
I1=I1*0.99 + read_adc(1) ;//цифровой выходной RC фильтр ток с шунта умноженный на( (100к/4.7к)+1)+ 195 мв смещения
I2=I2*0.99+ read_adc(2) ;//цифровой выходной RC фильтр ток с шунта
IJ=I1 *0.01-77; // отнимаем смещение
if (I1>99000) IJ=I2 *0.227; // если падение с шунта после ОУ превысило верхний предел АЦП меряем ток прямо с шунта
VJ=VJ - IJ*0.020 ; ///компенсация шунта (0.12 Ом) и проводов (0.08 Ом)..
X++;
print_int (VJ, IJ) ;
} // end тело 1
} /// end Main