21ic问答首页 - ATMEGA4809的时钟配置疑问?
ATMEGA4809的时钟配置疑问?
JackTang19942021-09-02
在使用AVR单片机时,发现设置MAIN时钟时需要对CCP寄存器进行解锁操作(4个内指令完成)。在查看了protected_io.S文件后,发现此使用了汇编来解锁以保证能在4个指令周期内完成时钟配置。但是有一个疑问就是,调用protected_write_io函数来操作CCP寄存器时,汇编代码中使用的寄存器为R20和R24为什么不是其他寄存器呢?#include <assembler.h>
PUBLIC_FUNCTION(protected_write_io)
#if defined(__GNUC__)
#ifdef RAMPZ
out _SFR_IO_ADDR(RAMPZ), r1 // Clear bits 23:16 of Z
#endif
//为什么使用R24和R22以及R20,为什么不能是其他寄存器。这些寄存器都是protected_write_io函数的形参
movw r30, r24 // Load addr into Z
out CCP, r22 // Start CCP handshake
st Z, r20 // Write value to I/O register
ret // Return to caller
#elif defined(__IAR_SYSTEMS_ASM__)
# if !defined(CONFIG_MEMORY_MODEL_TINY) && !defined(CONFIG_MEMORY_MODEL_SMALL) \
&& !defined(CONFIG_MEMORY_MODEL_LARGE)
# define CONFIG_MEMORY_MODEL_SMALL
# endif
# if defined(CONFIG_MEMORY_MODEL_LARGE)
ldi r20, 0
out RAMPZ, r20 // Reset bits 23:16 of Z
movw r30, r16 // Load addr into Z
# elif defined(CONFIG_MEMORY_MODEL_TINY)
ldi r31, 0 // Reset bits 8:15 of Z
mov r30, r16 // Load addr into Z
# else
movw r30, r16 // Load addr into Z
# endif
# if defined(CONFIG_MEMORY_MODEL_TINY)
out CCP, r17 // Start CCP handshake
st Z, r18 // Write value to I/O register
# elif defined(CONFIG_MEMORY_MODEL_SMALL)
out CCP, r18 // Start CCP handshake
st Z, r19 // Write value to I/O register
# elif defined(CONFIG_MEMORY_MODEL_LARGE)
out CCP, r19 // Start CCP handshake
st Z, r20 // Write value to I/O register
# else
# error Unknown memory model in use, no idea how registers should be accessed
# endif
ret
#else
# error Unknown assembler
#endif
END_FUNC(protected_write_io)
END_FILE()
PUBLIC_FUNCTION(protected_write_io)
#if defined(__GNUC__)
#ifdef RAMPZ
out _SFR_IO_ADDR(RAMPZ), r1 // Clear bits 23:16 of Z
#endif
//为什么使用R24和R22以及R20,为什么不能是其他寄存器。这些寄存器都是protected_write_io函数的形参
movw r30, r24 // Load addr into Z
out CCP, r22 // Start CCP handshake
st Z, r20 // Write value to I/O register
ret // Return to caller
#elif defined(__IAR_SYSTEMS_ASM__)
# if !defined(CONFIG_MEMORY_MODEL_TINY) && !defined(CONFIG_MEMORY_MODEL_SMALL) \
&& !defined(CONFIG_MEMORY_MODEL_LARGE)
# define CONFIG_MEMORY_MODEL_SMALL
# endif
# if defined(CONFIG_MEMORY_MODEL_LARGE)
ldi r20, 0
out RAMPZ, r20 // Reset bits 23:16 of Z
movw r30, r16 // Load addr into Z
# elif defined(CONFIG_MEMORY_MODEL_TINY)
ldi r31, 0 // Reset bits 8:15 of Z
mov r30, r16 // Load addr into Z
# else
movw r30, r16 // Load addr into Z
# endif
# if defined(CONFIG_MEMORY_MODEL_TINY)
out CCP, r17 // Start CCP handshake
st Z, r18 // Write value to I/O register
# elif defined(CONFIG_MEMORY_MODEL_SMALL)
out CCP, r18 // Start CCP handshake
st Z, r19 // Write value to I/O register
# elif defined(CONFIG_MEMORY_MODEL_LARGE)
out CCP, r19 // Start CCP handshake
st Z, r20 // Write value to I/O register
# else
# error Unknown memory model in use, no idea how registers should be accessed
# endif
ret
#else
# error Unknown assembler
#endif
END_FUNC(protected_write_io)
END_FILE()
赞0
评论
2021-09-02
您需要登录后才可以回复 登录 | 注册