📄 main.c
字号:
//ICC-AVR application builder : 2007-7-24 12:41:43
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
#include "head.h"
#define uchar unsigned char
#define uint unsigned int
#define xtal 8
extern void WaitForEnable(void);
extern void LcdWriteData(uchar W);
extern void LcdWriteCommand(uchar CMD,uchar Attribc);
extern void InitLcd(void);
extern void Display(uchar dd);
extern void DisplayOneChar(uchar x,uchar y,uchar Wdata);
extern void ePutstr(uchar x,uchar y,uchar const *ptr);
#define SPEED_0V PORTD|=BIT(PD0)
#define SPEED_5V PORTD&=~BIT(PD0)
#define POW_OFF PORTD|=BIT(PD1)
#define POW_ON PORTD&=~BIT(PD1)
#define POW_14V PORTD|=BIT(PD2)
#define POW_27V PORTD&=~BIT(PD2)
#define AIR_OFF PORTD|=BIT(PD3)
#define AIR_ON PORTD&=~BIT(PD3)
#define LCM_BLCON_OFF PORTD|=BIT(PD4)
#define LCM_BLCON_ON PORTD&=~BIT(PD4)
#define BZ_OFF PORTD|=BIT(PD7)
#define BZ_ON PORTD&=~BIT(PD7)
const uchar str0[]={" Shanghai Hon-Li"};//欢迎界面
const uchar str1[]={" Electronic Co. "};//欢迎界面
const uchar str2[]={"Welcome to Enter"};//欢迎界面
const uchar str3[]={"Testing Process "};//欢迎界面
const uchar cs0[]={" Testing...... "};//测试界面
const uchar cs1[]={"LVS: TPV: "};//测试界面
const uchar cs2[]={"RPM: RC: A"};//测试界面
const uchar cs3[]={"LC: A "};//测试界面
const uchar result[]={" Result...... "};//测试界面
uchar status=0;
uchar OutFlag=0;
uint cnt=0;
uint display_val=0;
uint current=0;
uchar LVS[4];
uchar TPV[4];
uchar RPM[4];
uchar RC[4];
uchar LC[4];
uint LLVS,TTPV,RRPM;
float RRC,LLC;
uchar Wait10S_Start_Flag=0,Wait10S_End_Flag=0;//10S启动结束标志
uchar Wait6S_Start_Flag=0,Wait6S_End_Flag=0;//6S启动结束标志
uchar Wait2S_Start_Flag=0,Wait2S_End_Flag=0;//2S启动结束标志
/***************读取计数器T1的数据*************/
uint read_T1(void)
{uint temp1,temp2;
temp1=(uint)TCNT1L;
temp2=(uint)TCNT1H;
temp2=(temp2<<8)+temp1;
return(temp2);
}
/***************清除计数器T1的数据************/
void clr_T1(void)
{
TCNT1L=0x00;TCNT1H=0x00;
}
/*****************watchdog初始化**************/
//Watchdog initialize
// prescale: 512K
void watchdog_init(void)
{
WDR(); //this prevents a timout on enabling
WDTCR = 0x0D; //WATCHDOG ENABLED - dont forget to issue WDRs
}
/******************ADC初始化*********************/
//ADC initialize
// Conversion time: 3uS
void adc_init(void)
{
ADCSR = 0x00; //disable adc
ADMUX = 0xC0; //select adc input 0
ACSR = 0x80;
ADCSR = 0xE1;
}
/*********************端口初始化*********************/
void port_init(void)
{
PORTA = 0xfe;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x0D;
PORTC = 0x00; //m103 output only
DDRC = 0xFF;
PORTD = 0xff;
DDRD = 0x9F;
}
/****************计数器T1初始化***********************/
//TIMER1 initialize - prescale:Falling edge
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 1KHz
// actual value: Out of range
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0x00 /*INVALID SETTING*/; //setup
TCNT1L = 0x00 /*INVALID SETTING*/;
OCR1AH = 0x00 /*INVALID SETTING*/;
OCR1AL = 0x00 /*INVALID SETTING*/;
OCR1BH = 0x00 /*INVALID SETTING*/;
OCR1BL = 0x00 /*INVALID SETTING*/;
ICR1H = 0x00 /*INVALID SETTING*/;
ICR1L = 0x00 /*INVALID SETTING*/;
TCCR1A = 0x00;
TCCR1B = 0x00; //start Timer
//TCCR1B = 0x86;
}
//*************定时器T0初始化********************
//TIMER0 initialize - prescale:64
// WGM: Normal
// desired value: 1mSec
// actual value: 1.000mSec (0.0%)
void timer0_init(void)
{
TCCR0 = 0x00; //stop
TCNT0 = 0x83; //set count
OCR0 = 0x7D; //set compare
TCCR0 = 0x03; //start timer
}
/************************T0 1mS中断*****************************/
#pragma interrupt_handler timer0_ovf_isr:10
void timer0_ovf_isr(void)
{
TCNT0 = 0x83; //reload counter value
//------------------------------------
if(Wait10S_Start_Flag==1)
{
cnt++;
if(cnt>6000){Wait10S_Start_Flag=0;Wait10S_End_Flag=1;cnt=0;}
}
//------------------------------------
if(Wait6S_Start_Flag==1)
{
cnt++;
if(cnt>6000){Wait6S_Start_Flag=0;Wait6S_End_Flag=1;cnt=0;}
}
//------------------------------------
if(Wait2S_Start_Flag==1)
{
cnt++;
if(cnt>2000){Wait2S_Start_Flag=0;Wait2S_End_Flag=1;cnt=0;}
}
}
/************************器件初始化***************************/
//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();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x01; //timer interrupt sources
SEI(); //re-enable interrupts
watchdog_init();
//all peripherals are now initialized
}
/**********************欢迎界面********************/
void welcome(void)
{
ePutstr(0,0,str0);
ePutstr(0,1,str1);
ePutstr(0,2,str2);
ePutstr(0,3,str3);
}
/************************测试界面*************************/
void test_display(void)
{
ePutstr(0,0,cs0);
ePutstr(0,1,cs1);
ePutstr(0,2,cs2);
ePutstr(0,3,cs3);
}
/**********************扫描按键**************************/
void scan_key(void)
{
uchar temp;
temp=PINA;
if(temp==0xbe)status++;
Delay_nms(50);
}
uint average(void)//数字化滤波子函数,取样10次后求平均值
{
uchar i;
uint avx=0,array[10];
for(i=0;i<10;i++)
{
array[i]=ADC_Convert();
Delay_nms(2);
}
for(i=0;i<10;i++)avx=avx+array[i];
avx=avx/10;
return avx;
}
/**********************主函数*****************************/
void main(void)
{ uint temp;float f;uchar i;
Delay_nms(400);
init_devices();
InitLcd();
welcome();
LCM_BLCON_ON;
Delay_nms(3000);
LCM_BLCON_OFF;
/********************************************/
while(1) //无限循环
{
switch(status)
{
case 0:scan_key();WDR();//等待按启动键
break;
case 1:LcdWriteCommand(0x01,1);//清屏
LcdWriteCommand(0x0c,1);//开显示
test_display();//显示测试界面
status=2;
break;
case 2:AIR_ON;Delay_nms(2000);status=3;//启动气缸并等待2S进入刹车
break;
case 3:SPEED_5V;Delay_nms(100);POW_27V;//转换成27V,RPM
Delay_nms(100);POW_ON;status=4;//开电源
break;
case 4:Wait2S_Start_Flag=1;cnt=0;status=5;//启动2S定时
break;
case 5:while(Wait2S_End_Flag==0)Delay_nms(10);Wait2S_End_Flag=0;status=6;//等待2S定时结束
break;
case 6:current=average();status=7;//取样堵转电流
current=current-14;
f=(float)current;f=(f*2560)/1024;
f=f*4.25;//修正
LLC=f;//暂存堵转电流
if(LLC<480)OutFlag++;//判断堵转电流大小
current=(uint)f;
temp=current;temp=temp/1000;LC[3]=(uchar)temp;
temp=current;temp=(temp/100)%10;LC[2]=(uchar)temp;
temp=current;temp=(temp/10)%10;LC[1]=(uchar)temp;
temp=current;temp=temp%10;LC[0]=(uchar)temp;
DisplayOneChar(3,3,LC[3]+0x30);
DisplayOneChar(4,3,0x2e);
DisplayOneChar(5,3,LC[2]+0x30);
DisplayOneChar(6,3,LC[1]+0x30);
break;
case 7:POW_OFF;Delay_nms(100);//关闭电源
AIR_OFF;Delay_nms(500);status=8;cnt=0;//关闭气缸
break;
case 8:SPEED_5V;//黄线接5V
POW_14V;Delay_nms(100);POW_ON;//启动测试14V低压
Wait10S_Start_Flag=1;cnt=0;clr_T1();//启动10S并开始T1计数
display_val=0;status=9;TCCR1B = 0x06;
break;
case 9:while(Wait10S_End_Flag==0)Delay_nms(10);Wait10S_End_Flag=0;status=10;//等待10S定时结束
break;
case 10:TCCR1B = 0x00;display_val=read_T1();status=11;//读取T1的计数(14V低压LVS)
f=(float)display_val;f=f*4.985;display_val=(uint)f;//修正
LLVS=display_val;//暂存LVS
if(LLVS>200)OutFlag++;//判断14V低压能否启动
temp=display_val;temp=temp/1000;LVS[3]=(uchar)temp;
temp=display_val;temp=(temp/100)%10;LVS[2]=(uchar)temp;
temp=display_val;temp=(temp/10)%10;LVS[1]=(uchar)temp;
temp=display_val;temp=temp%10;LVS[0]=(uchar)temp;
DisplayOneChar(4,1,LVS[3]+0x30);
DisplayOneChar(5,1,LVS[2]+0x30);
DisplayOneChar(6,1,LVS[1]+0x30);
DisplayOneChar(7,1,LVS[0]+0x30);
break;
case 11:POW_OFF;Delay_nms(100);POW_27V;SPEED_0V;//转换成27V,TPV
Delay_nms(100);POW_ON;status=12;
break;
case 12:Wait6S_Start_Flag=1;cnt=0;status=13;//启动6S
break;
case 13:while(Wait6S_End_Flag==0)Delay_nms(10);Wait6S_End_Flag=0;status=14;cnt=0;//等待6S定时结束
break;
case 14:Wait10S_Start_Flag=1;cnt=0;clr_T1();
TCCR1B = 0x06;display_val=0;status=15;//启动10S并开始T1计数
break;
case 15:while(Wait10S_End_Flag==0)Delay_nms(10);Wait10S_End_Flag=0;status=16;cnt=0;//等待10S定时结束
break;
case 16:TCCR1B = 0x00;display_val=read_T1();status=17;//读取T1的计数(TPV)
f=(float)display_val;f=f*3.425;display_val=(uint)f;//修正
TTPV=display_val;//暂存TVP
if((TTPV>1850)&&(TTPV<2200))OutFlag++;//判断TPV
temp=display_val;temp=temp/1000;TPV[3]=(uchar)temp;
temp=display_val;temp=(temp/100)%10;TPV[2]=(uchar)temp;
temp=display_val;temp=(temp/10)%10;TPV[1]=(uchar)temp;
temp=display_val;temp=temp%10;TPV[0]=(uchar)temp;
DisplayOneChar(12,1,TPV[3]+0x30);
DisplayOneChar(13,1,TPV[2]+0x30);
DisplayOneChar(14,1,TPV[1]+0x30);
DisplayOneChar(15,1,TPV[0]+0x30);
break;
case 17:SPEED_5V;status=18;//转换成27V,RPM
break;
case 18:Wait6S_Start_Flag=1;cnt=0;status=19;//启动6S
break;
case 19:while(Wait6S_End_Flag==0)Delay_nms(10);Wait6S_End_Flag=0;status=20;cnt=0;//等待6S定时结束
break;
case 20:Wait10S_Start_Flag=1;cnt=0;clr_T1();
TCCR1B = 0x06;status=21;//启动10S并开始计数
break;
case 21:while(Wait10S_End_Flag==0)Delay_nms(10);Wait10S_End_Flag=0;status=22;cnt=0;//等待10S定时结束
break;
case 22:TCCR1B = 0x00;display_val=read_T1();status=23;//读取T1的计数(RPM)
f=(float)display_val;f=f*3;display_val=(uint)f;//修正
RRPM=display_val;//暂存RPM
if((RRPM>3600)&&(RRPM<4000))OutFlag++;//判断RPM
temp=display_val;temp=temp/1000;RPM[3]=(uchar)temp;
temp=display_val;temp=(temp/100)%10;RPM[2]=(uchar)temp;
temp=display_val;temp=(temp/10)%10;RPM[1]=(uchar)temp;
temp=display_val;temp=temp%10;RPM[0]=(uchar)temp;
DisplayOneChar(4,2,RPM[3]+0x30);
DisplayOneChar(5,2,RPM[2]+0x30);
DisplayOneChar(6,2,RPM[1]+0x30);
DisplayOneChar(7,2,RPM[0]+0x30);
break;
case 23:current=average();status=24;//取样最大运行电流
current=current-14;
f=(float)current;f=(f*2560)/1024;
f=f*0.95;//修正
RRC=f;//暂存最大运行电流
if(RRC<550)OutFlag++;//判断最大运行电流大小
current=(uint)f;
temp=current;temp=temp/1000;RC[3]=(uchar)temp;
temp=current;temp=(temp/100)%10;RC[2]=(uchar)temp;
temp=current;temp=(temp/10)%10;RC[1]=(uchar)temp;
temp=current;temp=temp%10;RC[0]=(uchar)temp;
DisplayOneChar(11,2,RC[3]+0x30);
DisplayOneChar(12,2,0x2e);
DisplayOneChar(13,2,RC[2]+0x30);
DisplayOneChar(14,2,RC[1]+0x30);
break;
case 24:POW_OFF;Delay_nms(100);POW_14V;status=25;//关闭27V及POW
break;
case 25:AIR_ON;Wait6S_Start_Flag=1;cnt=0;status=26;//启动2S及气缸
break;
case 26:ePutstr(0,0,result);//显示输出结果
if(OutFlag==5)
{
DisplayOneChar(12,3,'P');//显示PASS
DisplayOneChar(13,3,'A');
DisplayOneChar(14,3,'S');
DisplayOneChar(15,3,'S');
}
else
{
DisplayOneChar(12,3,'F');//显示FAIL
DisplayOneChar(13,3,'A');
DisplayOneChar(14,3,'I');
DisplayOneChar(15,3,'L');
}
while(Wait6S_End_Flag==0)//等待2S定时结束
{
if(OutFlag==5){LCM_BLCON_ON;BZ_ON;Delay_nms(100);BZ_OFF;}//合格响一下
else {BZ_ON;LCM_BLCON_ON;Delay_nms(50);
BZ_OFF;LCM_BLCON_OFF;Delay_nms(50);}//不合格连续响
}
Wait6S_End_Flag=0;
AIR_OFF;cnt=0;//关闭气缸
while(status==26)
{scan_key();WDR();
if(OutFlag<5)
{BZ_ON;LCM_BLCON_ON;Delay_nms(50);BZ_OFF;LCM_BLCON_OFF;Delay_nms(50);}
}
break;
case 27:status=0;display_val=0;current=0;//清零所有变量
clr_T1();cnt=0;
for(i=0;i<4;i++)LVS[i]=0;
for(i=0;i<4;i++)TPV[i]=0;
for(i=0;i<4;i++)RPM[i]=0;
for(i=0;i<4;i++)LC[i]=0;
for(i=0;i<4;i++)RC[i]=0;
RRC=0;LLC=0;OutFlag=0;LCM_BLCON_OFF;
break;
default:break;
}
}
}
//************************************
uint ADC_Convert(void)//ADC转换
{uint temp1,temp2;
temp1=(uint)ADCL;
temp2=(uint)ADCH;
temp2=(temp2<<8)+temp1;
return(temp2);
}
//****************************************
void Delay_1ms(void) //1mS延时子函数
{ uint i;
for(i=1;i<(uint)(xtal*143-2);i++)
;
}
//=============================================
void Delay_nms(uint n) //n*1mS延时子函数
{
uint i=0;
while(i<n)
{Delay_1ms();WDR();
i++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -