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 support@ccsinfo.com

Setting ADC clock

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



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

Setting ADC clock
PostPosted: Tue Jun 05, 2012 6:23 am     Reply with quote

I'm new to PIC programming.
Can someone please tell me what are the factors that I should be concerned about when setting ADC and timer clock inputs. I have given a project to complete but this is not my study area. I'm learning everything from the beginning. I've written the code but I need it to be accurate.

Thanks in advance. Smile
_________________
"Silence is argument carried out by other means"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jun 05, 2012 12:27 pm     Reply with quote

Post your PIC and the oscillator frequency for the PIC.
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 1:28 am     Reply with quote

and (of course), the old answer, look at the data sheet for your PIC. Smile
There are normally examples there, showing what ADC clocks can be used for each processor clock.

Best Wishes
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 10:11 am     Reply with quote

hello PCM programmer, My PIC is 16F877A and frequency is 20 MHz.
Thanks both PCM Programmer and Ttelmah.

I read the datasheet.
According to that what I finally decided was, to satisfy the minimum Tad(1.6 us) I have to use 32 as the prescaler (setup_adc(ADC_CLOCK_DIV_32)).

Please tell me whether my decision is correct and tell me if I've missed anything.

And can you please help me with another thing. What we have got to do is to control the speed of a fan(AC motor with triac). Now I'm a bit comfortable with ADC converting part. Now I have to learn how to control AC motor with triac, from beginning. I am not responsible for making the circuit and all I've got to do is to write the code. Can you please tell me how should I begin. This is my first project and first PIC programming experience.

Thank you very much. Smile
_________________
"Silence is argument carried out by other means"
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 12:15 pm     Reply with quote

Here are two sample programs for using the adc on a 16F877A.
http://www.ccsinfo.com/forum/viewtopic.php?t=32168&start=1
http://www.ccsinfo.com/forum/viewtopic.php?t=40007&start=1
These programs are setup for a 4 MHz crystal, so to change it to
20MHz operation, you need to change fuse from XT to HS.
Also change the #use delay() to say 20MHz instead of 4MHz.
Also as you have noted, the setup_adc() must use ADC_CLOCK_DIV_32
for 20 MHz operation.
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 3:23 pm     Reply with quote

Good Start. Smile
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Wed Jun 06, 2012 7:31 pm     Reply with quote

Thanks again. Smile

I had already done this part. Smile Actually what I asked was how to begin that AC motor controlling part. Apologize for not being clear.
But I had kept following lines which were given by IDE when I created the project.
Code:

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES RC                       //Resistor/Capacitor Osc with CLKOUT
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected

I didn't know I have to bother about FUSES.... Looks like I have to learn all about this too.

And in my code after read_adc() I have a delay of only 10 us. It's smaller than the value in that sample code (500 ms). Is that to be decided according to the clock frequency?
As I thought that delay is for everything to settle down before next reading.

And for AC motor controlling can we use that PWM module in the PIC?
If this question is stupid I'm sorry. When I was starting to learn PWM I was told that we are using an AC motor and a triac instead of a DC motor that we had decided to use. So I should code for that. So now I'm going to learn what a triac is and the theory of this AC motor controlling.
_________________
"Silence is argument carried out by other means"
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 2:22 am     Reply with quote

Quote:
And in my code after read_adc() I have a delay of only 10 us. It's smaller than the value in that sample code (500 ms). Is that to be decided according to the clock frequency?
The delay allows the internal sampling capacitor in the ADC module to charge to the externally applied voltage. Roughly ~20us is usually enough, but it depends on the source impedance and the required precision. It's all in the microchip data sheet.

Triacs behave as an AC silicon switch (unlike an SCR which only works on DC). They are turned on by applying a pulse (or pulses) to the gate, and turn themselves off when the current through the device is zero (or close to it).

In your case, you repeatedly monitor the mains for voltage zero crossing, wait for the required time, then pulse the triac. The major software issue is that you are really wanting to control how long the triac is on. You can control the turn on, but have less control over the turn off, which is some time in the future, and is load & mains period dependant.

In your situation I'd be wanting to know about the interface to the triac BEFORE doing anything else:-

