代码给你吧,protues 需要再说
#include <reg51.h>
#define uint unsigned int
#define uchar unsigned char
#define PinData P0
sbit LED0 = P1^0
sbit LED1 = P1^1
sbit PinSTART = P1^5
sbit PinOE = P1^6
sbit PinEOC = P1^7
sbit CLK = P3^4
//短延时函数:ShortDelay()
void nNop(uchar i)
{
for(i>0i--)
}
//长延时函数:LongDelay()
void LongDelay(uint i)
{
uint j
for(i>0i--)
{ for(j=1000j>0j--)}
}
//引脚初始化
void InitIO()
{
PinData = 0xff
PinSTART = 1
PinEOC = 1
PinOE= 1
CLK = 1
}
//UART的初始化:模式1,8位,9600Bds
void InitSerial()
{
TMOD = 0x20 // Timer 1 in mode 2
TH1 = 0xF4 // 9600 Bds at 11.059MHz
TL1 = 0xF4 // 9600 Bds at 11.059MHz
TR1 = 1 // Timer 1 run
PCON = 0x00 // Timer 1 run
SCON = 0x50 // uart in mode 1 (8 bit), REN=1
}
//T0定孝慧伍时脉冲中断初始化
void init_uart()
{
TMOD=0x01
TH0= (65536-4000)/256
TL0= (65536-4000)%256
TR0=1
// ET0=1
EA=1
}
//等待A/D转换结束函数:WaitADCEnd()
void WaitADCEnd(void)
{
while(!PinEOC)
}
//读取A/D转换后的数据函数:ReadData()
uchar ReadData()
{
uchar tempt
ET0=1
PinSTART = 0
//nNop(1)
PinSTART = 1
PinSTART = 0
WaitADCEnd()
PinOE =1
nNop(2)
tempt = PinData &0xff
ET0=0
return(tempt)
//碧亮PinOE =0
}
//子机送数据给主机
void sendtomain(uchar temp)
{
SBUF = temp
while(!TI)
TI=0
}
void main()
{
uchar temp, tab
InitIO()
init_uart()
InitSerial()
while(1)
{
temp = ReadData()
sendtomain(temp)
while(!RI)
tab = SBUF
RI = 0
if(tab == 0)
{
LED0 = 1
LED1 = 0
}
else if(tab == 1)
{
LED0 = 1
LED1 = 1
}
else
{
LED0 = 0
LED1 = 1
}
}
}
void clk(void) interrupt 1 using 0
{
TH0= (65536-4000)/256
TL0= (65536-4000)%256
CLK= ~CLK
}
#include<reg51.h>
#define PDATA P2
#define aver 2
#define uint unsigned int
#define uchar unsigned char
sbit Ledlow = P1^3
sbit Ledhig = P1^4
uchar table2 []={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}
uchar table1 []={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}
//短延时函数:ShortDelay()
void nNop(uchar temp)
{
uint i
for(i=0 i<tempi++)
}
//UART初始化:模式1,8位,9600Bds at 11.059MHz
void InitSerial()
{ // uart in mode 1 (8 bit), REN=1
TMOD = 0x20 // Timer 1 in mode 2
TH1 = 0xF4 // 9600 Bds at 11.059MHz
TL1 = 0xF4 // 9600 Bds at 11.059MHz
TR1 = 1
PCON = 0x00 // Timer 1 run
SCON = 0x50
}
//主机接受信息给从机
uchar recivefromslave()
{
uchar temp
while(!RI)
temp=SBUF
RI=0
return(temp)
}
// 显示
void leddisp(uint temp )
{
uint i
uint getdata1 = 0
uint getdata2 = 0
temp=temp*aver
getdata1=temp/100
getdata2=temp/10
getdata2=getdata2%10
for(i=0i<30i++)
{
PDATA=table1[getdata1]
Ledlow =0
Ledhig = 1
nNop(1000)
Ledlow =0
Ledhig = 0
PDATA=table2[getdata2]
Ledlow =1
Ledhig = 0
nNop(1000)
Ledlow =0
Ledhig = 0
}
}
void main()
{
uint tab
uint tempt
Ledlow = 0
Ledhig = 0
PDATA = 0x00
InitSerial()
while(1)
{
tempt = (uint) recivefromslave() //接收从机采集的信息
leddisp(tempt) //显示信息
if(tempt<0x60) //判断信息
{
tab = 0
}
else if(tempt>0xc0)
{
tab = 2
}
else
{
tab = 1
}
SBUF = (uchar)tab //对采集信息进行控制
while(!TI)
TI = 0
}
}
汇编如下:S_SET BIT P3.4 秒增加1按钮位
M_SET BIT P3.5 分增加1按钮位
H_SET BIT P3.7 小时增加1按钮位
SECONDEQU 30H 秒计数值变量
MINUTEEQU 31H 分计数值变量
HOUR EQU 32H 小时计数值变量
TCNT EQU 34H 1秒定时计数器
ORG 00H 起始地址
JMP START
ORG 0BH Timer0中断向量地并盯址
JMP INT_T0
START:
MOVDPTR,#TABLE 装七段数码管段码数败滚据表TABLE
MOVHOUR,#0 计数值全部清0
MOVMINUTE,#0
MOVSECOND,#0
MOVTCNT,#0
MOVTMOD,#01H Timer 0作定时器,模式1
MOVTH0,#(65536-50000)/256定时50毫秒
MOVTL0,#(65536-50000)MOD 256
MOVIE,#82H中断使能
SETBTR0 启动Timer 0
以下是判断秒、分、小时增加1按钮是否被按下
A1:
CALL DISPLAY
JNB S_SET,S1 判断秒按钮
JNB M_SET,S2判断分按钮
JNB H_SET,S3 判断小时按钮
JMP A1循环检测
S1:
CALL DELAY去抖动
JBS_SET,A1
INC SECOND 秒位计数值加1
MOVA,SECOND A=SECOND
CJNEA,#60,J0 判断是否到60秒
MOVSECOND,#0 如果到了就清0秒位计数值
JMP K1跳到K1段
S2:
CALL DELAY
JBM_SET,A1
K1:
INC MINUTE 分位计数值加1
MOVA,MINUTE A=MINUTE
CJNEA,#60,J1 判断察蔽余是否到60分
MOVMINUTE,#0 如果到了就清0分位计数值
JMP K2
S3:
CALL DELAY
JBH_SET,A1
K2:
INC HOUR 小时计数值加1
MOVA,HOUR A=HOUR
CJNEA,#24,J2 判断是否到24小时
MOVHOUR,#0 如果到了就清0秒、分、小时位计数值
MOVMINUTE,#0
MOVSECOND,#0
JMP A1
以下程序等待秒、分、小时按钮放开
J0:
JBS_SET,A1
CALL DISPLAY
JMP J0
J1:
JBM_SET,A1
CALL DISPLAY
JMP J1
J2:
JBH_SET,A1
CALL DISPLAY
JMP J2
Timer0中断服务子程序, 进行秒、分、小时的计时
INT_T0:
MOVTH0,#(65536-50000)/256装计数初始值
MOVTL0,#(65536-50000)MOD 256
INC TCNT 1秒计数器增加1
MOVA,TCNTA=TCNT
CJNEA,#20,RETUNE 计时1秒
INC SECOND 秒计数器增加1
MOVTCNT,#0 清零TCNT
MOVA,SECOND A=SECOND
CJNEA,#60,RETUNE 计时60秒
INC MINUTE 分计数器增加1
MOVSECOND,#0 清零秒
MOVA,MINUTE A=MINUTE
CJNEA,#60,RETUNE 计时60分
INC HOUR 小时计数器增加1
MOVMINUTE,#0 清零分
MOVA,HOUR A=HOUR
CJNEA,#24,RETUNE 计时24小时
MOVHOUR,#0 秒、分、小时计数值清0
MOVMINUTE,#0
MOVSECOND,#0
MOVTCNT,#0
RETUNE:
RETI中断服务子程序结束
以下为七段数码管显示的子程序
DISPLAY:
MOVA,SECOND A=SECOND,显示秒
MOVB,#10B=10
DIV AB A除以B
MOV P2,#00100000B 使能秒的十位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
MOVA,B A=B
MOV P2,#00000000B 使能秒的个位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
MOVA,MINUTE A=MINUTE,显示分
MOVB,#10B=10
DIV AB A除以B
MOV P2,#01100000B 使能分的十位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
MOVA,B A=B
MOV P2,#01000000B 使能分的个位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
MOVA,HOUR A=HOUR,显示小时
MOVB,#10B=10
DIV AB A除以B
MOV P2,#10100000B 使能小时的十位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
MOVA,B A=B
MOV P2,#10000000B 使能小时的个位
MOVC A,@A+DPTR七段数码管段码数据装入A
MOVP0,A 从P0口输出
CALL DELAY
ORL P2,#0FFH 熄灭七段数码管
RET
TABLE: 七段数码管显示码表
DB 40H,79H,24H,30H,19H
DB 12H,02H,78H,00H,10H
DELAY: 延时子程序
MOVR6,#2
D1:
MOVR7,#250
DJNZR7,$
DJNZR6,D1
RET
END
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)