Msp430g2 library file usage instructions --- Io

Source: Internet
Author: User

This section describes how to use the I/O port of MSP430. First, you need to click here to download the library file. After decompression, there are the following files:

1. system. h contains some relational system files and header files of various module functions (I/O is only written );

2. Io. cpp. This file is an implementation file for IO;

3. Interrupt. cpp: The file is interrupted;


Decompress the package. CPP and interrupt. add the CPP file to your project, if the system. H is not placed in the directory where your project is located. Add the header file to the project.

Our compiling environment is TI's CCS. If you are using IAR, You need to modify <mongo.h> in system. h to <iow.h>.


Add the following code to the main. cpp file:
#include"System.h"int main(){SystemInit();SetPinMode(LED1,Out);while(1){DigtalWrite(LED1,1);Delay(1000);DigtalWrite(LED1,0);Delay(1000);}}

Click compile and download. After running, you will see led1, that is, the light on the left on the launchpad starts to flash. The following is an explanation of several functions: 1. systeminit () does not contain any parameters. It must also be run only at the beginning of the main function. This function is used to set the working frequency of the MSP430 to 16 MHz. The systeminit supports different frequencies in 4, namely 1 MHz, 8 MHz, 12 MHz, and 16 MHz. We use 16 MHz by default. Of course, this is not the fastest. Why do we choose these four frequencies? Because the initial values of these four frequency settings are fixed in Flash 430. We only need to read the fixed value and write it to the corresponding register. The default frequency is 16 MHz. What if I want to use another one? Here we provide you with the four options mentioned above. Modify method: Right-click the project and select Properties (the last item of the shortcut menu), as shown in: Add mclk_1mhz to the macro definition, at this time, when systeminit is called, the clock is set to 1 M. Similarly, you can set 8, 12, 16 m. In addition, if nothing is added here, the default frequency is 16 Mb. Note that mclk_1mhz and others can only be added to define, but cannot be added to multiple. If one is not added, the default value is 16 Mb;
2. setpinmode we know that a PIN may have several different methods, input and output. Of course, the input is also different. It can be set to input pull up or input pull down ), in floating ). Therefore, there are several Enum in system. h:
typedef enum{IPU,IPD,InFloating,Out,PrimaryIn,PrimaryOut,SecondIn,SecondOut}PinMode;

Currently, only the first four inputs and outputs are implemented. The first parameter of setpinmode is the const pin, and the second parameter is the const pinmode mode. What is the pin? In the system. h file, I define all the pins of P1 and P2. You can view the system. h file. If you do not understand what it means, you can view this blog (this is the implementation method I wrote under stm32, And the principle is the same ). I am a link. In this way, if you want to set the p1.0 pin to the output mode, you can write: setpinmode (P10, out); 3. digtalwrite does not need to be explained here. If you do not understand it, you can refer to the code below the stm32 platform: I am Link 4. delay, delayus the delay here is the delay parameter specified by Ms. In this case, the parameter type is int. In CCS, Int Is a 16-bit value, so the maximum range is 32767. delayus is the US specified by the latency parameter. Similarly, the parameter is also Int. Note that the delay and delayus are irrelevant to the clock frequency. Whether you are 1 m or 16 m, the latency can be reached.
Here, we have completed a simple flow lamp.
Pin Input: Enter the following code in the main function:
#include"System.h"int main(){SystemInit();SetPinMode(LED1,Out);SetPinMode(BUTTON,IPU);while(1){DigtalWrite(LED1,DigtalRead(BUTTON));}}

In Launchpad, this button is actually P13, led1 is actually P10, they are all the same define. These codes set the IO port of the switch to an up-pull input, which is a high level when the switch is not pressed, so that the read high level is written to led1. When the switch is pressed, digtalread reads low-level data, and led1 goes off.
Mclk_xxmhz:
#include"System.h"int main(){SystemInit();SetPinMode(LED1,Out);while(1){DigtalWrite(LED1,1);for(int i=0;i<30000;i++);DigtalWrite(LED1,0);for(int i=0;i<30000;i++);}}

By defining different mclk_xmhz in define, observe the led1 blinking speed. It can be seen that led1 is the fastest to flash at mclk_16 MHz, and the slowest to flash at mclk_1mhz.
In IO port interrupt 430, each pin of port1 and port2 can be set to interrupt mode. In this interrupt mode, you can select a rising or falling edge. Let's take a look at qianfan lib's support for interrupt: Enter the following code in main:
#include"System.h"int main(){SystemInit();SetPinMode(LED1,Out);DigtalWrite(LED1,0);SetPinMode(BUTTON,IPU);SetPinIt(BUTTON,true,High_Low);while(1){}}

The main function sets led1 as the output mode, and then closes led1 (digtalwrite (led1, 0 );). Next, set the button as the pull-up input, and interrupt is allowed. Interrupt detection is a descending edge (high_low ). The following is a brief explanation of setpinit (short for set pin interrupt ). This function has three parameters. The first parameter is the const pin, indicating which pin to operate on. The second parameter is bool isenable, indicating whether to enable interruption. True enabled, false disabled. The third parameter is const itselect select. Select the rising or falling edge. His statement is as follows:
typedef enum {Low_High,High_Low} ItSelect;

In this way, setpinit enables the button interruption and is set to be triggered as a descent edge. Here, we need to explain why the button is set to the pull-up input. Because the button is low when it is pressed. Therefore, it should usually be a high level, so that the descent edge can be triggered only when it is pressed. After writing the main function, write the interrupted function to open the interrupt. cpp file:
#pragma vector=PORT1_VECTOR__interrupt void Port1_Handler(void){  //Add your code in there...DigtalWrite(LED1,1);}#pragma vector=PORT2_VECTOR__interrupt void Port2_Handler(void){}#pragma vector=ADC10_VECTOR__interrupt void Adc10_Handler(void){}

In the above Code, portdeskhandler is the processing function for the interrupt of the port1 pin (P10 ~ P17. We open led1 in the processing function of port1, so that after pressing the button, we can see that led1 is on. (In fact, when we are dealing with port1 interruptions at this time, we should determine which interrupt occurred after the interruption, and clear the flag after the interruption is completed, here, Qifan is omitted, and readers must add it when writing ).


There is only so much code for the moment, and the rest will be updated later if you are free. Please wait!










Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.