AnsweredAssumed Answered

ATD measurement on M68DEMOGB60

Question asked by Carlos Mendez on Jan 14, 2011
Latest reply on Jan 26, 2011 by Carlos Mendez

Hello forum i need some help, i developed a simple code to acquire an analog input from PTB1 and display the result of this mesurement to SCI port (i use the SCI2D), so when i run the program i cant see the value of the measurement, some times the SCI port show me some values but they are wrong because some times i never put an analog signal into the port.

The range of the analog signal is from a AA battery (1.5 v)

When i put an analog signal i connect the PTB1(pin 34) to the baterry's positive and the ground goes to the card's ground, may i correct?


Please tell me where is my error i attach the code who i developed




#include <hidef.h> /* for EnableInterrupts macro */#include <MC9S08GB60.h> /* include peripheral declarations */#define initSOPT 0b01110011     //COP and STOP enable controls//                 |||   ||//                 |||   |+-RSTPE --- Reset pin enabled//                 |||   +--BKGDPE -- BKGD pin enabled//                 ||+------STOPE --- STOP allowed//                 |+-------COPT ---- long timeout 2^18//                 +--------COPE ---- COP off#define initICGC2 0b01110000    //Clock Generator Control 2//                  ||||||||     should write MFDx before ICGC1//                  |||||||+-RFD0 \//                  ||||||+--RFD1  --- post-PLL divider (div by 1)//                  |||||+---RFD2 ///                  ||||+----LOCRE --- loss of clock doesn't reset//                  |||+-----MFD0 \//                  ||+------MFD1  --- FLL loop multiplier N = 18x//                  |+-------MFD2 ///                  +--------LOLRE --- loss of lock doesn't reset// after FLL init, LOCRE will be set to force reset on clock loss#define initICGC1 0b00111000   //Clock Generator Control 1//                  0|||||xx     this setting for 32kHz//                   ||||+---OSCSTEN - don't keep osc on in stop mode//                   |||+----CLKS0 \ - select FLL engaged external//                   ||+-----CLKS1 /    (FEE) mode//                   |+------REFS ---- enable oscillator amplifier//                   +-------RANGE --- 32kHz crystal//////The following values has been test through communication with a PC//except Baudrate230400 because usually PC doesn't support such high baud rate#define Baudrate9600     0x007b;   // actually  9590.634#define Baudrate19200    0x003d;   // actually  19338.492#define Baudrate38400    0x001f;   // actually  38053.161#define Baudrate57600    0x0014;   // actually  58982.400#define Baudrate115200   0x000a;   // actually  117964.800#define Baudrate230400   0x0005;   // actually  235929.600//******************************************************************************************************************************************void delay(void){  uint i,j,k;  for(i=0;i<1000;i++)    for(j=0;j<500;j++)      k++;}//******************************************************************************************************************************************//Variables declarationvoid Transmit (unsigned char *sMessage);unsigned char result;unsigned char buffer [19] = "Voltage:  .     \n\r";      /* 16 char PC Buffer *///*****************************************************************************************************************************************/* -------------------------------------------------------------- Convert ATC CH1 conversion to a fractional number  with two decimal precision.  ------------------------------------------------------------ */ void Bin2Frac(unsigned char value){ //volatile unsigned int DecResult; volatile unsigned int temp; //volatile unsigned char remainder;  temp = (value*100)/51; buffer[12]= (unsigned char)(temp % 10); temp = temp / 10; buffer[11]= (unsigned char)(temp % 10); buffer[9]= (unsigned char)temp / 10;  buffer[9] += 0x30; buffer[11] += 0x30; buffer[12] += 0x30;}//******************************************************************************************************************************************void InitSCI1(void){  SCI2C1 = 0x00;    //Initializing SCI2C1  SCI2C3 = 0x00;    //Initializing SCI2C3  SCI2C2 = 0x00;     //Initializing SCI2C2  SCI2BD = Baudrate9600;  //Baudrate   SCI2C1_M = 0x00;    // eight bits  SCI2C1_PE = 0x00;   // hardware parity disable  SCI2C2_TE = 0x01; //Transmition Enable  SCI2C2_RE = 0x00;     //Reception Disable  //SCI2C2_RIE = 0x00;  //Receiver Interrupt Enable}//*****************************************************************************************************************************************void InitATD(void){ATD1C=0x80;         //Control Register configuration 10 bits.ATD1PE=0x02;       //Enable the ATP pin PTB1ATD1SC=0xA2;      //Select CCF=1,ATDCO=1 and CH1}//*****************************************************************************************************************************************void main(void) {  PTBSE = 0x00;  SOPT = initSOPT;    ICGC2 = initICGC2;  ICGC1 = initICGC1;     //32kHz -> 18.874368MHz bus rate  while(!ICGS1_LOCK);    //loop until FLL locks  ICGC2_LOCRE = 1;       //enable reset if ref clock fails   InitSCI1();                    //Function to initialize the SCI port    InitATD();                     //Function to initialize the ATD port          EnableInterrupts; /* enable interrupts */   /* include your code here */  for(;;) {   result= ATD1RL; //read channel         //__RESET_WATCHDOG(); /* feeds the dog */    //Transmit ("Hello PC\n\r");     Bin2Frac(result);   //Transform the ATD1RL(read channel and convert to format #.##)     Transmit(&buffer[0]); //Send to SCI port the measurement of CH1        ATD1SC = ATD1SC; //Re-start the conversion        delay();  } /* loop forever */  /* please make sure that you never leave this function */}//******************************************************************************************************************************************//This function allow to transmit data via RS232void Transmit (unsigned char *sMessage){ unsigned char k=0; while(sMessage[k] != 0){  SCI2D  = sMessage[k];  k++;   while(!SCI2S1_TDRE);   /* Polling for transmitter to be empty */ }/* END while(sMessage) */}


And this is some values who i get on hyperterminal, please see the attach