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

AD采样问题求助

AD采样问题求助

/*................................................
芯片:mc9s08qg4
设备:辅助电源
要求:主备电采样,报出主备电故障
主电检测通道ADP1
备电检测通道ADP0
要求检测主电时,不检测备电,检测备电时候,不检测主电
................................................*/
各位大大,我是第一次用这个芯片,也是第一次写程序,现在问题:
1.主电程序可以执行。备电无法执行。
2.250ms时备电灯无法常亮,为闪烁。
3.如何转换通道来达到要求?

谢谢各位大大

#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 = 0x00;\
ADCSC2 = 0x00;\
ADCSC1 = 0x01;\
}

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

/*****************************
**初始化MTI定时器中断模块
******************************/
#define InitMTI() {\
MTIMMOD = 150; \
MTIMCLK = 0x04;\
MTIMSC = 0x60;\
}


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

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_256ms() SRTISC = 0x15

/*************************************************************************
**关闭AD采样
**************************************************************************/
#define ADC_stop() ADCSC1_AIEN=0;ADCSC1_ADCH=0x1f


#define HOSTPOWER_ADC 170 /*主电24V*/
#define STANDBY_ADC 170 /*备电27V*/
#define charge_MAX_ADC 39 /*5v*/
#define charge_MIN_ADC 0 /*0v*/
unsigned char ADC_host = 0;
static unsigned char hostcnt = 0;
static unsigned char time = 0;
#define AD_SAMPLE 0x00

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_256ms();
//ics_intclk(0x04,0x08); //set ICS for FEI mode,Fbus=8M
InitADC_SAMPLE();
//turnon_relay(); //上电开继电器
//turnon_hostled(); //上电开主电灯
//turnon_standbyled(); //开备电时备电灯点亮
//permutationoff_DC0(); //上电置低
//permutationoff_BATT0();
EnableInterrupts; /* enable interrupts */

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


interrupt 23 void TIMER_ISR(void)
{//主电检测

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

ADC_host = ADCRL;

time++;

if (time == 2)
{
time = 0;

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

}
}

}

}


interrupt 23 void TIMER_RTI(void)
{
unsigned char ADC_standby = 0;
static unsigned char standbycnt = 0;
unsigned char ADC_charge =0;
static unsigned char chatgecnt = 0;

ADCSC1=AD_SAMPLE;

ADC_standby =ADCRL ;//ad0 vaule

if (ADC_standby >= STANDBY_ADC)
{
standbycnt = 0;
chatgecnt = 1;
turnon_standbyled();
//permutationoff_BATT0();

}
else
{
standbycnt++;
if (standbycnt >= 240)
{
standbycnt = 1;
turnoff_standbyled();
//permutationon_BATT0();

if (ADC_standby >= charge_MAX_ADC)
{
chatgecnt= 0;

}
}
}
}

还有我需要通过232来连接上位主机,我的波特率什么的该怎么设置啊?
TXD,RXD是ADP5,ADP4.
程序已改为:
各位大大,那里措了啊

#define HOSTPOWER_ADC 170 /*主电24V*/
#define STANDBY_ADC 170 /*备电27V*/
#define charge_MAX_ADC 39 /*5v*/
#define charge_MIN_ADC 0 /*0v*/
int black_flag = 0;
unsigned char ADC_host = 0;
static unsigned char hostcnt = 0;
static unsigned char time = 0;
unsigned char ADC_standby = 0;
static unsigned char standbycnt = 0;
unsigned char ADC_charge =0;
static unsigned char chatgecnt = 0;
#define a = 0;
#define AD_SAMPLE 0x00
#define ADC_TEMP 0x01

extern char RecChar(void);
extern void SendChar(char s_char);
//各通道A/D转换结果
//INT16U xdata luiv_ADC_ChannelThost = 0;
//INT16U xdata luiv_ADC_ChannelTstandby = 0;

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_256ms();
//ics_intclk(0x04,0x08); //set ICS for FEI mode,Fbus=8M
InitADC_SAMPLE();
//turnon_relay(); //上电开继电器
//turnon_hostled(); //上电开主电灯
//turnon_standbyled(); //开备电时备电灯点亮
//permutationoff_DC0(); //上电置低
//permutationoff_BATT0();
EnableInterrupts; /* enable interrupts */

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

}

/*
void Delay(INT8U lucv_Delay) //延时lucv_Delay * 1.0ms
{
INT8U n = 0;
INT16U m = 0;

for (n = 0; n < lucv_Delay; n++)
{
for (m = 0; m < 69; m++); //延时1.0ms
}
}

INT16U Get_ADC_Result(INT8U lucv_channel) //模数转换
{

INT8U lucv_ADC_Finished = 0; // 存储 A/D 转换标志
INT16U luiv_ADC_Data = 0;
ADCRL = 0;


ADCSC1 &= 0x80; //1110,0000 清 ADC_FLAG, ADC_START 位和低 3 位
ADCSC1 |= lucv_channel; //设置 A/D 当前通道

Delay(5); //延时, 使输入电压达到稳定

ADCSC1 |= 0x20; //0000,1000 令 ADC_START = 1, 启动A/D 转换

while (0 == lucv_ADC_Finished) // 等待A/D 转换结束
{
lucv_ADC_Finished = (ADCSC1 & 0x10); //0001,0000, ADC_FLAG ==1 测试A/D 转换结束否
}

ADCSC1 &= 0x00; //1111,0111 令 ADC_START = 0, 关闭A/D 转换,

luiv_ADC_Data = ADCRL;


}

*/


interrupt 23 void TIMER_ISR(void)
{//主电检测
SRTISC_RTIACK=1;
InitADC_SAMPLE();
ADCSC1 = ADC_TEMP;
ADC_host = ADCRL;
if (black_flag = 0)
{
time++;
if (time == 2)
{
time = 0;

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


if (black_flag = 1)
{
ADCSC1=AD_SAMPLE;
// BLED = !BLED;

ADC_standby =ADCRL ;//ad0 vaule

if (ADC_standby >= STANDBY_ADC)
{
standbycnt = 0;
chatgecnt = 1;
turnon_standbyled();
//permutationoff_BATT0();

}
else
{
standbycnt++;
if (standbycnt >= 240)
{
standbycnt = 1;
turnoff_standbyled();
//permutationon_BATT0();

if (ADC_standby >= charge_MAX_ADC)
{
chatgecnt= 0;
}
}
}
}
}
}
}
interrupt 23 void TIMER_ISR(void)
{//主电检测
SRTISC_RTIACK=1;
InitADC_SAMPLE();
ADCSC1 = ADC_TEMP;
while(!ADCSC1_COCO);
ADC_host = ADCRL;
time++;
if (time == 2)
{
time = 0;
if (ADC_host >= HOSTPOWER_ADC)
{
hostcnt = 0;
turnon_hostled();
//permutationoff_DC0();
}
else
{
hostcnt++;
if (hostcnt >= 10)
{
hostcnt = 1;
turnoff_hostled();
//turnoff_relay();
//permutationon_DC0();
}
}
}
//备电检测
InitADC_SAMPLE();
ADCSC1=AD_SAMPLE;
// BLED = !BLED;
while(!ADCSC1_COCO);
ADC_host = ADCRL;
if (ADC_standby >= STANDBY_ADC)
{
standbycnt = 0;
chatgecnt = 1;
turnon_standbyled();
//permutationoff_BATT0();
}
else
{
standbycnt++;
if (standbycnt >= 20)
{
standbycnt = 1;
turnoff_standbyled();
//permutationon_BATT0();
if (ADC_standby >= charge_MAX_ADC)
{
chatgecnt= 0;
}
}
}
}

主电灯亮,备电灯不亮,取反可以亮。
各位老大,问题已解决,是我疏忽了。
返回列表