Freescale MC9S12G128 GPIO

Publisher:chenxiaohong68Latest update time:2021-07-21 Source: eefocusKeywords:Freescale  GPIO Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

MC9S12G-Family Block Diagram

insert image description here

Different chip packaging ports:

insert image description here
insert image description here
insert image description here

Port J Related Registers

insert image description here

insert image description here

insert image description here
insert image description here

The PIM includes these distinctive registers:

• Data registers and data direction registers for ports A, B, C, D, E, T, S, M, P, J and AD when used

as general-purpose I/O

• Control registers to enable/disable pull devices and select pullups/pulldowns on ports T, S, M, P, J

and AD on per-pin basis

• Single control register to enable/disable pull devices on ports A, B, C, D and E, on per-port basis

and on BKGD pin

• Control registers to enable/disable open-drain (wired-or) mode on ports S and M

• Interrupt flag register for pin interrupts on ports P, J and AD

• Control register to configure IRQ pin operation

• Routing register to support programmable signal redirection in 20 TSSOP only

• Routing register to support programmable signal redirection in 100 LQFP package only

• Package code register preset by factory related to package in use, writable once after reset. Also

includes bit to reprogram routing of API_EXTCLK in all packages.

• Control register for free-running clock outputs


Register Description

The header files that the project code needs to introduce are:

#include /* common defines and macros /

#include “derivative.h” / derivative-specific definitions */

mc9s12g128.h (register related definitions)


PORTx (port data register): can read and write port data 1: high level, 0: low level;

DDRx (port direction register): set the port direction 1: output, 0: input;

PUCR (Pin Pull-up Control Register) 1: Enable pull-up, 0: Disable pull-up;

RDRIV (low power drive register) 1: low power consumption 0: normal power consumption;

x: The possible values ​​are A, B AB, C, D, CD, E, etc. (different versions of chips have different values, please refer to the actual chip definition)

Port mask definition, used for bit operation, can be found in the definition of mc9s12g128.h file


Port Operation

Port data register: PO

RTA,PORTB, PORTAB, PORTC, PORTD, PORTCD,PORTE

Port direction register: DDRA, DDRB, DDRAB, DDRC, DDRD, DDRCD, DDRE

AB operation (PA0…PA7, PB0…PB7, 16 GPIOs in total)


Each bit of the port data register or port direction register corresponds to a single GPIO;


Single I/O operation

This takes the PA port as an example:

BRINGS

PORTA_PA0 ~ PORTA_PA7

PORTAB_PB

PORTABLE_NOT


Set the direction of a single GPIO

DDRA

DDRA_DDRA0 ~ DDRA_DDRA7

DDRAB_DDRB

DDRAB_DDRA


Port J related registers:

/*** PTJ - Port J Data Register; 0x00000268 ***/

Port J data register (0: low level, 1: high level)

PTJ

PTJ_PTJ0 ~ PTJ_PTJ7

PTJ_PTJ0_MASK ~ PTJ_PTJ7_MASK


/*** PTIJ - Port J Input Register; 0x00000269 ***/

Port J input register (0: input level, 1: input high level)

PTIJ

PTIJ_PTIJ0 ~ PTIJ_PTIJ7

PTIJ_PTIJ0_MASK ~ PTIJ_PTIJ7_MASK


/*** DDRJ - Port J Data Direction Register; 0x0000026A ***/

Port J data direction register (0: input, 1: output)

GDR

DDRJ_DDRJ0 ~ DDRJ_DDRJ7

DDRJ_DDRJ0_MASK ~ DDRJ_DDRJ7_MASK


/*** PERJ - Port J Pull Device Enable Register; 0x0000026C ***/

Port J Pull Device Enable Register

Exceptionally

PERJ_PERJ0 ~ PERJ_PERJ7

PERJ_PERJ0_MASK ~ PERJ_PERJ7_MASK


/*** PPSJ - Port J Polarity Select Register; 0x0000026D ***/

Port J pull device polarity selection register (0: select pull-up, falling edge triggers interrupt, 1: select pull-down, rising edge triggers interrupt)

PPSJ

PPSJ_PPSJ0 ~ PPSJ_PPSJ7

PPSJ_PPSJ0_MASK ~ PPSJ_PPSJ7_MASK


/*** PIEJ - Port J Interrupt Enable Register; 0x0000026E ***/

Port J interrupt enable register (0: disable interrupt, 1: enable interrupt)

MORE

FEET_FEET0 ~ FEET_FEET7

PIEJ_PIEJ0_MASK ~ PIEJ_PIEJ7_MASK


/*** PIFJ - Port J Interrupt Flag Register; 0x0000026F ***/

Port J interrupt flag register (write 1 to clear interrupt, write 0 to have no effect)

PIFJ

PIFJ_PIFJ0 ~ PIFJ_PIFJ1

PIFJ_PIFJ0_MASK ~ PIFJ_PIFJ7_MASK


Other ports are similar and can be found in relevant documents;


GPIO LED lighting example:

#include       /* common defines and macros */

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



#define LEDPORT     PORTA

#define LEDDIR DDRA


#define LED1_PORT   PORTD_PD3

#define LED1_DIR    DDRD_DDRD3


/**

* @brief delay function

* @param none

* @return none

*/

void delay(void) {

  unsigned int i,j;

  for(i=0; i<2; i++) {

    for(j=0; j<50000; j++){

      ;

    }

  }

}


/**

* @brief LED GPIO initialization

* @param none

* @return none

*/

void LED_Init(void) {

  LEDDIR = 0xff; // Set PORTA (PA0~PA7 8PIN) port to output

  LEDPORT = 0x00; // Set PORTA default level to all 0

  

  LED1_DIR = 0x1; // Set PORTD_PD3 (PD3 PIN) GPIO to output

  LED1_PORT = 0x0; // Set PORTD_PD3 (PD3 PIN) GPIO low level

}


void main(void) {

  /* put your own code here */

  

  DisableInterrupts; // Disable total interrupt

  LED_Heat();

EnableInterrupts; // Enable general interrupt


  for(;;) {

  

    // Operate the entire port. Here is the PA port (PA0~PA7) 8 GPIOs

    delay();

    LEDPORT = 0xf0; // Port operation

    LED1_PORT = 0; // GPIO operation

    

    delay();

    LEDPORT = 0x0f; // Port operation

    LED1_PORT = 1; // GPIO operation

    

    _FEED_COP(); /* feeds the dog */

  } /* loop forever */

  /* please make sure that you never leave main */

}


Polling GPIO button example:

#include       /* common defines and macros */

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


#define KEYCODE_0 (0)

#define KEYCODE_1 (1)

#define KEYCODE_2 (2)

#define KEYCODE_3 (3)

#define KEYCODE_NONE (0xff)


#define LED_PORT PORTA

#define LED_DIR  DDRA



#define KEY0_IN  PTIJ_PTIJ0

#define KEY1_IN  PTIJ_PTIJ1

#define KEY2_IN  PTIJ_PTIJ2

#define KEY3_IN  PTIJ_PTIJ3


#define KEY0_DIR DDRJ_DDRJ0

#define KEY1_DIR DDRJ_DDRJ1

#define KEY2_DIR DDRJ_DDRJ2

#define KEY3_DIR DDRJ_DDRJ3



unsigned char keycode = KEYCODE_NONE;


void key_delay(void){

  unsigned int i;

  i = 10000;

  while(i--){

    ;

  }

}



void led_gpio_init(void) {

  LED_DIR = 0xFF;

  LED_PORT = 0xFF;  

}


void key_gpio_init(void){

  // Set GPIO input mode 0: input, 1: output

  KEY0_DIR = 0;

  KEY1_DIR = 0;

  KEY2_DIR = 0;

  KEY3_DIR = 0;

}


/*

* @brief Query method to detect keystrokes

*

*

*/

void key_scaning(void) {

  // A button is pressed

  if ((KEY0_IN == 0) || (KEY1_IN == 0) || (KEY2_IN == 0) || (KEY3_IN == 0)) {

     //Key debounce delay

     key_delay(); 


     //Confirm that a key is pressed     

     if ((KEY0_IN == 0) || (KEY1_IN == 0) || (KEY2_IN == 0) || (KEY3_IN == 0)) {

        if (KEY0_IN == 0) {

           keycode = KEYCODE_0;

        } else if (KEY1_IN == 0) {

           keycode = KEYCODE_1;

        } else if (KEY2_IN == 0) {

           keycode = KEYCODE_2;

        } else if (KEY3_IN == 0) {

           keycode = KEYCODE_3;

        } else {

           keycode = KEYCODE_NONE;

        }

     }

  } else {

     keycode = KEYCODE_NONE;

  }

}



void key_process(void) {

  switch(keycode) {

    case KEYCODE_0:

      LED_PORT = ~(0x01 << 0); // LED0亮

      break;

    case KEYCODE_1:

      LED_PORT = ~(0x01 << 1); // LED1亮

      break;

    case KEYCODE_2:

      LED_PORT = ~(0x01 << 2); // LED2亮

      break;

    case KEYCODE_3:

      LED_PORT = ~(0x01 << 3); // LED3亮

      break;

    default:

      LED_PORT = 0xff;

      break;

  }


}



void main(void) {

  /* put your own code here */

  

  DisableInterrupts;

  led_gpio_init();

  key_gpio_init();

EnableInterrupts;


 

  

  for(;;) {

    key_scaning();


    key_process();

    _FEED_COP(); /* feeds the dog */

  } /* loop forever */

  /* please make sure that you never leave main */

}


Interrupt mode key press example:

#include       /* common defines and macros */

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


#define KEYCODE_0 (0)

#define KEYCODE_1 (1)

#define KEYCODE_2 (2)

#define KEYCODE_3 (3)

#define KEYCODE_NONE (0xff)



#define LED PORTA

#define LED_dir DDRA


#define KEY0_IN   PTIJ_PTIJ0

#define KEY1_IN   PTIJ_PTIJ1

#define KEY2_IN   PTIJ_PTIJ2

#define KEY3_IN   PTIJ_PTIJ3


#define KEY0_dir  DDRJ_DDRJ0

#define KEY1_dir  DDRJ_DDRJ1

#define KEY2_dir  DDRJ_DDRJ2

#define KEY3_dir  DDRJ_DDRJ3



unsigned char keycode = KEYCODE_NONE;



void led_gpio_init(void) {

  LED_dir = 0xFF; // Set to output mode, 1: output 0: input

  LED = 0xFF; // All LEDs are off High level is off

}


void key_gpio_init(void) {

  KEY0_dir = 0; // Set to input mode, 1: output 0: input

  KEY1_dir = 0; // Set to input mode, 1: output 0: input

  KEY2_dir = 0; // Set to input mode, 1: output 0: input

  KEY3_dir = 0; // Set to input mode, 1: output 0: input

  

  PPSJ = 0x00; //Polarity selection register, select falling edge; 0: falling edge, 1: rising edge

  PIFJ = 0x0f; //Write 1 to each bit of PIFJ to clear the flag;

  PIEJ = 0x0f; //Interrupt enable register; 0: disable interrupt, 1: enable interrupt

  

}



void key_process(void) {

  switch(keycode) {

    case KEYCODE_0:

      LED = ~(0x01 << 0); // LED0亮

      break;

    case KEYCODE_1:

      LED = ~(0x01 << 1); // LED1 is on

      break;

    case KEYCODE_2:

      LED = ~(0x01 << 2); // LED2亮

      break;

    case KEYCODE_3:

      LED = ~(0x01 << 3); // LED3亮

      break;

    default:

      LED = 0xff;

      break;

  }


}


/*

* @brief key interrupt function

*/

#pragma CODE_SEG __NEAR_SEG NON_BANKED

interrupt VectorNumber_Vportj void PTJ_IRQ(void) {

  //Judge the interrupt flag

  if (PIFJ != 0) {

    PIFJ = 0xff; // Clear interrupt flag

    

    if (KEY0_IN == 0) {

        keycode =  KEYCODE_0;

    } else if (KEY1_IN == 0) {

        keycode =  KEYCODE_1;

    } else if (KEY2_IN == 0) {

        keycode =  KEYCODE_2;    

    } else if (KEY3_IN == 0) {

        keycode =  KEYCODE_3;    

    } else {

        //keycode = KEYCODE_NONE;

    }

  }

  

}

#pragma CODE_SEG DEFAULT



void main(void) {

  /* put your own code here */

  

  DisableInterrupts;

  led_gpio_init();

  key_gpio_init();

EnableInterrupts;



  for(;;) {

  

  key_process();

    _FEED_COP(); /* feeds the dog */

  } /* loop forever */

  /* please make sure that you never leave main */

}


Note that the interrupt service routine is written as follows:

Writing method 1, the interrupt function specifies the interrupt vector number:

main.c

#pragma CODE_SEG __NEAR_SEG NON_BANKED

interrupt interrupt vector number void interrupt service function name (void) {

//Interrupt service routine code segment

}

#pragma CODE_SEG DEFAULT

The interrupt vector number is defined in the mc9s12g128.h file, such as:

[1] [2]
Keywords:Freescale  GPIO Reference address:Freescale MC9S12G128 GPIO

Previous article:MC9S12XS128 realizes ultrasonic distance measurement
Next article:Freescale MC9S12G128 interrupt code implementation method

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号