/* I have written a small function for ADC MC9S08DN16 controller below is the code i have attached Every time i generate and adc conversion i am getting diffrent values but these values are logically not correct because vcc is 24 volts and when 24 volts is given the adc value is 225 when 15 volts is given the value is 185 when 10 volts is given the value is 165 when i take away the connection i.e 0 V i still read 98 i wanted to implement thsi on 10 bit but first i would like to implement it on 8 bit */
/* Please help as i am a new bie to this controller that the reason i am asking questions regularly */
#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
void ADC_init(void);
void mode_initialization(void);
void MCU_init(void);
void main(void)
{
unsigned char temp;
unsigned int higher, lower;
EnableInterrupts;
ADC_init();
mode_initialization();
MCU_init();
SOPT1_COPT = 0;
while(1)
{
ADCSC1_ADCH = 5;
ADCSC1 = ADCSC1 & 0x7f;
while(ADCSC1_COCO == 0);
higher = ADCRH;
lower = ADCRL;
}
}
void ADC_init(void)
{
APCTL1_ADPC4 = 1;
APCTL1_ADPC5 = 1; // chanell 5 selected
APCTL1_ADPC6 = 1;
ADCCFG_ADLPC = 0; // high speed configuration
ADCCFG_ADIV = 2; // input clock divided by 4
ADCCFG_ADLSMP = 0; // short sample time
ADCCFG_MODE = 0; // 8 bit conversion
ADCCFG_ADICLK = 0; // bus clock select
ADCSC2_ADTRG = 0; // software triger selected
ADCSC2_ACFE = 0; // compare function disable
ADCSC1_AIEN = 0; // interrupt disable
ADCSC1_ADCO = 0; // one conversion
}
void mode_initialization(void)
{
/* Transition from FEI mode to FBE mode */
SOPT1_COPT = 0; // watch dog timer turn off
MCGC2 = 0x36;
while (MCGSC_OSCINIT == 0);
asm SEI; // Disables all interrupt
MCGC1 = 0xB8;
while(MCGSC_IREFST == 1);
while(!(MCGSC_CLKST == 2));
/* Transition from FBE mode into BLPE mode */
MCGC2 = 0x3E;
asm CLI; // Enabled interrupt
MCGC1 = 0x98;
MCGC3 = 0x44;
while (MCGSC_PLLST == 0);
/* BLPE mode transitions into PBE mode: */
MCGC2_LP = 0;
while(MCGSC_LOCK == 0);
}
void MCU_init(void)
{
SOPT1_COPT = 0;
SRS = 0xFF; // Clears watch dog timer
SPMSC1 = 0x1C;
/* SPMSC2: LVWF=0,LVWACK=0,LVDV=0,LVWV=0,PPDF=0,PPDACK=0,PDC=0,PPDC=0 */
SPMSC2 = 0x00;
/* ICGC1: RANGE=1,REFS=0,CLKS1=0,CLKS0=0,OSCSTEN=1 */
/* Common initialisation of the CPU registers */
/* PTASE: PTASE7=0,PTASE6=0,PTASE5=0,PTASE4=0,PTASE3=0,PTASE2=0,PTASE1=0,PTASE0=0 */
PTASE = 0x00;
/* PTBSE: PTBSE7=0,PTBSE6=0,PTBSE5=0,PTBSE4=0,PTBSE3=0,PTBSE2=0,PTBSE1=0,PTBSE0=0 */
PTBSE = 0x00;
/* PTCSE: PTCSE7=0,PTCSE6=0,PTCSE5=0,PTCSE4=0,PTCSE3=0,PTCSE2=0,PTCSE1=0,PTCSE0=0 */
PTCSE = 0x00;
/* PTDSE: PTDSE7=0,PTDSE6=0,PTDSE5=0,PTDSE4=0,PTDSE3=0,PTDSE2=0,PTDSE1=0,PTDSE0=0 */
PTDSE = 0x00;
/* PTESE: PTESE7=0,PTESE6=0,PTESE5=0,PTESE4=0,PTESE3=0,PTESE2=0,PTESE1=0,PTESE0=0 */
PTESE = 0x00;
/* PTFSE: PTFSE7=0,PTFSE6=0,PTFSE5=0,PTFSE4=0,PTFSE3=0,PTFSE2=0,PTFSE1=0,PTFSE0=0 */
PTFSE = 0x00;
/* PTGSE: PTGSE7=0,PTGSE6=0,PTGSE5=0,PTGSE4=0,PTGSE3=0,PTGSE2=0,PTGSE1=0,PTGSE0=0 */
PTGSE = 0x00;
/* ### Init_COP init code */
// SRS = 0xFF; /* Clear WatchDog counter */
}
Hi Ankur,
Thanks for all the help and responces code is working fine now i dint change any thing but the code works :-)
Hi Vikas,
Before going through the code, we would like a few clarifications. DN16 is a 5V device, so the voltage of any signal at any of the pins must be between 0 to 5 V. When you say the input signal is 24V, you would probably mean that you are scaling down the voltage. The readings in that case in the result register would be as per the dividing factor applied on the hardware. Please verify your hardware settings or share the schematic.
Regards,
Ankur
Hi Ankur,
Sorry for the incomplete information, yes I am scaling down the voltage from 24 to 5 volts and i did verify the voltage at diffrent points through multimeter and the the voltage is between 0-5 volts.
Hi Vikas,
I dont have a DN16 hardware to test your settings. Would it be appropriate if I can give you a tested code on DZ60 (a similar device) and then you can port it on your system. Is there any specific requirement for the system ADC?
Regards
Ankur
Can you give me the tested code on DZ60? because i met a problem that the ADC value fluctuating too much. i cannot find the reason,so can you help me?