我的步驟如下: 使能ADC的時鐘和AIN10(PB4)的端口。 禁用與引腳B4相對應的DEN和DIR寄存器中的相應位。 使能AFSEL寄存器和PCTL寄存器*中的相應引腳。 設置寄存器:採樣率,優先級(SS3)等,如代碼所示。如何使我的ADC在tm4c123gxl微控制器中工作?
然後,我用另一個函數觸發它,但不知何故,我的ADC沒有讀取任何其他的施加電壓值。
我的第一個問題是關於PCTL和我們需要啓用ADC的價值?
我一直試圖解決它大約一天,但我仍然還沒有想出它。任何幫助深表感謝。
// Register definitions for clock enable
#define SYSCTL_RCGCGPIO_R (* ((volatile unsigned long *) 0x400FE608))
#define SYSCTL_RCGCADC_R (* ((volatile unsigned long *) 0x400FE638))
#define GPIO_PORTB_AFSEL_R (* ((volatile unsigned long *) 0x40058420))
#define GPIO_PORTB_PCTL_R (*((volatile unsigned long *)0x4005952C))
// Register definitions for GPIO port B ;;;;; AIN10 = PB4
#define GPIO_PORTB_DATA_R (* ((volatile unsigned long *) 0x400053FC))
#define GPIO_PORTB_DIR_R (* ((volatile unsigned long *) 0x40005400))
#define GPIO_PORTB_DEN_R ( *((volatile unsigned long *) 0x4000551C))
// Register definitions for ADC0 and Sample Sequencer 3
#define ADC0_PC_R (* ((volatile unsigned long *) 0x40038FC4))
#define ADC0_SSPRI_R (* ((volatile unsigned long *) 0x40038020))
#define ADC0_ACTSS_R (* ((volatile unsigned long *) 0x40038000))
#define ADC0_IM_R (* ((volatile unsigned long *) 0x40038008))
#define ADC0_RIS_R (* ((volatile unsigned long *) 0x40038004))
#define ADC0_ISC_R (* ((volatile unsigned long *) 0x4003800C))
#define ADC0_SAC_R (* ((volatile unsigned long *) 0x40038030))
#define ADC0_PSSI_R (* ((volatile unsigned long *) 0x40038028))
#define ADC0_SSCTL3_R (* ((volatile unsigned long *) 0x400380A4))
#define ADC0_SSFIFO3_R (* ((volatile unsigned long *) 0x400380A8))
unsigned char Lookup_7Seg_Disp [ 12 ] = {0xC0 , 0xF9 , 0xA4 , 0xB0 , 0x99 ,
0x92 , 0x82 , 0xF8 , 0x80 , 0x90 , 0xC6};
unsigned char Temperature_Value [ 3 ] = {0 , 0 , 0xA} ;
unsigned char i , value=0;
unsigned int ADC_value = 0 , voltage = 0 ;
int maxVoltage=0;
void ADC_Init() {
volatile unsigned long delay;
SYSCTL_RCGCGPIO_R |= 0x01; //Enable Clock for Port A
SYSCTL_RCGCADC_R |= 0x1; //Enable ADC0
delay = SYSCTL_RCGCGPIO_R; //Delay for clock to settle down
GPIO_PORTB_DIR_R &= ~(0x10);//PB4 as input
GPIO_PORTB_DEN_R &= ~(0x10);//PB4 as analog type
GPIO_PORTB_AFSEL_R |= 0x10;
GPIO_PORTB_PCTL_R |= 0x10;
//Clear sampling rate
ADC0_PC_R &= 0x00;
//Set sampling rate to 125ksps
ADC0_PC_R &= 0x01;
//Set priority to SSFI3
ADC0_SSPRI_R |= 0x3210;
//Disable sample sequence 3 befor configuration
ADC0_ACTSS_R &= ~0x8;
//Enable TS0, IE0 and END0 bits
ADC0_SSCTL3_R |= 0xE;
//Enable 16x hardware oversampling
ADC0_SAC_R |= 0x4;
//Disable Interrupt by writing 0 to corresponding bit
ADC0_IM_R &= ~(0x8);
//Activate sample sequencer
ADC0_ACTSS_R |= 0x8;
}
void SystemInit() {
}
void ADC_Voltage(void) {
ADC0_PSSI_R |= 0x8;
while ((ADC0_RIS_R & 0x8)==0);
ADC_value = (ADC0_SSFIFO3_R & 0xFFF);
voltage = (ADC_value)*44;
if(voltage>maxVoltage){
maxVoltage=voltage;
}
ADC0_ISC_R |= 0x08;
}
void delay(unsigned long counter) {
int i;
for(i=0;i<counter;i++)
{}
}
int main(void) {
ADC_Init();
delay(1000);
ADC_Voltage();
maxVoltage=maxVoltage*0.707;
}