📄 mian.c.120105211654
字号:
//ICC-AVR application builder : 2011-12-16 21:55:54
// Target : M8
// Crystal: 1.0000Mhz
//ICC-AVR application builder : 2011-12-18 18:42:31
// Target : M8
// Crystal: 4.0000Mhz
#include <iom8v.h>
#include <macros.h>
#include "mian.h"
u8 command;
void port_init(void)
{
PORTB = 0x3F;
DDRB = 0x2E;
PORTC = 0x3F; //m103 output only
DDRC = 0x3F;
PORTD = 0xFF;
DDRD = 0xFC;
}
//-------------------------------------------------------------
//TIMER0 initialize - prescale:256
// desired value: 2mSec
// actual value: 1.984mSec (0.8%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0xE1; //set count
TCCR0 = 0x04; //start timer
}
//-------------------------------------------------------------
#pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
void timer0_ovf_isr(void)
{
u8 i=0;
TCNT0 = 0xE1; //reload counter value
discount++;
if(discount>=8)
discount=0;
PORTD |= 0xf0;
PORTC |= 0x0f;
for(i=0;i<8;i++)
{
if(i==discount)
set_IO();
else
clr_IO();
clr_sck();
set_sck();
}
PORTD &= disbuff[discount]|0x0f ;
PORTC &= disbuff[discount]|0xf0;
}
//-------------------------------------------------------------
//TIMER1 initialize - prescale:1
// WGM: 14) PWM fast, TOP=ICRn
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1 = 0xE0C1; //setup
OCR1A = 0x3E7F;
OCR1B = 0x1F3F;
ICR1 = 0x3E7F;
TCCR1A = 0x32;
TCCR1B = 0x19; //start Timer
}
//-------------------------------------------------------------
//SPI initialize
// clock rate: 2000000hz
void spi_init(void)
{
SPCR = 0x50; //setup SPI
SPSR = 0x00; //setup SPI
}
//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();
spi_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//-------------------------------------------------------------
u8 Reset_Meter(void)
{
init_devices();
EEPROMReadBytes(0,(u8 *)(& Cal),12);
if(Cal.OutH==0xffff)
{
Cal.OutH=0x33C3;
Cal.OutM=0x2062;
Cal.OutL=0xA4C;
Cal.IB_H=0xfe;
Cal.IB_M=0x80;
Cal.IB_L=0X38;
Cal.IB_H=0x2B;
Cal.IB_M=0xB5;
Cal.IB_L=0X2A;
}
disbuff0=Table[1];
disbuff1=Table[2];
disbuff2=Table[3];
disbuff3=Table[4];
disbuff4=Table[5];
disbuff5=Table[6];
disbuff6=Table[7];
disbuff7=Table[8];
Clr_RST();
Delay(4000);
Set_RST();
CS5460a_Write(SYNC1,SYNC1,SYNC1,SYNC0);
CS5460A_REG_WR(0,0x00,0x00,0x11);
CS5460A_REG_WR(28,0,0x00,0x04);//控制寄存器
CS5460A_REG_WR(15,0xff,0xff,0xff);
CS5460A_REG_WR(26,0x80,0x00,0x00);
CS5460A_REG_WR(1,Cal.IB_H,Cal.IB_M,Cal.IB_L);
Delay(1000);
// CS5460A_REG_WR(I_GAIN,IK_H,IK_M,IK_L);
Delay(1000);
CS5460a_Write_Byte(0xe8);
CalTemp=0;
Per=2000;
return(GOTO_WAIT);
}
//-------------------------------------------------------------
u8 Wait(void)
{
unsigned long temp;
temp= CS5460a_Read(11)>>9;
disbuff7=Table[temp&0xf];
temp>>=4;
disbuff6=Table[temp&0xf];
temp>>=4;
disbuff5=Table[temp&0xf];
temp>>=4;
disbuff4=Table[temp&0xf];
temp>>=4;
disbuff3=Table[temp&0xf];
temp>>=4;
disbuff2=Table[temp&0xf];
temp>>=4;
disbuff1=Table[temp&0xf];
temp>>=4;
disbuff0=Table[temp&0xf];
if(SetKey==0 && AddKey==0)
{
Delay(50000);
if(SetKey==0 && AddKey==0)
{
OCR1B=Cal.OutH;
CalTemp=0;
disbuff0=Table[20];
disbuff1=Table[18];
disbuff2=Table[19];
disbuff3=Table[22];
Delay(50000);
while(SetKey==0);
return GOTO_SET_OUTH;
}
}
if(AddKey==0)
{
Delay(10);
if(AddKey==0)
{
if(Per<2000)
Per++;
Delay(10000);
}
}
if(SubbKey==0)
{
Delay(10);
if(SubbKey==0)
{
if(Per>400)
Per--;
Delay(10000);
}
}
if(CalTemp!=Per)
{
if(Per<1250)
{
temp=Cal.OutM-Cal.OutL; //1600
CalTemp=Per-400;
temp*=CalTemp;
temp/=850;
temp+=Cal.OutL;
}
else
{
temp=Cal.OutH-Cal.OutM; //1600
CalTemp=Per-1250;
temp*=CalTemp;
temp/=750;
temp+=Cal.OutM;
}
OCR1B=(u16)temp;
/*
disbuff0=Table[Per/1000];
disbuff1=Table[Per/100%10]&0x7f;
disbuff2=Table[Per/10%10];
disbuff3=Table[Per%10];
disbuff4=Table[(temp>>12)&0xf];
disbuff5=Table[(temp>>8)&0xf];
disbuff6=Table[(temp>>4)&0xf];
disbuff7=Table[temp &0xf];
*/
CalTemp=Per;
}
return GOTO_WAIT;
}
//-------------------------------------------------------------
u8 Set_OutH(void)
{
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
OCR1B=Cal.OutM;
CalTemp=0;
EEPROMWriteBytes(0,(u8 *)(& Cal),12);
disbuff0=Table[20];
disbuff1=Table[18];
disbuff2=Table[19];
disbuff3=Table[21];
Delay(50000);
while(SetKey==0);
return GOTO_SET_OUTM;
}
}
if(AddKey==0)
{
Delay(10);
if(AddKey==0)
{
if(Cal.OutH<0x3E7F)
Cal.OutH++;
OCR1B=Cal.OutH;
Delay(10000);
}
}
if(SubbKey==0)
{
Delay(10);
if(SubbKey==0)
{
if(Cal.OutH)
Cal.OutH--;
OCR1B=Cal.OutH;
Delay(10000);
}
}
if(CalTemp!=Cal.OutH)
{
disbuff4=Table[(Cal.OutH>>12)&0xf];
disbuff5=Table[(Cal.OutH>>8)&0xf];
disbuff6=Table[(Cal.OutH>>4)&0xf];
disbuff7=Table[Cal.OutH &0xf];
}
CalTemp=Cal.OutH;
return GOTO_SET_OUTH;
}//-------------------------------------------------------------
u8 Set_OutM(void)
{
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
OCR1B=Cal.OutL;
CalTemp=0;
EEPROMWriteBytes(0,(u8 *)(& Cal),12);
disbuff0=Table[20];
disbuff1=Table[18];
disbuff2=Table[19];
disbuff3=Table[23];
Delay(50000);
while(SetKey==0);
return GOTO_SET_OUTL;
}
}
if(AddKey==0)
{
Delay(10);
if(AddKey==0)
{
if(Cal.OutM<0x3E7F)
Cal.OutM++;
OCR1B=Cal.OutM;
Delay(10000);
}
}
if(SubbKey==0)
{
Delay(10);
if(SubbKey==0)
{
if(Cal.OutM)
Cal.OutM--;
OCR1B=Cal.OutM;
Delay(10000);
}
}
if(CalTemp!=Cal.OutM)
{
disbuff4=Table[(Cal.OutM>>12)&0xf];
disbuff5=Table[(Cal.OutM>>8)&0xf];
disbuff6=Table[(Cal.OutM>>4)&0xf];
disbuff7=Table[Cal.OutM &0xf];
}
CalTemp=Cal.OutM;
return GOTO_SET_OUTM;
}//-------------------------------------------------------------
u8 Set_OutL(void)
{
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
EEPROMWriteBytes(0,(u8 *)(& Cal),12);
Clr_RST();
Delay(4000);
Set_RST();
CS5460a_Write(SYNC1,SYNC1,SYNC1,SYNC0);
CS5460A_REG_WR(0,0x00,0x00,0x11);
CS5460A_REG_WR(28,0,0x00,0x04);//控制寄存器
CS5460A_REG_WR(15,0xff,0xff,0xff);
CS5460A_REG_WR(26,0x80,0x00,0x00);
Delay(4000);
CS5460a_Write_Byte(0xC9);
disbuff0=Table[12];
disbuff1=Table[10];
disbuff2=Table[17];
disbuff3=Table[11];
disbuff4=Table[17];
disbuff5=Table[17];
disbuff6=Table[17];
disbuff7=Table[17];
Delay(50000);
while(SetKey==0);
return GOTO_CAL_IB;
}
}
if(AddKey==0)
{
Delay(10);
if(AddKey==0)
{
if(Cal.OutL<0x3E7F)
Cal.OutL++;
OCR1B=Cal.OutL;
Delay(10000);
}
}
if(SubbKey==0)
{
Delay(10);
if(SubbKey==0)
{
if(Cal.OutL)
Cal.OutL--;
OCR1B=Cal.OutL;
Delay(10000);
}
}
if(CalTemp!=Cal.OutL)
{
disbuff4=Table[(Cal.OutL>>12)&0xf];
disbuff5=Table[(Cal.OutL>>8)&0xf];
disbuff6=Table[(Cal.OutL>>4)&0xf];
disbuff7=Table[Cal.OutL&0xf];
}
CalTemp=0;
return GOTO_SET_OUTL;
}
//-------------------------------------------------------------
u8 Cal_IB(void)
{
unsigned long temp;
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
temp= CS5460a_Read(1);
Cal.IB_L=temp & 0xff;
temp>>=8;
Cal.IB_M=temp & 0xff;
temp>>=8;
Cal.IB_H=temp & 0xff;
EEPROMWriteBytes(0,(u8 *)(& Cal),12);
Clr_RST();
Delay(4000);
Set_RST();
CS5460a_Write(SYNC1,SYNC1,SYNC1,SYNC0);
CS5460A_REG_WR(0,0x00,0x00,0x11);
CS5460A_REG_WR(28,0,0x00,0x04);//控制寄存器
CS5460A_REG_WR(15,0xff,0xff,0xff);
CS5460A_REG_WR(26,0x80,0x00,0x00);
CS5460A_REG_WR(1,Cal.IB_H,Cal.IB_M,Cal.IB_L);
Delay(4000);
CS5460a_Write_Byte(0xe8);
disbuff0=Table[12];
disbuff1=Table[10];
disbuff2=Table[17];
disbuff3=Table[16];
OCR1B=Cal.OutH;
while(SetKey==0);
return GOTO_CAL_IK;
}
}
return GOTO_CAL_IB;
}
//-------------------------------------------------------------
u8 Cal_IK(void)
{
unsigned long temp;
double temp_f;
temp= CS5460a_Read(11);
disbuff4=Table[temp/10000];
disbuff5=Table[temp/1000%10];
disbuff6=Table[temp/100%10];
disbuff7=Table[temp/10%10];
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
temp= CS5460a_Read(11);
temp_f=20000.0;
temp_f/=(double)temp;
temp_f*=0x400000;
temp=(unsigned long)temp_f;
Cal.IK_L=temp & 0xff;
temp>>=8;
Cal.IK_M=temp & 0xff;
temp>>=8;
Cal.IK_H=temp & 0xff;
EEPROMWriteBytes(0,(u8 *)(& Cal),12);
while(SetKey==0);
return GOTO_RESET;
}
}
return GOTO_CAL_IK;
}
//-------------------------------------------------------------
void main(void)
{
//insert your functional code here...
u8 Status_Flag=0;
while(1)
{
switch(Status_Flag)
{
case GOTO_WAIT:
Status_Flag=Wait();
break;
case GOTO_SET_OUTL :
Status_Flag=Set_OutL();
break;
case GOTO_SET_OUTM:
Status_Flag=Set_OutM();
break;
case GOTO_SET_OUTH:
Status_Flag=Set_OutH();
break;
case GOTO_RESET:
Status_Flag=Reset_Meter();
break;
case GOTO_CAL_IB:
Status_Flag=Cal_IB();
break;
case GOTO_CAL_IK:
Status_Flag=Cal_IK();
break;
}
}
}
void Delay(unsigned long T)
{
while(T--);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -