Hi. I need help with mc9s08qg8 ADC

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Hi. I need help with mc9s08qg8 ADC

跳至解决方案
795 次查看
santygo
Contributor I

 this is the code i am using and i am not able to get the output. can you suggest me with any possible sollutions please?

 

 

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

 

void main(void)

{

  EnableInterrupts;


 

  PTADD = 0x00;

  PTAPE = 0xFF;

  PTBDD = 0xFF;

 

  for(;:smileywink:

  {

    __RESET_WATCHDOG();

   

    ADCSC1_ADCH = 0x00; 

    ADCSC1_AIEN = 1;

    ADCSC1_ADCO = 0;

    ADCSC2_ADTRG = 0; 

    ADCSC2_ACFE = 0; 

    ADCCFG_ADICLK = 0; 

    ADCCFG_ADIV = 0; 

    ADCCFG_MODE = 0x00; 

    ADCCFG_ADLSMP = 0; 

    APCTL1 = 0x01;

 

      if(ADCSC1_COCO==1)

      {

           

            PTBD_PTBD0 = ADCRL_ADR0;

            PTBD_PTBD1 = ADCRL_ADR1;

            PTBD_PTBD2 = ADCRL_ADR2;

            PTBD_PTBD3 = ADCRL_ADR3;

            PTBD_PTBD4 = ADCRL_ADR4;

            PTBD_PTBD5 = ADCRL_ADR5;

            PTBD_PTBD6 = ADCRL_ADR6;

            PTBD_PTBD7 = ADCRL_ADR7;

                       

      }

 

   

  }

}

标签 (1)
标记 (1)
0 项奖励
回复
1 解答
550 次查看
bigmac
Specialist III

Hello,

 

I can see a few problems with your code.  As a general rule it is better (more efficient) to initialise all bits simultaneously within a hardware register.  This is particularly important for the ADCSC1 register, where each write process will abort any conversion in progress, and start a new conversion.

 

Because writing to the ADCSC1 register starts a conversion, this should be done last, so that ADCCFG, APCTL1 and ADCSC2 register will already have been configured prior to the start of the first conversion.  In fact, you might initialise these within a separate ADC_init() function, called prior to the main loop where the conversions take place.

 

I see that you have enabled the ADC interrupt, but do not have an interrupt service routine (ISR).  However, since you seem to require polling the COCO flag, the interrupt should remain disabled.  In fact, your polling is not correct - you are testing the COCO flag, but not waiting until it becomes set.  As a consequence, the existing code will never complete a conversion because it loops to re-write the ADCSC1 register, which aborts the current conversion.

 

Finally, the enabling of interrupts should take place after all the required peripherals have been initialised, although this would not have affected the operation of your code.

 

Perhaps the following code will do what you intend?

 

#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void main(void){    PTADD = 0x00;  PTAPE = 0xFF;  PTBDD = 0xFF;  // Initialise ADC:  ADCCFG = 0x00;   // 8-bit mode, short sample  ADCSC2 = 0x00;   // Software trigger  APCTL1 = 0x01;   // Channel 0 input  EnableInterrupts;    for( ; ; ) {    __RESET_WATCHDOG();    ADCSC1 = 0x00;            // Start conversion on channel 0    while (ADCSC1_COCO == 0); // Wait until conversion is complete    PTBD = ADCRL;  }}

 

Regards,

Mac

 

在原帖中查看解决方案

0 项奖励
回复
1 回复
551 次查看
bigmac
Specialist III

Hello,

 

I can see a few problems with your code.  As a general rule it is better (more efficient) to initialise all bits simultaneously within a hardware register.  This is particularly important for the ADCSC1 register, where each write process will abort any conversion in progress, and start a new conversion.

 

Because writing to the ADCSC1 register starts a conversion, this should be done last, so that ADCCFG, APCTL1 and ADCSC2 register will already have been configured prior to the start of the first conversion.  In fact, you might initialise these within a separate ADC_init() function, called prior to the main loop where the conversions take place.

 

I see that you have enabled the ADC interrupt, but do not have an interrupt service routine (ISR).  However, since you seem to require polling the COCO flag, the interrupt should remain disabled.  In fact, your polling is not correct - you are testing the COCO flag, but not waiting until it becomes set.  As a consequence, the existing code will never complete a conversion because it loops to re-write the ADCSC1 register, which aborts the current conversion.

 

Finally, the enabling of interrupts should take place after all the required peripherals have been initialised, although this would not have affected the operation of your code.

 

Perhaps the following code will do what you intend?

 

#include <hidef.h> /* for EnableInterrupts macro */#include "derivative.h" /* include peripheral declarations */void main(void){    PTADD = 0x00;  PTAPE = 0xFF;  PTBDD = 0xFF;  // Initialise ADC:  ADCCFG = 0x00;   // 8-bit mode, short sample  ADCSC2 = 0x00;   // Software trigger  APCTL1 = 0x01;   // Channel 0 input  EnableInterrupts;    for( ; ; ) {    __RESET_WATCHDOG();    ADCSC1 = 0x00;            // Start conversion on channel 0    while (ADCSC1_COCO == 0); // Wait until conversion is complete    PTBD = ADCRL;  }}

 

Regards,

Mac

 

0 项奖励
回复