adc by interrupt works

This commit is contained in:
hg
2015-02-22 00:09:15 +01:00
parent 89a2aa041c
commit c3305aeeae
2 changed files with 21 additions and 7 deletions

View File

@ -5,6 +5,8 @@
const uint32_t ADC_TIME_OUT = 100000;
const uint16_t ADC_MAX = 511;
const uint8_t DAMPING = 5;
const float Ctrl_P = 10.0;
const float Ctrl_I = 5.0;
const float Ctrl_D = 0.0;

View File

@ -16,16 +16,16 @@
volatile Control ctrl((float)PWM_MIN, (float)PWM_MAX, Ctrl_P, Ctrl_I, Ctrl_D);
volatile float u_des = 12.0;
volatile float u_des = 5.0;
volatile float u_curr = 0;
volatile uint16_t newPwm = 0;
void pwmInit() {
ADC10CTL0 = ADC10SHT_0 | ADC10ON | SREF_0 | ADC10IE;
ADC10CTL1 = INCH_7 | ADC10SSEL_3 | ADC10DIV_2;
ADC10AE0 = BIT7;
ADC10CTL0 = ADC10SHT_2 | ADC10ON | SREF_0 | ADC10IE;
ADC10CTL1 = INCH_7 | ADC10SSEL_3 | ADC10DIV_2;
ADC10AE0 = BIT7;
P1DIR |= BIT6;
P1SEL |= BIT6;
@ -40,12 +40,20 @@ void pwmInit() {
ISR(TIMER0_A0, TA0_ISR) {
ADC10CTL0 |= ENC | ADC10SC;
ADC10CTL0 |= ENC | ADC10SC;
}
ISR(ADC10, ADC_ISR) {
uint16_t adcIn = ADC10MEM;
adcIn >>= 1;
static uint8_t dampingCnt = 0;
static uint16_t adcIn = 0;
if (dampingCnt < DAMPING) {
uint16_t v = ADC10MEM;
v >>= 1;
adcIn += v;
dampingCnt++;
} else {
adcIn /= DAMPING;
float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom;
@ -54,6 +62,10 @@ ISR(ADC10, ADC_ISR) {
newPwm = (uint16_t) newPwm_f;
TACCR1 = newPwm;
adcIn = 0;
dampingCnt = 0;
}
}
float getDutyCycle() {