📄 main.c
字号:
/* include files */
#include "per_XDx512_L15Y.h"
#include "xgate_vectors.h"
#include "main.h"
#include "lcd.h"
#include "lcd_hw.h"
#include "load_meas.h"
void SetIntPrio(char channel, char prio) {
Interrupt.int_cfaddr = (channel << 1) & 0xf0;
Interrupt.int_cfdata[channel & 0x07].byte = prio;
}
/* initialise XGate */
/* code is copied from Flash to RAM during Start-up */
void Xgate_Init(void) {
XGATE.xgvbr = (int)((void*__far)XGATEVectorTable)-0x00c0; /* initialise Xgate vector base */
if (PARTID.word==0xc400) {
/* older version of Xgate */
XGATE.xgmctl = (XGE | XGFRZ | XGIE)<<8; /* interrupt enable, enable Xgate, stop Xgate in freeze mode */
} else {
/* new version of Xgate */
XGATE.xgmctl = 0xff00 | XGE | XGFRZ | XGIE; /* interrupt enable, enable Xgate, stop Xgate in freeze mode */
}
}
/* Set-ups the PLL, make sure the REFDV is set accordingly */
#define REFDV 3 /* 4 MHz crystal */
void PLLStartup(tU08 freq) {
/* switch to OSCCLK */
CRG.clksel.byte = 0;
CRG.pllctl.byte = CME | /* clock monitor enabled */
PLLON | /* PLL on */
AUTO | /* automatic bandwidth control enabled */
SCME; /* self clock mode enabled */
if(freq != 0) {
/* configure PLL */
/* fref = fosc / (REFDV+1) */
CRG.refdv.byte = REFDV;
/* fvco = 2*fref*(SYNR+1) */
CRG.synr.byte = freq - 1;
/* wait for lock FOR EVER! */
while(CRG.crgflg.bit.lock != SET);
if(CRG.crgflg.bit.lock == SET) {
/* switch to PLLCLK */
CRG.clksel.byte |= PLLSEL;
}
}
}
/* initialises the interrupt module */
void demo_init(void) {
PIT.pitce.bit.pce1 = 1; /* enable PIT channel 1 */
PIT.pitinte.bit.pinte1 = 1; /* enable interrupts from channel 1 */
PIT.pitmux.bit.pmux1 = 1; /* assign channel 1 to microtimer 1 */
PIT.pitmtld1.byte = 255; /* divide by 255 */
PIT.pitld1.word = 52082; /* 3Hz timeout frequency */
/* enable the PIT module and force reload of the micro counter */
PIT.pitce.bit.pce2 = 1; /* enable PIT channel 2 */
PIT.pitinte.bit.pinte2 = 1; /* enable interrupts from channel 2 */
PIT.pitmux.bit.pmux2 = 1; /* assign channel 2 to microtimer 1 */
PIT.pitld2.word = 5208; /* 30Hz timeout frequency */
/* enable the PIT module and force reload of the micro counter */
PIT.pitcflmt.byte = PITE | PITFRZ | PFLMT1;
PIT.pitflt.bit.pflt1 = 1; /* force reload of counter 1 */
PIT.pitflt.bit.pflt2 = 1; /* force reload of counter 2 */
asm(cli); /* enable interrupts */
PERT.byte|=0x03; /* enable pull-ups on the two buttons */
}
/* main */
void main(void) {
PLLStartup(40); /* 40MHz */
Xgate_Init(); /* initialise Xgate and the Int module */
LCD_start(40000,-5000); /* 40MHz bus clock, 2 idle cycles with contrast adjustment of -5000 for optimum contrast at 5V supply */
demo_init();
/* XGATE load measurement */
SetIntPrio(0x6F, RQST|3); /* ECT overflow */
SetIntPrio(0x39-LM_INIT_SWX, RQST|1); /* init */
LM_INIT(); /* initialise */
while(1);
}
#define CONTRAST_MIN (-32767+CONTRAST_STEP+1) /* darkest display */
#define CONTRAST_MAX (32767-CONTRAST_STEP-1) /* lightest display */
#define CONTRAST_UP (PTT.bit.bit0==0)
#define CONTRAST_DOWN (PTT.bit.bit1==0)
#define CONTRAST_STEP 500
/* LCD contrast adjustment */
void interrupt lcd_contrast(void) {
static int contrast=-5000;
PIT.pittf.byte = PTF2; /* clear the interrupt flag */
/* contrast */
if (CONTRAST_UP) {
contrast+=CONTRAST_STEP;
if (contrast>CONTRAST_MAX) contrast=CONTRAST_MAX;
LCD_contrast(40000,contrast);
} else if (CONTRAST_DOWN) {
contrast-=CONTRAST_STEP;
if (contrast<CONTRAST_MIN) contrast=CONTRAST_MIN;
LCD_contrast(40000,contrast);
}
}
/* demo for taking the LCD through its paces */
void interrupt lcd_demo(void) {
static unsigned char state1=0;
static int counter1=195;
static unsigned char state2=0;
static int counter2=-22;
static unsigned char state3=0;
static char counter3=0;
static unsigned char state4=0;
static char counter4=0;
PIT.pittf.byte = PTF1; /* clear the interrupt flag */
switch(state1) {
case(0):
/* display1 going up, display 3 digits with decimal point and degC */
wt_diplay1_set(counter1,1);
WT_DISPLAY(WT_DISP1_DEGC);
counter1++;
if (counter1>=226) {
counter1=67;
state1=1;
}
break;
case(1):
/* display1 going down, display 2 digits and degF */
wt_diplay1_set(counter1,2);
WT_ERASE(WT_DISP1_DEGC);
counter1--;
if (counter1<=44) {
counter1=195;
state1=0;
}
}
switch(state2) {
case(0):
/* display2 going up, degC, outside */
wt_diplay2_set(counter2);
WT_DISPLAY(WT_DISP2_DEGREE);
WT_DISPLAY(WT_DISP2_DEGC);
WT_ERASE(WT_DISP2_DEGF);
WT_DISPLAY(WT_OUTSIDE);
counter2++;
if (counter2>=-10) {
counter2=121;
state2=1;
}
break;
case(1):
/* display2 going down, degF */
wt_diplay2_set(counter2);
WT_DISPLAY(WT_DISP2_DEGREE);
WT_ERASE(WT_DISP2_DEGC);
WT_DISPLAY(WT_DISP2_DEGF);
WT_ERASE(WT_OUTSIDE);
counter2--;
if (counter2<=90) {
counter2=-22;
state2=0;
}
}
switch(state3) {
case(0):
/* windscreen */
WT_DISPLAY(WT_WINDSCREEN);
WT_ERASE(WT_ARROW_TOP);
WT_ERASE(WP_ARROW_BOTTOM);
WT_ERASE(WP_BODY);
counter3++;
if (counter3>=5) {
counter3=0;
state3=1;
}
break;
case(1):
/* windscreen + body + arrow bottom */
WT_DISPLAY(WT_WINDSCREEN);
WT_ERASE(WT_ARROW_TOP);
WT_DISPLAY(WP_ARROW_BOTTOM);
WT_DISPLAY(WP_BODY);
counter3++;
if (counter3>=5) {
counter3=0;
state3=2;
}
break;
case(2):
/* windscreen + body + arrow top */
WT_DISPLAY(WT_WINDSCREEN);
WT_DISPLAY(WT_ARROW_TOP);
WT_ERASE(WP_ARROW_BOTTOM);
WT_DISPLAY(WP_BODY);
counter3++;
if (counter3>=5) {
counter3=0;
state3=3;
}
break;
case(3):
/* body + arrow top + arrow bottom */
WT_ERASE(WT_WINDSCREEN);
WT_DISPLAY(WT_ARROW_TOP);
WT_DISPLAY(WP_ARROW_BOTTOM);
WT_DISPLAY(WP_BODY);
counter3++;
if (counter3>=5) {
counter3=0;
state3=4;
}
break;
case(4):
/* nothing */
WT_ERASE(WT_WINDSCREEN);
WT_ERASE(WT_ARROW_TOP);
WT_ERASE(WP_ARROW_BOTTOM);
WT_ERASE(WP_BODY);
counter3++;
if (counter3>=5) {
counter3=0;
state3=0;
}
break;
}
switch(state4) {
case(0):
/* bargraph increasing, A/C */
WT_ERASE(WT_AUTO);
if (counter4>0) WT_DISPLAY(WT_AC);
if (counter4>1) {
WT_DISPLAY(WT_FAN);
wt_display_bargraph(counter4-2);
} else {
WT_ERASE(WT_FAN);
wt_display_bargraph(0);
}
counter4++;
if (counter4>=12) {
counter4=11;
state4=1;
}
break;
case(1):
/* bargraph decreasing, AUTO */
if (counter4>0) WT_DISPLAY(WT_AUTO); else WT_ERASE(WT_AUTO);
WT_ERASE(WT_AC);
if (counter4>1) {
WT_DISPLAY(WT_FAN);
wt_display_bargraph(counter4-2);
} else {
WT_ERASE(WT_FAN);
wt_display_bargraph(0);
}
counter4--;
if (counter4<=-1) {
state4=0;
counter4=1;
}
break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -