Basic template program for Freescale XS128

Publisher:HarmonyJoyLatest update time:2021-10-19 Source: eefocusKeywords:Freescale Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

/************************************************** 

***************************************************/ 

#include /* common defines and macros */

#include "derivative.h" /* derivative-specific definitions */

#include /* derivative information */

 

//1M=1006080

 

//40MHz BUS_CLOCK

/*

#define SynrDefine 0x53

#define RefdvDefine 0x07 //Clock frequency equals 2*crystal frequency*(SYNR0+1)/(REFDV0+1)

#define SciDefine 0x106 //Serial bus frequency = clock frequency / (16*SCI0BDL)

*/

//----------------------------------------------------- 

 

//80MHz BUS_CLOCK

#define SynrDefine 0xD3

#define RefdvDefine 0x03 //Clock frequency equals 2*crystal frequency*(SYNR0+1)/(REFDV0+1)

#define SciDefine 0x20c //Serial bus frequency = clock frequency / (16*SCI0BDL)

#define WAITTIME 100000 //Start delay time

#define SpeedInterrupt 1000 //Speed ​​cycle sampling time, timing 1000 * 0.01ms = 10ms  

#define AdInterrupt 100 //Ad period sampling time, timing 100 * 0.01ms = 1ms 

int SpeedNow=0; //Speed ​​sampling initialization

int sensor_ad[5]=0; //i=0~4, AD data sampling result

//----------------------------------------------------- 

 

//92MHz BUS_CLOCK

/*

#define SynrDefine 0xD6

#define RefdvDefine 0x03 //Clock frequency equals 2*crystal frequency*(SYNR0+1)/(REFDV0+1)

#define SciDefine 0x25a //Serial bus frequency = clock frequency / (16*SCI0BDL)

*/

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: Wait_Time

* Input parameters: None

*Export parameters: None

*Function description: Simple delay program

***********************************************************************/

void Wait_Time(ulong cnt) {

    unsigned char a,b;

    while(--cnt) {

    for(b=4;b>0;b--)

        for(a=248;a>0;a--);

    }

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: PLL_Init

* Entry parameters: SNNR register value, REFDV0 register value

*Export parameters: None

*Function description: Using PLLCLK, clock frequency = 2* crystal frequency*(SYNR0+1)/(REFDV0+1)

***********************************************************************/

void PLL_Init(byte SYNR0,byte REFDV0)

{

    CLKSEL=0x00; //disable pll; 

    CLKSEL_PLLSEL = 0; 

    PLLCTL_PLLON = 0;

    SYNR = SYNR0; //

    REFDV = REFDV0; 

    PLLCTL = 0xc0; //0xC0 CME=1,PLLON=1

    PLLCTL_PLLON = 1; 

    while(!CRGFLG_LOCK); 

    CLKSEL_PLLSEL = 1;

    return; 

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: SCI_Init

* Entry parameter: SCI0BD register value

*Export parameters: None

*Function description: Use SCI_Init, serial bus frequency = clock frequency/(16*SCI0BDL)

***********************************************************************/

static void SCI_Init(int SCI0) 

{

    SCI0CR1 = 0x00;

    SCI0CR2 =0x2c; //enable Receive Full Interrupt, RX enable, Tx enable      

    SCI0BD = SCI0; //SCI0BDL = busclk / (16 * SCI0BDL)

                  //busclk 8MHz, 9600bps,SCI0BD=0x34

                  //busclk 16MHz, 9600bps,SCI0BD=0x68

                  //busclk 24MHz, 9600bps,SCI0BD=0x9C

                  //busclk 32MHz, 9600bps,SCI0BD=0xD0  

                  //busclk 40MHz, 9600bps,SCI0BD=0x106

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: Sci0Tx

* Entry parameter: SCI0DRL register value

*Export parameters: None

*Function description: Send serial port data

***********************************************************************/

void Sci0Tx(unsigned char text)

{

    unsigned char temp;

    temp=SCI0SR1; /*clear flag*/

    //Before sending, check whether SC0DR is in busy state. SC0DR.7=0: in busy state

    while (!SCI0SR1_TDRE); /* wait for output buffer empty */

    while(!SCI0SR1_TC); //Wait for data transmission to end

    SCI0DRH=0;

    SCI0DRL=text;

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: Sci0Rx

* Input parameters: None

*Export parameters: serial port data

*Function description: Receive serial port data

***********************************************************************/

byte Sci0Rx(void)

{

    byte result,temp;

    temp=SCI0SR1; /*clear flag*/

    if((temp&0x20)>0)

    result=SCI0DRL;

    return result;

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: PWM_Init

* Input parameters: None

*Export parameters: None

*Function description: PWM initialization (BUS_CLOCK=80M), 1 motor, 6 servos

***********************************************************************/

void PWM_Init(void) {

 

    PWME=0x00; //Disable PWM module

    PWMCAE=0x00; //All left-aligned output

    PWMPOL=0xFF; //All output high level first and then low level

    PWMCLK=0xFF; //01 cascade-motor, 6-servo pre-scaling

                     //CLOCK A or CLOCK SA controls the PWM of channels 0, 1, 4, and 5, and CLOCK B or CLOCK SB controls the PWM of channels 2, 3, 6, and 7

    PWMPRCLK=0x22; //CLOCK SA and CLOCK SB are both divided by 4

                     //Set frequency A=bus clock/4=20MHz, B=bus clock/4=20MHz

 

    PWMSCLA=0x01; //CLOCK SA is divided by 10, i.e. 20MHZ/2/1=10MHZ

    PWMSCLB=0x05; //CLOCK SB is divided by 10, i.e. 20MHZ/2/5=2MHZ

 

    PWMCTL_CON01=1; //01 cascade

    PWMPER01=2500; //output frequency algorithm: SA clock/2500=4KHz;

      

    PWMDTY01=1250; //Channel 01 initializes the duty cycle to 50%

 

    PWMPER6=2000; //output frequency algorithm: SB clock/2000=100Hz;

 

    PWMDTY6=1000; //Channel 6 initializes the duty cycle to 50%

 

    PWME=0x22; //Open 01, 6 channels (just open the high bit)

 

}

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: PIT_Init

* Input parameters: None

*Export parameters: None

*Function description: Clock cycle interrupt initialization program

***********************************************************************/

void PIT_Init(void)   

{  

    PITCFLMT_PITE=0; //Close PIT  

 

    // Loading register of micro timer  

    PITMTLD0 = 80-1; //8-bit timer initial value setting. 80 division, under 80MHzBusClock, is 0.1MHz, that is, 10us=0.01ms  

    //PITMTLD1= 80-1; //8-bit timer initial value setting. 80 division, under 80MHzBusClock, is 0.1MHz, that is, 10us=0.01ms  

    //PITMTLD1= 160-1; //8-bit timer initial value setting, 160 division, at 16MHz bus clock frequency, it is 0.1MHz, that is, 1us=0.01ms  

      

    //Timer loading register 

    //Interrupt timing reference time setting 

    PITLD0 = SpeedInterrupt - 1; //16-bit timer initial value setting. PITTIME*0.01MS, timing 1000 * 0.01ms = 10ms  

    PITLD1 = AdInterrupt - 1; // Timing 100*0.01ms = 1ms

 

    PITCE_PCE0=1; //Enable channel 0  

    PITCE_PCE1=1; //Enable channel 0

      

    PITMUX_PMUX0=0; //0: The corresponding 16-bit timer is connected to micro time base 0  

    PITMUX_PMUX1=0; //0: The corresponding 16-bit timer is connected to micro time base 0

    //PITMUX_PMUX1=1; //1: The corresponding 16-bit timer is connected to micro time base 1  

      

    PITINTE_PINTE0=1; // Enable the overflow interrupt of PIT0 timer      

    PITINTE_PINTE1=1; // Enable the overflow interrupt of PIT1 timer   

 

    PITCFLMT_PITE=1; //Enable PIT   

}  

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: ECT_Init

* Input parameters: None

*Export parameters: None

*Function description: Pulse accumulation counting, PTT7 port, circuit connection: pull-up resistor 5.1 kilo ohms required

***********************************************************************/

void ECT_Init(void)

{

    TSCR1 = 0x00; //disable clock  

    TIOS = 0x7f; //Set channel 7 to input capture function

    PACTL=0x50;

 

    PACNT=0x00;

    TSCR2 = 0x00;

    TCTL3 = 0x40; //Set channel 7 rising edge capture

    TSCR1=0x80; //Enable clock counting

}  

//----------------------------------------------------- 

 

/**************************************************************************

*Function name: AD_Init

* Input parameters: None

*Export parameters: None

*Function description: AD module conversion program

***********************************************************************/

void AD_init() 

 

  ATD0CTL1=0b00000000; // 8-bit conversion

                       //ATD0CTL1=0b00100000; // 10-bit conversion

                       //ATD0CTL1=0b01000000; // 12-bit conversion

                       

  ATD0CTL2=0x40; //Power on, clear the flag quickly, ignore external triggers, and disable interrupts.

  

  ATD0CTL3 = 0b10101000; //Conversion sequence length is 7, result register data is right aligned

                         //S8C S4C S2C S1C conversion sequence length

                         // 0 0 0 0 8 ATD0CTL3 = ob10000000

                         // 0 0 0 1 1 ATD0CTL3 = ob10001000

                         // 0 0 1 0 2 ATD0CTL3 = ob10010000

                         // 0 0 1 1 3 ATD0CTL3 = ob10011000

                         // 0 1 0 0 4 ATD0CTL3 = ob10100000

                         // 0 1 0 1 5 ATD0CTL3 = ob10101000

                         // 0 1 1 0 6 ATD0CTL3 = ob10110000

                         // 0 1 1 1 7 ATD0CTL3 = ob10111000

                         // 1 x x x 8 ATD0CTL3 = ob11xxx000

                         

  ATD0CTL4=0x44; // The number of sampling cycles is 8, A/D Clocks= 80M / 2*(4+1) = 8M 

                 //SMP2 SMP1 SMP0 PRS4 PRS3 PRS2 PRS1 PRS0

                 //SMP2 SMP1 SMP0 PS, the seventh bit of DG128 is SRES8

                 // 0 0 0 The number of sampling cycles is 4

                 // 0 0 1 The number of sampling cycles is 6

                 // 0 1 0 The number of sampling cycles is 8

                 // 0 1 1 The number of sampling cycles is 10

                 // 1 0 0 The number of sampling cycles is 12

                 // 1 0 1 The number of sampling cycles is 16

[1] [2]
Keywords:Freescale Reference address:Basic template program for Freescale XS128

Previous article:Analysis of Freescale MCU PWM module (3)
Next article:Analysis of Freescale MCU PWM module (2)

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号