首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

AD中断采样

AD中断采样

#include /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#include "..\include\config.h"
#include

typedef unsigned char INT8U;
BIT_BYTE my_bits;
BIT_BYTE my_bits1;
BIT_BYTE my_bits2;
BIT_BYTE my_bits3;

byte AD_temp;
const volatile byte flash_protect @0x0000FFBD = { 0xF0 };//FLASH除第一页外,全部保护

/*操作FLASH的代码,保存在FLASH中的程序段,需要的时候调用到内存*/
const volatile unsigned char PGM_FLASH[59] = {
0x87,0xC6,0x18,0x25,0xA5,0x10,0x27,0x08,0xC6,0x18,0x25,0xAA,0x10,0xC7,0x18,0x25,
0x9E,0xE6,0x01,0xF7,0xA6,0x20,0xC7,0x18,0x26,0x45,0x18,0x25,0xF6,0xAA,0x80,0xF7,
0x9D,0x9D,0x9D,0x9D,0x45,0x18,0x25,0xF6,0xF7,0xF6,0xA5,0x30,0x27,0x04,0xA6,0xFF,
0x20,0x07,0xC6,0x18,0x25,0xA5,0x40,0x27,0xF9,0x8A,0x81};

#pragma DATA_SEG FAR FAR_RAM
volatile unsigned char PGM[59]={0}; //内存中用于存放FLASH操作程序代码的数组

#define enter_stop() asm ("stop")
#define Enter_Wait asm(wait)
#define Page_Erase PGM[21]=0x40; ((unsigned char(*)(unsigned int))(PGM))
#define Program_Byte PGM[21]=0x20; ((unsigned char(*)(unsigned int, unsigned char))(PGM))

#define InitPorts()\
PTAPE = 0xFE;\
PTADD = 0x3c;\
PTADS = 0x1e;\
\
MLED=1;\
BLED=1;\
\
PTBPE = 0xF9;\
PTBDD=0x1c;\
PTBDS=0x19;\
\
RXD=1;\
TXD=1;\
BATT0=1;\
DC0=1;\
CHG0=1;\
DC=0;\
BATT=0;\
CK=1;\
CD=0


#define InitSysOpts() \
SOPT1 = SOPT1val;\
SOPT2 = SOPT2val;\
SPMSC1 = SPMSC1_LVD_OFF



#define CLK_FBI() ICSC1 = 0x44; ICSC2 = 0x00;while (ICSC1_CLKS != ICSSC_CLKST) {}
#define CLK_FEI_8M() ICSC1 = 0x04; ICSC2 = 0x00;while (ICSC1_CLKS != ICSSC_CLKST) {}
#define CLK_FEI_4M() ICSC1 = 0x04; ICSC2 = 0x40;while (ICSC1_CLKS != ICSSC_CLKST) {}
#define CLK_FEI_2M() ICSC1 = 0x04; ICSC2 = 0x80;while (ICSC1_CLKS != ICSSC_CLKST) {}
#define CLK_FEI_1M() ICSC1 = 0x04; ICSC2 = 0xC0;while (ICSC1_CLKS != ICSSC_CLKST) {}

/****************************************************
函数名称:ics_intclk
输入变量:C1,C2
返回值:无
功能描述:初始化系统的时钟系统
创建人:
创建时间:
修改人:
修改内容:
修改时间:
*****************************************************/

void ics_intclk(byte C1, byte C2) {

if (NVICSTRM != 0xFF) {
ICSTRM = NVICSTRM; // load trim value if location not blank
}
ICSC1 = C1;
ICSC2 = C2;
while (ICSC1_CLKS != ICSSC_CLKST) {} // wait for clk state to match clk select
FCDIV = 39;
} //end ics_setup


/*************************************************************************
**打开AD采样中断,参数adc_chnl为所需采样的通道
**************************************************************************/
void ADC_Go(byte adc_chnl) {

ADCSC1_AIEN=1;
ADCSC1_ADCO=1;
ADCSC1_ADCH = adc_chnl;
} //end ADC_Go



/*************************************************************************
**初始化用来采样时候的AD模块,大概40秒采集4个数据
**************************************************************************/
#define InitADC_SAMPLE() {\
ADCCFG = 0x08;\
ADCSC2 = 0x00;\
}

/******************************************************
**初始化用于AD采样时的MTI定时器中断模块
*******************************************************/
#define InitMTI_FOR_AD() {\
MTIMMOD = 120; \
MTIMCLK = 0x04;\
MTIMSC = 0x60;\
}




/******************************************************
**初始化RTI中断模块,512ms中断一次

SRTISC = 0b01010111; //disable RTI while debugging\
||||||||\
|||||+++-- RTIS = RTI delay select: 001=8 msec\
||||+----- unimplemented\
|||+------ RTIE = RTI int enable: 1=enabled\
||+------- RTICLKS = RTI clock select: 1=external src\
|+-------- RTIACK = RTI flag acknowledge\
+--------- RTIF = RTI flag\

*******************************************************/
#define InitRTI_512ms() SRTISC = 0x16



#define HOSTPOWER_ADC 170 /*主电24V*/
#define STANDBY_ADC 193 /*备电27V*/
#define charge_ADC 155 /*充电关*/
#define charge_MAX_ADC 39 /*5v*/
#define charge_MIN_ADC 0 /*0v*/


extern char RecChar(void);
extern void SendChar(char s_char);

void main(void)
{
/*SRS记录系统的重启原因,比如是上电重启还是看门狗重启等等*/
RAMSRS = SRS; //Save SRS vaue to RAM
SRS=0;

/* Uncomment this function call after using Device Initialization
to use the generated code */
/* MCU_init(); */
InitPorts(); //初始化端口
InitSysOpts(); //初始化系统寄存器
InitRTI_512ms();
//ics_intclk(0x04,0x08); //set ICS for FEI mode,Fbus=8M
InitADC_SAMPLE();

EnableInterrupts; /* enable interrupts */

for(;;)
{
__RESET_WATCHDOG();
SendChar(0x55);

}

}

interrupt 23 void TIMER_ISR(void)
{
unsigned char ADC_host = 0;
static unsigned char hostcnt = 0;

//SRTISC |= 0x16;
InitRTI_512ms();
SRTISC_RTIACK=1;
//MLED = ~MLED;


ADC_host = ADCRL;

if (ADC_host >= HOSTPOWER_ADC)
{
hostcnt = 0;
turnon_hostled();
//permutationoff_DC0();
}
else
{
hostcnt++;
if (hostcnt >= 120)
{
hostcnt = 1;
turnoff_hostled();
//turnoff_relay();
//permutationon_DC0();

}
}

}
MLED取反可以 进去中断,500ms一次,但主程序就不能进去,请各位老大帮我看下是为什么啊?谢谢~!
谢谢各位已解决~!!
返回列表