嵌入式开发入门到放弃的关键时刻
51点灯
51开发板(以STC51单片机为例)
Keil C51、STC-ISP下载软件
#include <reg51.h>
sbit LED = P1^0;
void main(){ LED = 0;
while(1);}STM32点灯
STM32开发板
#include 'stm32f4xx.h'/* 主函数*/int main(void){/*开启 GPIOH 时钟,使用外设时都要先开启它的时钟*/RCC_AHB1ENR |= (1<<7);/* LED 端口初始化 *//*GPIOH MODER10 清空*/GPIOH_MODER &= ~( 0x03<< (2*10));/*PH10 MODER10 = 01b 输出模式*/GPIOH_MODER |= (1<<2*10);/*GPIOH OTYPER10 清空*/GPIOH_OTYPER &= ~(1<<1*10);/*PH10 OTYPER10 = 0b 推挽模式*/GPIOH_OTYPER |= (0<<1*10);/*GPIOH OSPEEDR10 清空*/GPIOH_OSPEEDR &= ~(0x03<<2*10);/*PH10 OSPEEDR10 = 0b 速率 2MHz*/GPIOH_OSPEEDR |= (0<<2*10);/*GPIOH PUPDR10 清空*/GPIOH_PUPDR &= ~(0x03<<2*10);/*PH10 PUPDR10 = 01b 上拉模式*/GPIOH_PUPDR |= (1<<2*10);/*PH10 BSRR 寄存器的 BR10 置 1,使引脚输出低电平*/GPIOH_BSRR |= (1<<16<<10); //点灯while (1);}
#include 'stm32f10x.h'
/* LED时钟端口、引脚定义*/#define LED_PORT GPIOC #define LED_PIN GPIO_Pin_0#define LED_PORT_RCC RCC_APB2Periph_GPIOC
void LED_Init(){ GPIO_InitTypeDef GPIO_InitStructure; //定义结构体变量
RCC_APB2PeriphClockCmd(LED_PORT_RCC, ENABLE);
GPIO_InitStructure.GPIO_Pin = LED_PIN; //选择你要设置的IO口 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置推挽输出模式 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //设置传输速率 GPIO_Init(LED_PORT,&GPIO_InitStructure); //初始化GPIO
GPIO_SetBits(LED_PORT, LED_PIN); //将LED端口拉高,熄灭LED}
int main(){ LED_Init(); GPIO_ResetBits(LED_PORT,GPIO_Pin_0);//点灯
while(1);}Linux点灯
下载U-boot源码,配置、编译;
下载Linux内核、配置、编译(一般开发板都会有现成的配置文件);
制作跟文件系统;(以上三个步骤,如果没有一定的Linux基础,可以使用一键烧写)
移植开源库WiringPi;
查看电路图找到LED对应的引脚,程序需要用到引脚号;
编码、交叉编译;
下载运行。
#include <wiringPi.h>int main(void){wiringPiSetup() ;pinMode (7, OUTPUT);gitalWrite(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;}
最后
赞 (0)
