基于51单片机的led彩灯设计。 9个灯(红绿蓝三色)。按键控制 c语言程序

基于51单片机的led彩灯设计。 9个灯(红绿蓝三色)。按键控制 c语言程序,第1张

#include<reg52.h>扒让

#define uint unsigned int

sbit S1=P1^0

sbit S2=P1^1

sbit S3=P1^2

sbit S4=P1^3

sbit LED1=P1^4

void delay(uint)

main()

{

S1 = 1

S2 = 1

S3 = 1

S4 = 1

while(1)

{

if(S1==0)

{

P2 = 0X00

LED1 = 0

delay(500)

P2 = ~P2

LED1 = ~LED1

delay(500)

}

if(S2==0)

{

P2 = 0Xc0

delay(500)

P2 = 0xff

LED1 = 1

delay(500)

}

if(S3==0)

{

P2 = 0X07

LED1 = 0

delay(500)

P2 = 0XFF

LED1 = 1

delay(500)

}

if(S4==0)

{

P2 = 0X38

LED1 = 0

delay(500)

P2 = 0xff

LED1 =1

delay(500)

}

}

}

void delay(uint z)

{

uint x, y

for(x=85x>源此判0x--)

for(y=zy>0y--)

}

具体还要看你单片机和LED引脚的雹改连线,原理大概就是上面那样了,可以下载到机子验证。

首先,ColorListener的getColor方法取消,把这个方法所有语句移到构造方法中。

其次,WindouwColor中,a,b,c,p直接在定义时赋值

再有,init()方法再稍作修改:

import java.awt.*

import javax.swing.*

public class WindouwColor extends JFrame{

JButton a=new JButton("红色")

JButton b=new JButton("蓝色")

JButton c=new JButton("绿色")

JPanel p=new JPanel()

ColorListener l=new ColorListener(a,b,c,p)

WindouwColor(){

init()

setVisible(true)

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

}

void init(){

// setLayout(new FlowLayout())

p.setBounds(0,0,460,360)

p.setLayout(new FlowLayout())

p.add(a)

p.add(b)

p.add(c)

a.addActionListener(l)

b.addActionListener(l)

c.addActionListener(l)

add(p,BorderLayout.CENTER)

}

}

import javax.swing.*

import java.awt.Color

import java.awt.event.*

public class ColorListener implements ActionListener{

JButton a

JButton b

JButton c

JPanel p

public ColorListener(JButton a,JButton b,JButton c,JPanel p){

this.a=a

this.b=b

this.c=c

this.p=p

}

public void actionPerformed(ActionEvent e) {

if(a==(JButton)e.getSource())

p.setBackground(Color.red)

else if(b==(JButton)e.getSource())

p.setBackground(Color.blue)

else

p.setBackground(Color.green)

}

}

public class MainClass2 {

public static void main(String[] args) {

WindouwColor win=new WindouwColor()

win.setBounds(100,100,460,360)

win.setTitle("改变窗口颜色")

}

}

text(10,10,'渗郑大Red','Color','r')

text(20,10,'Green','Color'丛竖,'丛仔g')

text(30,10,'Blue','Color','b')

axis([0 60 0 60])


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/8197166.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-14
下一篇2023-04-14

发表评论

登录后才能评论

评论列表(0条)

    保存