Loading Trim Values at Run-Time for HCS08/RS08 Micros
In-Circuit Flash Programming , In-Circuit Debugging , HCS08/RS08/HC08 Micrcontrollers
For time-sensitive HCS08/RS08 applications the developer often needs to trim the internal reference clock in order to generate a desired bus frequency. P&E's HCS08 and RS08 Flash Programmers provide a command called “Program Trim” that allows developers to program a pre-calculated value to the non-volatile flash locations that are reserved for storing ICSTRM and ICSSC registers. These can then be loaded at run-time.
Here’s a demonstration of how the “Program Trim” command can be used to generate a bus frequency of 8 MHz on a 9S08QE128 microcontroller. For the 9S08QE128, the “Program Trim” command will generate a value that will trim the Internal Reference Clock to 31.25 KHz with an accuracy of up to +/- 0.2%. The command will then program the generated value to 0xFFAE and 0xFFAF. We will be working with an assembly file that configures the Internal Clock Source module and toggle Port A every 20 CPU cycles.
Configuration source file:
ROMSTART equ $2080
SOPT1 equ $1802
ICSC2 equ $0039
ICSTRM equ $003A
ICSSC equ $003B
PTAD equ $0000
PTADD equ $0001
Org ROMSTART
Main:
lda SOPT1
and #$7F
sta SOPT1 ; Disable watchdog
lda $FFAF
sta ICSTRM ; Load TRIM bits from Flash and store it into ICSTRM
lda ICSSC
and #$FE
ora $FFAE ; Load FTRIM bit from flash and store it into ICSSC
sta ICSSC
lda ICSC2
and #$3F ; Set BDIV to Divide DCOOUT by 1
sta ICSC2 ; FLL factor= 512, therefore 31.25Khz*512/1=16 MHz=DCOOUT
; 16MHz/2=8MHz=Bus Frequency
mov #$ff,PTADD ; Set all PTAD pins as outputs
mov #$ff,PTAD ; Set all PTA outputs as high
Bra Loop
Loop:
mov #$00,PTAD ; 4 cycles
nop
nop
nop
nop
nop
nop
mov #$ff,PTAD ; 4 cycles
nop
nop
jmp loop ; 4 cycles
Org $FFFE
dw Main ;Reset Vector
After saving the above source file section as "9S08QE128_Example.asm" and assembling it, we can use PROGHCS08 to program the generated 9S08QE128_Example.s19 file into flash. The programming sequence outlined below will program our generated .S19 and the pre-calculated trim value.
CM ; Choose module 9S08QE128.S8P
SS ; Specify our object file 9S08QE128_Example.S19
EM ; Erase module
BM ; Blank check module
PM ; Program module
VM ; Verify module
PT ; Program Trim Value
On a power-on reset, our 9S08QE128 target will disable the watchdog, load trim values from flash and store them into their corresponding ICS registers, set the bus frequency divider to 1, and toggle PTA pins every 20 cycles. With a bus frequency of 8MHz, if we were to put a scope on any of the PTA pins, we would expect to observe a signal with a 400 KHz frequency +/-0.2% accuracy.

