M051 latest bspcmsis, self-made GPIO simplified set macro function

Source: Internet
Author: User
Recently, because of the beginning of the real use of the new Tang M051, began to understand the new Tang M0 especially the charm of M051. Learn the official good intentions. Experience the unique charm of m0+51, yes, he is to mess up the 8-bit machine market, the goal is to become and 8051 comparable to the single-chip microcomputer. He did it. Many people in the AVR era, feel that the port alone assignment is inconvenient: for example-
Two X LCD ports:
Lcd_clk
Lcd_dat
To pull high and low, a lot of people use standard c,porta|= (1<<lcd_clk_bit); this way, later I use the bit field + macro function to achieve a very 51 more compatible operating habits. As long as the definition:
#define LCD_CLK PORT (a,7)
#define LCD_DAT PORT (a,6)
where Port (m,n) for their own use macro function to construct the macro, specifically Baidu "macro definition AVR io" To see my implementation method.
This can be as convenient as the 51 era. LCD_CLK = 1; The difference is that the port definition for. 51 is sbit LCD_CLK = px.y; In M0 especially the new Tang M0 can achieve this goal: the answer is yes
Because the latest M051 series BSP----m051seriesbsp_cmsis_v2.01.002, these definitions have been taken care of, as shown below:



Of course, the use of these gpio, that is, ordinary IO port money, they must be set in the GPIO mode, to ensure that their other functions, such as the ad serial port, the latter PWM and so on are not open.
This will set up the MFP register: Generally, we follow a step to operate the normal IO port,
void Led_init (void)
{
/* Configure p4.0-p4.5 as GPIO mode */
_SYS_P40_MFP (Sys_mfp_p40_gpio);
_SYS_P41_MFP (Sys_mfp_p41_gpio);
_SYS_P42_MFP (Sys_mfp_p42_gpio);
_SYS_P43_MFP (Sys_mfp_p43_gpio);
_SYS_P44_MFP (Sys_mfp_p44_gpio);
_SYS_P45_MFP (Sys_mfp_p45_gpio);

/* Configure p3.2-p3.7 as GPIO mode */
_SYS_P32_MFP (Sys_mfp_p32_gpio);
_SYS_P33_MFP (Sys_mfp_p33_gpio);
_SYS_P34_MFP (Sys_mfp_p34_gpio);
_SYS_P35_MFP (Sys_mfp_p35_gpio);
_SYS_P36_MFP (Sys_mfp_p36_gpio);
_SYS_P37_MFP (Sys_mfp_p37_gpio);

/* Configure p4.0-p4.5 as Output mode */
_gpio_set_pin_mode (P4, 0, Gpio_pmd_output);
_gpio_set_pin_mode (P4, 1, gpio_pmd_output);
_gpio_set_pin_mode (P4, 2, gpio_pmd_output);
_gpio_set_pin_mode (P4, 3, gpio_pmd_output);
_gpio_set_pin_mode (P4, 4, gpio_pmd_output);
_gpio_set_pin_mode (P4, 5, gpio_pmd_output);

/* Configure p3.2-p3.7 as Output mode */
_gpio_set_pin_mode (P3, 2, gpio_pmd_output);
_gpio_set_pin_mode (P3, 3, gpio_pmd_output);
_gpio_set_pin_mode (P3, 4, gpio_pmd_output);
_gpio_set_pin_mode (P3, 5, gpio_pmd_output);
_gpio_set_pin_mode (P3, 6, gpio_pmd_output);
_gpio_set_pin_mode (P3, 7, gpio_pmd_output);

/* Configure p4.0-p4.5 as High */
P40=p41=p42=p43=p44=p45=1;
/* Configure p3.2-p3.7 as High */
P32=p33=p34=p35=p36=p37=1;
}
1, first, set the MFP to Gpio
2, second, set the Gpio pin mode, as output or input, or bidirectional, or open-drain;
3, it is best to operate the port. Similar to 51;
The above trilogy, especially the first 2, must be defined by the IO definition in the schematic diagram of the system itself, constantly modifying m and N in PMN, such as these 2:
_SYS_P40_MFP (Sys_mfp_p40_gpio);
_SYS_P41_MFP (Sys_mfp_p41_gpio);
The above sentence is finished, the following sentence directly copy the above, and then put the statement, all the 40 changed to 41, in addition to this MFP settings to change, the back of the
_gpio_set_pin_mode (P4, 0, Gpio_pmd_output);
_gpio_set_pin_mode (P4, 1, gpio_pmd_output);

Also do a response change. If IO is a lot of trouble, boring, in fact, can use C's powerful macro to replace us to complete these, if the following so, feel more relaxed.
/* 1--configure p0.1\p0.2\p0.3\p0.4\p0.5\ as GPIO mode
* Meanwhile, set the PIN mode:
*/
Set_mfp_gpio (0,1,gpio_pmd_quasi);
Set_mfp_gpio (0,2,gpio_pmd_quasi);
Set_mfp_gpio (0,3,gpio_pmd_quasi);
Set_mfp_gpio (0,4,gpio_pmd_input);
Set_mfp_gpio (0,5,gpio_pmd_input);
, the first sentence to write well, the second sentence, is to change the 1-2 number of things. Very convenient, of course, when the pin mode to change, you can change the back of the Gpio_pmd_quasi, the schema definition to other. There are several ways to do this. Set_mfp_gpio (M,n,mode) This function helps a lot. The function itself is also aided by the official BSP, only to make a summary. Specific technical details are as follows:


/** set PM.N MFP as Gpio and select IO mode
* 1,setting Port_m's pin_n pin is Gpio
* 2,setting port_m pin_n pin input Output mode pin mode
*/
#define SET_MFP_GPIO (Port_m,pin_n,pinmode) _sys_p# #port_m # #pin_n # #_MFP (sys_mfp_p# #port_m # #pin_n # #_GPIO); \
_gpio_set_pin_mode (p# #port_m, Pin_n, Pinmode)

The main use of the macro-link, # #, the original dispersion of the distribution operations. Centralize. Unified management operations. Fellow students, feel useful. You can collect it. No code additions, but improved convenience.

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.