Connect with us

ADC on PIC24FV16ka301

Discussion in 'Microcontrollers, Programming and IoT' started by jackthelad, Jan 19, 2012.

  1. jackthelad

    jackthelad

    2
    0
    Jan 19, 2012
    hello,

    I'm having some trouble getting the ADC to work on 16-bit PIC. I'm using C for the first time so i'm not quite sure were i'm going wrong.

    the aim of the program is to get a value with ADC and send it over the UART. I know the uart is functioning correctly. I think the problem is taking the 10-bit ADC result from the 16-bit register and putting it in the 8-bit transmit register. If someone could tell me how to do this it would be very much appriciated!

    Code:
    //
    // ADC trial code
    //
    
    //init ADC
    
    #include <p24fv16ka301.h>
    
    #define DELAY 3000
    
    void initADC(void)
    {
    ANSBbits.ANSB2 = 1; // select analog input pins
    AD1CON1 = 0x00E0; // automatic conversion start after sampling
    AD1CSSL = 0; // no scanning required
    AD1CON2 = 0; // use MUXA, AVss and AVdd are used as Vref+/-
    AD1CON3 = 0x1F04; // Tsamp = 32 x Tad; Tad=125ns
    AD1CON1bits.ADON = 1; // turn on the ADC
    } 
    
    
    //Read from ADC
    int readADC(void)
    {
    AD1CHS = 4; // 1. select analog input channel
    AD1CON1bits.SAMP = 1; // 2. start sampling
    while (!AD1CON1bits.DONE); // 3. wait for the conversion to complete
    return ADC1BUF0; // 4. read the conversion result
    } // readADC
    
    //set up UART
    void UART1Init(void)
    {
       // Set up registers
       U1BRG 	=   12;	//set baud speed
       U1MODE	=	0x8000;	 //turn on module (all zero except enable bit)
       U1STA	=	0x400;// just enable transmitter 
    }
    
    //UART transmit function, parameter Ch is the character to send
    int UART1PutChar(int Ch)
    {
       //transmit ONLY if TX buffer is empty
       while(U1STAbits.UTXBF == 1);
       U1TXREG = Ch;
    }
    
    
    main(){
    
    
    	T1CON = 0x8030;
    	int result;
    	UART1Init();
    	initADC();
    	
    	while(1){
    		while ( TMR1 < DELAY)
    							{
    							}
    	TMR1 = 0;
                     result =readADC(); // i think this line is the problem 
    	UART1PutChar(result);
    
    
    			}		 
    }
     
  2. jackthelad

    jackthelad

    2
    0
    Jan 19, 2012
    Have just corrected a problem with ADCON1. still not working though. please let me know if you have any ideas?
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-