Hello forum I need your help I'm trying to acquirer an analog signal and convert to digital and send via SCI so I developed a code but I don't no where is my error because the CCF flag has not change 0 to 1 because that I can not do the measurement and transmit to SCI port could anyone suggest me any idea please see the code
Hardware Demo908gb60
#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 declaration void 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 PTB1 ATD1SC=0x41; //Select ATDCO=1 and CH1 ATD1C=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 RS232 void 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) */ }
thanks for your time and I hope you can help me
Solved! Go to Solution.
ATD1SC=0x41; //Select ATDCO=1 and CH1
This code doesn't do what the commet says, but instead it enables interrupts, which you don't handle in your code, and it sets channel to AD0. This is why you shouldn't setup register with hex values, but with pre-defined bit masks.
void delay(void){
uint I,j,k;
for(I=0;I<1000;I++)
for(j=0;j<500;j++)
k++;
}
This function does nothing and the compiler will most likely optimize it away entirely. I would strongly suggest using hardware timers instead. If you insist on keeping this burn-away loop, you must make all loop iterators volatile or there are no guarantees it will work.
ATD1SC=0x41; //Select ATDCO=1 and CH1
This code doesn't do what the commet says, but instead it enables interrupts, which you don't handle in your code, and it sets channel to AD0. This is why you shouldn't setup register with hex values, but with pre-defined bit masks.
void delay(void){
uint I,j,k;
for(I=0;I<1000;I++)
for(j=0;j<500;j++)
k++;
}
This function does nothing and the compiler will most likely optimize it away entirely. I would strongly suggest using hardware timers instead. If you insist on keeping this burn-away loop, you must make all loop iterators volatile or there are no guarantees it will work.