Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.20 KB | None | 0 0
  1. #include <mdr32f8_port.h>
  2. #include <mdr32f8_clkctrl.h>
  3. #include <mdr32f8_can.h>
  4. #include "spec.h"
  5. #include "Setup_func.h"
  6. #include "blinky.h"
  7. #include "Can_Receive.h"
  8. #include "mdr32f8_tim.h"
  9. //#pragma import(__use_no_heap_region)
  10.  
  11. /**
  12.   ******************************************************************************
  13.   * @file    main.c
  14.   * @author  Applied IR spectroscopy laboratory
  15.   * @version V1.0.0
  16.   * @date    30/10/2018
  17.   * @brief   This is demonstration of CAN packets transmittion
  18.   ******************************************************************************
  19.   * <br><br>
  20.   *
  21.   *
  22.   * <h2><center>&copy; COPYRIGHT 2018 MIPT</center></h2>
  23.   */
  24.  
  25. PORT_InitTypeDef PORT_InitStructure;
  26. CAN_InitTypeDef  CAN_InitStructure;
  27. CAN_TxMsgTypeDef TxMsg;
  28. CAN_RxMsgTypeDef RxMsg;
  29. CAN_DataTypeDef RecBuf;
  30. void INT_TMR0_Handler(void);
  31. void Decode_Message(void);
  32. void Erase_Packets(void);
  33. void Initialise(void);
  34. void Line_Aquisition(void);
  35. void Temp_Stab(void);
  36. void Line_Stab(float, float);
  37. float Line_Center_Search(void);
  38. void Re_Decode_Message(void);
  39. void Set_Timer(int);
  40. int Status_Check(void);
  41. void Set_PI_Temp(int);
  42. void Set_PI_Line(int);
  43.  
  44. volatile uint32_t Debug_message_1, Debug_message_2;
  45. volatile uint32_t DEBG;
  46. volatile uint32_t Write_incr, Enable_to_Write=0;
  47. volatile uint16_t Data_Samples[1024];
  48.  
  49. volatile uint8_t CAN_DLC,CAN_IDE,CAN_PRIOR_0;
  50. volatile uint32_t CAN_ID ; // for CAN
  51.  
  52. volatile uint16_t ISS[32]; //сообщение от ИСС
  53. volatile uint16_t MAI_0[10][1024]; // сообщение от МАИ
  54. volatile uint32_t Receiving_A2 = 0, Temp_Mes[2];
  55.  
  56. /*Переменные для работы с сообщением от ИСС*/
  57. volatile uint32_t Device_Identifier, EP_MAI_Mode, Scan_Mode, Delta_Current, Control_Byte;
  58. volatile uint64_t LM1_Parameters, LM2_Parameters;  
  59. volatile uint8_t Search, UKS_Enable, SRAM_On, PhD_Gain, N, Delt_I1, Delt_I2, MD1, MD2,\
  60. AC_Laser_Current_LM1,DC_Laser_Current_LM1, DC_Laser_Current_LM2, AC_Laser_Current_LM2, Start;
  61. volatile uint16_t Temperature_1_LM1, Temperature_2_LM1, Temperature_1_LM2, Temperature_2_LM2;
  62.  
  63. volatile uint8_t Enable_Heater, Enable_Line_Stab, Current_Work_State;
  64. volatile uint8_t A2_increment = 0;
  65. /*Переменные для отправки к ИСС*/
  66. volatile uint16_t Tref, TC_dT, Tbase, Planned_Temperature;
  67. volatile uint8_t LM_module_number, LM_module_number_mode, TEC_Error, TP;
  68. volatile uint16_t Tdet, Tdet_B;
  69. volatile uint8_t Start_EP_MAI, PreAmps, Initial_DC_Laser_Current, Initial_AC_Laser_Current;
  70. volatile uint8_t    Step_Counter, Cycle_Counter;
  71. volatile uint16_t TU_Tb, TU_Tt, TU_Tc, TC_DAC;
  72. volatile uint8_t Command_Error, Ack_0, Data_end, Saturation, Hardware_Error, LM_Error, UKS_Error;
  73.  
  74. volatile uint8_t N_converted, N_converted_current; 
  75. volatile uint8_t Sending_process = 0, Experiment_Status = 0, TIMER_Counter = 0, Experiment_Init = 0;
  76. volatile uint32_t  Number_of_packets_send = 0;
  77.    
  78. volatile uint8_t Experiment_Re_Init = 0, Enable_Experiment = 0, Timer_Handler_Occured = 0, Enable_Temp_Stab = 0;
  79. volatile uint8_t Enable_Line_Stab = 0, Enable_Data_Aquisition = 0, Enable_Data_Averaging = 0;
  80. volatile uint8_t Cyclogram_Laser[4], Cyclogram_Temp[4];
  81. volatile float Line_Center = 0;
  82. volatile uint32_t Cyclogram_Counter=0, Timer_Counter = 0, Data_Packets_Counter = 0, Single_Frame_Counter = 1, Line_Center_required = 0;
  83.  
  84.  
  85. int main(void)
  86. {
  87.     //POR_disable(); //POR - power on reset
  88.     Cache_Setup();
  89.     Clock_Setup_8();
  90.     Can_Port_Setup();  
  91.     Can_Init_80();
  92.     Can_Setup();
  93.     Timer_Setup();
  94.     IRQ_Init();
  95.     Erase_Packets();
  96.     Initialise();
  97.     int i, j;
  98.    
  99.     while (1){
  100.         if(Experiment_Init == 1){
  101.             Experiment_Init = 0;
  102.             Cyclogram_Counter = 0;
  103.             Erase_Packets();
  104.             Initialise();
  105.             Decode_Message();
  106.             while(Cyclogram_Counter < 4){  
  107.                 if (Status_Check() == 1){
  108.                         break;
  109.                 }
  110.                 Erase_Packets();
  111.                 Initialise();
  112.                 Set_Timer(Cyclogram_Counter);
  113.                 Set_PI_Temp(Cyclogram_Counter);
  114.                 Set_PI_Line(Cyclogram_Counter);
  115.                 Enable_Experiment = 1;
  116.                 while(Enable_Experiment == 1){
  117.                     if (Status_Check() == 1){
  118.                         break;
  119.                     }
  120.                     Line_Aquisition();
  121.                     Line_Center = Line_Center_Search();
  122.                     if(Enable_Temp_Stab == 1){
  123.                         Temp_Stab();
  124.                     }
  125.                     else if(Enable_Line_Stab == 1){
  126.                         Line_Stab(Line_Center, Line_Center_required);
  127.                     }
  128.                     if((Enable_Data_Aquisition == 1)&&(Single_Frame_Counter < 512)){ // Data accumulation
  129.                         Single_Frame_Counter++;
  130.                         for(j = 0; j < 1024; j++){
  131.                             Data_Samples[j] += j;
  132.                         }
  133.                     }
  134.                 }
  135.             }      
  136.         }
  137.     }
  138. }
  139.  
  140. void INT_TMR0_Handler(){
  141.         Timer_Handler_Occured = 1;
  142.         Timer_Counter += 1;
  143.        
  144.         if(Timer_Counter < 3){
  145.             Enable_Temp_Stab = 1;
  146.             Enable_Line_Stab = 0;
  147.             Enable_Data_Aquisition = 0;
  148.         }
  149.        
  150.         else if(Timer_Counter == 3){
  151.             Enable_Temp_Stab = 0;
  152.             Enable_Line_Stab = 1;
  153.             Enable_Data_Aquisition = 0;
  154.         }
  155.         else if(Timer_Counter == 6){
  156.             Enable_Temp_Stab = 0;
  157.             Enable_Line_Stab = 1;
  158.             Enable_Data_Aquisition = 1;
  159.             Single_Frame_Counter = 1;
  160.             Data_Packets_Counter = 0;
  161.         }
  162.         else if((Timer_Counter > 6) && (Timer_Counter < 16)){
  163.             if(Data_Packets_Counter < 9){
  164.                 int i;
  165.                 for(i = 0; i < 1024; i++){
  166.                     MAI_0[Data_Packets_Counter][i] = Data_Samples[i]/Single_Frame_Counter;
  167.                     Data_Samples[i] = 0;
  168.                 }
  169.                 Single_Frame_Counter = 1;
  170.                 Data_Packets_Counter++;
  171.             }
  172.         }
  173.         else if(Timer_Counter >= 16){
  174.             Enable_Temp_Stab = 1;
  175.             Enable_Line_Stab = 0;
  176.             Enable_Data_Aquisition = 0;
  177.             Data_Packets_Counter = 0;
  178.             //Enable_Experiment = 0;
  179.             MDR_TMR0->CNTRL = 0;
  180.             if(Cyclogram_Counter >= 4){
  181.        
  182.             }
  183.         }
  184.         MDR_TMR0->CNT = 0x00000000;
  185.         TIMER_ClearFlag(MDR_TMR0, TIMER_STATUS_CNT_ARR);
  186. }
  187.  
  188. void Erase_Packets(){
  189.     int i, j;
  190.     for(i = 0; i < 1024; i++){
  191.             Data_Samples[i] = 0;
  192.     }
  193.     for(j = 0; j < 10; j++){
  194.         for(i = 0; i < 1024; i++){
  195.             MAI_0[j][i] = 0;
  196.         }
  197.     }
  198. }
  199.  
  200. void Decode_Message(){
  201.     int i,j;
  202.     for(j = 0; j < 10; j++){
  203.         for(i = 0; i < 32; i++){
  204.             MAI_0[j][i] = ISS[i];
  205.         }
  206.         MAI_0[j][1023] = j;
  207.     }
  208.     for(i = 0; i < 4; i++){
  209.         Cyclogram_Laser[i] = 0;
  210.         Cyclogram_Temp[i] = 0;
  211.     }
  212.     /*fill up cyclogram*/
  213.     for(i = 0; i < 4; i++){
  214.         Cyclogram_Laser[i] = 1;
  215.         Cyclogram_Temp[i] = 1;
  216.     }
  217.     /*decide from UKS bits*/
  218.     Line_Center_required = 850;
  219. }
  220.  
  221. void Set_Timer(int i){
  222.     MDR_TMR0->CNTRL = 1; // enable timer
  223. }
  224.  
  225. void Initialise(){
  226.     Experiment_Re_Init = 0;
  227.     Experiment_Init = 0;
  228.     Number_of_packets_send = 0;
  229.     Timer_Counter = 0;
  230.     Data_Packets_Counter = 0;
  231.     Single_Frame_Counter = 1;
  232.     Enable_Experiment = 0;
  233.     Enable_Temp_Stab = 0;
  234.     Enable_Line_Stab = 0;
  235.     Enable_Data_Aquisition = 0;
  236.     Enable_Data_Averaging = 0;
  237.     Enable_to_Write = 0;
  238. }
  239. int Status_Check(){
  240.     if(Experiment_Init == 1){
  241.         return 1;
  242.     }
  243.     else if(Experiment_Re_Init == 1){
  244.         Experiment_Re_Init = 0;
  245.         if(Cyclogram_Counter < 4){
  246.             Cyclogram_Counter++;
  247.         }
  248.         return 1;
  249.     }  
  250.     else{
  251.         return 0;
  252.     }
  253. }
  254. void Line_Aquisition(){
  255.  
  256. }
  257. void Temp_Stab(){
  258.  
  259. }
  260. void Line_Stab(float Line_Center, float Line_Center_required){
  261.  
  262. }
  263. void Set_PI_Temp(int i){
  264.  
  265. }
  266. void Set_PI_Line(int i){
  267.    
  268. }
  269. float Line_Center_Search(){
  270.     return 0;
  271. }
  272. void HardFault_Handler(){
  273.     Enable_to_Write = 0;
  274. }
  275. void MemoryMngFault_Handler(){
  276.  
  277. }
  278. void BusFault_Handler(){
  279.    
  280. }
  281. void UsageFault_Handler(){
  282.  
  283. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement