📄 lpc935.c
字号:
break;
default:
Rec_Idx=0;
break;
}
}
EA=1;
}
//------------------------- //功能发送中断 //-------------------------
void Send_ISR() interrupt 13
{
EA=0;
EA=1;
} //--------------------------
//**************************************************
//10ms 中断函数
void timer0_int(void) interrupt 1
{
EA=0;
TH0=TH0_DATA;
TL0=TL0_DATA;
Cnt_100us++;
if(Cnt_100us>=10)
{
Cnt_MS++;
Snd_Time_Cnt++;
Cnt_100us=0;
}
if (Cnt_MS>=1000)
{
Cnt_MS=0;
//Flag_Second=true;
Second_Cnt++;
}
//if(Snd_Time_Cnt>=Send_Ms_Cnt&&Flag_AD) Send_AD_Result();
EA=1;
}
void timer0_init(void)
{
// configure timer 0
TMOD = 0x01;
// timer values
TH0 = TH0_DATA;
TL0 = TL0_DATA;
// enable timer 0 interrupt
ET0 = 1;
// start timer 0
TR0 = 1;
} // timers_init
void ADC_init(void)
{
ADINS=0x31; //0011 0001, 选择 AD11,AD10,AD00 3 channels
ADMODA=0x11; //0001 0001, 选择转换工作模式:自动扫描,单次转换模式
//ADMODA=0x44; //0001 0001, 选择转换工作模式:自动扫描,连续转换模式
ADMODB=0x00; //500Khz < ADC CLK < 3.3Mhz,,
// disable adc interrupt
EAD = 0;
}
void delayms(word ms)
{
byte i;
while (ms>0) {
for (i=0;i<200;i++);
ms--;
}
}
void SIO_Out_Hex(byte b)
{
SBUF='0';
while (!TI);
TI=0;
SBUF='x';
while (!TI);
TI=0;
if ((b>>4)<0x0a)
SBUF=0x30+(b>>4);
else
SBUF=0x37+(b>>4);
while (!TI);
TI=0;
if ((b&0x0f)<0x0a)
SBUF=0x30+(b&0x0f);
else
SBUF=0x37+(b&0x0f);
while (!TI);
TI=0;
SBUF='H';
while (!TI);
TI=0;
}
void SIO_Out_Dec(byte b)
{
byte temp;
temp=b/10;
SBUF=0x30+(temp);
while (!TI);
TI=0;
SBUF=0x30+(b-temp*10);
while (!TI);
TI=0;
}
void SIO_Out_Byte(byte b)
{
SBUF=b;
while (!TI);
TI=0;
}
void SIO_Out_Str (byte *pstr)
{
while (*pstr!=0x00)
{
SBUF=*pstr;
while (!TI);
TI=0;
pstr++;
}
}
void Clear_Timer(void)
{
EA=0;
Flag_10ms=false;
Flag_Second=false;
EA=1;
}
void Do_ADConvert()
{
ADCON0=0x05; //选择转换触发模式;并立即启动
AD_Current=AD0DAT0;
ADCON0&=0xF7; //清除中断完成标志
ADCON1=0x05; //选择转换触发模式;并立即启动
AD_Load=AD1DAT0; // ALL AD Convert in sample frequencs
AD_Battery=AD1DAT1;
ADCON1&=0xF7; //清除中断完成标志
}
void delayus(word us)
{
while (us--);
}
void Send_AD_Result(void)
{
SIO_Out_Byte(AD_Load);
SIO_Out_Byte(AD_Battery);
SIO_Out_Byte(AD_Current);
SIO_Out_Byte(0x0D);
SIO_Out_Byte(0x0A);
//Snd_Time_Cnt=0;
}
/***********************************************************************
DESC: Initializes Counter/timers
Timer 0 is not used
Timer 1 generates an interrupt every 1 falling edges on T1
RETURNS: Nothing
CAUTION: If interrupts are being used then EA must be set to 1
after calling this function
************************************************************************/
void Counter_init(void)
{
// configure timer 1
TMOD &= 0x0F;
TMOD |= 0x10;
// timer values
TH1 = 0x00;
TL1 = 0x00;
// enable timer 1 interrupt
ET1 = 1;
// stop timer 1
TR1 = 0;
} // timers_init
/***********************************************************************
DESC: Counter/Timer 1 Interrupt Service Routine
RETURNS: Nothing
CAUTION: timers_init must be called first
EA must be set to 1
************************************************************************/
void Counter_isr(void) interrupt 3
{
// reinitialize
TH1 = 0x00;
TL1 = 0x00;
} // Counter_isr
/***********************************************************************
DESC: Starts Counter/timer 1
RETURNS: Nothing
CAUTION: timers_init must be called first
************************************************************************/
void Counter_start(void)
{
TR1 = 1;
} // Counter_start
/***********************************************************************
DESC: Stops Counter/timer 1
RETURNS: Nothing
CAUTION: timers_init must be called first
************************************************************************/
void Counter_stop(void)
{
TR1 = 0;
} // Counter_stop
/***********************************************************************
DESC: Generates a 13 us delay needed to stabilize a
comparator output after enabling.
Note that the datasheet mentions a 10 us delay.
Because the timer may be clocked from the watchdog timer, which
can be up to 30% faster than stated, 30% has been added to the
absolute minimum delay of 10us to give 13us.
Uses timer 0
Actual delay: 13.02 us
RETURNS: Nothing
CAUTION: The delay must be an absolute minimum of 10us
************************************************************************/
void comparators_13usdelay(void)
{
/*
// ensure timer 0 stopped
TR0 = 0;
// configure timer 0 as 16-bit timer
TMOD &= 0xF0;
TMOD |= 0x01;
TAMOD &= 0xFE;
// set reload value
TH0 = 0xFF;
TL0 = 0xD0;
// disable timer interrupt
ET0 = 0;
// run timer and wait for overflow
TF0 = 0;
TR0 = 1;
while (!TF0);
// stop timer and clean up
TR0 = 0;
TF0 = 0;
*/
}
/***********************************************************************
DESC: Initializes a comparator
Selects the comparator inputs/reference voltage source, enables
comparator output, enables comparator, configures I/O pins
needed, enables interrupts
If a comparator is being enabled then comparators_13usdelay
is called to provide a 13us delay to stabilize the comparator
RETURNS: Nothing
CAUTION: Set EA to 1 to enable interrupts after calling
************************************************************************/
void comparators_init()
{
P0M1 |= 0x10;
P0M2 &= ~0x10;
PT0AD |= 0x10;
CMP1=0x28;
// enable comparator interrupt
delayms(1);
EC = 1;
}
/***********************************************************************
DESC: Comparator Interrupt Service Routine
Uses register bank 1
RETURNS: Nothing
CAUTION: comparators_init must be called and EA set to 1 to enable
interrupts.
Called when the output of any enabled comparator changes
************************************************************************/
void comparators_isr(void) interrupt 8
{
byte temp_TH1,temp_TL1;
// check if comparator 1 caused interrupt
EA=0;
temp_TH1=TH1;
temp_TL1=TL1;
LED7=1;
delayms(2);
LED7=0;
if (CMP1 & 0x01)
{
// clear interrupt flag
CMP1 &= ~0x01;
Counter_stop();
SIO_Out_Byte(Temp_TH);
SIO_Out_Byte(temp_TH1);
SIO_Out_Byte(temp_TL1);
SIO_Out_Byte(0x0D);
SIO_Out_Byte(0x0A);
Flag_GetCounter=true;
}
// check if comparator 2 caused interrupt
if (CMP2 & 0x01)
{
// clear interrupt flag
CMP2 &= ~0x01;
}
EA=1;
}
/***********************************************************************
DESC: Disables a comparator
RETURNS: Nothing
CAUTION: The port pins used by the comparator are not reconfigured to
be digital inputs or outputs.
************************************************************************/
void comparators_disable
(
bit compnum // comparator number: COMP_1 or COMP_2
)
{
// disable comparator 1
if (compnum == COMP_1)
{
CMP1 &= ~0x20;
}
// disable comparator 2
else
{
CMP2 &= ~0x20;
}
}
/***********************************************************************
DESC: Gets the current output of a comparator
RETURNS: Current comparator output
CAUTION: comparators_init must be called first
************************************************************************/
bit comparators_getoutput
(
bit compnum // comparator number: COMP_1 or COMP_2
)
{
// get output of comparator 1
if (compnum == COMP_1)
{
return (CMP1 >> 1) & 0x01;
}
// get output of comparator 2
else
{
return (CMP2 >> 1) & 0x01;
}
}
/***********************************************************************
DESC: Selects a positive input source for a comparator
RETURNS: Nothing
CAUTION: comparators_init must be called first.
The comparator interrupt is disabled while the input is
changed. This means that the other comparator not being changed
will also not generate an interrupt.
************************************************************************/
void comparators_selectposinput
(
bit compnum, // comparator number: COMP_1 or COMP_2
unsigned char posinput // positive input A or B: COMP_INPUTA or COMP_INPUTB
)
{
// disable comparator interrupt
EC = 0;
// configure comparator 1
if (compnum == COMP_1)
{
// initialize port pins according to configuration
if (posinput == COMP_INPUTA)
{
// select CIN1A as analog input
P0M1 |= 0x10;
P0M2 &= ~0x10;
PT0AD |= 0x10;
}
else
{
// select CIN1B as analog input
P0M1 |= 0x08;
P0M2 &= ~0x08;
PT0AD |= 0x08;
}
// clear input selection
CMP1 &= ~0x10;
// select new input
CMP1 |= posinput;
}
// configure comparator 2
else
{
// initialize port pins according to configuration
if (posinput == COMP_INPUTA)
{
// select CIN2A as analog input
P0M1 |= 0x04;
P0M2 &= ~0x04;
PT0AD |= 0x04;
}
else
{
// select CIN2B as analog input
P0M1 |= 0x02;
P0M2 &= ~0x02;
PT0AD |= 0x02;
}
// clear input selection
CMP2 &= ~0x10;
// select new input
CMP2 |= posinput;
}
// enable comparator interrupt
EC = 1;
}
void Get_Count()
{
EA=0;
DIVM=1;
Init_IO();
LED7=0;
Counter_init();
PDA=Low;
delayms(200);
LED7=1;
delayms(1);
LED7=0;
TH1 = 0x00;
TL1 = 0x00;
TR1=1;
P0M1 |= 0x10;
P0M2 &= ~0x10;
PT0AD |= 0x10;
CMP1=0x28;
EC = 1;
TR0=0;
EA=1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -