调试5:温度测定,AD数据采集
今天把AD采样程序给调试成功了。
1、图片
2、视频:
3、关键代码:
系统初始化:
void systeminit(void)
{
PIOR = 0x00U;
CG_ReadResetSource();
PORT_Init();
CLOCK_Init();
NOKIA_5110_init();//这里用到了显示屏
AD_Init();//AD采样的初始化
IT_Init();
CRC0CTL = 0x00U;
IAWCTL = 0x00U;
}
主程序:
void main(void)
{
/* Start user code. Do not edit comment generated here */
/* Peripheral start function calls */
IT_Start(); /* Start Interval Timer */
ADC_Init_ResultBuffer();
AD_ComparatorOn(); /* Enable ADC voltage comparator (ADCE = on) */
UINT i;
for (i=0; i < 100; i++)
{
NOP();
}
AD_Start(); /* Start ADC (ADCS = 0n) */
//显示屏的初始化显示
NOKIA_5110_write_chinese_string(0,0,12,7,0,0);//瑞萨开发板试用
NOKIA_5110_write_chinese_string(0,2,12,3,7,0);//电压
NOKIA_5110_write_chinese_string(0,4,12,3,10,0);//温度
NOKIA_5110_write_chinese_string(73,4,12,1,13,0);//度
lcdUpdateChar(2,7,'.');
lcdUpdateChar(2,13,'V');
lcdUpdateChar(4,8,'.');
/* Endless control loop */
while (1U)
{
/* ADC Conversions (External Voltage, Temperature and Reference Voltage) */
while (!ADIF) {} // Wait for first AD conversion completion
ADIF = 0;
temp_read = (USHORT)(ADCR >> 6U); // Ignore first ADC reading
ADC_Calculation_Ref(); // Calculate internal reference voltage value
AD_SelectADChannel(ADCHANNEL2); // Switch to external voltage (Channel 2)
for (i=0; i < 100; i++)
{
NOP();
}
ADC_Calculation_ExtVoltage(); // Calculate external voltage value
AD_SelectADChannel(ADTEMPERSENSOR0); // Switch to temperature sensor channel
for (i=0; i < 500; i++)
{
NOP();
}
ADC_Calculation_Temp(); // Calculate temperature value
AD_SelectADChannel(ADTEMPERSENSOR1); // Switch to internal reference voltage channel
for (i=0; i < 100; i++)
{
NOP();
}
LCD_Accel_Value2(2,6,(int)(g_voltage*1000)); //转换电压值
LCD_Accel_Value(4,6,(int)(g_temperature*100)); //转换温度
}
其中:
void AD_Init(void)
{
ADCEN = 1U; /* supply AD clock */
ADM0 = _00_AD_ADM0_INITIALVALUE; /* disable AD conversion and clear ADM0 register */
ADMK = 1U; /* disable INTAD interrupt */
ADIF = 0U; /* clear INTAD interrupt flag */
/* Set INTAD low priority */
ADPR1 = 1U;
ADPR0 = 1U;
ADPC = _04_AD_ADPC_3ANALOG;
/* Set ANI0 - ANI2 pin as analog input */
PM2 |= 0x07U;
ADM0 = _20_AD_CONVERSION_CLOCK_6 | _00_AD_TIME_MODE_STANDARD_1 | _00_AD_OPERMODE_SELECT;
ADM1 = _00_AD_TRIGGER_SOFTWARE | _00_AD_CONVMODE_CONSELECT;
ADM2 = _00_AD_POSITIVE_VDD | _00_AD_NEGATIVE_VSS | _00_AD_AREA_MODE_1 | _00_AD_RESOLUTION_10BIT;
ADUL = _FF_AD_ADUL_VALUE;
ADLL = _00_AD_ADLL_VALUE;
ADS = _81_AD_INPUT_TEMPERSENSOR_1;
}
void AD_Start(void)
{
ADIF = 0U; /* clear INTAD interrupt flag */
ADMK = 0U; /* enable INTAD interrupt */
ADCS = 1U; /* enable AD conversion */
}
void ADC_Init_ResultBuffer(void)
{
UCHAR i;
for (i = 0 ; i < 10 ; i++)
{
ADC_result[i] = 0; // Clear ADC result buffer
}
}
void ADC_Calculation_ExtVoltage(void)
{
if (samples >= 3)
{
samples = 0;
}
AD_Read(&ADC_result[samples]); // Store the conversion result in the buffer
temp = (float) 1.44 / g_ref_hexvalue;
g_voltage = (float) ((ADC_result[samples] & 0xFFFC) * temp); // Calculate the external voltage value
samples++;
}
void ADC_Calculation_Temp(void)
{
if (samples >= 3)
{
samples = 0;
}
AD_Read(&ADC_result[samples]); // Store the conversion result in the buffer
temp = (float) 1.44 / g_ref_hexvalue;
g_temperature = (float) (25 - ((((ADC_result[samples] * temp) - 1) * 1000) / 3.6)); // Calculate the temperature value in 癈
samples++;
}
void ADC_Calculation_Ref(void)
{
if (samples >= 3)
{
samples = 0;
}
AD_Read(&ADC_result[samples]); // Store the conversion result in the buffer
g_ref_hexvalue = ADC_result[samples];
samples++;
}
读取数据
void AD_Read(USHORT *buffer)
{
*buffer = (USHORT)(ADCR >> 6U);
}
//选择通道
MD_STATUS AD_SelectADChannel(enum ADChannel channel)
{
MD_STATUS status = MD_OK;
if (((channel > ADCHANNEL7) && (channel < ADCHANNEL16)) || ((channel > ADCHANNEL19) && (channel < ADTEMPERSENSOR0)) || (channel > ADTEMPERSENSOR1))
{
status = MD_ARGERROR;
}
else
{
ADS = (UCHAR)channel;
}
return (status);
}
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |