PIC Clock frequency change by registers and edit project window - frequency

I am trying to set MCU clock frequency but I got confused. When I set it by registers (By coding) it seems like it does not change it. But when I change it from "edit project" window it starts to work. So my questions are:
What is the difference between changing MCU Clock by registers and from "edit project" window? Do I need to change both of them? What happens when they are 2 different frequencies?
Is there a difference betweeen oscillator frequency and MCU Clock frequency?
If I need to have 1 mHz clock frequency do I need to set MCU Clock to 1 mHz or to 4 mHz? Because in some websites they say that 1 operation take 4 clock cycles in PIC.

You didn't say which PIC but in modern ones you can change clock frequency in several places. One is configuration bits and this is what can be set in "edit project". Config.bits set the clock source (external,internal) as well as PLL. Another place is registers (OSCCON1 and friends), where you can switch between clock sources. There is also a config bit that allows/disallows clock switching.
The easiest way to make sense of all that is to install Microchip Code Configurator plugin into MplabX and set your clock/peripherals there. It will output chip-specific C code which you can then use in your project.

Related

How to deep sleep an Attiny until an analog value of a photoresistor changes?

for a battery powered project I would like to put an Attiny85 into deep sleep mode immediately after program start and let it wake up only when a sensor value (in this case a photo resistor) changes. Unfortunately I could only find examples for interrupts by a button and not for photo resistors in the internet. Does anyone have an idea how I could implement it, or if it is impossible?
Turn out that this is probably a software question.
Probably to lowest power and simplest way to implement this would be to...
Connect the analog sensor value to any one of the analog input pins on the ATTINY.
Make sure you disable the digital buffer on that pin.
Set up the ADC to point to the pin and set other relevant values like precaller.
Set up a watchdog timer to fire a periodic interrupt.
Go into deep sleep and wait for the watchdog timer to fire.
Each time the watchdog fires...
Enable the the ADC.
Take a sample.
Jump to main code if the value has changed more than your threshold.
Disable ADC.
Go back to deep sleep.
How power efficient this will be really depends on how often the timer interrupt fires - the less often the better. If your application can live with only checking the sensor, say, once per second then I bet power usage will be single digits of microamps or less.
If you really need very low latency when that sensor values changes, then you could instead use the build in analog comparitor...
.. to generate an interrupt when the input voltage goes above or below a threshold value, but this will likely use much more power since just the analog comparitor itself uses ~30ua while on, and you will also need to generate the voltage that you are comparing to either with the internal 1.1 voltage reference or an external resistor bridge or buffer capacitor.

Declared MCU Clock Frequency vs oscillator frequency

I am quite confused about the relationship between the declared MCU Clock frequency ( which we set from edit project window) and the oscillator frequency (which we set by codding). So for an example let's say I need 31 kHz frequency. Do I also need to set the MCU Clock frequency from edit project window? If yes, in case of I want to change the frequency during the process, how am I supposed to change the clock frequency during run time?( Since I will not be able to change declared clock frequency.)
If you are using MPLAB X, the Harmony Plug-In will create code from your settings in UI. So it's the same and initial frequency on startup. If you want to change the frequency during runtime, you have to set the registers accordingly - but this sounds somehow odd.

What exactly meant in FreeRTOS's configCPU_CLOCK_HZ description?

The configCPU_CLOCK_HZ option explanation starts with this:
Enter the frequency in Hz at which the internal clock that driver the peripheral used to generate the tick interrupt will be executing.
Although I do more or less understand what it means, I need some finer explanation of what exactly is said there. Removing obvious "the peripheral used to generate the tick interrupt" from the middle I'm getting the "Enter the frequency in Hz at which the internal clock that driver will be executing", and this phrase looks a bit uncoordinated to me. What did the autor want to say with this? Some "that" driver, unlike, say, "this"? What "that"? The context doesn't imply any "that" here.
I think 'driver' should be 'drives' in that explanation.
configCPU_CLOCK_HZ is the frequency of the platform dependent timer that generates the tick interrupt. It is used by some ports to program the timer so it generates the correct FreeRTOS tick rate (see configTICK_RATE_HZ).
Example: configCPU_CLOCK_HZ is 1000000 (1 MHz) and configTICK_RATE_HZ is 100, then you configure the timer to generate an interrupt every 1000000/100 = 10000 ticks. That interrupt is your FreeRTOS system tick.
Take a look at an ARM Cortex-M port for one of the most common examples of this that uses the Cortex-M SysTick

STM32 clock adjustment

