BLDC control on ATAVRMC303 with ATxMega128A1
main.c
Go to the documentation of this file.
1 
11 //_____ I N C L U D E S ___________________________________________________
12 #include "config.h"
13 
14 #include "mc_interface.h"
15 #include "mc_control.h"
16 #include "mc_drv.h"
17 #include "DB101_IF.h"
18 #include "adc_drv.h"
19 
20 #include "uart\uart_lib.h"
21 #include "ushell_task.h"
22 #include "clksys_driver.h"
23 
24 //_____ M A C R O S ________________________________________________________
25 
26 
27 //_____ D E F I N I T I O N S ______________________________________________
28 
29 //_____ D E C L A R A T I O N S ____________________________________________
31 U16 motor_speed = 0;
32 extern Bool g_tick; //see mc_drv.c Use for sampling time
33 
34 void clock_init(void)
35 {
36  /* Set system clock to 32MHz. */
37  CLKSYS_Enable(OSC_RC32MEN_bm);//CLKSYS_Enable(OSC_XOSCEN_bm);
38  do {} while ( CLKSYS_IsReady( OSC_RC32MRDY_bm ) == 0 );//do {} while ( CLKSYS_IsReady( OSC_XOSCRDY_bm ) == 0 );
39 
40  CLKSYS_PLL_Config( OSC_PLLSRC_RC32M_gc, 16 );//CLKSYS_PLL_Config( OSC_PLLSRC_XOSC_gc, 4 );
41  CLKSYS_Enable(OSC_PLLEN_bm);
42  CLKSYS_Prescalers_Config(CLK_PSADIV_1_gc, CLK_PSBCDIV_2_2_gc);
43  do {} while ( CLKSYS_IsReady( OSC_PLLRDY_bm ) == 0 );
44  CLKSYS_Main_ClockSource_Select( CLK_SCLKSEL_PLL_gc );
45 }
46 
47 
53 int main(void)
54 {
55 
56  clock_init();
57  // init motor
58  mc_init(); // launch initialization of the motor application
59 
60  //init_adc();
61 
62  // Initialyze the communication system for External Command through Uart
64 
65  // Start the motor
67  mci_run();
68 
69  PMIC.CTRL |= PMIC_LOLVLEX_bm;
70  __enable_interrupt();
71 
72 
73  while(1)
74  {
75 
76  // Launch regulation loop
77  // Timer 1 generate an IT (g_tick) all 250us
78  // Sampling period = n * 250us
79  if (g_tick == TRUE)
80  {
81  g_tick = FALSE;
84  if ( g_regulation_period >= 1000 ) //n * 250us = Te
85  {
87 
88  if (ushell_active == FALSE)
89  {
90  // Set User Speed Command with potentiometer
92  }
93  mc_regulation_loop(); // launch regulation loop
94 #ifdef DB101
95  UpdateScreen();
96 #endif
97  }
98  ushell_task();
99  mc_inrush_task(); /* manage the inrush current */
100  }
101 
102  // Show PSC state according to the Over Current information
103  //if(PCTL2 & (1<<PRUN2)) switch_OFF_LED();// PSC ON
104  //else switch_ON_LED();//PSC OFF => Over_Current
105  }
106 }
volatile Bool ushell_active
Definition: ushell_task.c:39
void ushell_task_init(void)
This function initializes the hardware/software ressources required for ushell task.
Definition: ushell_task.c:66
void mci_set_motor_speed(U8 speed)
Definition: mc_interface.c:108
void UpdateScreen(void)
Definition: DB101_IF.c:22
U16 g_regulation_period
Define the sampling period.
Definition: main.c:30
void CLKSYS_Prescalers_Config(CLK_PSADIV_t PSAfactor, CLK_PSBCDIV_t PSBCfactor)
This function changes the prescaler configuration.
Bool g_tick
Use for control the sampling period value.
Definition: mc_drv.c:39
#define CLKSYS_Enable(_oscSel)
This macro enables the selected oscillator.
Definition: clksys_driver.h:81
void clock_init(void)
Definition: main.c:34
void mc_init()
use to init programm
Definition: mc_interface.c:92
void mc_inrush_task(void)
the purpose of this function is to manage the delay used when the overcurrent detection is disabled...
Definition: mc_drv.c:431
void mci_run(void)
mci_run run the motor with parameter
Definition: mc_interface.c:34
void CLKSYS_PLL_Config(OSC_PLLSRC_t clockSource, uint8_t factor)
This function configures the internal high-frequency PLL.
uint8_t CLKSYS_Main_ClockSource_Select(CLK_SCLKSEL_t clockSource)
This function selects the main system clock source.
U16 motor_speed
User Speed Order.
Definition: main.c:31
void mc_regulation_loop()
launch speed control or no regulation
Definition: mc_control.c:159
void mc_ADC_Scheduler(void)
Launch the scheduler for the ADC.
Definition: mc_drv.c:388
XMEGA Clock System driver header file.
int main(void)
Main Entry.
Definition: main.c:53
#define CLKSYS_IsReady(_oscSel)
This macro check if selected oscillator is ready.
Definition: clksys_driver.h:94
U8 mc_get_potentiometer_value(void)
Get the potentiometer value.
Definition: mc_interface.c:199
void ushell_task(void)
Entry point of the explorer task management.
Definition: ushell_task.c:93