ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
clock_trim_scan.c File Reference
#include "fw_hal.h"

Go to the source code of this file.

Functions

uint8_t readCode (uint8_t offset)
 
void main (void)
 

Function Documentation

◆ main()

void main ( void  )

Definition at line 36 of file clock_trim_scan.c.

37{
38 uint8_t i, j, vrtrim = 0x01, irtrim = 0;
39
40 // Set system clock. Invoke it to set the frequency band
42 // UART1 configuration: baud 115200 with Timer2, 1T mode, no interrupt
44
45 while(1)
46 {
47 // The upper limit of IRTRIM depends on VRTRIM, MCU becomes unstable if it goes
48 // too high, set 0x30 to a lower value if scan stucks at beginning
49 if (++irtrim == 0x30 + vrtrim)
50 {
51 irtrim = 0x00;
52 // VRTRIM controls the voltage level, normally it won't exceed 0x40
53 if (++vrtrim == 0x40)
54 {
55 vrtrim = 0x00;
56 }
57 }
58 SYS_TrimClock(vrtrim, irtrim);
59
60 UART1_TxString("ADDR: 0x");
62 UART1_TxHex(__CID_ADDR & 0xFF);
63 UART1_TxString("\r\n");
64 UART1_TxString("MCUID: ");
65 for (j = 0; j < 7; j++)
66 {
67 UART1_TxHex(readCode(18 + j));
68 UART1_TxChar(' ');
69 }
70 UART1_TxString("\r\n");
71 UART1_TxString("Current IRCBAND:");
72 UART1_TxHex(IRCBAND);
73 UART1_TxString(", VRTRIM:");
74 UART1_TxHex(VRTRIM);
75 UART1_TxString(", IRTRIM:");
76 UART1_TxHex(IRTRIM);
77 UART1_TxString(", LIRTRIM:");
78 UART1_TxHex(LIRTRIM);
79 UART1_TxString("\r\n\r\n");
80
81 SYS_Delay(50);
82 }
83}
uint8_t readCode(uint8_t offset)
#define __CID_ADDR
Definition: fw_cid_stc8h.h:21
void SYS_SetClock(void)
Definition: fw_sys.c:40
void SYS_TrimClock(uint8_t vrtrim, uint8_t irtrim)
Definition: fw_sys.c:58
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
@ HAL_State_ON
Definition: fw_types.h:71
unsigned char uint8_t
Definition: fw_types.h:18
void UART1_TxChar(char dat)
Definition: fw_uart.c:72
void UART1_TxString(uint8_t *str)
Definition: fw_uart.c:85
@ UART1_BaudSource_Timer2
Definition: fw_uart.h:34
void UART1_TxHex(uint8_t hex)
Definition: fw_uart.c:79
void UART1_Config8bitUart(UART1_BaudSource_t baudSource, HAL_State_t _1TMode, uint32_t baudrate)
Definition: fw_uart.c:56
uint8_t __XDATA i

◆ readCode()

uint8_t readCode ( uint8_t  offset)

Scan to find most close VRTRIM and IRTRIM for specified frequency.

Change build_flags in platformio.ini D__CONF_FOSC -> target frequency, e.g. 36864000UL D__CONF_MCU_MODEL -> MCU type, e.g. MCU_MODEL_STC8H3K64S2 D__CONF_IRCBAND -> frequency band depending on the frequency STC8H1K - 00:20MHz, 01:35MHz STC8H3K,STC8H8K - 02:24MHz, 03:40MHz

Connect UART at baud 115200 and read output, when the output is not garbled, find the middle value of VRTRIM and IRTRIM, lower VRTRIM is preferred

Definition at line 31 of file clock_trim_scan.c.

32{
33 return (*(unsigned char volatile __CODE *)(__CID_ADDR + offset));
34}