#include <iom8v.h>
#include <macros.h>
#include <stdlib.h>
#include <lcd.h>
#include <math.h>
#include<string.h>
#define S0 1<<0 //pc0 gnd
#define S1 1<<1 //2v
#define S2 1<<7 //200mv PD7
#define S3 1<<2 //-vref
#define S4 1<<4 //-20mv
#define S5 1<<3 //discharge
#define S6 1<<5 //Ux
#define S7 1<<6 //Ix
//全局變量
unsigned char timetemp=0;
unsigned char time100ms=0;
unsigned int time500ms=0;
unsigned char stepflag=0;//轉(zhuǎn)換階段標志 -uref ? -uref/2 7?
unsigned char conflag=0; //正在轉(zhuǎn)換標志
unsigned int voltcount=0; //ad值
float volt=0; //電壓值
unsigned char timeH=0; //計數(shù)器值
unsigned char timeL=0;
//函數(shù)
void init_devices(void);
void port_init(void);
//1ms
void timer2_init(void);
void timer2_ovf_isr(void);
//100us
void timer1_init(void);
void timer1_ovf_isr(void);
void adadjust(void);
void adini(void);
void adconvert(unsigned char data);
//main()
void main()
{
unsigned char i;
unsigned char lcdbuf[32];
unsigned char *lcdp=lcdbuf;
init_devices();
Inti_Disp();
adadjust();
adini();
while(1)
{
adini();
adconvert(0x08);//
set_cur(0);
lcdp=ftoa(volt,0);
putstr(lcdp);
set_cur(16);
ltoa(lcdbuf,voltcount,10);
putstr(lcdbuf);
}
//abc();
}
void adini(void)
{
PORDC=0x08; //放電30ms
timetemp=30;
conflag=1;
}
void adconvert(unsigned char data)//充電
{
//充電196ms
PORDC=data; //打開Ux
timetemp=196;
conflag=1;
while(timetemp);//等待
//反向放電
//讀取U0值
if(PIND&0x08)
{
PORDC=0x10;//接入-uref/2 7
stepflag=1;//qi dong counter
timeH=0;
timeL=0;
}
else
{
PORDC=0x04;//接入-uref
stepflag=2; //qi dong counter
timeH=0;
timeL=0;
}
dischar1:
while(stepflag==2)
{
if(PIND&0x08)
{
}
else
{
PORDC=0x10;//接入-uref/2 7
stepflag=1;//qi dong counter
}
}
while(PIND&0x04);
voltcount=timeH;
voltcount=(voltcout<<7)+timeL;
}
void port_init(void)
{
PORTB = 0xFF;
DDRB = 0xff;
PORTC = 0x08; //m103 output only
DDRC = 0xff;
PORTD = 0x7F;
DDRD = 0xf3;
}
//TIMER1 initialisation - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 500uSec
// actual value: 500.000uSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x83;
OCR1AH = 0x00;
OCR1AL = 0x7D;
OCR1BH = 0x00;
OCR1BL = 0x7D;
ICR1H = 0x00;
ICR1L = 0x7D;
TCCR1A = 0x00;
TCCR1B = 0x03; //start Timer
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0xFF; //reload counter high value
TCNT1L = 0x83; //reload counter low value
if(stepflag==1)
{
//timeH
timeL++;
}
if(stepflag==2)
{
timeH++;
}
}
//TIMER2 initialisation - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer2_init(void)
{
TCCR2 = 0x00; //stop
ASSR = 0x00; //set async mode
TCNT2 = 0x06; //setup
OCR2 = 0xFA;
TCCR2 = 0x04; //start
}
//1ms
#pragma interrupt_handler timer2_ovf_isr:5
void timer2_ovf_isr(void)
{
TCNT2 = 0x06; //reload counter value
if(timetemp!=0)
timetemp--;
else
conflag=0;
if(time500ms==0)
{
PORTC^=0x20;
time500ms=500;
}
time500ms--;
}
//call this routine to initialise all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
timer1_init();
timer2_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x44; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialised
}
|