ARM-day6

 输入“water_on"控制流水灯

main

#include "uart4.h"
 
// void delay_ms(int ms){
// 	int i,j;
// 	for(i=0;i<ms;i++){
// 		for(j=0;j<2000;j++){
 
// 		}
// 	}
// }
 
 
int main()
{
	uart4_config();
	//char a;
	
	char buf[32];
 
	RCC->MP_AHB4ENSETR |= (0x3<<4);
	led1_init();
	led2_init();
	led3_init();
	
 
	while(1){
		/*
		a=getchar();
		putchar(a+1);
		*/
		/*a=getchar();
		putchar(a);*/
		gets(buf);
		if(!strcmp(buf,"led1_on")){
			led1_ctl(1);
		}
		if(!strcmp(buf,"led1_off")){
			led1_ctl(0);
		}
		if(!strcmp(buf,"led2_on")){
			led2_ctl(1);
		}
		if(!strcmp(buf,"led2_off")){
			led2_ctl(0);
		}
		if(!strcmp(buf,"led3_on")){
			led3_ctl(1);
		}
		if(!strcmp(buf,"led3_off")){
			led3_ctl(0);
		}
		if(!(strcmp(buf,"water_on"))){
			led_water(1);
		}
		if(!(strcmp(buf,"water_off"))){
			led_water(0);
		}
		puts(buf);
	}
	
	
	return 0;
}

led.c

#include "uart4.h"
 
 
void led1_init(){
    //设置PE10为输出模式
    GPIOE->MODER &= (~(0x3<<20));
    GPIOE->MODER |= (0x1<<20);
 
    GPIOE->OTYPER &= (~(0x1<<10));
    GPIOE->OSPEEDR &= (~(0x3<<20));
    GPIOE->PUPDR &= (~(0x3<<20));
    /*  */
 
}
 
void led2_init(){
    //设置PF10为输出模式
    GPIOF->MODER &= (~(0x3<<20));
    GPIOF->MODER |= (0x1<<20);
 
    GPIOF->OTYPER &= (~(0x1<<10)); 
    GPIOF->OSPEEDR &= (~(0x3<<20));
    GPIOF->PUPDR &= (~(0x3<<20));
 
}
 
void led3_init(){
    //设置PE8为输出模式
    GPIOE->MODER &= (~(0x3<<16));
    GPIOE->MODER |= (0x1<<16);
 
    GPIOE->OTYPER &= (~(0x1<<8));
    GPIOE->OSPEEDR &= (~(0x3<<16));
    GPIOE->PUPDR &= (~(0x3<<16));
 
}
 
void delay_ms(int ms){
	int i,j;
	for(i=0;i<ms;i++){
		for(j=0;j<2000;j++){
 
		}
	}
}
 
void led1_ctl(int flag){
    if(flag==1){
        GPIOE->ODR |= (0x1<<10);
    }
    else if(flag==0){
        GPIOE->ODR &= (~(0x1<<10));
    }
}
 
void led2_ctl(int flag){
    if(flag==1){
        GPIOF->ODR |= (0x1<<10);
    }
    else if(flag==0){
        GPIOF->ODR &= (~(0x1<<10));
    }
}
 
void led3_ctl(int flag){
    if(flag==1){
        GPIOE->ODR |= (0x1<<8);
    }
    else if(flag==0){
        GPIOE->ODR &= (~(0x1<<8));
    }
}
 
void led_water(int flag){
    char buf[32];
    while(flag==1){
        led1_ctl(1);
        led2_ctl(0);
        led3_ctl(0);
        delay_ms(500);
        led1_ctl(0);
        led2_ctl(1);
        led3_ctl(0);
        delay_ms(500);
        led1_ctl(0);
        led2_ctl(0);
        led3_ctl(1);
        delay_ms(500);
        
    }
    led1_ctl(0);
    led2_ctl(0);
    led3_ctl(0);
}

uart4.c

#include "uart4.h"
 
 
//UART4 initialization function
void uart4_config(){
//1.enable UART4,GPIOG,GPIOB of peripheral clock
RCC->MP_APB1ENSETR |= (0x1<<16);
RCC->MP_AHB4ENSETR |= (0x1<<1);
RCC->MP_AHB4ENSETR |= (0x1<<6);
 
//2.set PB2 for reuse function
GPIOB->MODER &= (~(0x3<<4));
GPIOB->MODER |= (0x1<<5);
//2.2set PB2 for UART_RX reuse function
GPIOB->AFRL &= (~(0xf<<8));
GPIOB->AFRL |= (0x8<<8);
 
//3.set PG11 for UART_RX reuse function
GPIOG->MODER &= (~(0x3<<22));
GPIOG->MODER |= (0x2<<22);
 
GPIOG->AFRL &= (~(0xf<<12));
GPIOG->AFRL |= (0x6<<12);
 
//4.Disable serical port
USART4->CR1 &= (~(0x1));
 
//5.set 8 bit data
USART4->CR1 &= (~(0x1<<12));
USART4->CR1 &= (~(0x1<<28));
 
//6.set parity check bit
USART4->CR1 &= (~(0x1<<10));
 
//7.set stop bit
USART4->CR2 &= (~(0x3<<12));
 
//8.set Baud rate
USART4->BRR = 0x22b;
 
//9.set Frequency indiscriminate
USART4->PRESC &= (~0xF);
 
//10.set 16 sampling multiple
USART4->CR1 &= (~(0x1<<15));
 
//11.set send enable
USART4->CR1 |= (0x1<<3);
 
//12.set recv enable
USART4->CR1 |= (0x1<<2);
 
//13.set UE enable
USART4->CR1 |= (0x1);
 
}
 
void putchar(char c){
    while(!(USART4->ISR&(0x1<<7)));  
 
    USART4->TDR=c;
 
    while(!(USART4->ISR&(0x1<<6)));  
 
}
 
char getchar(){
    
    while(!(USART4->ISR&(0x1<<5)));
    return USART4->RDR;
    
}
 
void puts(char* s){
    while(*s){
        putchar(*s);
        s++;
    }
    putchar('\n');
    putchar('\r');
}
 
void gets(char* s){
    while(1){
        *s =getchar();
        putchar(*s);
        if(*s=='\r')
        {
            break;
        }
        s++;
    }
    *s='\0';
    putchar('\n');
}
 
int strcmp(char* s,char* p){
    while(*s || *p){
        if(*s!=*p){
            return 1;
            break;
        }
        s++;
        p++;
    }
    return 0;
}

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值