Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugs for 328d adc/dac reference voltage #16

Open
youxiaojie opened this issue Apr 8, 2021 · 0 comments
Open

bugs for 328d adc/dac reference voltage #16

youxiaojie opened this issue Apr 8, 2021 · 0 comments

Comments

@youxiaojie
Copy link

BUGS!! in write_analogue.c

void analogReference(uint8_t mode)
{
	// can't actually set the register here because the default setting
	// will connect AVCC and the AREF pin, which would cause a short if
	// there's something connected to AREF.
	analog_reference = mode;

#if defined(__LGT8FX8E__) || defined(__LGT8FX8P__)
	#if defined(__LGT8FX8E__)
	if(analog_reference == INTERNAL2V56) {
		VCAL = VCAL2;
	} else {
		VCAL = VCAL1;
	}
	#else
	// set analog reference for ADC/DAC
	if(analog_reference == EXTERNAL) {
		DACON = (DACON & 0x0C) | 0x1;
		if((PMX2 & 0x2) == 0x2) {
			GPIOR0 = PMX2 & 0xfd;
			PMX2 = 0x80; 
			PMX2 = GPIOR0;
		}
	} else if(analog_reference == DEFAULT) {
		DACON &= 0x0C;
	} else {
		DACON = (DACON & 0x0C) | 0x2;
		cbi(ADCSRD, REFS2);
		if(analog_reference == INTERNAL2V048) {
			VCAL = VCAL2;	// 2.048V
		} else if(analog_reference == INTERNAL4V096) {
			sbi(ADCSRD, REFS2);
			VCAL = VCAL3;	// 4.096V
		} else	{
			VCAL = VCAL1;	// 1.024V
		}
	}
	#endif

	ADMUX = (analog_reference << 6);
#endif
}


dear friend,
I looked at this code,find that if I was 328d, there is a sentence on page 204,
使用内部基准(1.25V/2.56V)注意事项:
芯片上电后,默认将内部基准校准为 1.25V,用户如果使用 1.25V 的内部基准,可以直
接使用,无需其他操作。但如果需要使用 2.56V 的内部参考电压,需要自行更新内部基准的
校准值。 2.56V 的校准值在上电后被加载到寄存器 VCAL2(0xCE),在程序初始化时,将 VCAL2
的值读入并写入到 VCAL(0XC8)寄存器即完成 2.56V 的校准。
需要注意的是,当更新了 VCAL 寄存器后,会导致 VCAL1 即 1.25V 的校准值同时更新为
VCAL2,因此如果需要在后续使用过程中重新选择 1.25V 基准,需要提前将 VCAL1 的值保存
到变量中以供后续使用。

after power on, system set calibrate to 1v25 as default, no other operation needed. to using 2V56 as VCAL, need read calibrate value from VCAL2(0xce) and write to VCAL(0xC8). However when update the calibrate of VCL, the calibrate value in VCL1 will update to VCL2 simultaneously , so we must save calibrate of VCL1 in case we use 1v25 again after changing to 2V56.

so there seems a bug:
	#if defined(__LGT8FX8E__)
	if(analog_reference == INTERNAL2V56) {
		VCAL = VCAL2;
	} else {
		VCAL = VCAL1; // if  we use 1v25 again after changing to 2V56. VCAL1 store the calibrate value of 2V56
	}
	#else

To workout, I see in the main.cpp,GPIOR1 save the calibration,

main.cpp-#if defined(__LGT8FX8E__)
main.cpp-// store ivref calibration 
main.cpp:       GPIOR1 = VCAL1;
main.cpp:       GPIOR2 = VCAL2;
main.cpp-

can we replace VCAL1 with GPIOR1 here to workout this problem?


	#if defined(__LGT8FX8E__)
	if(analog_reference == INTERNAL2V56) {
		VCAL = VCAL2;
	} else {
		VCAL = GPIOR1; // will it get some side effect? 
	}if 
	#else

or are there some other good way to work around? does GPIOR1 be overwritten by other program?
Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant