Declared MCU Clock Frequency vs oscillator frequency - 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.

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.

PIC Clock frequency change by registers and edit project window

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.

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

When using the SPI protocol, is the output data rate synonymous with the baud rate?

I'm trying to learn how the SPI protocol works, and I'm working on a basic project using the STM32F407G-Discovery board.
This board has a built-in accelerometer (LIS3DSH), and it uses the SPI protocol. In the user manual, it states the following:
The LIS3DSH has ±2g/±4g/±6g/±8g/±16g dynamically selectable full-scale
and it is capable of measuring acceleration with an output data rate
of 3.125 Hz to 1.6 kHz.
This accelerometer is using SPI1, which is connected to APB2. I'm using STM32CubeMX to generate the initialization code (including the clock configuration), and it looks like the APB2 peripheral clock has a default value of 84 Mhz.
Does this mean that I need to configure the APB2 peripheral clock to have such a value that it falls between the range of 3.125 Hz and 1.6 kHz? I can't imagine this is true because I can't get the value low enough
in STM32CubeMX since it throws an error if I go too low.
I'm also accounting for the baud rate control SPI register, which allows you to go as low as f-PCLK/256.
In other words, I'm a bit stuck on which clock frequency to use and which baud rate control to use.
I'm still learning embedded programming, and so my terminology might be incorrect.
the two are not related. the max SPI clock rate is 10Mhz (page 14). The out rate of 3.125Hz to 1.6Khz is how fast the chip does an acceleration conversion. At 3.125Hz, a new conversion result is ready every 320ms, and at 1.6Khz, they are available every 625us. There is a trade off between conversion rates, power consumption and accuracy. The data sheet leaves a lot of holes, I would suggest reading the MMA7660 data sheet to get a better understanding of how these types of chips work and then revert back to your datasheet for implementation details.
You could use the SPI clock frequency with up to 10MHz to get data from this chip.
(So a prescaler of 16 and the full rate (84MHz) APB2 clock would be ok)
The SPI clock determines how fast the data is transferred from the chip to the controller not how fast the chip generated new results.
To always get the newest data you could use the IRQ lines from the chip or use an timer to trigger the transmission corresponding to sampling rate.

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.