AN2326 STMICROELECTRONICS | Alldatasheet

Document overview

  • Manufacturer or author: Provided By ALLDATASHEET.COM(FREE DATASHEET DOWNLOAD SITE)
  • PDF pages: 20

Technical content

Datasheet sections

  • 1 Calibration software
  • 1.1 Software principle
  • 1.2 Basic version
  • 1.3 Average version
  • 2 Power supply and timebase deliver y circuit
  • 2.1 Basic circuit
  • 2.2 Hardware protection
  • 3 Conclusion
  • 4 Software examples
  • 4.1 Single alternance
  • 4.1.1 Main program
  • 4.1.2 Input capture interrupt
  • 4.1.3 Timebase interrupt
  • 4.1.4 Writing in non volatile memory for products without Data EEPROM
  • 4.1.5 Writing in non volatile memory fo r products with Data EEPROM
  • 4.1.6 Detailed basic version software flowchart
  • 4.2 Average version
  • 4.2.1 Main program
  • 4.2.2 Input capture interrupt
  • 4.2.3 Timebase interrupt
  • 5 Revision history

Calibrating the RC oscillator of the ST7ULTRALITE MCU using the mains Introduction The ST7ULTRALITE microcontroller contains an internal RC oscillator which can be trimmed to a specific frequency with the required accuracy. The oscillator frequency has to be calibrated by software using the RCCR register (RC Control Register) and the SICSR register (System Integrity Control/Status Register). The value entered in the RCCR/SICSR registers will switch ON a corresponding number of resistors that will modify the oscillator frequency. Whenever the ST7ULTRALITE microcontroller is reset, the 10-bit value contained in the RCCR/SICSR registers is restored to its default value (3FFh) i.e. the lower possible frequency, so each time the device is reset, you have to load the calibration value in the RCCR/SICSR registers. There are predefined calibration values stored in memory (refer to the” Internal RC Oscillator Adjustment” section in the ST7ULTRALITE datasheet). Y ou can load one of these values in the RCCR/SICSR registers if one of the operating conditions matches that in your application. Otherwise, you can define your own value, store it in non volatile memory and load it in the RCCR/SICSR registers after each reset. However, if any of the external conditions (temperature or voltage, for instance) changes too drastically, the stored value may no longer produce the required accuracy. One solution is to recalculate the RCCR/SICSR register values after each reset, based on an external reference. The purpose of this application note is to present a software solution using the frequency of the European standard mains (220V/50Hz) as a timebase to adjust the internal RC oscillator of the ST7ULTRALITE to 8 MHz. The same approach can also be used for the US mains standard (110V/60Hz). The basic software takes less than 200 ms to calibrate the oscillator and uses less than128 bytes of program memory and five bytes of RAM for its simplest version. These RAM bytes can be freed for other purposes when the calibration is done. Another example using averages is given in this application note. This can be useful with noisy mains. This application note also contains the diagram of a low cost circuit which converts the mains into a 5 volt power supply and protects the microcontroller from overcurrent on the input connected to the mains. Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s)

1 Calibration software

1.1 Software principle

8MHz internal RC is divided by 8 thanks to the AVTCHR register. value is decreased in order to increase the RC oscillator frequency. Figure 1. Dichotomous analysis of RCCR value 10 iterations the value of RCCR is set with an accuracy of one bit.

Figure 2. Using the timer input capture to measure the mains frequency each edge of the mains the value of the free running counter is stored as shown in Figure 2.. Then the microcontroller calculates the elapsed time between the two edges of the mains. on the mains and F9h is the overflow value of the free running counter.

1.2 Basic version

detailed flowchart can be found in Section 4.

Figure 3. Basic software flowchart

1.3 Average version

motor starts it generates a tension pick and this can be considered as a mains edge.

Figure 4. Average software flowchart

2 Power supply and timebase delivery circuit

2.1 Basic circuit

as well as the 110V/60Hz of the US mains, into 5V DC. Figure 5. Power supply and timebase delivery circuit diagram

the LTIC input. A 220kΩ resistor is enough in this case. Table 1. Maximum MCU current

2.2 Hardware protection

frequency which could be understood by the microcontroller as a mains edge. is really noisy. Any kind of filter can be added on the LTIC. Figure 6. Band pass filter this filter is given in the table below. Table 2. Resistor values

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) AN2326 Conclusion

3 Conclusion

This system allows you to have a power supply for the microcontroller and an auto adjustable clock set to 1, 2, 4 or 8MHz, selected by prescaler, with the required accuracy whatever the external conditions. This solution also offers the advantage of being less expensive than a solution with a transformer and requires less space. It requires a small amount of space in program memory (less than 128 bytes) in its smallest version.

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) Software examples AN2326

4 Software examples

A zip file attached to this application note contains the complete software of this calibration method.

4.1 Single alternance

This version performs only one count between two edges and changes the value of the RCCR/SICSR registers according to this measurement. This can lead to bad tuning if there is noise on the reference signal.

4.1.1 Main program

;All the bytes from locations 80h to 85h are used by this software to store values or as control registers but they can be reused safely after the clock has been set. ;dichotomy value .value equ $81 ;this byte contains the value which will be added or subtracted to/from the RCCR last value at the end of each round ;capture values .capture1 equ $82 .capture2 equ $83 ;these two bytes contain the two values of the counter captured on the edge of the mains, they are used to calculate the time elapsed between the two edges ;number of overflows .nbover equ $84 ;this byte contains the number of counter overflows during the measurement ;control register .cr equ $85 ;this byte is used as a control register for the measurement. Its bits allow or not the interrupts and show which step of the count is the current one. .strtstp equ 1 ;this is set to start the count and reset to stop it .lsb_RCCR equ 7 ;this bit is set when the first capture has occurred. It allows the overflows to be counted ;address to program in NVM (optional) .E2ADDR equ $86 .RAM equ $88 .main ld A, #$63 ld AVDTHCR, A ; set internal clock to fRC/8 meaning 1MHz targeted clr SICSR ; clear the 2 lowest significant bits located in this register ld A, #$80 ;value containing the value which will be ld value, A ;add or subs to/from RCCR during the dichotomy ld RCCR, A ;RCCR is set to the middle of its range of value clr cr ;clear the byte use as control register for the count next clr nbover ;clear the byte containing the number of timer overflow ld A, LTICR ;clear the ICF bit rim ;interrupts enable bset LTCSR, #7 ;enable input capture interrupt bset cr,#strtstp ;set the start-stop bit of cr: count can start count btjt cr, #strtstp, count; wait for the end of count clr LTCSR ;lite timer interrupts disable srl value ;dichotomy value divided by 2 ld A,#$F9 ;these lines calculate this equation: ld X,nbover ; mul X,A ;(nbover*$F9)+ capture2 - capture1 add A, capture2 ; jrnc nocarry ;this equation is calculated with 16 bits inc X ;

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) AN2326 Software examples nocarry sub A, capture1 ;MSB are in register X jrnc noneg ; dec X ;and LSB in register A. noneg cp X, #$01 ;if mains frequency is 50Hz the reference value is $138 jrmi minus ;if it is 60Hz the reference is $104.the program first jreq compare ;compares MSB with $01 and then compare LSB with jp plus ;$38 for 50Hz and $04 for 60Hz. if the calculated comparecp A, #$38 ;value is smaller than the reference the program jump to jrmi minus ;minus to decrease RCCR else it increase RCCR plus btjt cr,#lsb_RCCR,plus_lsb ld A, RCCR add A, value ;add value if counted value is greater than ref jra new plus_lsb btjt value, #5, p_lsb0 btjt value, #4, end ld A, value ld SICSR, A ;add value if counted value is greater than ref jra next p_lsb0 bset SICSR,#5 ;add value if counted value is greater than ref jra next minus btjt cr,#lsb_RCCR,minus_lsb ld A, RCCR sub A, value ;subtract value if Y is smaller jra new minus_lsb btjt value,#5, m_lsb0 btjt value, #4, clr_lsb0 bres RCCR,#0 ;reset the LSb of RCCRH ld A, value ld SICSR, A ;substract value if counted value is smaller than ref jra next m_lsb0 bres SICSR,#6 bset SICSR,#5 ;subtract value if Y is smaller jra next clr_lsb0 bres SICSR,#5 ;reset the LSb of RCCRL jra end new ld RCCR,A ;enter the new value in RCCR btjf value, #0, next;stop after 7 rounds bset cr, #lsb_RCCR ld A, #$80 ld value, A jra next .end_calibration ; end of the calibration ; at this point the Fcpu is set to 1MHz ; the last operation is to set the clock to the targeted value by setting the prescaler register ld A, #$03 ; set clock to fRC meaning 8MHz ; ld A, #$23 ; set prescaler to fRC/2 ; ld A, #$43 ; set prescaler to fRC/4 ; ld A, #$63 ; set prescaler to fRC/8 ld AVDTHCR,A ; in the three cases AVD is set off

4.1.2 Input capture interrupt

ld A, LTICR ;load captured value in A btjt LTCSR, #4, finish ;test if it is first or second capture bset LTCSR, #4 ;allow timebase interrupt in order to count the number of overflows ld capture1, A ;captured value is stored in capture1 jp endit1 finish ld capture2, A ;if it is the second capture, captured value is stored in capture2 clr cr ;clear cr to end the count

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) Software examples AN2326 endit1 iret

4.1.3 Timebase interrupt

ld A, LTCSR ;clear TB bit inc nbover ;increment number of overflows endit2 iret

4.1.4 Writing in non volatile memo ry for products without Data EEPROM

To store the final value of RCCR/SICSR registers in non volatile memory, the following lines must be added after the timer interrupts have been disabled in the main program. As Data EEPROM is not em- bedded in the ST7ULTRALITE, the storage is done in program memory, so the sector 0 size must be 0.5k (selected by option byte). The programmation routine must be executed from RAM, so it is first loaded in RAM and then called. Both RCCR and SICSR registers are saved. ; Content of RCCR and SICSR is saved at FC00h and FC01h ; < RESET_FCSR > LD A,#$56 ; Enter RASS keys to unlock FCSR register LD FCSR,A LD A,#$AE LD FCSR,A ; < LOAD_RAM > LD X,#$3F ; Copy programming software driver 32 bytes = 4 lines .RAM_Copy ; into RAM from address 0083h LD A,(RAM_Driver,X) LD (RAM,X),A DEC X JRPL RAM_Copy ; < USER_APPLICATION_PROGRAM > ; < FIRST_PROG > LD A,#$FC ; High address LD X,A ; clr Y ; define FC00 as destination address LD {E2ADDR},X ; Address high to be programmed (0081h) is in X LD {E2ADDR+1},Y ; Address low to be programmed (0082h) is in Y CALL RAM ; Call the programming driver located into RAM JP USER_APPLICATION ; ROUTINE: XemulE2_ByteProg ; DESCRIPTION: Emulated data EEPROM byte programming driver routine ; BEFORE: A = data to be programmmed ; X:Y = address where it has to be programmed [FC00h..FDFFh] .RAM_Driver BSET FCSR,#LAT ; Enable Emul. EEPROM latches LD A,FCSR CLR X LD A, RCCR LD ([E2ADDR.w],X),A; Set address/data to be programmed inc X LD A, SICSR LD ([E2ADDR.w],X),A; Set address/data to be programmed BSET FCSR,#PGM ; Launch the Emul. EEPROM programming

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) AN2326 Software examples .EEPROM_Prog BTJT FCSR,#PGM,EEPROM_Prog; Wait end of programming (~5ms) RET

4.1.5 Writing in non volatile me mory for products with Data EEPROM

To store final value of RCCR/SICSR registers in EEPROM, add theses lines after disabling the timer in- terrupts in the main program. ld RCCR, A bset EECSR,#1 ;start to enter value in the EEPROM ld $1003,A ;load value of the RCCR in EEPROM ld A, SICSR ld $1004,A ;load value of the SICSR in EEPROM bset EECSR,#0 ;start to write in the EEPROM wait btjt EECSR,#0,wait ;wait for the end of writing in EEPROM

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) Software examples AN2326

4.1.6 Detailed basic version software flowchart

ENABLE INPUT CAPTURE INTERRUPT (NBOVER, CR) STORE STARTING VALUE 80H --> VALUE 80H --> RCCR EDGE ON LTIC INPUT STORE CAPTURED VALUE IN CAPTURE 1 ENABLE TIMEBASE INTERRUPT TO STORE CAPTURED VALUE IN CAPTURE 2 DISABLE TIMEBASE AND INPUT CAPTURE INTERRUPTS START TO COUNT TIMER OVERFLOWS DIVIDE VALUE BY 2 CALCULATION OF: NBOVER X F9 + CAPTURE 2 - CAPTURE 1 SUBTRACT VALUE TO RCCRADD VALUE TO RCCR RC OSCILLATOR IS TRIMMED TO 1 MHZ COMPARE RESULT WITH 138H IS THE DICHOTOMY FINISHED? NO NO NO YES YES YES SMALLER GREATER EDGE ON LTIC INPUT EDGE ON LTIC INPUT

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) AN2326 Software examples

4.2 Average version

This version performs the count between two edges four times and changes the value of the RCCR/SICSR registers according to the average of these measurements. This method allows to perform a better tune of the RC oscillator especially in noisy environment.

4.2.1 Main program

