C语言按键控制led灯和蜂鸣器

循环流水灯和蜂鸣器,按键结束
#define rGPJ2CON (*(volatile unsigned long *)0xe0200280)
#define rGPJ2DAT (*(volatile unsigned long *)0xe0200284)

#define rGPD0CON (*(volatile unsigned long *)0xe02000a0)
#define rGPD0DAT (*(volatile unsigned long *)0xe02000a4)

#define rGPH2CON (*(volatile unsigned long *)0xe0200c40)
#define rGPH2DAT (*(volatile unsigned long *)0xe0200c44)

void Led_RunDelay(volatile int count);
int main(void)
{
        int i=0,temp=0,k1;
        rGPJ2CON = 0x1111;
        rGPJ2DAT = 0xf;
        
        rGPH2CON =0x00;
        rGPH2DAT = 0XFF;
        
        
        while (k1!=0)
        {
                rGPJ2DAT = ~(0x1<<i);
                Led_RunDelay(0xe0000);
                rGPD0DAT=0x0;

                rGPD0CON = 0x1111;
                rGPD0DAT = 0x0;
                
                
                i++;
                if(i==4)
                {
                        i=0;
                        rGPJ2DAT = 0xf;
                        rGPD0DAT=0x1;
                }
                
                temp = rGPH2DAT;
                k1 = (temp & 0x01);
                
        }
        return 0;
}


void Led_RunDelay(volatile int count)
{
        while (count--);
}

流水灯循环5次

#define rGPJ2CON *((volatile unsigned int *)0xe0200280)
#define rGPJ2DAT *((volatile unsigned int *)0xe0200284)
void c_delay(void);
//要定义下函数,如果是放到api.h文件里的,就可以导入不用定义了
void led_test(void)
{
	led_init();
	int i=5;
	while(i--)
	{
		rGPJ2DAT=0xe;
		c_delay();
		rGPJ2DAT=0xd;
		c_delay();
		rGPJ2DAT=0xb;
		c_delay();
		rGPJ2DAT=0x7;
		c_delay();
		
	}
}
int led_init(void)
{
		rGPJ2CON&=~(0xffff<<0);
		rGPJ2CON|=(0x1111<<0);
		rGPJ2DAT|=(0xf<<0);
}
void c_delay(void)
{
        volatile int i,j;
        for(j = 0; j < 0x100; j++)
                for(i = 0; i < 1000; i++);
}


makefile

all:led.bin clean
led.bin:led.o
        arm-linux-ld -Ttext=0x40000000 led.o -o led.elf
        arm-linux-objcopy -O binary -S led.elf led.bin
led.o:led.c
        arm-linux-gcc -c -nostdlib -nostdinc led.c -o led.o
clean:
        rm -rf led.o led.elf


  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是基于LD3320语音识别模块、Arduino Uno开发板、蜂鸣器LED的代码示例: ``` #include <SoftwareSerial.h> SoftwareSerial mySerial(2, 3); // 设置串口通信的引脚 #define LED_PIN 13 // LED的引脚 #define BUZZER_PIN 8 // 蜂鸣器的引脚 void setup() { pinMode(LED_PIN, OUTPUT); // 将LED引脚设置为输出模式 pinMode(BUZZER_PIN, OUTPUT); // 将蜂鸣器引脚设置为输出模式 digitalWrite(LED_PIN, LOW); // 初始状态下关闭LED digitalWrite(BUZZER_PIN, LOW); // 初始状态下关闭蜂鸣器 Serial.begin(9600); // 开启串口通信 mySerial.begin(9600); // 开启软件串口通信 } void loop() { if (mySerial.available()) { // 判断串口是否有数据 String command = mySerial.readStringUntil('\n'); // 读取串口数据 if (command == "LED ON") { // 如果收到的命令是 "LED ON" digitalWrite(LED_PIN, HIGH); // 开启LED delay(1000); // 等待1秒 digitalWrite(LED_PIN, LOW); // 关闭LED } else if (command == "BUZZER ON") { // 如果收到的命令是 "BUZZER ON" digitalWrite(BUZZER_PIN, HIGH); // 开启蜂鸣器 delay(1000); // 等待1秒 digitalWrite(BUZZER_PIN, LOW); // 关闭蜂鸣器 } } } ``` 此代码假设你已经完成了LD3320语音识别模块和Arduino Uno开发板的连接,并且已经按照模块说明书配置了正确的模块参数。你还需要使用串口助手或其他串口通信工具将命令发送到串口上,例如 "LED ON" 或 "BUZZER ON",然后程序会根据收到的命令控制LED蜂鸣器

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值