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

View File

@ -2,6 +2,8 @@
#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_I = 5.0;

View File

@ -11,7 +11,6 @@
#include "pwm.h"
#include "control.h"
#include "params.h"
#include "adc.h"
@ -24,84 +23,64 @@ volatile uint16_t newPwm = 0;
void pwmInit() {
P1DIR |= BIT6;
P1SEL |= BIT6;
P1OUT = 0;
ADC10CTL0 = ADC10SHT_0 | ADC10ON | SREF_0 | ENC | ADC10IE;
ADC10CTL1 = INCH_7 | ADC10SSEL_3 | ADC10DIV_2 | SHS_1;
ADC10AE0 = BIT7;
TACCR0 = PWM_MAX;
TACCR1 = 128;
TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
P1DIR |= BIT6;
P1SEL |= BIT6;
P1OUT = 0;
TACCR0 = PWM_MAX;
TACCR1 = 0;
// TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
}
ISR(TIMER0_A0, TA0_ISR) {
static uint8_t cycleCnt = 0;
cycleCnt++;
ISR(ADC10, PWM_ISR) {
uint16_t adcIn = ADC10MEM;
if (cycleCnt >= CYCLE_DELAY) {
cycleCnt = 0;
float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom;
float newPwm_f = ctrl.cycle(u_des, u_curr);
newPwm = (uint16_t) newPwm_f;
uint16_t adcIn = adcGet();
float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom;
float newPwm_f = ctrl.cycle(u_des, u_curr);
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() {
__disable_interrupt();
uint16_t my_newPwm = newPwm;
__enable_interrupt();
float dutyCycle = ((float)my_newPwm) / ((float)PWM_MAX) * 100.0;
return dutyCycle;
__disable_interrupt();
uint16_t my_newPwm = newPwm;
__enable_interrupt();
float dutyCycle = ((float)my_newPwm) / ((float)PWM_MAX) * 100.0;
return dutyCycle;
}
float getUCur() {
__disable_interrupt();
float my_u_curr = u_curr;
__enable_interrupt();
return my_u_curr;
__disable_interrupt();
float my_u_curr = u_curr;
__enable_interrupt();
return my_u_curr;
}
float getUDes() {
__disable_interrupt();
float my_u_des = u_des;
__enable_interrupt();
return my_u_des;
__disable_interrupt();
float my_u_des = u_des;
__enable_interrupt();
return my_u_des;
}
void setUDes(float uDes) {
if (uDes < 0) {
uDes= 0;
}
__disable_interrupt();
u_des = uDes;
__enable_interrupt();
if (uDes < 0) {
uDes= 0;
}
__disable_interrupt();
u_des = uDes;
__enable_interrupt();
}