You are on page 1of 3

CUHK Robocon 2012 MCU Programming Workshop, 19 MAR 2012 http://tinyurl.

com/cu-mario
Topics Discussed: GPIO, UART, .. Unless specified, the page number below is for the USER MANUAL. You can search the register names in the manual for further information. Do NOT copy the code below, since it may contain errors, especially when the word converts the symbol to a non-standard char. Type your own. I hear and I forget. I see and I remember. I do and I understand. Fill in the blanks (in gray) 1. GPIO a. functions: for simple control and sensing b. steps: to init a GPIO function on a particular pin, we have to fist set the relative bits in the register PINSEL0, PINSEL1, or PINSEL2 [P.59]. Then the direction (INPUT[0] or OUTPUT[1]) has to be set by the register IODIR (e.g. IO0DIR for P0.xx, IO1DIR for P1.xx) [P.80]. The Red Led will be lit up when the pin P0.22 in HIGH: // lighting up the Red LED and then off PINSEL1 &= ~(3<<12); // Set P0.22 as GPIO IO0DIR |= 1<<22; // Set P0.22[GPIO] as OUTPUT IO0SET = 1<<22; // Set P0.22[GPIO] to HIGH LED ON // delay here IO0CLR = 1<<22; // Clear P0.22[GPIO] to LOW LED OFF Using the Robocons Library, #include AAL/aal_gpio.c GPIO_Init(22, OUTPUT); GPIO_Set(22, 1); // delay here GPIO_Set(22, 0); Note: When using the library for port 1, i.e. P1.29, the syntax is like this: GPIO_Init(129, OUTPUT); When sensing the pin value, IO0PIN can be used. Like the button at P0.20: If (IO0PIN>>20&0x01 == 0x01) // the button is NOT pressed // else // the button is pressed //. or if (GPIO_Read(22)==0x01) // else

//

Note: A simple delay function (rough time) is like this: void delayMs(int z){ int i, j; for (i=0; i<=z; i++) for (j=0; j<=5000; j++); } A nested loop is used to waste the time of the MCU. Calling the function with z=1000 will give approx 1sec delay. Sometimes when the number is not that much and is always positive, we will use unsigned char (less number of bits) instead of int, in order to save memory. int main(){ // delayMs(1000); // 1 second delay // while(1); // main program stops here } 2. UART The UART(Universal asynchronous receiver/transmitter) is very common in MCU-MCU or MCU-computer communications. #include AAL/aal_uart.c Since there are two UARTs on the LPC213x, i.e. UART0 and UART1, we first init the UART with the id and a baud rate (symbols per second). Like: UART_Init(0, 57600); // Init UART0 with baud rate 57600 (refer to AAL/aal_uart.c) Then many functions, like UART_SendString(0, Hello, world!); can be used now!! :D :D Write/type your notes here:

You might also like