Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Clock frequency
Goto page Previous  1, 2
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Thu Aug 13, 2020 1:58 pm    Post subject: Reply with quote

I got a reply from microchip but it was not helping. they only told about the baud procedure which i now implemented as an option.
but like i told microchip : it would make more sense to adjust the center frequency so all timing is correct.
i hope for some description on how to use the sigrow value to adjust the calib register.
but maybe you find something.
for now till i get a solution i leave it.

_________________
Mark
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 34

blank.gif
PostPosted: Thu Aug 13, 2020 2:50 pm    Post subject: Reply with quote

So setting the OSC20MCALIBA register and thus the frequency of the oscillator is simple.
If someone needs it, it can be done like this in bascom (this will raise the frequency from the company's 19.63MHz to 19.96MHz). The value in the register is changed by adding 1. The step is 0.33MHz.
Now I will think about what they meant by the q1.10 format for a 7-bit register
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Thu Aug 13, 2020 8:49 pm    Post subject: Reply with quote

yes it is simple. but i do not see the relation from the offset reg to the calib register.
how do you know what to add/sub ?

i asked microchip. so i wait for that.

to get the output signal :
Config Sysclock = 20mhz , Prescale = 1 , Clockout = Enabled
Config Portb.5 = Output 'for the clock output

when you do not set the port to output you also can measure the output !
but it seems less stable.

what i still do not get : why do they not load a good correction value ?why let the user do all kind of stuff to get a good result?

_________________
Mark
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Fri Aug 14, 2020 8:40 am    Post subject: Reply with quote

ok, i got a reply. they do not tell me where the OSC20MCALIBA value comes from only that it is set during production.
There is no procedure to adjust the clock.

I guess it would be similar to the baud adjust procedure.
I find it a bit odd. I do not get why they did not put values into SIGROW for say 1.8V, 3V3 and 5V. that would make more sense.
Maybe i will try to make a procedure later myself but for now i leave this as is.

_________________
Mark
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 34

blank.gif
PostPosted: Fri Aug 14, 2020 10:03 am    Post subject: Reply with quote

Well, it's also interesting that he doesn't remember the manual correction after the restart, even though they write in the data sheet that they do.
I just sent how it can be changed, now I'm preparing an automatic correction that calculates it itself and moves the register where it is needed.
It seems to me better to set the correction of the main oscillator as a better idea than to recalculate baud rates or time loops. Something memory will take the other side for the accuracy it's probably worth. Unfortunately, there are critical places where one needs a delay as accurately as possible and not just approximately. I have already solved several times the problem with communications, such as SPI and others, when I was not able to communicate from devices at higher speeds.
I'm just confused by the 0.33MHz step is terribly much. That's why I'm researching what their Q1.10 means.
The error is stored as a compressed Q1.10 fixed point 8-bit value, in order not to lose resolution, where
the MSB is the sign bit and the seven LSBs the lower bits of the Q1.10. This should apply only to the 4 constants that are used only for baud rate correction and apparently have nothing to do with the system frequency rate.
I know how to recalculate it, I just can't calculate a good result.
The number is int8_t, which means 8-bit signed. It follows to me that as they write, I have 7 bits left for my own number, I just don't understand where they have the dot in the fraction.
If I use atmel studio it returns values ​​to me (gcc compiler as known)
Sigrow 16MHz 3V: -4
Sigrow 16MHz 5V: -1
Sigrow 20MHz 3V: -7
Sigrow 20MHz 5V: 0
If I load it hard, Bascom will read it from the registry
SIGROW_OSC20ERR3V = 249dec 1111 1001bin
SIGROW_OSC20ERR5V = 0dec
SIGROW_OSC16ERR3V = 252dec 1111 1001bin
SIGROW_OSC16ERR5V = 255dec 1111 1111bin
If I take the first number and imagine that 8bit is a sign, I have 111 1001bin left.
Once upon a time I used the Q format for calculations (real numbers put a lot of strain on processors and this was nice and quite simple how not to lose accuracy) link how to do it here https://www.allaboutcircuits.com/technical-articles/fixed-point- representation-the-q-format-and-addition-examples /
So from the description we know that we have one number before the decimal point and 9 after. So we will add the number to 10 places 111 1001 000 and determine the number according to the procedure above. 1 * 2 (0) + 1 * 2 (-1) + 1 * 2 (-2) + 1 * 2 (-3) + 0 + 0 + 1 * 2 (-6) + 0 + 0 + 0 add it up and we get the number 1.890625. If I count correctly, I do not get anything from what I read. So what am I doing wrong?
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Fri Aug 14, 2020 10:43 am    Post subject: Reply with quote

From the PDF :
The error is stored as a compressed Q1.10 fixed point 8-bit value, not to lose resolution, where the MSb is the sign
bit, and the seven LSbs are the lower bits of the Q1.10.

Q1.10 means you have 1 bit for the integer and 10 bits for the fraction. somehow that will not fit into a byte with also a sign.
interesting is that they assign it to a short int which is just a signed byte.
i think they omitted some bits.
that is why they give this for the baud :
int8_t sigrow_val = SIGROW.OSC16ERR3V; // read signed error
int32_t baud_reg_val = 600; // ideal BAUD register value
assert (baud_reg_val >= 0x4A); // Verify legal min BAUD register
value with max neg comp
baud_reg_val *= (1024 + sigrow_val); // sum resolution + error
baud_reg_val /= 1024; // divide by resolution
USART0.BAUD = (int16_t) baud_reg_val; // set adjusted baud rate

i did check and it works. the 1024 is of course 10 bits.

so similar to this, you could do this with the calib register.
I tried that but it does not work because even the smallest bit change will result in a huge offset like you noticed too.



Code:

Config Sysclock = 20mhz , Prescale = 1 , Clockout = Enabled
Config Portb.5 = Output                                     'for the clock output

'configure the USART
Config Com1 = 250000 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1 , Baud_offset = Osc20_5v

Waitms 2000

Print "Test USART"
Dim B As Byte

Do
   Print "this is a baud test"
   Print Hex(clkctrl_osc20mcaliba)                          '9E
   B = Inkey()
   If B = "+" Then
     Cpu_ccp = &HD8
     Incr Clkctrl_osc20mcaliba
   Elseif B = "-" Then
     Cpu_ccp = &HD8
     Decr Clkctrl_osc20mcaliba
   End If
   Waitms 500
Loop
 


so i think you can not get a precise clock. at least not with the internal osc.
for a lower frequency such as a baud rate the compensation will work out ok.
till somebody proves otherwise i leave it as is.

_________________
Mark
Back to top
View user's profile Visit poster's website
Netzman

Bascom Expert



Joined: 25 Nov 2005
Posts: 113
Location: Graz

austria.gif
PostPosted: Fri Aug 14, 2020 11:52 am    Post subject: Reply with quote

The format is stated in the datasheet p.62:
"The Error is stored as a compressed Q1.10 fixed point 8 bit value, in order not to lose resolution, wherethe MSB is the sign bit and the 7 LSBs the lower bits of the Q.10."

The number is in fixed point format, where the integer part is fixed to 0, the sign is denoted by bit 7, the remaining 7 bits are the least significant bits of the 10-bit wide fractional part:
+/- 0.[000XXXXXXX]

To convert to a floating point number: [url]https://en.wikipedia.org/wiki/Q_(number_format)#Q_to_float[/url] (BBCode, what's up?)
Or, in other words, bit 6 of the register value has a magnitude of 0.0625, bit 5 has 0,03125, ..., bit 0 has 0.001953125.

So it is a linear factor which can be applied to the baud rate calculation, wait loops, system clock etc. My guess why the user has to do it, is because the hardware implementation of the RC frequency correction is quite coarse, the user could decide where more accurate calibration is needed (the baud rate calculation has smaller steps). Additionally, the RC temperature calibration is also more application-specific.

The correction factor is applied in a linear equation (doesn't matter which unit is used for Freq, could be Hz, the baud rate register setting, or something other):
Freq_corr = Freq * (1 + factor)

Which translates in fixed point math to (Bascom-like pseudocode):
Code:
Dim Freq As Integer
Dim Freq_corr As Integer
Dim sigrow_val As Integer
sigrow_val = SIGROW.OSC16ERR3V
Shift sigrow_val, Left, 8
Shift sigrow_val, Right, 8, Signed
Freq_corr = Freq * (1024 + sigrow_val)
Shift Freq_corr, Right, 10

_________________
LCD Menu | Proportional Fonts
Back to top
View user's profile Visit poster's website
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Fri Aug 14, 2020 12:07 pm    Post subject: Reply with quote

thanks for clearing this up : 0.[000XXXXXXX
Now it makes sense. i did not think about the fixed zeros.

the offset works good for baud. in fact :
Code:
Config Com1 = 250000 , Mode = Asynchroneous , Parity = None , Databits = 8 , Stopbits = 1 , Baud_offset = Osc20_5v

the new baud_offset option will read the specified sigrow value and applies the offset.

i would have preferred a better osc, or a better way to tweak it. the center freq. can not be adjusted to a precise value. but on the other hand, there exist other options.
ok, all clear Very Happy

_________________
Mark
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 34

blank.gif
PostPosted: Fri Aug 14, 2020 12:37 pm    Post subject: Reply with quote

Ok I understood it, I will add it wrong in the model example (see above) three 0 before and I will get the result 0.1181640625. So how AS got the value -7 for SIGROW_OSC20ERR3V. I have measured that the frequency of the oscillator at a correction factor of 23 oscillates at 19.63MHz (when changing to 24 oscillations at 19.96MHz). I don't like when something doesn't work as it should.
It looks like a binary accessory to me
Just for fun
with a correction factor of 0 oscillates the oscillator at 12.24MHz. The core is able to run up to 32MHz then resets.
If you're wondering why I'm researching this. I don't like artificially modified values that I can't influence and I sometimes need to time very precisely. This doesn't seem like a stable thing to me. By the way, that's why I needed that temperature.
To be honest, the RC oscillator error of 40% during production is nothing a microchip should boast. Such a large compensation does not indicate good production. Let anyone be angry with me who wants this at the old avr was not a habit.
I have to think about what to raise the frequency internally who knows how and then trust it is a lottery bet.
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 5061
Location: Holland

blank.gif
PostPosted: Fri Aug 14, 2020 7:51 pm    Post subject: Reply with quote

I agree. And i would not expect the huge offset until i measured it. But when you need very precise timing it is best to use a temp. controlled xtal. also, it is possible to use the RTC. with the internal 32 KHz or external 32 KHz.
So an exact timing would still be possible. I see these internal osc as a nice extra that was not in the older AVR.
I think the ones in xmega were better but on the other hand i did not test that lately. so who knows.

_________________
Mark
Back to top
View user's profile Visit poster's website
SZTRAD

Bascom Member



Joined: 30 Dec 2019
Posts: 34

blank.gif
PostPosted: Sat Aug 15, 2020 8:37 am    Post subject: Reply with quote

Unfortunately, the price of the resonator (crystal is out of the question for 14pin circuits) exceeds the price of the circuit itself twice, and I'm not saying that at 5V there are already saffron.
Well, sometimes it helps to read the data sheet until the end, for example, section 31.9. Smile
I lost my smile while peeking at the table. I wanted to use a 32kHz oscillator and perform an internal calibration. Well, he's not much better at measuring it. So the only option is calibration from the outside and this will be a bit more complicated than I wanted, especially in series production. Unfortunately the customer will have to reconsider the use of microchip circuits and I will forget about Bascom. They will go there stm8 and my unpopular c. Price and stability speak for them.
Anyway, thank you for the good work, Mark.

Regards RS
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page Previous  1, 2
Page 2 of 2

 
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
You cannot attach files in this forum
You cannot download files in this forum