;All the bytes from locations 80h to 8Bh are used by this software to store values or as control registers but they can be reused safely after the clock has been set. ;dichotomy value .value equ $81 ;this byte contains the value which will be added or subtracted to/from the RCCR last value at the end of each round ;capture values .capture1 equ $82 .capture2 equ $86 ;these bytes contain the values of the counter captured on the edge of the mains, they are used to calculate the time elapsed between the two edges ;number of overflows .nbover equ $8A ;this byte contains the number of counter overflows during the measurement ;control register .cr equ $8B ;this byte is used as a control register for the measurement. Its bits allow or not the interrupts and show which step of the count is the current one. .strtstp equ 1 ;this is set to start the count and reset to stop it .lsb_RCCR equ 7 ;this bit is set when the first capture has occurred. It allows the overflows to be counted ;address to program in NVM (optional) .E2ADDR equ $86 .RAM equ $88 .main ld A, #$63 ld AVDTHCR, A ; set internal clock to fRC/8 meaning 1MHz targeted ld A, #$80 ;value containing the value which will be ld value, A ;add or subs to/from RCCR during the dichotomy ld RCCR, A ;RCCR is set to the middle of its range of value clr cr ;clear the byte use as control register for the count next clr nbover ;clear the byte containing the number of timer overflow ld A, LTICR ;clear the ICF bit clr Y capture rim ;interrupts enable bset LTCSR, #7 ;enable input capture interrupt bset cr,#strtstp ;set the start-stop bit of cr: count can start count btjt cr, #strtstp, count; wait for the end of count clr LTCSR ;lite timer interrupts disable inc Y cp Y,#4 ;repeat the capture four time to make an average jrne capture srl value ;dichotomy value divided by 2 clr Y ld A,#$F9 ;these lines calculate this equation for the four ld X,nbover;measures: mul X,A ;(nbover*$F9)+ capture2 - capture1 calcul add A, (capture2,Y) ; jrnc nocarry ;this equation is calculated with 16 bits inc X ; nocarry sub A, (capture1,Y);MSB are in register X jrnc noneg ; dec X ;and LSB in register A. noneg inc Y cp Y,#4

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) Software examples AN2326 jrne calcul srl A ;these lines calculate the average of the last four srl X ;measures by dividing their total by 4. It is done by jrnc carry1 ;two consecutive right shift on the 16 bit result. add A,#$80 carry1 srl A srl X jrnc carry2 add A,#$80 carry2 cp X, #$01 ;if mains frequency is 50Hz the reference value is $138 jrmi minus ;if it is 60Hz the reference is $104.the program first jreq compare;compares MSB with $01 and then compare LSB with jp plus ;$38 for 50Hz and $04 for 60Hz. if the calculated compare cp A, #$38 ;value is smaller than the reference the program jump to jrmi minus ;minus to decrease RCCR else it increase RCCR plus btjt cr,#lsb_RCCR,plus_lsb ld A, RCCR add A, value ;add value if counted value is greater than ref jra new plus_lsb btjt value, #5, p_lsb0 btjt value, #4, end ld A, value ld SICSR, A ;add value if counted value is greater than ref jra next p_lsb0 bset SICSR,#5 ;add value if counted value is greater than ref jra next minus btjt cr,#lsb_RCCR,minus_lsb ld A, RCCR sub A, value ;subtract value if Y is smaller jra new minus_lsb btjt value,#5, m_lsb0 btjt value, #4, clr_lsb0 bres RCCR,#0 ;reset the LSb of RCCRH ld A, value ld SICSR, A ;substract value if counted value is smaller than ref jra next m_lsb0 bres SICSR,#6 bset SICSR,#5 ;subtract value if Y is smaller jp next clr_lsb0 bres SICSR,#5 ;reset the LSb of RCCRL jra end new ld RCCR,A ;enter the new value in RCCR btjf value, #0, next_near;stop after 7 rounds bset cr, #lsb_RCCR ld A, #$80 ld value, A next_near jp next .end_calibration ; end of the calibration ; at this point the Fcpu is set to 1MHz ; the last operation is to set the clock to the targeted value by setting the prescaler register ld A, #$03 ; set clock to fRC meaning 8MHz ; ld A, #$23 ; set prescaler to fRC/2 ; ld A, #$43 ; set prescaler to fRC/4 ; ld A, #$63 ; set prescaler to fRC/8 ld AVDTHCR,A ; in the three cases AVD is set off

4.2.2 Input capture interrupt

ld A, LTICR ;load captured value in A btjt LTCSR, #4, finish ;test if it is first or second capture

Obsolete Product(s) - Obsolete Product(s) Obsolete Product(s) - Obsolete Product(s) AN2326 Software examples bset LTCSR, #4 ;allow timebase interrupt in order to count the number of overflows ld (capture1,Y), A ;captured value is stored in capture1 jp endit1 finish ld (capture2,Y), A ;if it is the second capture, captured value is stored in capture2 clr cr ;clear cr to end the count endit1 iret

4.2.3 Timebase interrupt

ld A, LTCSR ;clear TB bit inc nbover ;increment number of overflows endit2 iret

Table 3. Detailed average version software flowchart

5 Revision history

Table 4. Document revision history 03-April-2006 1 Initial release.