(1) Am I going to be working with raw mains or isolated?
(2) What is the nature of the zero crossing detection (resisitor, transformer, opto etc)?
(3) What is the nature of the coupling to the triac (again resistor, transformer, opto etc).?
(4) How will I be able to use a 'scope to monitor progress?

Mike


Last edited by Mike Walne on Thu Jun 07, 2012 2:52 am; edited 2 times in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 2:38 am     Reply with quote

Yes, you need to worry about fuses....
These control a whole suite of basic configurations/settings for the PIC, that have to be right, for the chip to work at all, and vary according to _your_ hardware. For instance you have one here that is probably wrong. RC. If again you look at the data sheet, and look at the oscillator settings (under 'special features of the CPU' - section 14.2). You have a choice of setting the oscillator to use 'LP' (low power crystal), 'XT' (standard crystal up to 4MHz). 'HS' ('high speed' crystal >4MHz to 25MHz), and 'RC' (simple resistor capacitor oscillator). You have this last one selected. Figure 14-3, shows the external circuit needed to make this work, but unless timing is _very_ unimportant, this is a rarely used setting. You almost certainly need to change this to 'HS' for a 20MHz crystal.
You don't need _any_ delay 'after read_adc'. You need one _before_. There must be a minimum of 'Tacq', after selecting an ADC channel, before reading it, _and_ between successive readings. The sample code is updating a value to a display, so delays, since the display can't update very quickly anyway... The delay is done after reading, so the display can be seen, but still occurs 'before' the next reading. To give the required delay _before_ the very first reading, there is an extra 20uSec delay before the loop is started. The Tacq, for your chip, is again in the data sheet. Equation 11-1. 10uSec is too quick. The _best_ for this chip, with a 'zero' source impedance feeding the converter, is 10.57uSec. Even with a 1KR source impedance, you need just under 12uSec.

Depends what you want to 'do' with the motor... Generally, with an AC motor, you can't control the speed. Depends on the motor design, but most 'true' AC motors, are effectively like stepper motors, with a series of coils round the motor, which are energised in turn by three phase electricity, and the magnet in the core 'chases' the electric field this produces. Their speed is dependant on how fast the field is moving, rather than on voltage, with the rotor slipping some distance behind the field. Then you have single phase versions of these, which basically won't turn until something makes the shaft turn - these use mechanical switch, or a capacitor, or a 'shaded pole' to introduce a little 'twist' into the magnetic field to get them started. Again though they are chasing a magnetic field, and you can't easily turn down the voltage to slow them. (they will control this way to some extent, as the slip increases as voltage drops). These are the commonest motors in existence, turning things like machine tools, fans, pumps, etc. etc.. The only really reliable way to make both these motor types adjust speed, is to actually synthesise your own AC electricity at different frequencies....

If you look at things where speed control is common, they are generally DC motors, which just happen to be being run off AC. Things like an electric drill, will have a rectifier, and feed DC to the motor. With these you can use the same type of circuit that is used to dim a light, to reduce the effective RMS voltage being delivered, and this then gives a lower DC voltage to the motor, adjusting the speed. For these, yes you can use a TRIAC dimmer. A search online will give you lots of details for simple circuits using these. These have the advantage of retaining torque as the speed drops (this is the big problem with slowing single phase motors).

CAVEAT (big caveat....). Make very sure you know what you are doing, if you are starting to try to control AC mains. This is potentially _dangerous_.

TRIAC's are really quite nasty devices to work with, if you are not going to make RF interference at massive levels (early light dimmers were great at this). You really need to be using 'phase angle' control, rather than PWM. Here you synchronise your code to the zero crossing point of the AC, and adjust 'when' after this you turn on the power. Remember the hardware CCP module, as well as offering 'PWM', offers precision timing functions, and these are what is needed here.

There are several 'application notes' on the Microchip site about using the PIC to control TRIAC's, and these may help.

Best Wishes
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 3:11 am     Reply with quote

Quote:
Things like an electric drill, will have a rectifier, and feed DC to the motor.

My electric drill does not have a rectifier. It has both stator and rotor coils. It will run on DC or AC. It turns in the same direction whichever way round the DC is applied. Hence it can run an AC which it sees as DC with rapidly changing polarity.

