adc hangs in c8051f350

Discussion in 'Embedded Systems and Microcontrollers' started by freemanantony, Dec 19, 2012.

  1. freemanantony

    Thread Starter New Member

    May 28, 2011
    9
    0
    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
     
  2. RamaD

    Active Member

    Dec 4, 2009
    254
    33
    Please post your code.
     
  3. freemanantony

    Thread Starter New Member

    May 28, 2011
    9
    0
    here is my code ramad

    Code ( (Unknown Language)):
    1.  
    2. #include<c8051F350.h>
    3. #include<math.h>
    4. sfr16 ADC0DEC = 0x9A; // ADC0 Decimation Ratio Register
    5. #define SYSCLK 24500000 // System clock frequency in Hz
    6. #define MDCLK 2457600 // Modulator clock in Hz (ideal is
    7. // (2.4576 MHz)
    8. #define OWR 20 // Desired Output Word Rate in Hz
    9. unsigned char present_voltage = 0x00;
    10. void OSCILLATOR_Init (void);
    11. void adc_init(void);
    12. void dac_init(void);
    13. void port_init(void);
    14. void initialise();
    15. void read_adc();
    16. void delay();
    17. /*@@@@@@@@@@@@@@@@@@@@@ OSCILLATOR INITIALIZATION @@@@@@@@@@@@@@@@@@@@@@*/
    18. void OSCILLATOR_Init (void)
    19. {
    20. unsigned char delay1 = 100;
    21. OSCICN |= 0x03; // Configure internal oscillator for
    22. // its maximum frequency
    23. RSTSRC = 0x04; // Enable missing clock detector // the SYSTEMCLOCK source
    24.  
    25.  
    26. }
    27.  
    28. /******************** ADC INITIALIZATION*********************/
    29. void adc_init(void)
    30. {
    31. REF0CN |= 0x01;
    32. ADC0CN = 0x00;
    33. ADC0CF = 0x00;
    34. ADC0CLK = (SYSCLK/MDCLK)-1;
    35. ADC0DEC = ((unsigned long) MDCLK / (unsigned long) OWR /
    36. (unsigned long) 128) - 1;
    37. ADC0BUF = 0x00;
    38. ADC0MUX = 0x08;
    39.  
    40. EIE1 |= 0x08; // Enable ADC0 Interrupts
    41. ADC0MD = 0x80;
    42. }
    43. /******************** DAC INITIALIZATION*********************/
    44. void dac_init(void)
    45. {
    46. IDA0CN = 0xF3;
    47. IDA1CN = 0xF3;
    48. //REF0CN |= 0x01;
    49. }
    50. /******************** PORT INITIALIZATION*********************/
    51. void port_init(void)
    52. {
    53.  
    54. // P0MDOUT |= 0x00; // Enable UTX as push-pull output
    55. //P0 = 0xFF;
    56. P1MDOUT |= 0xC0; // All P1 pins open-drain output
    57. //P1 &= 0x0A;
    58. P2MDOUT |= 0x00; // Make the LED (P2.2) a push-pull
    59. // output
    60. /*P3MDOUT = 0x00;
    61. P3 = 0X00;
    62. P4MDOUT = 0x00;
    63. P4 = 0;*/
    64. XBR0 = 0x00; // Enable UART on P0.4(TX) and P0.5(RX)
    65. XBR1 = 0x00; // Enable crossbar and weak pull-ups
    66. P0SKIP = 0XFF;
    67. P1SKIP = 0XFF;
    68.  
    69. }
    70.  
    71.  
    72. /******************* INITIALIZATION *********************/
    73. void initialise()
    74. {
    75.  
    76. OSCILLATOR_Init();
    77. port_init();
    78. adc_init();
    79. dac_init();
    80.  
    81.  
    82.  
    83. }
    84.  
    85.  
    86.  
    87. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
    88. void read_adc()
    89. {
    90. //while(AD0BUSY);
    91. while(!AD0INT); // Wait till conversion complete
    92. AD0INT = 0; // Clear ADC0 conversion complete flag
    93. present_voltage = ADC0L;
    94. present_voltage = ADC0M;
    95. present_voltage = ADC0H;
    96.  
    97. }
    98. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    99. void delay()
    100. {
    101. unsigned char i,j;
    102. for(j=0x00;j<=0x7f;j++)
    103. {
    104. for(i=0x00;i<=0x7f;i++)
    105. {
    106. }
    107. }
    108. }
    109. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    110. // MAIN CODE
    111. /*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/
    112.  
    113. void main (void)
    114. {
    115.  
    116. PCA0MD &= ~0x40;//disable watchdog timer
    117. initialise();
    118. AD0INT = 0;
    119. AD0INT = 0;
    120. ADC0MD = 0x83; // Start continuous conversions
    121. EA = 1; // Enable global interrupts
    122. //present_voltage = 0x22;
    123. while(1)
    124. {
    125. //read_adc();
    126. //delay();
    127. IDA1 = ((present_voltage)*(0x02));
    128. //IDA1 = (present_voltage);
    129.  
    130. }
    131. }
    132.  
    133. void ADC0_ISR (void) interrupt 10
    134. {
    135.  
    136. while(!AD0INT); // Wait till conversion complete
    137. AD0INT = 0; // Clear ADC0 conversion complete flag
    138. // Copy the output value of the ADC
    139. //present_voltage = 0x00;
    140. present_voltage = (unsigned char)ADC0H;
    141. // rawValue.Byte[Byte1] = (unsigned char)ADC0M;
    142. // rawValue.Byte[Byte0] = (unsigned char)ADC0L;
    143. }
    144.  
     
    Last edited by a moderator: Dec 19, 2012
  4. RamaD

    Active Member

    Dec 4, 2009
    254
    33
    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?
     
  5. tshuck

    Well-Known Member

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

    Code ( (Unknown Language)):
    1. void ADC0_ISR (void) interrupt 10
    2. {
    3.  
    4. while(!AD0INT); // Wait till conversion complete
    5. [B]AD0INT = 0; // Clear ADC0 conversion complete flag[/B]
    6. // Copy the output value of the ADC
    7. //present_voltage = 0x00;
    8. present_voltage = (unsigned char)ADC0H;
    9. // rawValue.Byte[Byte1] = (unsigned char)ADC0M;
    10. // rawValue.Byte[Byte0] = (unsigned char)ADC0L;
    11. }
    Code ( (Unknown Language)):
    1. void read_adc()
    2. {
    3. //while(AD0BUSY);
    4. [B]while(!AD0INT); // Wait till conversion complete[/B]
    5. AD0INT = 0; // Clear ADC0 conversion complete flag
    6. present_voltage = ADC0L;
    7. present_voltage = ADC0M;
    8. present_voltage = ADC0H;
    9.  
    10. }
     
Loading...