Main Page | File List | Globals

main.c File Reference


Detailed Description

Atmel Corporation

Revision
1.3
Date
Friday, August 19, 2005 11:29:48 UTC

Definition in file main.c.

#include <inavr.h>
#include <ioavr.h>
#include "motor.h"
#include "tables.h"

Include dependency graph for main.c:

Go to the source code of this file.

Functions

void main (void)
 Program entry point.
__interrupt void Timer0_Overflow_ISR (void)
 Timer/Counter0 overflow interrupt.

Variables

__no_init __regvar volatile
unsigned char 
activePhase
 The currently active phase.
__no_init __regvar volatile
unsigned int 
comTick
 Commutation tick counter.
volatile unsigned int currentSpeedTick
 The number of ticks between the two last commutations.
__no_init __regvar volatile
unsigned char 
nextADMUX
 The ADMUX value to be used at next commutation.
__no_init __regvar volatile
unsigned char 
nextPhase
 The next phase to activate.
__no_init __regvar volatile
unsigned char 
nextTCCR0A
 The TCCR0A value to be used at next commutation.
__eeprom volatile unsigned int numWatchdogResets
 Number of Watchdog resets. Stored in EEPROM for health monitoring.
volatile unsigned char speedRef = 0
 Speed reference.
__no_init __regvar volatile
unsigned char 
speedRefUpdated
 Flag that tells main loop that speed reference is updated.


Function Documentation

void main void   ) 
 

Program entry point.

Main initializes all subsystems, starts the motor and goes into an eternal loop where the motor speed is controlled by adjusting the driving PWM frequency.

Definition at line 76 of file main.c.

References SET_DUTY_BOTH_PHASES.

00077 {
00078   unsigned char duty;
00079 
00080   CheckResetSource();
00081   WDTOff();
00082 
00083   // Initialize all subsystems
00084   InitPORTB();
00085   InitPWM();
00086   InitADC();
00087 
00088   // Run startup procedure
00089   StartMotor();
00090 
00091   InitWDT();
00092 
00093   __enable_interrupt();
00094 
00095   for(;;)
00096   {
00097     duty = CalculateDutyCycle(duty);
00098     SET_DUTY_BOTH_PHASES(duty);
00099 
00100     WaitMillisecs(1);
00101   }
00102 }

__interrupt void Timer0_Overflow_ISR void   ) 
 

Timer/Counter0 overflow interrupt.

This interrupt service routine is called every time Timer/Counter1 reaches 0. (Once every PWM cycle). Sensorless commutation and reading of external speed reference is performed here.

Definition at line 217 of file main.c.

References activePhase, ADC_PHASE1, ADC_PHASE2, ADC_SPEED_REF, ADC_THRESHOLD, ADC_WAIT_TICKS, comTick, currentSpeedTick, MAX_RPM_TICKS, nextADMUX, nextPhase, nextTCCR0A, PHASE1, PHASE2, PWM_OFF, PWM_PHASE1, PWM_PHASE2, speedRef, and speedRefUpdated.

00218 {
00219   unsigned char adcvalue;
00220   unsigned char tempADMUX;
00221 
00222   // Chance ADC channel to read analog reference once the zero-cross detection is done.
00223   tempADMUX = ADMUX;
00224   ADMUX = ADC_SPEED_REF;
00225 
00226   // Has the necessary time passed since last commutation?
00227   if (comTick >= ADC_WAIT_TICKS)
00228   {
00229     // Wait for AD conversion to complete
00230     while( (ADCSRA & (1 << ADSC)) )
00231     {
00232 
00233     }
00234 
00235     // Read ADC value
00236     adcvalue = ADCH;
00237 
00238     // Commutate if ADC value is lower than commutation threshold
00239     if (adcvalue < ADC_THRESHOLD)
00240     {
00241       TCCR0A = nextTCCR0A;
00242       tempADMUX = nextADMUX;
00243       activePhase = nextPhase;
00244 
00245       // Reset watchdog if the motor is not spinning suspicously fast.
00246       if (comTick > MAX_RPM_TICKS)
00247       {
00248         __watchdog_reset();
00249       }
00250       // Otherwise, turn off PWM outputs and wait for watchdog reset.
00251       else
00252       {
00253         TCCR0A = PWM_OFF;
00254         for(;;)
00255         {
00256           // Wait here until watchdog reset.
00257         }
00258       }
00259 
00260       currentSpeedTick = comTick;
00261       comTick = 0;
00262 
00263 
00264       // Prepare for next commutation and toggle tacho signal
00265       if (activePhase == PHASE1)
00266       {
00267         nextTCCR0A = PWM_PHASE2;
00268         nextADMUX = ADC_PHASE1;
00269         nextPhase = PHASE2;
00270 #ifdef TACHO_ENABLED
00271         DDRB |= (1 << TACHO_OUT_PIN);
00272 #endif
00273       }
00274       else
00275       {
00276         nextTCCR0A = PWM_PHASE1;
00277         nextADMUX = ADC_PHASE2;
00278         nextPhase = PHASE1;
00279 #ifdef TACHO_ENABLED
00280         DDRB &= ~(1 << TACHO_OUT_PIN);
00281 #endif
00282       }
00283     }
00284     // Start ADC conversion, analog speed reference.
00285     ADCSRA |= (1 << ADSC);
00286   }
00287   else
00288   {
00289     // Wait for first AD conversion
00290     while( (ADCSRA & (1 << ADSC)) )
00291     {
00292 
00293     }
00294     // Start ADC conversion, analog speed reference.
00295     ADCSRA |= (1 << ADSC);
00296   }
00297 
00298   // Wait for ADC reading (speed ref)
00299   while( (ADCSRA & (1<<ADSC)) )
00300   {
00301 
00302   }
00303   speedRef = ADCH;
00304   speedRefUpdated = 1;
00305 
00306   // Change ADC channel back for zero-cross detection.
00307   ADMUX = tempADMUX;
00308 
00309   comTick++;
00310 }


Variable Documentation

__no_init __regvar volatile unsigned char activePhase
 

The currently active phase.

Definition at line 50 of file main.c.

Referenced by Timer0_Overflow_ISR().

__no_init __regvar volatile unsigned int comTick
 

Commutation tick counter.

Definition at line 56 of file main.c.

Referenced by Timer0_Overflow_ISR().

volatile unsigned int currentSpeedTick
 

The number of ticks between the two last commutations.

Definition at line 65 of file main.c.

Referenced by Timer0_Overflow_ISR().

__no_init __regvar volatile unsigned char nextADMUX
 

The ADMUX value to be used at next commutation.

Definition at line 47 of file main.c.

Referenced by Timer0_Overflow_ISR().

__no_init __regvar volatile unsigned char nextPhase
 

The next phase to activate.

Definition at line 53 of file main.c.

Referenced by Timer0_Overflow_ISR().

__no_init __regvar volatile unsigned char nextTCCR0A
 

The TCCR0A value to be used at next commutation.

Definition at line 44 of file main.c.

Referenced by Timer0_Overflow_ISR().

__eeprom volatile unsigned int numWatchdogResets
 

Number of Watchdog resets. Stored in EEPROM for health monitoring.

Definition at line 68 of file main.c.

volatile unsigned char speedRef = 0
 

Speed reference.

Definition at line 62 of file main.c.

Referenced by Timer0_Overflow_ISR().

__no_init __regvar volatile unsigned char speedRefUpdated
 

Flag that tells main loop that speed reference is updated.

Definition at line 59 of file main.c.

Referenced by Timer0_Overflow_ISR().


Generated on Thu Sep 29 15:48:29 2005 for AVR440 - Sensorless Control of Two-Phase Brushless DC Motor by  doxygen 1.4.4