Click on our menu buttons immediately below to find MegaSquirt® information quickly:


GPIO Input/Output Quick-Start

MShift FunctionPortI/Oturn ONturn OFFto READNOTES
Brake SensePAD07I----(PORTAD0 & 0x80)digital input
Paddle DOWNPAD06I ----(PORTAD0 & 0x40)on/off
non-CAN MAP/TPS/MAFPAD05I----ATD0DR5ADC count
line pressure sensorPAD04I -- --ATD0DR4ADC count
switchCPAD03I----ATD0DR3ADC count (0-1024)
Temp SensorPAD02I----ATD0DR2ADC count (0-1024)
switchBPAD01I ----ATD0DR1ADC count (0-1024)
switchAPAD00I ----ATD0DR0ADC count (0-1024)
Spare Output1PT7O*pPTTpin[7] |= 0x80;*pPTTpin[7] &= ~0x80;---on/off
Paddle UPPT6I ----(PTT & 0x40)digital input
ISSPT5I------Interrupt ISS_timer()interrupt service rotuine called on edge
Speedo OutputPT4OPWMDTY4 = 0;PWMDTY4 = PWMPER4;PWMDTY40 < PWMDTY4 < PWMPER4
TCCPT3OPWMDTY3 = 0;PWMDTY3 = PWMPER3;PWMDTY30 < PWMDTY3 < PWMPER3
PCPT2OPWMDTY2 = 0;PWMDTY2 = PWMPER2;PWMDTY20 < PWMDTY2 < PWMPER2
Sol32PT1OPWMDTY1 = 0;PWMDTY1 = PWMPER1;PWMDTY10 < PWMDTY1 < PWMPER1
VSSPT0I------Interrupt VSS_timer()interrupt service called on edge
LED4PB4OPORTB |= 0x10;PORTB &= ~0x10;---on/off
LED3PM5O*pPTMpin[5] |= 0x20;*pPTMpin[5] &= ~0x20;---on/off
LED1PM4O*pPTMpin[4] |= 0x10;*pPTMpin[5] &= ~0x10;---on/off
LED2PM3O*pPTMpin[3] |= 0x08;*pPTMpin[5] &= ~0x08;---on/off
Sol BPM2O*pPTMpin[2] |= 0x04;*pPTMpin[5] &= ~0x04;---on/off
Sol APE4OPORTE |= 0x10;PORTE &= ~0x10;---on/off
4WD inputPE1I----(PORTE & 0x02)digital input
Spare Output2PA0OPORTA |= 0x01;PORTA &= ~0x01;---on/off

Port DDR (data direction register) Configuration

Many of the I/O port pins on the 9S12C64 processor can be configured as either an input or an output. The way you tell the processor which should be what is by filling zeros or ones in the data direction registers.

0=input, 1=output, x=not assigned (default is input)

For the MegaShift, for example, we want:

Bit76543210decimalhexadecimal
PAxxxxxxx1= 01DDRA= 0x0001
PBxxx1xxxx= 16DDRB= 0x0010
PExxx1xxx1= 16DDRE= 0x0011
PMxx1111xx= 60DDRM= 0x003C
PT10011110=158DDRT= 0x009E
These registers are typically defined with DDRx names. Note that the data direction registers are located at fixed memory addresses, but there are convenient names defined for these addresses in the hcs12def.h file (open it and look for things like DDRA for port A, etc.).

BTW, these user configurable input/output ports are called "general purpose I/O" ports which is where the GPIO board gets its name.

Examples:

PORTE is defined in hcs12def.h and is memory addresss (*((volatile unsigned char*)(0x0008))) where "volatile unsigned char" means that this is an 8 bit postive only value (unsigned char) that might be changed externally to the code logic ("volatile"), such as by an interrupt.

By 'ANDing' PORTE with 0x10, we are comparing PTT to 0x10 (which is hex, the binary is 00010000 - 8 bits). This compares the current values bit by bit with 00010000, and set the corresponding PTT bit to 1 if the bits in both are 1, otherwise it is set to zero. Operations like this are called 'bitwise'.

The net result is that since bit 4 (they are numbered 0 to 7 from left to right) is one it will be ANDed to 1 if bit 4 in PORTE is one, since the rest of the ANDs will be zero (anything ANDed with zero is zero). So the result is true (==1) if bit 4 is 'on', and false (==0) if bit 4 is 'off'.

Note that we can AND or OR. AND is usually used to check a bit state. Both AND and OR are used to set bit states.

For example, if we want to set port B pin 3 high, we would use:

PORTB |= 0x08; //(i.e., if either PORTB or 000001000 are bitwise one, set that bit to one)

To turn the same bit off, we use:

PORTB &= ~0x08; // (i.e., if both PORTB and ~00001000 == 11110111 are one, set to one, set to zero otherwise. Since the #3 bit of ~0x08 is never one, this sets the corresponding PORTB bit to zero)



©2009 Al Grippo and Bruce Bowling - All rights reserved. MegaSquirt® and MicroSquirt® are registered trademarks.