adc hangs in c8051f350

Thread Starter

freemanantony

Joined May 28, 2011
9
hai every one ,
i am using C8051350 for reading voltage and writing to dac proportional to input voltage to adc,while reading adc section is commented code is working other wise code hangs can any one help me
 

Thread Starter

freemanantony

Joined May 28, 2011
9
here is my code ramad

Rich (BB code):
#include<c8051F350.h>
#include<math.h>
sfr16 ADC0DEC = 0x9A; // ADC0 Decimation Ratio Register
#define SYSCLK 24500000 // System clock frequency in Hz
#define MDCLK 2457600 // Modulator clock in Hz (ideal is
// (2.4576 MHz)
#define OWR 20 // Desired Output Word Rate in Hz
unsigned char present_voltage = 0x00;
void OSCILLATOR_Init (void);
void adc_init(void);
void dac_init(void);
void port_init(void);
void initialise();
void read_adc();
void delay();
/*@@@@@@@@@@@@@@@@@@@@@ OSCILLATOR INITIALIZATION @@@@@@@@@@@@@@@@@@@@@@*/
void OSCILLATOR_Init (void)
{
unsigned char delay1 = 100;
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency
RSTSRC = 0x04; // Enable missing clock detector // the SYSTEMCLOCK source
 
 
}
 
/******************** ADC INITIALIZATION*********************/
void adc_init(void)
{
REF0CN |= 0x01;
ADC0CN = 0x00;
ADC0CF = 0x00;
ADC0CLK = (SYSCLK/MDCLK)-1;
ADC0DEC = ((unsigned long) MDCLK / (unsigned long) OWR /
(unsigned long) 128) - 1;
ADC0BUF = 0x00;
ADC0MUX = 0x08;
 
EIE1 |= 0x08; // Enable ADC0 Interrupts
ADC0MD = 0x80;
}
/******************** DAC INITIALIZATION*********************/
void dac_init(void)
{
IDA0CN = 0xF3;
IDA1CN = 0xF3;
//REF0CN |= 0x01; 
}
/******************** PORT INITIALIZATION*********************/
void port_init(void)
{
 
// P0MDOUT |= 0x00; // Enable UTX as push-pull output
//P0 = 0xFF;
P1MDOUT |= 0xC0; // All P1 pins open-drain output
//P1 &= 0x0A;
P2MDOUT |= 0x00; // Make the LED (P2.2) a push-pull
// output
/*P3MDOUT = 0x00; 
P3 = 0X00;
P4MDOUT = 0x00;
P4 = 0;*/
XBR0 = 0x00; // Enable UART on P0.4(TX) and P0.5(RX)
XBR1 = 0x00; // Enable crossbar and weak pull-ups
P0SKIP = 0XFF;
P1SKIP = 0XFF;
 
}
 
 
/******************* INITIALIZATION *********************/
void initialise()
{
 
OSCILLATOR_Init(); 
port_init();
adc_init();
dac_init();
 
 
 
}
 
 
 
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
void read_adc()
{
//while(AD0BUSY);
while(!AD0INT); // Wait till conversion complete
AD0INT = 0; // Clear ADC0 conversion complete flag
present_voltage = ADC0L;
present_voltage = ADC0M;
present_voltage = ADC0H;
 
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
void delay()
{
unsigned char i,j;
for(j=0x00;j<=0x7f;j++)
{
for(i=0x00;i<=0x7f;i++)
{
}
}
}
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
// MAIN CODE
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
 
void main (void)
{
 
PCA0MD &= ~0x40;//disable watchdog timer
initialise();
AD0INT = 0;
AD0INT = 0;
ADC0MD = 0x83; // Start continuous conversions
EA = 1; // Enable global interrupts
//present_voltage = 0x22;
while(1)
{
//read_adc();
//delay();
IDA1 = ((present_voltage)*(0x02));
//IDA1 = (present_voltage);
 
}
}
 
void ADC0_ISR (void) interrupt 10
{
 
while(!AD0INT); // Wait till conversion complete
AD0INT = 0; // Clear ADC0 conversion complete flag
// Copy the output value of the ADC
//present_voltage = 0x00;
present_voltage = (unsigned char)ADC0H;
// rawValue.Byte[Byte1] = (unsigned char)ADC0M;
// rawValue.Byte[Byte0] = (unsigned char)ADC0L;
}
 
Last edited by a moderator:

RamaD

Joined Dec 4, 2009
328
IDAC output pins P1.6 & 1.7 should be configured as Analog. Use P1MDIN.

Can you show which 'commented lines' when uncommented, makes it hang? Is the uC still running or not?
 

tshuck

Joined Oct 18, 2012
3,534
It's because your processor clears the interrupt flag in your ISR and you wait in your read_adc()

Rich (BB code):
void ADC0_ISR (void) interrupt 10
{
 
while(!AD0INT); // Wait till conversion complete
AD0INT = 0; // Clear ADC0 conversion complete flag
// Copy the output value of the ADC
//present_voltage = 0x00;
present_voltage = (unsigned char)ADC0H;
// rawValue.Byte[Byte1] = (unsigned char)ADC0M;
// rawValue.Byte[Byte0] = (unsigned char)ADC0L;
}
Rich (BB code):
void read_adc()
{
//while(AD0BUSY);
while(!AD0INT); // Wait till conversion complete
AD0INT = 0; // Clear ADC0 conversion complete flag
present_voltage = ADC0L;
present_voltage = ADC0M;
present_voltage = ADC0H;
 
}
 
Top