CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Triggering A/D conversion using PWM special event trigger

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








Triggering A/D conversion using PWM special event trigger
PostPosted: Thu Mar 12, 2009 7:38 am     Reply with quote

From reading the PIC18F1330 manual I can see that the PWM special event trigger can be used to start an a/d conversion by setting the ADCON0 register appropriately. However the CCS manual does not explain how you would go about doing this. Similarly the PIC's header file makes no mention of how you set up the A/D to be triggered by the special event trigger. I would be very grateful if someone could explain to me how I can achieve this.
Guest








PostPosted: Thu Mar 12, 2009 8:10 am     Reply with quote

After looking at the code in EX_POWER_PWM.c and the header file for the 18F2431 I can see that there is a function to trigger the ADC from the PWM - setup_adc(adc_when_ppwm). I need this exact function but for the 18F1330. It's in the manual but the header file does not have it included as a function. Is there anything I can do about this? Thanks
Guest








PostPosted: Thu Mar 12, 2009 9:01 am     Reply with quote

I have tried programming the ADCON0 register directly to enable the special event trigger using ADCON0 = 0b10001101 in main(), but the compiler gives an error saying undefined idenifier ADCON0. This is my first attempt at directly programming registers rather than using the built in functions so I'm not entirely sure how I go about doing this.
Ttelmah
Guest







PostPosted: Thu Mar 12, 2009 9:58 am     Reply with quote

For anything where you want to access the registers, you need to define them yourself (some exceptions like the timers).

However there is a 'caveat', in that you shouldn't trigger a reading, in the same operation as setting the config. Better to get the config set, then set the trigger afterwards.
Just do this:
Code:

#bit SEVTEN=0xFC2.7

setup_adc(Use the CCS settings for clock etc. here);
set_adc_channel(3); //You select channel 3 with your setting
                              //- is this right?.
SEVTEN=TRUE; //This will enable the special event trigger



//Then in your read code, use:
val=read_adc(ADC_READ_ONLY); //Read the acquired value

Basically get the whole ADC setup, then turn on the special event trigger after this. When the event triggers, the ADC will read, and in the INT_ADC interrupt (which will trigger after the reading has completed), just read the value (ADC_READ_ONLY, gets the value, without triggering the ADC, or waiting for it - since it has already acquired).


Best Wishes
Guest








PostPosted: Thu Mar 12, 2009 10:25 am     Reply with quote

Thank you for that info. So does #bit SEVTEN=0xFC2.7 define bit 7 of the ADCON0 register? If you could briefly explain that it would be great as I don't quite understand the syntax of directly accessing a register. Or point me in the direction of somewhere that explains this.
The program will also have other A/D conversions to carry out, but these should not be synchronised with the PWM. Can I do them normally by putting SEVTEN=FALSE; before calling the A/D function?

Kind Regards
Ttelmah
Guest







PostPosted: Thu Mar 12, 2009 11:27 am     Reply with quote

Read the manual..... Smile

Two instructions. #bit, and #byte. The first defines a _bit_ variable, at a specific address and bit. The second defines a byte variable at a specific address (and can also be used to place larger variables at a specific location).

Yes.
You could turn of the special event trigger, then just call:
val=read_adc();

and this will trigger a conversion, wait for it to complete, and read the result.

Best Wishes
Guest








PostPosted: Thu Mar 12, 2009 11:53 am     Reply with quote

Thank you for your help, I think I'll be able to get it to work now. It's a shame that I have to manually/directly set the register when the exact function for it is present in the header file for many other PIC devices. It is something CCS must have forgotten to add for the 18F1330.
Ken Johnson



Joined: 23 Mar 2006
Posts: 197
Location: Lewisburg, WV

View user's profile Send private message

PostPosted: Thu Mar 12, 2009 1:48 pm     Reply with quote

Also, look at getenv(), which saves you having to lookup the actual register address in the data sheet.

Examples:

#pragma BYTE TXREG1 = getenv("SFR:TXREG")

#pragma BIT RC1IE = getenv("BIT:RCIE")

Ken
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group