I'm using STM32f103 micro controller for a while and today I just confused about clock source and PLL configuration!
I know the clock source is HSI by default when micro starts and startup_stm32f10x_xx.s runs, but I don't know if PLL sets or not!? how can I know whats my micro freq?
thank you
A call to RCC_GetClocksFreq() will tell you the clock frequencies (SYSCLK, HCLK, PCLK1, PCLK2, ADCCLK).
If you are using the CMSIS library for the STM32, it has functions to configure the clock and also functions to tell you at runtime what the clock is.
If you are not, you will have to look to see where the clock source is being set, and if it is the HSE you will need to know what crystal you have. Once you have that info, you can then look at the M, N, and P parameters of the PLL (if used) to calculate your HCLK. You should be able to find all this information in the reference manual for the STM32F103 in the RCC (reset and clock control) section.

Lighting Control with the Arduino

I'd like to start out with the Arduino to make something that will (preferably) dim my room lights and turn on some recessed lighting for my computer when a button or switch is activated.
First of all, is this even possible with the Arduino?
Secondly, how would I switch on and off real lights with it? Some sort of relay, maybe?
Does anyone know of a good tutorial or something where at least parts of this are covered? I'll have no problems with the programming, just don't know where to start with hardware.
An alternative (and safer than playing with triacs – trust me I've been shocked by one once and that's enough!) is to use X-10 home automation devices.
There is a PC (RS232) device (CM12U UK or CM11 US) you can get to control the others. You can also get lamp modules that fit between your lamp and the wall outlet which allows you to dim the lamp by sending signals over the mains and switch modules which switch loads on and off.
The Arduino has a TTL level RS232 connector (it's basically what the USB connection uses) – Pins 0 and 1 on the Diecimila so you could use that, connect it via a level converter which you can buy or make and connect to the X-10 controller, theirs instructions on the on the Arduino website for making a RS232 port.
Alternatively you could use something like the FireCracker for X-10 which uses 310MHz (US) or 433MHz (UK) and have your Arduino send out RF signals which the TM12U converts into proper X-10 mains signals for the dimmers etc.
In the US the X-10 modules are really cheep as well (sadly not the case in the UK).
Most people do it using triacs. A triac is like two diodes in anti-parallel (in parallel, but with their polarity reversed) with a trigger pin. A triac conducts current in either direction only when it's triggered. Once triggered, it acts as a regular diode, it continues to conduct until the current drops bellow its threshold.
You can see it as a bi-directional switch on a AC line and can vary the mean current by triggering it in different moments relative to the moment the AC sine-wave crosses zero.
Roughly, it works like this: At the AC sine-wave zero, your diodes turn off and your lamp doesn't get any power. If you trigger the diodes, say, halfway through the sine's swing, you lamp will get half the normal current it would get, so it lights with half of it's power, until the sine-wave crosses zero again. At this point you start over.
If you trigger the triac sooner, your lamp will get current for a longer time interval, glowing brighter. If you trigger your triac latter, your lamp glows fainter.
The same applies to any AC load.
It is almost the same principle of PWM for DC. You turn your current source on and off quicker than your load can react, The amount of time it is turned on is proportional to the current your load will receive.
How do you do that with your arduino?
In simple terms you must first find the zero-crossing of the mains, then you set up a timer/delay and at its end you trigger the triac.
To detect the zero-crossing one normally uses an optocoupler. You connect the led side of the coupler with the mains and the transistor side with the interrupt pin of your arduino.
You can connect your arduino IO pins directly to the triacs' triggers, bu I would use another optocoupler just to be on the safe side.
When the sine-wave approaches zero, you get a pulse on your interrupt pin.
At this interrupt you set up a timer. the longer the timer, the less power your load will get. You also reset your triacs' pins state.
At this timers' interrupt you set your IO pins to trigger the triacs.
Of course you must understand a little about the hardware side so you don't fry your board, and burn your house,
And it goes without saying you must be careful not to kill yourself when dealing with mains AC =).
HERE is the project that got me started some time ago.
It uses AVRs so it should be easy to adapt to an arduino.
It is also quite complete, with schematics.
Their software is a bit on the complex side, so you should start with something simpler.
There is just a ton of this kind of stuff at the Make magazine site. I think you can even find some examples of similar hacks.
I use MOSFET for dimming 12V LED strips using Arduino. I chose IRF3710 for my project with a heat sink to be sure, and it works fine. I tested with 12V halogen lamp, it worked too.
I connect PWM output pin from Arduino directly to mosfet's gate pin, and use analogWrite in code to control brightness.
Regarding 2nd question about controlling lights, you can switch on/off 220V using relays, as partially seen on my photo, there are many boards for this, I chose this:
As a quick-start, you can get yourself one of those dimmerpacks (50-80€ for four lamps).
then build the electronics for the arduino to send DMX controls:
Arduino DMX shield
You'll get yourself both the arduino-expirience + a good chance of not frying your surrounding with higher voltage..