diff --git a/src/adc.cpp b/src/adc.cpp deleted file mode 100644 index 3b6cfb5..0000000 --- a/src/adc.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/* - * adc.cpp - * - * Created on: 03.10.2014 - * Author: wn - */ - -#include -#include -#include - -#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; -} - diff --git a/src/adc.h b/src/adc.h deleted file mode 100644 index 1613fd8..0000000 --- a/src/adc.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * adc.h - * - * Created on: 03.10.2014 - * Author: wn - */ - -#ifndef ADC_H_ -#define ADC_H_ - -#include - - -const uint32_t ADC_TIME_OUT = 100000; -const uint16_t ADC_MAX = 1023; - -void adcInit(); -uint16_t adcGet(); - - - -#endif /* ADC_H_ */ diff --git a/src/debug.txt b/src/debug.txt new file mode 100644 index 0000000..1014473 --- /dev/null +++ b/src/debug.txt @@ -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; diff --git a/src/main.cpp b/src/main.cpp index 5906158..5847c4c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,6 @@ #include #include "pwm.h" -#include "adc.h" @@ -31,7 +30,6 @@ void init() { int main() { init(); - adcInit(); pwmInit(); __enable_interrupt(); diff --git a/src/params.h b/src/params.h index 7d26602..eb333db 100644 --- a/src/params.h +++ b/src/params.h @@ -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; diff --git a/src/pwm.cpp b/src/pwm.cpp index 4fedfa3..21f6e14 100644 --- a/src/pwm.cpp +++ b/src/pwm.cpp @@ -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(); }