上次使用学习了一下Timer,每隔1秒钟中断一次,现在准备实现每隔1S钟读取一下内部的温度值,并在LCD上显示出来。
从手册288页上可以看出,LM3S8962的内部温度传感器有两个目的:
1. 防止温度过高或过低,影响系统芯片正常运行
2. 校准RTC,这个以后想玩玩,看看实际的效果
实现读取内部传感器值,需要有以下几步:
1. 使能ADC,内部传感器的值是通过ADC获取的
SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
SysCtlADCSpeedSet( SYSCTL_ADCSPEED_250KSPS );
2. 配置ADC
ADCReferenceSet(ADC_BASE, ADC_REF_INT);//设置ADC参考电压为内部电压
ADCSequenceDisable(ADC_BASE, 3); //禁止ADC采样
ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);//设置ADC为处理器事件
ADCSequenceStepConfigure(ADC_BASE, 3, 0, (ADC_CTL_TS | ADC_CTL_END | ADC_CTL_IE));//采样温度值结束后并申请中断
ADCSequenceEnable(ADC_BASE, 3);//使能ADC采样
3. 触发一次ADC采样
ADCProcessorTrigger(ADC_BASE, 3);
要不然得不到正确的温度值
通过以上的配置,配合Timer中断,我们在LCD上可以看到如下显示:
Temp is
00026
如果要使得效果更叫的明显一点,可以对这芯片哈口气,数值会剧烈变化,哈哈
实验代码如下:
#include "inc/hw_ints.h"
#include "inc/hw_memmap.h"
#include "inc/hw_types.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/interrupt.h"
#include "driverlib/sysctl.h"
#include "driverlib/timer.h"
#include "drivers/rit128x96x4.h"
#include "inc/hw_adc.h"
#include "driverlib/ADC.h"
#define LED GPIO_PIN_0
unsigned long cnt, adc_val;
unsigned long adc_isr_ret;
char bcd[10];
void
Timer0IntHandler(void)
{
TimerIntClear(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
GPIOPinWrite(GPIO_PORTF_BASE, LED, LED^GPIOPinRead(GPIO_PORTF_BASE,LED));
ADCProcessorTrigger(ADC_BASE, 3);
ADCSequenceDataGet(ADC_BASE, 3, &adc_val);
adc_val = (151040 - 225 * adc_val) / 1024;
bcd[0] = (unsigned char)(adc_val / 10000 + 0x30);
bcd[1] = (unsigned char)((adc_val % 10000) / 1000 + 0x30);
bcd[2] = (unsigned char)((adc_val % 1000) / 100 + 0x30);
bcd[3] = (unsigned char)((adc_val % 100) / 10 + 0x30);
bcd[4] = (unsigned char)((adc_val % 10) + 0x30);
RIT128x96x4StringDraw("Temp is:", 10, 40, 10);
RIT128x96x4StringDraw(bcd, 10, 55, 10);
}
int
main(void)
{
SysCtlPeripheralEnable(SYSCTL_PERIPH_TIMER0);
SysCtlPeripheralEnable( SYSCTL_PERIPH_ADC );
SysCtlADCSpeedSet( SYSCTL_ADCSPEED_250KSPS );
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
GPIODirModeSet(GPIO_PORTF_BASE,LED,GPIO_DIR_MODE_OUT);
GPIOPadConfigSet(GPIO_PORTF_BASE,LED,GPIO_STRENGTH_4MA, GPIO_PIN_TYPE_STD);
ADCReferenceSet(ADC_BASE, ADC_REF_INT);
ADCSequenceDisable(ADC_BASE, 3);
ADCSequenceConfigure(ADC_BASE, 3, ADC_TRIGGER_PROCESSOR, 0);
ADCSequenceStepConfigure(ADC_BASE, 3, 0, (ADC_CTL_TS | ADC_CTL_END | ADC_CTL_IE));
ADCSequenceEnable(ADC_BASE, 3);
TimerConfigure(TIMER0_BASE, TIMER_CFG_32_BIT_PER);
TimerLoadSet(TIMER0_BASE, TIMER_A, SysCtlClockGet()/4);
IntEnable(INT_TIMER0A);
TimerIntEnable(TIMER0_BASE, TIMER_TIMA_TIMEOUT);
TimerEnable(TIMER0_BASE, TIMER_A);
IntMasterEnable();
RIT128x96x4Init(1000000);
RIT128x96x4Clear();
ADCProcessorTrigger(ADC_BASE, 3);
while(1)
{
}
}