Advertisement
Wistaro

Truth Machine en embed C

Jun 6th, 2021
955
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.08 KB | None | 0 0
  1. /*
  2.  * 06/06/2021
  3.  *******************
  4.  * Auteur: Wistaro *
  5.  * Wistaro#9487    *
  6.  *******************
  7.  *
  8.  * Implémentation d'un "Truth Machine" en c sur embarqué
  9.  *
  10.  * Principe:
  11.  * - Si l'utilisateur rentre "0", alors le programme affiche "0" sur la sortie (ici, la liaison série)
  12.  * - Si l'utilisateur rentre "1", alors le programme affiche des "1" de manière infinie
  13.  *
  14.  * Contraintes:
  15.  * - Ne PAS UTILISER de boucles "For", "While", "Goto" et "Do-While" pour le traitement
  16.  * - Ne PAS UTILISER des conditions "If", "Switch-Case" et ternaires
  17.  *
  18.  * Board: Texas Instruments MSP432P401R
  19.  * Compiler: TI v20.2.5 LTS
  20.  * IDE: Code Composer Studio v10.3.1
  21.  *
  22.  * Démonstration en vidéo: https://www.youtube.com/watch?v=Onrtpy0hs_4
  23.  */
  24.  
  25. #include <ti/devices/msp432p4xx/driverlib/driverlib.h>
  26.  
  27. void sendData(char data);
  28. void sendIntro(void);
  29. char isOneReceived(char data);
  30. char isZeroReceived(char rData);
  31.  
  32. #define GET_COUNT(T) T*(CS_12MHZ / 16) //calcul pour déterminer le nombre de comptages pour 1s
  33. #define TIME_COUNT GET_COUNT(0.1) //1 dixieme de seconde
  34.  
  35. #define OFFSET_ASCII 0x30
  36. #define TIMER_NOT_COUNTING -1
  37.  
  38. char isZero;
  39. char isOne;
  40.  
  41. const eUSCI_UART_ConfigV1 uartConfig =
  42. {
  43.         EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source (128kHz pour la clock de référence)
  44.         78,                                     // BRDIV = 78
  45.         2,                                       // UCxBRF = 2
  46.         0,                                       // UCxBRS = 0
  47.         EUSCI_A_UART_NO_PARITY,                  // No Parity
  48.         EUSCI_A_UART_LSB_FIRST,                  // LSB First
  49.         EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
  50.         EUSCI_A_UART_MODE,                       // UART mode
  51.         EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION,  // Oversampling
  52.         EUSCI_A_UART_8_BIT_LEN                  // 8 bits de donnée
  53. };
  54.  
  55. int main(void){
  56.  
  57.     MAP_WDT_A_holdTimer(); //empêche le watchdog de continuer
  58.     CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12); //sélection de la clock à 12Mhz
  59.  
  60.     //Configure une led pour le debug
  61.     P1->DIR |= BIT0;
  62.     P1->OUT &= ~BIT0;
  63.  
  64.     /*Configure le timer1 sur 32 bits, avec un prescaler de 16, en mode périodique, et basé sur l'horloge MCLK*/
  65.     MAP_Timer32_initModule(TIMER32_BASE, TIMER32_PRESCALER_16, TIMER32_32BIT, TIMER32_PERIODIC_MODE);
  66.     MAP_Interrupt_enableInterrupt(INT_T32_INT1); //active les interruptions sur le timer
  67.  
  68.     TIMER32_1->LOAD= TIMER_NOT_COUNTING; //désactive compteur au démarrage
  69.  
  70.     MAP_Timer32_startTimer(TIMER32_BASE, true);
  71.  
  72.     /*Configure l'UART pour la liaison série*/
  73.     MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1, GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
  74.  
  75.     MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig); //initialise l'uART
  76.     MAP_UART_enableModule(EUSCI_A0_BASE); //active le module UART
  77.     MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
  78.     MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
  79.  
  80.     MAP_Interrupt_enableMaster(); //active les interruptions générales
  81.  
  82.     sendIntro();
  83.  
  84.     isZero = 0;
  85.     isOne = 0;
  86.  
  87.     while(1) MAP_PCM_gotoLPM0(); //empêche la mise en veille du MCU
  88.  
  89. }
  90.  
  91.  
  92. /* Fonction d'interruption de l'UART de reception */
  93. void EUSCIA0_IRQHandler(void)
  94. {
  95.     uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
  96.  
  97.     while(!(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)); //attente du flag de transmission
  98.  
  99.     isZero = isZeroReceived((char)MAP_UART_receiveData(EUSCI_A0_BASE));
  100.     isOne = isOneReceived((char)MAP_UART_receiveData(EUSCI_A0_BASE));
  101.  
  102.     TIMER32_1->LOAD= TIMER_NOT_COUNTING + (isZero | isOne);
  103. }
  104.  
  105. /* Envoi un octet sur la liaison série */
  106. void sendData(char data){
  107.  
  108.     uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);
  109.  
  110.     while((status & EUSCI_A_UART_TRANSMIT_INTERRUPT_FLAG)); //attente du flag de transmission
  111.  
  112.     MAP_UART_transmitData(EUSCI_A0_BASE, data);
  113.  
  114. }
  115.  
  116. /* Envoi "?>" sur la liaison série */
  117. void sendIntro(void){
  118.     sendData('\n');
  119.     sendData('\n');
  120.     sendData('?');
  121.     sendData('>');
  122.     sendData(' ');
  123. }
  124.  
  125. /* Fonction d'interruption du TIMER1 */
  126. void T32_INT1_IRQHandler(void)
  127. {
  128.     MAP_Timer32_clearInterruptFlag(TIMER32_BASE);
  129.     MAP_GPIO_toggleOutputOnPin(GPIO_PORT_P1, GPIO_PIN0);
  130.  
  131.     TIMER32_1->LOAD= TIMER_NOT_COUNTING + isOne*TIME_COUNT;
  132.  
  133.     sendData((1-isZero)+OFFSET_ASCII); //envoi "0" ou "1" suivant ce qu'à saisi l'utilisateur
  134. }
  135.  
  136. /*
  137.  * Input: valeur ascii lue depuis l'uart
  138.  * Ouput: 0x01 si l'entrée est '1', 0x0 sinon
  139.  *
  140.  * */
  141. char isOneReceived(char rData){
  142.  
  143.     return (char)(((rData&(1<<5))>>5) + ((rData&(1<<4))>>4) + ((rData&(1<<0))>>0) + (1-((rData&(1<<1))>>1)) + (1-((rData&(1<<2))>>2)) + (1-((rData&(1<<3))>>3)) )/6;
  144. }
  145.  
  146. /*
  147.  * Input: valeur ascii lue depuis l'uart
  148.  * Ouput: 0x01 si l'entrée est '0', 0x0 sinon
  149.  *
  150.  * */
  151. char isZeroReceived(char rData){
  152.  
  153.     return (char)( (1-((rData&(1<<0))>>0)) +  (1-((rData&(1<<1))>>1)) + (1-((rData&(1<<2))>>2)) + (1-((rData&(1<<3))>>3)) + ((rData&(1<<4))>>4) + ((rData&(1<<5))>>5)  )/6;
  154. }
  155.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement