PIC184550 2 A/D conversion and PWM error

Discussion in 'Embedded Systems and Microcontrollers' started by limjunda, Aug 11, 2010.

  1. limjunda

    Thread Starter New Member

    Aug 11, 2010
    3
    0
    hi i am new to this forum and i am having trouble trying to get my microcontroller to read my analog signal from AN1 after finishing reading from AN0.
    i am using PIC184550.
    Code ( (Unknown Language)):
    1.  
    2.     T0CON=0b00000111;       //Off Timer0, 16-bits mode, prescaler to 256
    3.  
    4.     T2CON=0b00000111;       //Timer2 is On, Prescaler is 16
    5.  
    6.     CCP1CON=0b00001100;     //Turn on PWM on CCP1, output at RC2
    7.     CCP2CON=0b00001100;        //Turn on PWM on CCP2
    8.     PR2 = 149;              //Load period of PWM 0.2msec for 5KHz
    9.  
    10.  
    11.     TRISA=0xFF;
    12.     TRISCbits.TRISC1 = 0;   // RC1 as output pin
    13.     PORTCbits.RC1    = 0;   // RC1 is connected to Relay
    14.  
    15.     TRISCbits.TRISC2 = 0;   // RC2 as output pin
    16.     PORTCbits.RC2    = 0;   // RC2 is connected to Motor
    17.  
    18.     ADCON0 = 0b00000001;  
    19.     ADCON1 = 0b00001101;
    20.     ADCON2 = 0b00000111;   
    21. while(1)
    22. {
    23.     ADCON0 = 0b00000001;                //select channel 0 AN0
    24.     ADCON2 = 0b00000111;
    25.  
    26.         ADCON0bits.GO = 1;      // START CONVERSION NOW
    27.            
    28.         while(ADCON0bits.GO == 1);  // Waiting for DONE
    29.         {} 
    30.         result1=ADRESH;
    31. if(result1>0b10001001)
    32. {
    33.        
    34.             CCP1CON=0b00001100;
    35.             CCPR1L = 0b11100000;       
    36.             CCP1CONbits.DC1B1=1;       
    37.             CCP1CONbits.DC1B0=0;
    38.             Delay1sec();
    39. }
    40. else
    41. {
    42.        
    43.             CCP1CON=0b00000000;
    44.             CCPR1L = 0b00000000;       
    45.             CCP1CONbits.DC1B1=1;       
    46.             CCP1CONbits.DC1B0=0;
    47.             Delay1sec();
    48. }
    49.  
    50. ADCON0 = 0b00010001;                       //select channel 1 AN1
    51. ADCON2 = 0b00100111;
    52.  
    53.         ADCON0bits.GO = 1;      // START CONVERSION NOW
    54.            
    55.         while(ADCON0bits.GO == 1);  // Waiting for DONE
    56. {}
    57. PORTD=ADRESH;
    58. result=ADRESH;
    59. if(result>0b10001001)
    60. {
    61.             CCP2CON=0b00001100;
    62.             CCPR2L = 0b11100000;       
    63.             CCP2CONbits.DC2B1=1;       
    64.             CCP2CONbits.DC2B0=0;
    65.             Delay1sec();
    66. }
    67. else
    68. {
    69.            
    70.             CCP2CON=0b00000000;
    71.             CCPR2L = 0b00000000;       
    72.             CCP2CONbits.DC2B1=0;       
    73.             CCP2CONbits.DC2B0=0;
    74.             Delay1sec();
    75. }
    76.  
    77. }
    78.  
     
  2. lowy

    New Member

    Aug 11, 2010
    1
    0
    Why are you not using the C18-functions? I have found a tutorial explaining how you can read analog sensors using the 18F4550. The tutorial uses C and you can find it on http://www.dwengo.org/tutorials/light-measurement
    When you have read the tutorial, you can extend it easily to multiple sensors by just selecting the correct channel using changing SetChanADC(ADC_CH0) to an other channel for instance SetChanADC(ADC_CH1). Make sure that your sensors are installed correctly. If you need more than 5 AD-channels you will need to set some configuration-bits differently using the OpenADC() function (but that is explained in that tutorial).
     
  3. limjunda

    Thread Starter New Member

    Aug 11, 2010
    3
    0
    hi erm i am sorry cause using function from the adc.h is out of my knowledge currently as i am only a polytechnic student and that adc.h is not within the topic i studied for the module.
    Therefore i am constraint to using the longer method shown in our textbook.
     
  4. limjunda

    Thread Starter New Member

    Aug 11, 2010
    3
    0
    i have also tried using the function by editing a sample code i found on the net.
    but it is still unable to get reading for my 2nd analog signal input which is the AN1 from PORTA.

    my programme is able to work totally fine when there is only 1 motor and 1 a/d input.
     
  5. elementalrage

    Active Member

    Jul 30, 2009
    59
    3
    From where I'm sitting, that will select Channel AN4 according to page 267 of the PIC 18F4550 datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/39632e.pdf)
     
Loading...