ADC0804 and 8051 interfacing problem!

Discussion in 'Embedded Systems and Microcontrollers' started by hashim5003, Jan 23, 2014.

  1. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    I was using adc0831 and lm35 with at89s52. Now I am using adc0804 instead. I have written the code, it simulates on proteus but not running on the hardware. I am posting the code I have written. Plz mention if there is any problem.
    Problem:
    when I use while (intr == 1) lcd only shows black boxes. and without this statement all it shows "temp:000"

    Code ( (Unknown Language)):
    1. #include<reg52.h>
    2.  
    3. #define adc_input P2
    4. #define lcd_data P1
    5.  
    6. void delay (int);
    7. void lcdcmd (char);
    8. void lcddata (char);
    9. void lcd_init(void);
    10.  
    11. sbit r = P3^1;
    12. sbit w = P3^2;
    13. sbit rs = P3^7;
    14. sbit rw = P3^6;
    15. sbit e = P3^5;
    16. sbit intr = P3^3;
    17.  
    18. void main()
    19. {  
    20.     signed char x, a, b, c;
    21.     while (1)
    22.     {
    23.         r = 1;
    24.         w = 0;
    25.         delay (1);
    26.         w = 1;
    27.         while (intr == 1);
    28.         r = 0;
    29.    
    30.         lcd_init();
    31.    
    32.         lcddata ('t');
    33.         lcddata ('e');
    34.         lcddata ('m');
    35.         lcddata ('p');
    36.         lcddata (':');
    37.    
    38.         x = adc_input / 10;
    39.         a = x / 10;
    40.         b = x % 10;
    41.         c = adc_input % 10;
    42.    
    43.         lcddata ( a + 0x30 );
    44.         lcddata ( b + 0x30 );
    45.         lcddata ( c + 0x30 );
    46.     }
    47. }
    48.  
    49. void delay( int a )  
    50. {
    51.     int i,j ;
    52.     for(i=0;i<a;i++)
    53.         for(j=0; j<1275; j++);
    54. }
    55.  
    56. void lcdcmd (char value)
    57. {
    58.     rs=0;
    59.     lcd_data = value;
    60.     rw=0;
    61.     e=1;
    62.     delay (1);
    63.     e=0;
    64.  
    65. }
    66.  
    67. void lcd_init(void)
    68. {
    69.    lcdcmd(0x38);    
    70.    lcdcmd(0x0c);    
    71.    lcdcmd(0x06);
    72.    lcdcmd(0x80);    
    73. }
    74.    
    75.  
    76. void lcddata (char value)
    77. {
    78.     rs=1;
    79.     lcd_data = value;
    80.     rw=0;
    81.     e=1;
    82.     delay (1);
    83.     e=0;
    84.  
    85. }
    86.  
     
  2. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    I'm not really familiar with 8051;
    But, have you set the INTR pin as input?

    Try remove the while (intr == 1);
    and see if the lcd displays anything. If it does, then your adc might not accessed correctly.

    If no, there's might be a hardware issue.

    This is what I've used before for adc,


    To my rough observation, it looks quite familiar to yours.
    Have you checked if the ADC get its clock?
     
    Last edited: Jan 23, 2014
  3. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    when i remove while (intr == 1).
    it works but only shows "temp: 000" (000 is supposed to be temperature)
    no change in value happens.
     
  4. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    yep. by using internal clock with 150pF capacitor and 10k resistor.
     
  5. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    If you already built the hardware, with the while(intr==1) still intact,
    could you just manipulate the mcu-pin directly?

    i.e. with the adc-chip disconnected, take a wire which is connected to p3.3,
    drive it to either vcc or ground, and observe what the lcd displays.

    If there's any sign of activity towards the pin input change, your adc might not wired correctly.

    Better yet, just hook an oscilloscope to the pin, and monitor for signal issued by the adc, especially the clock and intr pin.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,394
    1,606
    The "000" is consistence with not getting any A2C data, or always reading zero. I don't see any code to initialize the device you use to do that conversion.

    (And while I'm at it, while I see your LCD initialization, it lacks delays and doesn't get called anyway.)
     
  7. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    yep, It's not getting data from adc. I have initialized the adc according to it's datasheet.
    I think it's not getting clk pulses by using internal clock pulse. Could that be the problem?
     
  8. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Today I have used adc without using microcontroller giving pulses from switched. It didn't worked either. I am guessing that it could be the internal clock pulse that is not converting.
     
  9. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    Have you tried to operate it in free-running mode?
    With LEDs as the output, and a potentiometer as input,
    just curious to know if the ADC still alive. :)
     
  10. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    how do i run in free running mode?
    I had replaced the adc too :( that didn't worked either.
     
  11. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    Datasheet will give some clues. :)

    http://www.engineersgarage.com/electronic-components/adc0804-datasheet

    Download the datasheet from the site, and scroll to page 11, Fig. 17, 'free running connection'.

    Just wire everything as shown in the diagram, and you're good to go. :)

    Don't forget to include Vref/2 on the ADC.
    For your 8051 case, just connect it to resistive voltage divider; by half, i.e. 2.5V.
    The start switch is a momentary contact, once it closed and re-open, the ADC will run by itself.

    The absence of suitable Vref would render the reading useless.

    Connect 7th pin to ground and 6th pin to your intended analog input.
    Assign all of the output to drives LEDs.
    If the LEDs readout changes as the analog input varies, then you're good to proceed with the mcu.
     
    Last edited: Jan 26, 2014
  12. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Problem has been solved. It was clock pulse that wdc was not getting.
    Now I am interfacing lcd but it's showing weird characters.
     
  13. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    Glad the ADC is now working.

    How weird the character is?
    Do the "temp:" properly displayed?
     
  14. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Like when i write 'TEMP' in program. on display it shows temP, TGmP, toMp or sometime spaces between characters, and sometimes it misses the character also.

    could it be the problem in the lcd?
     
  15. AfdhalAtiffTan

    Active Member

    Nov 20, 2010
    117
    11
    There's might be a problem with your LCD routine.
    Since you're not checking busy-flag, you could try to increase the delay.
     
  16. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    Here is my code now.
    Problem is still there.
    How these lcds get clock pulse? Do they have internal clocl source?
    Here is my code now:
    Code ( (Unknown Language)):
    1. #include<reg52.h>
    2.  
    3. sbit en = P3^5;
    4. sbit rw = P3^6;
    5. sbit rs = P3^7;
    6. sbit busy = P1^7;
    7.  
    8. void delay()
    9. {
    10.     TMOD = 0x01;
    11.     TH0 = 0x00;
    12.     TL0 = 0x00;
    13.     TR0 = 1;
    14.     while(TF0 == 0);
    15.     TR0 = 0;
    16.     TF0 = 0;
    17. }
    18.  
    19. void lcdbusy()
    20. {
    21.     rs = 0;
    22.     rw = 1;
    23.     while(busy == 1)
    24.     {
    25.         en = 0;
    26.         delay();
    27.         en = 1;
    28.     }
    29. }
    30.  
    31. void lcdcmd(char y)
    32. {
    33.    
    34.     P1 = y;
    35.     rs = 0;
    36.     rw = 0;
    37.     en = 1;
    38.     delay();
    39.     en = 0;
    40.     lcdbusy();
    41. }
    42. void lcddata(char z)
    43. {
    44.    
    45.     P1 = z;
    46.     rs = 1;
    47.     rw = 0;
    48.     en = 1;
    49.     delay();
    50.     en = 0;
    51.     lcdbusy();
    52.    
    53. }
    54.    
    55. void main()
    56. {
    57.     delay();
    58.     lcdcmd(0x30);
    59.     delay();
    60.     lcdcmd(0x30);
    61.     delay();
    62.     lcdcmd(0x30);
    63.     delay();
    64.     lcdcmd(0x38);
    65.     delay();
    66.     lcdcmd(0x0E);
    67.     delay();
    68.     lcdcmd(0x01);
    69.     delay();
    70.     lcdcmd(0x06);
    71.     lcddata('T');
    72.     lcddata('E');
    73.     lcddata('M');
    74.     lcddata('P');
    75.     while(1);
    76. }
    77.  
     
  17. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    You strobe the enable pin (en).
    en=1;
    short delay;
    en=0;

    Anyway, I see a couple of problems in the code.
     
  18. hashim5003

    Thread Starter Member

    Apr 29, 2013
    32
    0
    I have used delay between enable pin. you mean to say that I have to use short delay?
    Can you plz mention these mistakes? It would be really helpful.
     
  19. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    In your lcdcmd and lcddata P1 is not defined.
    You say P1=y and P1=z. But what is P1?

    I know that you know what P1 is. But the chip/computer does not know and, what is worse, does not care.
     
  20. shteii01

    AAC Fanatic!

    Feb 19, 2010
    3,395
    497
    Also.
    lcdcmd and lcddata needs return; in the end to return to main.
     
Loading...