adc by interrupt

This commit is contained in:
hg 2015-02-21 23:32:50 +01:00
parent fd4cfa05bd
commit 56ba5ed343
6 changed files with 52 additions and 126 deletions

View File

@ -1,43 +0,0 @@
/*
* adc.cpp
*
* Created on: 03.10.2014
* Author: wn
*/
#include <msp430g2553.h>
#include <isr_compat.h>
#include <stdint.h>
#include "adc.h"
void adcInit() {
ADC10CTL0 = ADC10SHT_0 | ADC10ON | SREF_0;
ADC10CTL1 = INCH_7 | ADC10SSEL_3 | ADC10DIV_2;
ADC10AE0 = BIT7;
}
uint16_t adcGet() {
uint16_t res = 0xffff;
ADC10CTL0 |= ENC | ADC10SC;
uint32_t timeOut = ADC_TIME_OUT;
while (1) {
timeOut--;
if (timeOut == 0)
break;
if ((ADC10CTL0 & ADC10IFG) != 0) {
res = ADC10MEM;
ADC10CTL0 &= ~(ADC10IFG | ENC);
break;
}
}
return res;
}

View File

@ -1,22 +0,0 @@
/*
* adc.h
*
* Created on: 03.10.2014
* Author: wn
*/
#ifndef ADC_H_
#define ADC_H_
#include <stdint.h>
const uint32_t ADC_TIME_OUT = 100000;
const uint16_t ADC_MAX = 1023;
void adcInit();
uint16_t adcGet();
#endif /* ADC_H_ */

12
src/debug.txt Normal file
View File

@ -0,0 +1,12 @@
// P1OUT |= BIT1;
// for (uint8_t i = 0; i < 16; i++) {
// P1OUT |= BIT3;
// uint16_t x = newPwm >> i;
// if ((x & 1) == 1) {
// P1OUT |= BIT2;
// } else {
// P1OUT &= ~BIT2;
// }
// P1OUT &= ~BIT3;
// }
// P1OUT &= ~BIT1;

View File

@ -11,7 +11,6 @@
#include <stdint.h> #include <stdint.h>
#include "pwm.h" #include "pwm.h"
#include "adc.h"
@ -31,7 +30,6 @@ void init() {
int main() { int main() {
init(); init();
adcInit();
pwmInit(); pwmInit();
__enable_interrupt(); __enable_interrupt();

View File

@ -2,6 +2,8 @@
#define PARAMS_H_ #define PARAMS_H_
const uint32_t ADC_TIME_OUT = 100000;
const uint16_t ADC_MAX = 1023;
const float Ctrl_P = 10.0; const float Ctrl_P = 10.0;
const float Ctrl_I = 5.0; const float Ctrl_I = 5.0;

View File

@ -11,7 +11,6 @@
#include "pwm.h" #include "pwm.h"
#include "control.h" #include "control.h"
#include "params.h" #include "params.h"
#include "adc.h"
@ -24,29 +23,25 @@ volatile uint16_t newPwm = 0;
void pwmInit() { void pwmInit() {
ADC10CTL0 = ADC10SHT_0 | ADC10ON | SREF_0 | ENC | ADC10IE;
ADC10CTL1 = INCH_7 | ADC10SSEL_3 | ADC10DIV_2 | SHS_1;
ADC10AE0 = BIT7;
P1DIR |= BIT6; P1DIR |= BIT6;
P1SEL |= BIT6; P1SEL |= BIT6;
P1OUT = 0; P1OUT = 0;
TACCR0 = PWM_MAX; TACCR0 = PWM_MAX;
TACCR1 = 128; TACCR1 = 0;
TACCTL0 = CCIE; // TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7; TACCTL1 = OUTMOD_7;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR; TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
} }
ISR(TIMER0_A0, TA0_ISR) { ISR(ADC10, PWM_ISR) {
static uint8_t cycleCnt = 0; uint16_t adcIn = ADC10MEM;
cycleCnt++;
if (cycleCnt >= CYCLE_DELAY) {
cycleCnt = 0;
uint16_t adcIn = adcGet();
float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX); float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom; u_curr = u_adc * (R_top + R_bottom) / R_bottom;
@ -54,23 +49,7 @@ ISR(TIMER0_A0, TA0_ISR) {
float newPwm_f = ctrl.cycle(u_des, u_curr); float newPwm_f = ctrl.cycle(u_des, u_curr);
newPwm = (uint16_t) newPwm_f; newPwm = (uint16_t) newPwm_f;
// P1OUT |= BIT1;
// for (uint8_t i = 0; i < 16; i++) {
// P1OUT |= BIT3;
// uint16_t x = newPwm >> i;
// if ((x & 1) == 1) {
// P1OUT |= BIT2;
// } else {
// P1OUT &= ~BIT2;
// }
// P1OUT &= ~BIT3;
// }
// P1OUT &= ~BIT1;
TACCR1 = newPwm; TACCR1 = newPwm;
}
} }
float getDutyCycle() { float getDutyCycle() {