ATD measurement on M68DEMOGB60

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

ATD measurement on M68DEMOGB60

Jump to solution
2,703 Views
Sasuke
Contributor I

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

Labels (1)
0 Kudos
Reply
1 Solution
1,940 Views
bigmac
Specialist III

Hello,

 

No, you still have a ATD clock frequency that is far too high.  It seems that the bus clock is 18.9MHz (based on your baud rate divisors).  Your present setting will give an ATD clock of 9.45 MHz.  This should actually be 2 MHz. maximum for the higher Vdd range, and 1 MHz for the low Vdd range.

 

You have also now also enabled ATD interrupts, without providing a suitable ISR function.  This is also problematic.

 

Within the ATD initialisation function, writing to the ATD1SC register will start the first conversion, so the two other register should be written first - ATD1SC should be written last.  You might also follow this with the delay() function to allow for the first conversion period.

 

Attempting to monitor the CCF flag during debug will fail if you are also monitoring ATD1RH and/or ATD1RL registers.  The debug process actually provides the flag clearing mechanism.  Since you have a relatively long delay between readings, the testing of CCF is optional, but should do no harm.

 

When starting the next conversion, I would probably be more explicit with ATD1SC = 0x01; rather than using ATD1SC = ATD1SC;

 

Regards,

Mac

 

View solution in original post

0 Kudos
Reply
6 Replies
1,940 Views
bigmac
Specialist III

Hello,

 

With left-justified 10-bit mode, reading ATD1RL alone will provide only the two LSB's of each measurement.

 

Regards,

Mac

 

0 Kudos
Reply
1,940 Views
Sasuke
Contributor I

Hello MAC,

 

I changed the resolution to 8 bits and put the ATD1RH but the problem still continue do you have any suggestion?

0 Kudos
Reply
1,940 Views
bigmac
Specialist III

Hello,

 

You also seem to have a channel mismatch between the ATD1PE register and the channel selection within the ATD1SC register.

 

ATD1PE = 0x02; would indicate that AD1 channel has its pin enabled (this is a bit field), whereas

ATD1SC = 0xA2; would indicate measurements for AD2 channel will occur (the channel value is a number in this case).

 

Since you are starting a new conversion, after you have read the previous result, it is quite inappropriate to select continuous conversion mode.  Additionally the value written to the CCF bit is inconsequential - this is read only.

 

There may also be a problem with the prescale PRS = 0 (divide by 2).  This could imply a maximum allowable bus frequency of 2 MHz for correct operation of the ATD module.

 

Regards,

Mac

 

0 Kudos
Reply
1,940 Views
Sasuke
Contributor I

Hello BigMac... I made the suggestion who you explain but CCF  while(!ATD1SC_CCF)
 flag never change 0 to 1 do you have any idea i post the code again.. Thanks

 

#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){ATD1PE=0x02;       //Enable the ATP pin PTB1ATD1SC=0x41;      //Select ATDCO=1 and CH1ATD1C=0xA0;         //Control Register configuration }//************************************************??*************************************************?*?***************************************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(;;) {         __RESET_WATCHDOG(); /* feeds the dog */    //Transmit ("Hello PC\n\r");       while(!ATD1SC_CCF);       result=ATD1RH;     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) */}

 

0 Kudos
Reply
1,941 Views
bigmac
Specialist III

Hello,

 

No, you still have a ATD clock frequency that is far too high.  It seems that the bus clock is 18.9MHz (based on your baud rate divisors).  Your present setting will give an ATD clock of 9.45 MHz.  This should actually be 2 MHz. maximum for the higher Vdd range, and 1 MHz for the low Vdd range.

 

You have also now also enabled ATD interrupts, without providing a suitable ISR function.  This is also problematic.

 

Within the ATD initialisation function, writing to the ATD1SC register will start the first conversion, so the two other register should be written first - ATD1SC should be written last.  You might also follow this with the delay() function to allow for the first conversion period.

 

Attempting to monitor the CCF flag during debug will fail if you are also monitoring ATD1RH and/or ATD1RL registers.  The debug process actually provides the flag clearing mechanism.  Since you have a relatively long delay between readings, the testing of CCF is optional, but should do no harm.

 

When starting the next conversion, I would probably be more explicit with ATD1SC = 0x01; rather than using ATD1SC = ATD1SC;

 

Regards,

Mac

 

0 Kudos
Reply
1,940 Views
Sasuke
Contributor I

Thanks mac i solved the problem... My configuration was wrong...

0 Kudos
Reply