#我的代码交通灯和pc连,位码s0和s1与pb0,pb1连,如何使交通灯与pb连,s0和s1与pc0,pc1连
#data segment
io8255a equ 28ah
io8255b equ 28bh
io8255c equ 288h
led db 3fh,06h,5bh,4fh,66h,6dh,7dh,07h,7fh,6fh
buffer1 db 0,6
bz dw ?
mess db 'TPCA omterrupt!',0dh,0ah,'$'
portc1 db 24h,44h,04h,44h,04h,44h,04h
db 81h,82h,80h,82h,80h,82h,80h
db 0ffh
data ends
code segment
assume cs:code,ds:data
start:
mov ax,cs
mov ds,ax
mov dx,offset int3
mov ax,250bh
int 21h
in al,21h
and al,0f7h
out 21h,al
mov cx,15
sti
mov ax,data
mov ds,ax
mov dx,io8255b
mov al,80h
out dx,al
ll: mov dx,io8255a
re_on: mov bx,0
on: mov al,portc1[bx]
cmp al,0ffh
jz re_on
out dx,al
inc bx
mov cx,500
test al,21h
jz de1
push ax
push bx
push cx
push dx
call shumaguang
pop dx
pop cx
pop bx
pop ax
jmp next
de1: mov di,9000
de0: dec di
jnz de0
loop de1
next:push dx
mov ah,06h
mov dl,0ffh
int 21h
pop dx
jz on
exit:mov ah,4ch
int 21h
shumaguang proc
mov di,offset buffer1
loop1:mov cx,040h
loop2:mov bh,02
lll: mov byte ptr bz,bh
push di
dec di
add di,bz
mov bl,[di]
pop di
mov bh,0
mov si,offset led
add si,bx
mov al,byte ptr [si]
mov dx,io8255c
out dx,al
mov al,byte ptr bz
mov dx,289h
out dx,al
push cx
mov cx,100
delay: loop delay
pop cx
mov al,00h
out dx,al
mov bh,byte ptr bz
shr bh,1
jnz lll
loop loop2
mov ax,word ptr [di]
cmp ah,00
jnz set
cmp al,00
jnz set
mov ax,0600h
mov [di],al
mov [di+1],ah
ret
set: mov ax,word ptr [di]
dec al
aas
mov [di],al
mov[di+1],ah
jmp loop1
shumaguang endp
jmp ll
int3:
mov ax,data
mov ds,ax
mov dx,offset mess
mov ah,09
int 21h
mov ax,0000
mov [di],al
mov [di+1],ah
mov al,20h
out 20h,al
loop llll
in al,21h
or al,08h
out 21h,al
sti
mov ah,4ch
int 21h
llll:iret
code ends
end start
首先,你需要将交通灯的两个位码 S0 和 S1 分别与 PC0 和 PC1 进行连接。而将开关 PB0 和 PB1 连接到交通灯,则需要将这两个开关的连接进行调整。
以下是一个可能的代码示例,将交通灯连接到 PC0 和 PC1,将 PB0 和 PB1 用于控制交通灯:
#include <stm32f4xx.h>
int main(void) {
// 配置 PC0 和 PC1 为输出模式
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitTypeDef gpio_init;
GPIO_StructInit(&gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
gpio_init.GPIO_Mode = GPIO_Mode_OUT;
gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &gpio_init);
// 配置 PB0 和 PB1 为输入模式
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_StructInit(&gpio_init);
gpio_init.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
gpio_init.GPIO_Mode = GPIO_Mode_IN;
gpio_init.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &gpio_init);
while (1) {
// 读取 PB0 和 PB1 的状态
int pb0 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0);
int pb1 = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1);
// 根据 PB0 和 PB1 控制交通灯
if (pb0 == 1 && pb1 == 1) {
GPIO_SetBits(GPIOC, GPIO_Pin_0); // 红灯亮
GPIO_ResetBits(GPIOC, GPIO_Pin_1); // 绿灯灭
} else if (pb0 == 0 && pb1 == 1) {
GPIO_ResetBits(GPIOC, GPIO_Pin_0); // 红灯灭
GPIO_SetBits(GPIOC, GPIO_Pin_1); // 绿灯亮
} else if (pb0 == 1 && pb1 == 0) {
// 点灯效果,每隔一段时间交替亮红绿灯
static int count = 0;
if (count % 50 < 25) {
GPIO_SetBits(GPIOC, GPIO_Pin_0); // 红灯亮
GPIO_ResetBits(GPIOC, GPIO_Pin_1); // 绿灯灭
} else {
GPIO_ResetBits(GPIOC, GPIO_Pin_0); // 红灯灭
GPIO_SetBits(GPIOC, GPIO_Pin_1); // 绿灯亮
}
count++;
}
}
}
如上代码所示,我们将交通灯的 S0 位码连接到 PC0 引脚,将 S1 位码连接到 PC1 引脚,然后将 PB0 和 PB1 连接到开关上。在 while 循环中,我们通过读取 PB0 和 PB1 的状态,来控制交通灯的亮灭。使用 GPIO_SetBits 和 GPIO_ResetBits 函数可以分别设置和清除对应引脚的输出状态,从而控制交通灯的亮灭。