Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

ADC hangs in C8051F350

Status
Not open for further replies.

freemanantony

Member level 4
Joined
May 19, 2010
Messages
70
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Chennai, India
Activity points
2,005
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 here i am including my code can any one help me


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#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 = 0;
 
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;
    
    ADC0MD = 0x83;
}
 
/******************** 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();
    
    EIE1 = 0x00;
    EA = 0;
}
 
 
 
 
 
 
/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
 
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;
    //present_voltage = 0x22;
    while(1)
    {
    read_adc(); 
//  delay();
    IDA1 = ((present_voltage)*(0x02));
   //IDA1 = (present_voltage);
    //delay();
    //IDA1 = 0x90;
    }
}

 
Last edited by a moderator:

Hi freemanantony. In case you are using the development kit, you need to check whether the pin headers are connected properly. Refer the datasheet for connection details. Then try running the sample program given along with the SiLabs IDE.

As far as the program is concerned, I believe that the MCU hangs in the statement "//while(AD0BUSY);". Try clearing the AD0BUSY bit first and reset all interrupts related to ADC before reading from ADC.
 

hey goutham it is not a development kit .since i suspected it is getting hanged with "while(AD0BUSY)" (AD0BUSY is a read only bit),i tried with while(!AD0INT);but the problem is still there:-(.i disabled all interrupts
 

I encountered the same problem. but when i altered the status of few bits (which i dont remember now), i got the adc working. But for a constant input, say 2V, the last 8 bits of the 24 bit output were changing everytime I ran the program.

Does your program need to just sample the analog input and convert it into a digital output? If not, tell me what your program does. I will try to write the program.
 

In my code i am not considering the last since they would get changed frequently, i am considering ADC0H only.In my application upto 688v will be given to card and using voltage divider it is getting converted to 3.17V(max), so proportional to input voltage i am writing the DAC register which will give output 0 - 10 V.
 

I'll include the ADC program which i used for my project. I didnt check the program.. try it out once and see if it works.

Code:
#include <C8051F350.h>

#define SYSCLK     24500000         
#define MDCLK      2457600 

void request_adc(void);

unsigned int adcvalue;

int i;

void main(void)
	{
		EA = 1;
	 	PCA0MD &= ~0x40;

		request_adc();
		
		for(i=0;i<10;i++);		//adds a delay of 10 cycles
		
		adcvalue=(ADC0H*0x100)+ADC0L;	//Save ADC value 
		
		while(1);			//loop forever
	}

void request_adc(void)
	{
		ADC0MUX = 0x01;
     		ADC0CN = 0x00;		//0x00 for unipolar, 0x10 for bipolar mode
	 	
		EIE1 = 0x08;

	 	OSCICN = 0x83;
	     	RSTSRC = 0x04;
	
	     	REF0CN = 0x03;
	     	ADC0MD = 0x80;
	     	ADC0CF = 0x00;
	     	ADC0CLK = (SYSCLK/MDCLK)-1;
	 	ADC0DECH = 0x02;
	     	ADC0DECL = 0x80;
	     	ADC0BUF = 0x00;
     
	 	ADC0MD = 0x81;
	     	while(AD0CALC != 1);

	 	AD0INT = 0;	
	     	ADC0MD = 0x83;
		while(AD0INT!=1);
	}

void ADC0_ISR (void) interrupt 10
	{
	 	AD0INT = 0;
	}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top