// SPI Config SpiaRegs.SPICCR.all =0x0047; // Reset on, (clk pol=1 : Falling edge), 8-bit char bits SpiaRegs.SPICTL.all =0x0006; // Enable master mode, phase (without delay), enable talk, and SPI int disabled. SpiaRegs.SPIBRR =19; // 150 MHz/4/(19+1)= 1.97 MHz SpiaRegs.SPICCR.all =0x00C7; // Relinquish SPI from Reset SpiaRegs.SPIPRI.bit.FREE = 1; // Set so breakpoints dont disturb xmission |
//---------------------------------------------------- // initialisation de l'écran LCD //---------------------------------------------------- inline void LCD_initScreen() { DispLCDOrder( 0xC0, 0x00); // foncé DelayNmSec(2); DispLCDOrder( 0x00, 0x80); // clear LCD, 1.53 ms DelayNmSec(2); DispLCDOrder( 0x00, 0x60); // increment, 1.53 ms DelayNmSec(2); DispLCDOrder( 0x00, 0x30); // display on, cursor Off DelayNmSec(1); //debug // DispLCDChar( 0x20, 0x80); // A // DelayNmSec(1); // DispLCDChar( 0x20, 0x40); // B // DelayNmSec(1); LCDupdate=1; // doit MAJ la table sur l Ecran LCD LCDcol=0; } |
//---------------------------------------------------- // Envoi 3 bytes pour Afficher : ordre requiert hi et low //---------------------------------------------------- inline void DispLCDChar(int hi, int low) { // debug : voir si on peut utiliser le fifo pour alleger le cpu LCD_CS=0; // Active le LCD via le /CS // SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 SpiaRegs.SPITXBUF=0xFA00; // Transmit start Char Order (8 bits left justified on 16 bits) while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé SpiaRegs.SPITXBUF=low<<8; // 8 bits left justified on 16 bits while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé SpiaRegs.SPITXBUF=hi<<8; // 8 bits left justified on 16 bits while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé LCD_CS=1; // Desacive le LCD via le /CS } //---------------------------------------------------- // Envoi 3 bytes pour Afficher : ordre requiert hi et low //---------------------------------------------------- inline void DispLCDOrder(int hi, int low) { // debug : voir si on peut utiliser le fifo pour alleger le cpu LCD_CS=0; // Active le LCD via le /CS // SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 SpiaRegs.SPITXBUF=0xF800; // Transmit start Ordre (8 bits left justified on 16 bits) while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé SpiaRegs.SPITXBUF=low<<8; // 8 bits left justified on 16 bits while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé SpiaRegs.SPITXBUF=hi<<8; // 8 bits left justified on 16 bits while ( ! SpiaRegs.SPISTS.bit.INT_FLAG ) {} SpiaRegs.SPIRXBUF=SpiaRegs.SPIRXBUF; // clear le SPISTS,BIT6 : donnée_envoyé LCD_CS=1; // Desacive le LCD via le /CS } |
//---------------------------------------------------- // Routine trChar : transforme le char LCDChar en LCDChar_m (pour le LCD) //---------------------------------------------------- inline void trChar() { Uint16 tmp; LCDChar_m=0x00; for (tmp=8; tmp>0; tmp--) { LCDChar_m=LCDChar_m<<1 | (LCDChar & 1); LCDChar=LCDChar>>1; } LCDlo=LCDChar_m & 0xF0; LCDhi=LCDChar_m<<4 & 0xF0; } |
//---------------------------------------------------- // Convertit un "Word" hexa en "4 chars Hexadécimaux" // et les sort sur la table en memoire //---------------------------------------------------- inline void ConvHexa(int Var, int LCDtablePos) { Uint16 tmp; tmp=Var & 0x000F; if (tmp<=9) tmp+=0x30; else tmp+=0x37; LCDtable[LCDtablePos+3]=tmp; tmp=Var>>4 & 0x000F; if (tmp<=9) tmp+=0x30; else tmp+=0x37; LCDtable[LCDtablePos+2]=tmp; tmp=Var>>8 & 0x000F; if (tmp<=9) tmp+=0x30; else tmp+=0x37; LCDtable[LCDtablePos+1]=tmp; tmp=Var>>12 & 0x000F; if (tmp<=9) tmp+=0x30; else tmp+=0x37; LCDtable[LCDtablePos]=tmp; } |
//---------------------------------------------------- // Routine d envoi de donnees sur SPI (pour LCD) // met à jour la LCDtable sur l écran //---------------------------------------------------- inline void AfficheLCDTable() { LCDChar=LCDtable[LCDcol]; trChar(); DispLCDChar( LCDhi, LCDlo); // sortir le Char actuel et inutile d attendre car routine ttes les 100 us if (++LCDcol>=32) { LCDcol=0; LCDupdate=2; // demande de remise à la 1er ligne du curseur } } |
// For this example, set HSPCLK to SYSCLKOUT / 6 (25Mhz assuming 150Mhz SYSCLKOUT) EALLOW; // SysCtrlRegs.HISPCP.all = 0x3; // HSPCLK = SYSCLKOUT/2*ADC_MODCLK2 = 150/(2*3) = 25MHz SysCtrlRegs.HISPCP.all = 0x1; // HSPCLK = SYSCLKOUT/2 = 75 MHz (System Reset) SysCtrlRegs.LOSPCP.all = 0x2; // LOPCLK = SYSCLKOUT/4 = 54 MHz (System Reset) // Enable PWM pins GpioMuxRegs.GPAMUX.all = 0x00FF; // EVA PWM 1-6 pins GpioMuxRegs.GPBMUX.all = 0x00FF; // EVB PWM 7-12 pins GpioMuxRegs.GPFMUX.all=0x000F; // Select GPIOs to be SPI pins GpioMuxRegs.GPFMUX.bit.XF_GPIOF14 = 1; // XF active GpioMuxRegs.GPGMUX.all = 0x0000; // LCD_CS GPIOG4 and Gyro_CS GPIOG5 GpioMuxRegs.GPGDIR.all = 0x0030; // CS are outpout EDIS; |
// Interrupts that are used in this example are re-mapped to // ISR functions found within this file. EALLOW; // This is needed to write to EALLOW protected register PieVectTable.ADCINT = &adc_isr; EDIS; // This is needed to disable write to EALLOW protected registers // Step 4. Initialize all the Device Peripherals: // This function is found in DSP281x_InitPeripherals.c // InitPeripherals(); // Not required for this example InitAdc(); // For this example, init the ADC init_ev(); init(); // init my variables // Configure ADC AdcRegs.ADCMAXCONV.all = 0x0005; // Setup 6 convs on SEQ1 AdcRegs.ADCCHSELSEQ1.bit.CONV00 = 0x0; // Setup ADCINA0 as 1st SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV01 = 0x1; // Setup ADCINA1 as 2nd SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV02 = 0x2; // Setup ADCINA2 as 3rd SEQ1 conv. AdcRegs.ADCCHSELSEQ1.bit.CONV03 = 0x3; // Setup ADCINA3 as 4th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV04 = 0x4; // Setup ADCINA4 as 5th SEQ1 conv. AdcRegs.ADCCHSELSEQ2.bit.CONV05 = 0x5; // Setup ADCINA5 as 6th SEQ1 conv. AdcRegs.ADCTRL2.bit.EVA_SOC_SEQ1 = 1; // Enable EVASOC to start SEQ1 AdcRegs.ADCTRL2.bit.INT_ENA_SEQ1 = 1; // Enable SEQ1 interrupt (every EOS) AdcRegs.ADCTRL3.bit.ADCCLKPS = 0x6; // ADC peripheral clk divider = /6 AdcRegs.ADCTRL1.bit.CPS = 0; // encore un ADC peripheral clk prescaler =/1 |
//-------------------------------------------------------------------------- void init_ev() { // Configure EVA // Assumes EVA Clock is already enabled in InitSysCtrl(); EvaRegs.T1PR = FullDuty; // Setup period register MLI à 16 kHz=62.5 us EvaRegs.T1CNT = 0x0000; // Timer1 counter EvaRegs.GPTCONA.bit.T1TOADC = 0x2; // Enable EVASOC in EVA on PRD match EvaRegs.T1CON.all = 0x8800; // prepare T1, disable (up & down count mode) // EVA Configure PWM1-PWM6 // Enable compare for PWM1-PWM6 EvaRegs.CMPR1 = HalfDuty; EvaRegs.CMPR2 = HalfDuty-300; EvaRegs.CMPR3 = 20; // Compare action control. Action that takes place on a cmpare event // output pin 1 CMPR1 - active low // output pin 2 CMPR1 - active high // output pin 3 CMPR2 - active low // output pin 4 CMPR2 - active high // output pin 5 CMPR3 - active low // output pin 6 CMPR3 - active high EvaRegs.ACTRA.all = 0x0999; EvaRegs.DBTCONA.all = 0x0000; // Disable deadband EvaRegs.COMCONA.all = 0xA600; // Configure EVB // Initialize EVB Timer3 // Timer3 controls T3PWM and PWM7-12 EvbRegs.T3PR = FullDuty; // Timer3 period EvbRegs.T3CNT = 0x0000; // Timer3 counter EvbRegs.T3CON.all = 0x8800; // prepare T3, disable (up & down count mode) // // Enable compare for PWM7-PWM12 EvbRegs.CMPR4 = HalfDuty; EvbRegs.CMPR5 = HalfDuty-300; EvbRegs.CMPR6 = 20; // Compare action control. Action that takes place // on a cmpare event // output pin 1 CMPR4 - active low // output pin 2 CMPR4 - active high // output pin 3 CMPR5 - active low // output pin 4 CMPR5 - active high // output pin 5 CMPR6 - active low // output pin 6 CMPR6 - active high EvbRegs.ACTRB.all = 0x0999; EvbRegs.DBTCONB.all = 0x0000; // Disable deadband EvbRegs.COMCONB.all = 0xA600; } //-------------------------------------------------------------------------- |
// Enable ADCINT in PIE PieCtrlRegs.PIEIER1.bit.INTx6 = 1; IER |= M_INT1; // Enable CPU Interrupt 1 EINT; // Enable Global interrupt INTM ERTM; // Enable Global realtime interrupt DBGM // start Timer1 and 3 EvaRegs.T1CON.bit.TENABLE = 1; // Enable timer 1 counting (up & down count mode) EvbRegs.T3CON.bit.TENABLE = 1; // Enable timer 3 counting (up & down count mode) // Wait for ADC interrupt while(1) { } |
//-------------------------------------------------------------------------- interrupt void adc_isr(void) { asm(" setc XF "); // Set XF for monitoring LED on Ref1= AdcRegs.ADCRESULT0 >>4; Ref2= AdcRegs.ADCRESULT1 >>4; Iar = AdcRegs.ADCRESULT2 >>4; Ibr = AdcRegs.ADCRESULT3 >>4; Ial = AdcRegs.ADCRESULT4 >>4; Ibl = AdcRegs.ADCRESULT5 >>4; // Reinitialize for next ADC sequence AdcRegs.ADCTRL2.bit.RST_SEQ1 = 1; // Reset SEQ1 AdcRegs.ADCST.bit.INT_SEQ1_CLR = 1; // Clear INT SEQ1 bit PieCtrlRegs.PIEACK.all = PIEACK_GROUP1; // Acknowledge interrupt to PIE // change rapport cyclique // Moteur Right EvaRegs.CMPR1 = Ref2; EvaRegs.CMPR2 = Ref2; EvaRegs.CMPR3 = Ref2; // Moteur Left EvbRegs.CMPR4 = FullDuty-MyAlpha; EvbRegs.CMPR5 = FullDuty-MyAlpha; EvbRegs.CMPR6 = FullDuty-MyAlpha; // mise à jour d une phrase // sprintf(Str_Ialim,"-none-"); switch (LCDupdate) { case 1 : AfficheLCDTable(); break; case 2 : LCDupdate=0; DispLCDOrder( 0x00, 0x40); //Retour curseur à l origine break; default: // si zero alors demande la MAJ des I1 et I2 // execute toutes les (1000+32+1)*100 us if (--SeqComm>=0) break; SeqComm=SeqCommMax; LCDupdate=1; // Cursor home ConvHexa(Ref1, offsRef1); // Ref1 en Hexa ConvHexa(Ref2, offsRef2); // Ref1 en Hexa // LCDtable[offsH+2]=HallA+0x30; // LCDtable[offsH+1]=HallB+0x30; // LCDtable[offsH ]=HallC+0x30; // LCDtable[offsS]=Secteur+0x30; LCDtable[offsI ]=Str_Ialim[0]; LCDtable[offsI+1]=Str_Ialim[1]; LCDtable[offsI+2]=Str_Ialim[2]; LCDtable[offsI+3]=Str_Ialim[3]; LCDtable[offsI+4]=Str_Ialim[4]; LCDtable[offsI+5]=Str_Ialim[5]; } // fin eteint la diode et sort asm(" clrc XF "); // Clear XF for monitoring LED off } |