Quote:
CAVEAT (big caveat....). Make very sure you know what you are doing, if you are starting to try to control AC mains. This is potentially _dangerous_.

Can't agree more. SAFETY FIRST. 240V UK live mains is LETHAL. (Excuse me shouting.)

In the power supply industry we were not allowed to work alone in the labs. There had to be another person around to hit the panic button in response to screams (loud or otherwise) before touching the victim.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 3:29 am     Reply with quote

Mike Walne wrote:
Quote:
Things like an electric drill, will have a rectifier, and feed DC to the motor.

My electric drill does not have a rectifier. It has both stator and rotor coils. It will run on DC or AC. It turns in the same direction whichever way round the DC is applied. Hence it can run an AC which it sees as DC with rapidly changing polarity.

Quote:
CAVEAT (big caveat....). Make very sure you know what you are doing, if you are starting to try to control AC mains. This is potentially _dangerous_.

Can't agree more. SAFETY FIRST. 240V UK live mains is LETHAL. (Excuse me shouting.)

In the power supply industry we were not allowed to work alone in the labs. There had to be another person around to hit the panic button in response to screams (loud or otherwise) before touching the victim.

Mike

Most modern drills are not like this. Given that this design needs brushes, just like a DC motor, it is cheaper to give 'easy' speed control, by just dimming the effective voltage to a DC motor using a conventional dimmer circuit. I have a couple of older generation drills like this, but all the ones I have bought in the last few years are now using DC motors.
There are several things I've said that are rather too 'over generalised'. It is surprising (for example), how common it is to see single phase induction motors being controlled using a simple TRIAC circuit. However the efficiency is awful, it doesn't work reliably with capacitive start motors, and most won't meet the current RF regulations.

Best Wishes
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 7:30 am     Reply with quote

Thank you Mike Walne and Ttelmah.

Ttelmah, Our project is to count the number of people entering a room, display it in a lcd display. And control the fan speed according the temperature inside the room(fan should be turned off if the room is empty). The temperature should also be displayed. We are counting people using interrupts.

Circuit making is done by two other friends in the group. Still I don't know how things happen in the circuit. All I know is these these inputs are given to the pic and these should be done by my code. Simply, I don't know hardware.. Well.. not a good way at all but due to some reasons we've got to do it this way. I've coded for people counting, displaying it and displaying temperature which is calculated according to the input at A0 pin. I simulated this code using Oshonsoft simulator. Unfortunately with all those problems in the code, I got the expected result... after having 3,4 days in the hell, learning word by word from the beginning.

Now the problem is controlling the fan. My question is, do I need to learn the whole motor controlling circuit to write the code ? If I just learn what's the pic's role here will I be able to do it?
This has to be finished within less than two weeks but at the moment there's no way of meeting my mates who make the circuit. And we've got no time to work only on this.. May be this is so funny sir but it's like that.. Thank you very much for your help..
_________________
"Silence is argument carried out by other means"
Ttelmah



Joined: 11 Mar 2010
Posts: 19369

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 8:22 am     Reply with quote

You need to know what the fan control board 'does'. Hopefully if someone else has/is building it who understands what a processor is capable of, the control input will be something like a voltage input from 0 to 5v (in which case you can drive it via an integrated PWM output), or some form of other control input, in which case they can tell you 'what voltage it needs', 'what frequency it needs', etc. etc.. You need _data_. Data is always your friend in electronics. Without knowing what sort of control input is involved, you can go nowhere. If is is (say) the gate of a TRIAC, then you will need to know how it is isolated, or provide isolation yourself, and realistically will need a zero crossing detection 'output' from the supply feeding the fan.

Best Wishes
Patronus



Joined: 05 Jun 2012
Posts: 10
Location: Sri Lanka

View user's profile Send private message

PostPosted: Thu Jun 07, 2012 8:45 am     Reply with quote

Ok sir. Thank you very much.. Yes they know it. I'll get data from them and try to write the code. After writing that part and simulating, if it causes problems I hope you'll help me.
_________________
"Silence is argument carried out by other means"
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