day4 pcb板子电灯小实验

三个小灯轮流转。好似华清过圣诞

main.c

#include "led.h"
extern void printf(const char *fmt, ...);
void delay_ms(int ms)
{
	int i,j;
	for(i = 0; i < ms;i++)
		for (j = 0; j < 1800; j++);
}


int main()
{
	*((volatile int*)0x50000A28) |= (0b11 << 4);
	led_init(GPIOE,10);
	led_init(GPIOF,10);
	led_init(GPIOE,8);
	while(1)
	{
		led_on(GPIOE,10);
		delay_ms(500);
		led_off(GPIOE,10);
		delay_ms(500);
		led_on(GPIOF,10);
		delay_ms(500);
		led_off(GPIOF,10);
		delay_ms(500);
		led_on(GPIOE,8);
		delay_ms(500);
		led_off(GPIOE,8);
		delay_ms(500);
	}
	return 0;
}

led.h

#ifndef __LED_H__
#define __LED_H__

typedef struct{
	unsigned int MODER;
	unsigned int OTYPER;
	unsigned int OSPEEDR;
	unsigned int PUPDR;
	unsigned int IDR;
	unsigned int ODR;
}gpio_t;

#define GPIOE ((volatile gpio_t*)0x50006000)
#define GPIOF ((volatile gpio_t*)0x50007000)

int my_strlen(char*);
char *my_strcpy(char*,char*);
char *my_strcat(char*,char*);

void led_init(volatile gpio_t*,int);
void led_on(volatile gpio_t*,int);
void led_off(volatile gpio_t*,int);

#endif

led.c

#include "led.h"
int my_strlen(char *s1){
	
	int len = 0;
	while(*s1){
		len++;	
	}
	return len;

}

char *my_strcpy(char *s1, char *s2){

	int i = 0;
	while(*s2){
	
		s1[i] = s2[i];
		i++;
	}
	s1[i] = '\0';
	return s1;
}

char *my_strcat(char *s1,char *s2){

	int i = 0;
	int len1 = my_strlen(s1);
	int len2 = my_strlen(s2);
	for(int i = 0; i <= len2; i++){
		s1[len1 + i + 1] = s2[i];
	}
	s1[i] = '\0';
	return s1;
}

void led_init(volatile gpio_t* gpio,int pin){
 

	gpio->MODER &= ~(0b11 << pin * 2);
	gpio->MODER |= (0x1 << pin * 2);
	gpio->OTYPER &= ~(0x1 << pin);
	gpio->OSPEEDR &= ~(0b11 << pin * 2);
	gpio->PUPDR &= ~(0b11 << pin * 2);

}

void led_on(volatile gpio_t* gpio,int pin){
	
	gpio->ODR |= (0x1 << pin);
}

void led_off(volatile gpio_t* gpio,int pin){

	gpio->ODR &= ~(0x1 << pin);

}

void gpio_init(volatile gpio_t* gpio,int num){

	for(int i = 0; i < num; i++){
		gpio = (volatile gpio_t*)(0x50002000 + 0x1000 * num); 
	}
}

void operate_pin(volatile gpio_t* gpio,int pin,int operation_code){
	
	gpio->MODER &= ~(0b11 << pin * 2);
	gpio->MODER |= (0x1 << pin * 2);
	gpio->OTYPER &= ~(0x1 << pin);
	gpio->OSPEEDR &= ~(0b11 << pin * 2);
	gpio->PUPDR &= ~(0b11 << pin * 2);
	if( operation_code == 1){
		gpio->ODR |= (0x1 << pin);
	}else{
		gpio->ODR &= ~(0x1 << pin);
	}
}

你可能感兴趣的:(c++,开发语言,c语言)