Displaying ATD readout through SCI.

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

Displaying ATD readout through SCI.

951 Views
Cn
Contributor I

Hello forum.. It's my first time programming a freescale's devices so I have a little question for all of you... I want to acquirer an analog signal and i want to transmit my results to SCI, could anyone indicate me where is my problem in my code

 

I'm using M68demo908gb60

 

#include <hidef.h> /* for EnableInterrupts macro */
#include "derivative.h" /* include peripheral declarations */
#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
//*********************************************************************************************************************************************
uchar ReceiveBuffer[10];

void delay(void){
  uint i,j,k;
  for(i=0;i<1000;i++)
    for(j=0;j<500;j++)
      k++;
}

//*********************************************************************************************************************************************
//SCI Init
void InitSCI1(void){
  SCI2C1 = 0x00;
  SCI2C3 = 0x00;
  SCI2C2 = 0x00;
  SCI2BD = Baudrate9600;
  SCI2C1_M = 1;    // nine bits
  SCI2C1_PE = 1;   // hardware parity enable
  SCI2C2_TE = 0x01;
  SCI2C2_RE = 0x01;
  SCI2C2_RIE = 0x01;
}

//*********************************************************************************************************************************************
  // ATDinit
  void initATD(void){
   
  ATD1PE = 0x02;     //Initialize the registers for ATD
  ATD1C = 0xA0;    //Configure the ATD1C register for the ATD module
  ATD1SC = 0x41; //Configure the ATD1SC register for the ATD module
 
  }
//*********************************************************************************************************************************************

byte SCI1_Transmit(byte Chr){
  if(!SCI2S1_TDRE) {           // Is the transmitter empty?
    return -1;                 // If no then error
  }
  SCI2D = (byte)Chr;
  return 0;                    // OK
}
//******************************************************************************************************************************************
      
void main(void) {
  //int result;
  byte TransmitStatus;
  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();
  initATD();
  EnableInterrupts; /* enable interrupts */
 
  /* include your code here */
 
  for(;:smileywink: {
    // __RESET_WATCHDOG(); /* feeds the dog */
    TransmitStatus = SCI1_Transmit(ATD1RH); // send data
      delay();
      ATD1SC = ATD1SC;  
  } /* loop forever */
  /* please make sure that you never leave this function */
}

Labels (1)
0 Kudos
Reply
2 Replies

510 Views
bigmac
Specialist III

Hello,

 

With a quick perusal of your code, I can see a number of potential problem areas.

 

  1. You do not say what the SCI module is to communicate with, but I would question whether you actually need 9-bit mode, with parity enabled.  Also, successively writing the individual bits of the SCI2C1 and SCI2C2 registers is a poor programming practice, in that it is less efficient than simultaneously writing all bits.
  2. Your ATD clock frequency seems to be much too high.
  3. The ATD module is currently set to an inactive low power state.
  4. You have configured the ATD for single conversion mode, with interrupt enabled.  But I do not see your ISR code to handle the interrupt.  Even if you had intended to use polling, I do not see where you are testing the CCF flag for completion of the current conversion.
  5. Whether you are using interrupts or polling, your code will need to start the next conversion, after reading the result of the previous conversion.
  6. You seem to be attempting to send the ADC data as raw, 8-bit binary data.  If this is intended to be human readable, I would assume the value should be converted to a 3 or 4 character numeric ASCII sequence.  A four-character sequence would allow for the full 10-bit resolution of the ATD module.

 

Regards,

Mac

 

0 Kudos
Reply

510 Views
Cn
Contributor I

Hello mac thanks for your observation..

 

Cold you please post an example how can i correct all my mistakes.

 

Thank CN

0 Kudos
Reply