Follow+Star Public Account Number, don’t miss wonderful content
Compiled by | strongerHuang
WeChat Public Account | Embedded Column
51 LED Control
-
51 Development Board (taking STC51 microcontroller as an example)
-
Keil C51, STC-ISP Download Software
#include <reg51.h>
sbit LED = P1^0;
void main(){ LED = 0;
while(1);}
STM32 LED Control
-
STM32 Development Board
-
Keil MDK, ST-LINK Utility Download Software
#include "stm32f4xx.h"
/* Main function*/int main(void){ /* Enable GPIOH clock, always enable the clock first when using peripherals*/ RCC_AHB1ENR |= (1<<7);
/* LED port initialization */ /*GPIOH MODER10 clear*/ GPIOH_MODER &= ~( 0x03<< (2*10));
/*PH10 MODER10 = 01b output mode*/ GPIOH_MODER |= (1<<2*10);
/*GPIOH OTYPER10 clear*/ GPIOH_OTYPER &= ~(1<<1*10);
/*PH10 OTYPER10 = 0b push-pull mode*/ GPIOH_OTYPER |= (0<<1*10);
/*GPIOH OSPEEDR10 clear*/ GPIOH_OSPEEDR &= ~(0x03<<2*10);
/*PH10 OSPEEDR10 = 0b speed 2MHz*/ GPIOH_OSPEEDR |= (0<<2*10);
/*GPIOH PUPDR10 clear*/ GPIOH_PUPDR &= ~(0x03<<2*10);
/*PH10 PUPDR10 = 01b pull-up mode*/ GPIOH_PUPDR |= (1<<2*10);
/*PH10 BSRR register's BR10 set to 1 to output low level*/ GPIOH_BSRR |= (1<<16<<10); // LED on
while (1);}
#include "stm32f10x.h"
/* LED clock port, pin definitions*/#define LED_PORT GPIOC #define LED_PIN GPIO_Pin_0#define LED_PORT_RCC RCC_APB2Periph_GPIOC
void LED_Init(){ GPIO_InitTypeDef GPIO_InitStructure; // Define structure variable
RCC_APB2PeriphClockCmd(LED_PORT_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN; // Select the IO port you want to set GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; // Set push-pull output mode GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; // Set transmission speed GPIO_Init(LED_PORT,&GPIO_InitStructure); // Initialize GPIO
GPIO_SetBits(LED_PORT, LED_PIN); // Set LED port high, turn off LED}
int main(){ LED_Init(); GPIO_ResetBits(LED_PORT,GPIO_Pin_0);// LED on
while(1);}
Linux LED Control
-
Download U-boot source code, configure, and compile;
-
Download Linux kernel, configure, and compile (generally, development boards will have ready-made configuration files);
-
Create a root file system; (for the above three steps, if you lack a certain Linux foundation, you can use one-click burning)
-
Port the open-source library WiringPi;
-
Check the circuit diagram to find the pin corresponding to the LED; the program needs to use the pin number;
-
Code and cross-compile;
-
Download and run.
#include <wiringPi.h>
int main(void){ wiringPiSetup() ; pinMode (7, OUTPUT); digitalWrite(7, HIGH); while(1);}
#include <linux/init.h>#include <linux/module.h>#include <linux/fs.h>#include <linux/miscdevice.h>#include <linux/ioctl.h>#include <linux/gpio.h> #include <mach/regs-gpio.h> #include "led.h"
static int led_open(struct inode *inode, struct file *file){ s3c2410_gpio_cfgpin(S3C2410_GPB(5), S3C2410_GPIO_OUTPUT); s3c2410_gpio_setpin(S3C2410_GPB(5), 1); return 0;}
static int led_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg){ switch (cmd) { case LED_ON: s3c2410_gpio_setpin(S3C2410_GPB(5), 0); return 0; case LED_OFF: s3c2410_gpio_setpin(S3C2410_GPB(5), 1); return 0; default: return -EINVAL; }}
static struct file_operations led_fops = { .owner = THIS_MODULE, .open = led_open, .ioctl = led_ioctl,};
static struct miscdevice led_misc = { .minor = MISC_DYNAMIC_MINOR, .name = "led", .fops = &led_fops,};
static int led_init(void){ return misc_register(&led_misc);}
static void led_exit(void){ misc_deregister(&led_misc);}
MODULE_LICENSE("Dual BSD/GPL");module_init(led_init);module_exit(led_exit);
#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/ioctl.h>#include <unistd.h>#include <stdio.h>#include "led.h"
int main(void){ int fd; fd = open("/dev/led", O_RDWR); if (fd < 0) { printf("No such device!\n"); return -1; } while (1) { ioctl(fd, LED_ON); sleep(1); ioctl(fd, LED_OFF); sleep(1); }
close(fd); return 0;}
Finally
Click “Read Original” for more shares.