📄 mian.c.120103175752
字号:
//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;
PORTC|=0X0F;
PORTD|=0XF0;
for(i=0;i<8;i++)
{
if(i==discount)
set_IO();
else
clr_IO();
clr_sck();
set_sck();
}
PORTC&=disbuff[discount]|0xf0;
PORTD&=disbuff[discount]|0x0f;
}
//-------------------------------------------------------------
//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 = 0x22;
TCCR1B = 0x19; //start Timer
}
//-------------------------------------------------------------
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
void uart0_init(void)
{
UCSRB = 0x00; //disable while setting baud rate
UCSRA = 0x00;
UCSRC = BIT(URSEL) | 0x06;
UBRRL = 0x33; //set baud rate lo
UBRRH = 0x00; //set baud rate hi
UCSRB = 0x90;
}
//-------------------------------------------------------------
#pragma interrupt_handler uart0_rx_isr:iv_USART0_RXC
void uart0_rx_isr(void)
{
command= UDR;//uart has received a character in UDR
}
//-------------------------------------------------------------
//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();
uart0_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),4);
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,0x71);
CS5460A_REG_WR(15,0xff,0xff,0xff);
CS5460A_REG_WR(26,0x80,0x00,0x00);
// CS5460A_REG_WR(U_GAIN,UK_H,UK_M,UK_L);
Delay(1000);
// CS5460A_REG_WR(I_GAIN,IK_H,IK_M,IK_L);
Delay(1000);
CS5460a_Write_Byte(0xe8);
CalTemp=0;
Per=400;
return(GOTO_WAIT);
}
//-------------------------------------------------------------
u8 Wait(void)
{
unsigned long temp;
temp= CS5460a_Read(5);
/*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)
{
Delay(10);
if(SetKey==0)
{
Cal.OutH=0x325C;
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)
{
temp=Cal.OutH-Cal.OutL; //1600
CalTemp=Per-400;
temp=temp*CalTemp;
temp=temp/1600+Cal.OutL;
OCR1B=(u16)temp;
disbuff0=Table[Per/1000];
disbuff1=Table[Per/100%10];
disbuff2=Table[Per/10%10]&0x7f;
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;
if(command)
{
temp=(Cal.OutH-Cal.OutL);
temp*=command;
temp/=100;
temp=Cal.OutL+temp;
OCR1B=( u16 )temp;
disbuff4=Table[temp/1000];
disbuff5=Table[temp/100%10];
disbuff6=Table[temp/10%10];
disbuff7=Table[temp%10];
command=command*16/10+40;
disbuff0=Table[0];
disbuff1=Table[command/100];
disbuff2=Table[command/10%10]&0x7f;
disbuff3=Table[command%10];
command=0;
}
return GOTO_WAIT;
}
//-------------------------------------------------------------
u8 Set_OutH(void)
{
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
Cal.OutL=0xACC;
OCR1B=Cal.OutL;
CalTemp=0;
EEPROMWriteBytes(0,(u8 *)(& Cal),4);
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.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_OutL(void)
{
if(SetKey==0)
{
Delay(10);
if(SetKey==0)
{
OCR1B=0xF00;
EEPROMWriteBytes(0,(u8 *)(& Cal),4);
while(SetKey==0);
return GOTO_RESET;
}
}
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;
}
//-------------------------------------------------------------
void main(void)
{
//insert your functional code here...
u8 Status_Flag=0;
while(1)
{
switch(Status_Flag)
{
case GOTO_SET_OUTH:
Status_Flag=Set_OutH();
break;
case GOTO_SET_OUTL:
Status_Flag=Set_OutL();
break;
case GOTO_WAIT:
Status_Flag=Wait();
break;
default:
Status_Flag=Reset_Meter();
break;
}
}
}
void Delay(unsigned long T)
{
while(T--);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -