kind of working

This commit is contained in:
hg 2015-02-21 23:13:59 +01:00
parent 43e1c5c384
commit 9cd6d87704
4 changed files with 62 additions and 46 deletions

View File

@ -14,9 +14,9 @@
void adcInit() {
ADC10CTL0 = ADC10SHT_1 | ADC10ON | SREF_0;
ADC10CTL1 = INCH_5;
ADC10AE0 = BIT5;
ADC10CTL0 = ADC10SHT_0 | ADC10ON | SREF_0;
ADC10CTL1 = INCH_7 | ADC10SSEL_3;
ADC10AE0 = BIT7;
}
uint16_t adcGet() {

View File

@ -25,6 +25,8 @@ void init() {
BCSCTL1 = XT2OFF | RSEL0 | RSEL1 | RSEL2 | RSEL3;
BCSCTL2 = 0;
BCSCTL3 = 0;
P1DIR |= BIT0 | BIT1 | BIT2 | BIT3;
}
int main() {

View File

@ -17,77 +17,91 @@
volatile Control ctrl((float)PWM_MIN, (float)PWM_MAX, Ctrl_P, Ctrl_I, Ctrl_D);
volatile float u_des = 0;
volatile float u_des = 12.0;
volatile float u_curr = 0;
volatile uint16_t newPwm = 0;
void pwmInit() {
P1DIR |= BIT6;
P1SEL |= BIT6;
P1OUT = 0;
P1DIR |= BIT6;
P1SEL |= BIT6;
P1OUT = 0;
TACCR0 = PWM_MAX;
TACCR1 = 8;
TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
TACCR0 = PWM_MAX;
TACCR1 = 128;
TACCTL0 = CCIE;
TACCTL1 = OUTMOD_7;
TACTL = MC_1 | ID_0 | TASSEL_2 | TACLR;
}
void pwmSet(uint16_t v) {
TACCR1 = v;
}
ISR(TIMER0_A0, TA0_ISR) {
static uint8_t cycleCnt = 0;
cycleCnt++;
static uint8_t cycleCnt = 0;
cycleCnt++;
if (cycleCnt >= CYCLE_DELAY) {
cycleCnt = 0;
if (cycleCnt >= CYCLE_DELAY) {
cycleCnt = 0;
uint16_t adcIn = adcGet();
float u_adc = ((float)adcIn) * U_ref / ((float)ADC_MAX);
u_curr = u_adc * (R_top + R_bottom) / R_bottom;
uint16_t adcIn = adcGet();
float newPwm_f = ctrl.cycle(u_des, u_curr);
newPwm = (uint16_t) newPwm_f;
pwmSet(newPwm);
}
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;
}
}
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();
}

View File

@ -12,7 +12,7 @@
const uint16_t PWM_MIN = 0;
const uint16_t PWM_MAX = 511;
const uint16_t CYCLE_DELAY = 2;
const uint16_t CYCLE_DELAY = 1;
void pwmInit();