Hi, Here's more news about the progress so far. I have added Jan's code on the initialization in the initialization.c file. My quesiton on the initialization is the fact that I'm unsure of how I should deal with the first parameter of adc_open function. I have typed in my guess after looking at adc.h, and indicated my doubts on my initialization.c.
I have a separate source file (adcisr.c) that deals with the a/d conversion ISR. I have basically incorporated Jan's code, except I have modified it to work for one a/d channel (AN0) to free up the other channels as I only need to sample one channel. I have also an accessor function that allows me to access the a/d channel input values from any other source file of my project. However, when I tested my code using the PWM toggling method. I don't seem to see a PWM output at the corresponding port B pin no matter how I vary the voltage input from adjusting a potentiometer. I infer that there is something wrong with the structure of my project as I have limited experience working with multiple source files (I don't want to squeeze everything into a single main.c file either). Any insights would be much appreciated. Thanks, Stanley On 10/28/07, Stanley Lee <[EMAIL PROTECTED]> wrote: > > Hi, > > I haven't reworked my code based on the recommendations yet, but I would > like to make a reply on some comments. > > If my application only requires me to use one a/d channel, with the input > of the signal being non-periodic (changes relatively slowly, i.e. change > from 2.5 V to 3 V and stay there for at least a few seconds), would the > acquisition time that I set in ADCON2 matter (I don't think so)? > > Also, what would be the benefit of using an ISR for a/d sampling rather > than polling for ADCON0bits.GO to clear? > > Thanks, > > Stanley > > On 10/28/07, [EMAIL PROTECTED] < [EMAIL PROTECTED]> wrote: > > > > Hi > > > > Stanley Lee wrote: > > > The project folder that I have so far is in > > > http://www.mediafire.com/?c1444jbnizc b/c the message would get > > > otherwise too large to be posted. What I'm trying to do right now is > > get > > > the a/d conversion module working, and see the output on a combination > > > > > of LEDs, through USART to the computer on terminal or something, > > through > > > looking at the PWM variation or outputting to a LCD display. I'm > > > following the guidelines as described in section 19 of the PIC18F2620 > > > manual. I have some questions about the code below: > > > > I would suggest using the sdcc adc libs to do this. Just include adc.h > > and do a > > adc_open(channel_num_to_read, ADC_FOSC_64, ADC_CFG_05A_0R, ADC_FRM_RJUST > > > > | ADC_INT_OFF); > > There is a bug in the sdcc adc lib which forces you to manually set > > ADCON2bits.ACQT0 = 1; > > ADCON2bits.ACQT1 = 1; > > ADCON2bits.ACQT2 = 1; > > TRISA = 0xff; // input > > (see also my patch > > > > https://sourceforge.net/tracker/?func=detail&atid=300599&aid=1776197&group_id=599 > > for a pic18f2550) > > adc_conv(); // start initial sampling > > > > later in your programm do: > > while(1) { > > if (!ADCON0bits.GO) { > > // read result > > adc = adc_read(); // read value > > adc_setchannel(next_channel_to_read); // set to next channel > > adc_conv(); // restart sampling > > } > > } > > > > > > 1. For ADCON2, I have picked the minimum acquisition time and the > > > fastest clock, which is Fosc/4. Would anyone know about the > > > implications, pros and cons of picking different acquisition time and > > > a/d clock source? > > If i understood this correct the ACQ and ADC_CLOCK (TAD) are different > > things. The Tad defines how fast each bit of the 10-bit adc is sampled, > > thus it defines the maximum time for a complete conversion, which is 11 > > times tad. > > The acq defines (in tad units) how long the internal capacitor is > > charged before the conversion is actually started. The length of the acq > > depends on the frequency of the signal you want to convert. The higher > > the acq the more the actual read value is averaged. > > The acq is a fine thing if you read out different channels successively. > > Without this you would always need to wait a specific time after > > changing the channel and starting a conversion to give the internal > > capacitor the chance to charge with the analog value. > > > > > > > > 2. For the a/d input acquisition time, I have a delay loop that > > involves > > > using a while loop that has a delay of 27 us rather than the minimum > > > time suggested in section 19.1 of 2.4 us. Could there be any > > > implications of doing what I did in my code? > > This is no prob if you don't read out the value before the ADCONbits.GO > > bit is cleared. See also my code above. > > > > > > > > 3. For using the a/d interrupt functionality, I infer that I would > > write > > > an ISR that is dependent on ADIF interrupt flag bit. I'm not sure > > where > > > I can find the interrupt number for a/d interrupt as I would need it > > to > > > complete my ISR properly. Can anyone help me on this? I was having > > > trouble finding it in the SDCC manual. > > you need to enable the adc interrupt by using ADC_INT_ON when doing the > > adc_open. Then use an isr like (it reads all 13 cahnnels) : > > int adc_value[13]; > > unsigned chardc_channel; > > > > void low_isr(void) interrupt 2 { > > PORTCbits.RC1 = 1; > > // adc isr > > if (PIR1bits.ADIF) { > > adc_value[adc_channel] = adc_read(); > > // set next channel > > if (adc_channel == 12) adc_channel = 0; > > else adc_channel++; > > adc_setchannel(adc_channel); > > // reset interrupt > > PIR1bits.ADIF = 0; > > adc_conv(); // restart sampling, don't omit this > > } > > PORTCbits.RC1 = 0; > > } > > > > > > > > > > 4. Maybe be a bit of a silly question. I'm planning to add up the high > > > and low result bits into a single result integer and return it to the > > > function calling the a/d sampling function. What would be the limit > > for > > > how large an integer type can be? > > > > the maximum value for an unsinged integer is 2^16-1 for a singed integer > > its half of that. Since the adc only has 10-bits. The maxmimum value is > > 2^10-1. So you can use an unsigned integer. > > > > jan > > > > > > ------------------------------------------------------------------------- > > This SF.net email is sponsored by: Splunk Inc. > > Still grepping through log files to find problems? Stop. > > Now Search log events and configuration files using AJAX and a browser. > > Download your FREE copy of Splunk now >> http://get.splunk.com/ > > _______________________________________________ > > Sdcc-user mailing list > > Sdcc-user@lists.sourceforge.net > > https://lists.sourceforge.net/lists/listinfo/sdcc-user > > > >
dlscode.tar.gz
Description: GNU Zip compressed data
------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________ Sdcc-user mailing list Sdcc-user@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/sdcc-user