利用80C51单片机的定时器T0设计相应程序
#include
sbit Wave=P1^6;
void main()
{
Wave=0;
EA=1;//开总中断
TMOD=0x01;//设置定时器0为工作方式1
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;
ET0=1;//开定时器0中断
TR0=1;//启动定时器0
}
void exter0() interrupt 1 //中断函数
{
TH0=(65536-10000)/256;
TL0=(65536-10000)%256;//定时器重新赋初值
Wave=~Wave;//波形翻转
}
因为晶振为6MHz 所以经过12分频之后的时钟周期为0.5MHz 那么想要得到50Hz 的波形 就是说半个周期为1/50s 而定时器加1的时间是1/500000s 所以要这个时间乘以10000才能得到1/50s 这也就是TH0=(65536-10000)/256;TL0=(65536-10000)%256;为什么这样赋值的原因 其他部分的代码我想你应该可以看懂 我就不解释了 如果再有问题咱们再交流!
/*****************************************/
/*我这有一个程序,你参考一下 */
/* function:唱生日快乐 */
/ */
/*****************************************/
#include
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define led RD
#define key_rec T1
sbit BUZZER=P3^7;//发声音口
uchar code T0_H[16]={0xFF,0xFB,0xFB,0xFC,0xFC,0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE};
uchar code T0_L[16]={0xF1,0x4,0x90,0xC,0x44,0xAC,0x9,0x34,0x82,0xC8,0x06,0x22,0x56,0x85,0x9A,0xC1};
uchar code music[32]={8,0,8,9,8,0xB,0xA,0,8,0,8,9,8,0xC,0xB,0,8,0,8,0xF,0xD,0xB,0xA,9,0xE,0,0xE,0xD,0xB,0xC,0xB,0x0};
uchar code music_L[32]={2,1,1,4,4,4,4,4,2,1,1,4,4,4,4,4,2,1,1,4,4,4,4,4,2,1,1,4,4,4,4,4};
uchar a,b;
uint second,minute;
long t0count;
void delay_music(uint v)
{
while(v!=0) v--;
}
void delay_music1(uint j)
{
while(j!=0){j--;delay_music(200);}
}
void delay_music2(uint j)
{
while(j!=0){j--;delay_music1(80);}
}
main()
{//uint i;
uchar k,n,m;
delay_music(100);
TMOD=0x11;
TH0=0xfc;
TL0=0x66;
t0count=0;
EA=1;
ET0=0;
TR0=0;
ET1=0;
TR1=0;
while(1)
{
ET1=1;
TR1=1;
for(k=0;k<32;k++)
{
n=music[k];
m=music_L[k];
a=T0_H[n];
b=T0_L[n];
delay_music2(m);
}
}
}
time0() interrupt 1 using 1
{
TH0=0xFC;
TL0=0x66;
t0count=t0count+1;
if (t0count==1000)
{
second++;
if (second==60)
{
minute++;
if (minute==60) minute=0;
second=0;}
t0count=0;
}
}
time1 () interrupt 3 using 3
{
TH1=a;
TL1=b;
BUZZER=!BUZZER;
}
早说嘛!!
改一下不就行了嘛
下面是汇编写的
ORG 0000H
JMP MAIN
ORG 000BH
JMP INTT0
ORG 0100H
MAIN: MOV SP,#60H
MOV TMOD,#01H
SETB ET0
SETB EA
SETB TR0
START0: SETB INT0
MOV 30H,#00H
NEXT: MOV A,30H
MOV DPTR,#TABLE
MOVC A,@A+DPTR
MOV R2,A
JZ ENDD
ANL A,#0FH
MOV R5,A
MOV A,R2
SWAP A
ANL A,#0FH
JNZ SING
CLR TR0
JMP D1
SING: DEC A
MOV 22H,A
RL A
MOV DPTR,#TABLE1
MOVC A,@A+DPTR
MOV TH0,A
MOV 21H,A
MOV A,22H
RL A
INC A
MOVC A,@A+DPTR
MOV TL0,A
MOV 20H,A
SETB TR0
D1: CALL DELAY
INC 30H
JMP NEXT
ENDD: CLR TR0
JMP START0
INTT0:
PUSH PSW
PUSH ACC
MOV TL0,20H
MOV TH0,21H
CPL INT0
POP ACC
POP PSW
RETI
DELAY: MOV R7,#02
DELAY0: MOV R4,#187
DELAY1: MOV R3,#248
DJNZ R3,$
DJNZ R4,DELAY1
DJNZ R7,DELAY0
DJNZ R5,DELAY
RET
TABLE: DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
DB 82H,01H,81H,94H,84H,0B4H,0A4H,04H
DB 82H,01H,81H,94H,84H,0C4H,0B4H,04H
DB 82H,01H,81H,0F4H,0D4H,0B4H,0A4H,94H
DB 0E2H,01H,0E1H,0D4H,0B4H,0C4H,0B4H,04H
TABLE1: DW 64260,64400,64524,64580,64684,64777,64820,64898
DW 64968,65030,65058,65110,65157,65178,65217
END
include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCP=P2^2;
sbit SCP=P2^1;
sbit SI=P2^0;
sbit S1=P2^3;
sbit S2=P2^4;
sbit S3=P2^5;
sbit S4=P2^6;
sbit S5=P2^7;
sbit DA0832=P3^3;
sbit DA0832_ON=P3^2;
uchar fun=0,b=0,c=0,d=0,tl,th;
uchar code tab[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
uchar code tosin[256]={0x80,0x83,0x86,0x89,0x8d,0x90,0x93,0x96,0x99,0x9c,0x9f,0xa2,0xa5,0xa8,0xab,0xae,0xb1,0xb4,0xb7,0xba,0xbc,0xbf,0xc2,0xc5
,0xc7,0xca,0xcc,0xcf,0xd1,0xd4,0xd6,0xd8,0xda,0xdd,0xdf,0xe1,0xe3,0xe5,0xe7,0xe9,0xea,0xec,0xee,0xef,0xf1,0xf2,0xf4,0xf5
,0xf6,0xf7,0xf8,0xf9,0xfa,0xfb,0xfc,0xfd,0xfd,0xfe,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xfe,0xfd
,0xfd,0xfc,0xfb,0xfa,0xf9,0xf8,0xf7,0xf6,0xf5,0xf4,0xf2,0xf1,0xef,0xee,0xec,0xea,0xe9,0xe7,0xe5,0xe3,0xe1,0xde,0xdd,0xda
,0xd8,0xd6,0xd4,0xd1,0xcf,0xcc,0xca,0xc7,0xc5,0xc2,0xbf,0xbc,0xba,0xb7,0xb4,0xb1,0xae,0xab,0xa8,0xa5,0xa2,0x9f,0x9c,0x99
,0x96,0x93,0x90,0x8d,0x89,0x86,0x83,0x80,0x80,0x7c,0x79,0x76,0x72,0x6f,0x6c,0x69,0x66,0x63,0x60,0x5d,0x5a,0x57,0x55,0x51
,0x4e,0x4c,0x48,0x45,0x43,0x40,0x3d,0x3a,0x38,0x35,0x33,0x30,0x2e,0x2b,0x29,0x27,0x25,0x22,0x20,0x1e,0x1c,0x1a,0x18,0x16
,0x15,0x13,0x11,0x10,0x0e,0x0d,0x0b,0x0a,0x09,0x08,0x07,0x06,0x05,0x04,0x03,0x02,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00
,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02 ,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0d,0x0e,0x10,0x11,0x13,0x15
,0x16,0x18,0x1a,0x1c,0x1e,0x20,0x22,0x25,0x27,0x29,0x2b,0x2e,0x30,0x33,0x35,0x38,0x3a,0x3d,0x40,0x43,0x45,0x48,0x4c,0x4e
,0x51,0x55,0x57,0x5a,0x5d,0x60,0x63,0x66 ,0x69,0x6c,0x6f,0x72,0x76,0x79,0x7c,0x80 };
void display(unsigned char command)
{
unsigned char i;
LCP=0;
for(i=8;i>0;i--)
{
SCP=0;
if((command & 0x80)==0)
{
SI=0;
}
else
{
SI=1;
}
command<<=1;
SCP=1;
}
LCP=1;
}
void key1(void)
{
fun++;
if(fun==4)
fun=0x00;
}
void key2(void)
{
tl++;
if(tl==0x1f)
th++;
}
void key3(void)
{
tl--;
if(tl==0x00)
th--;
}
void key4(void)
{
double t;
int f;
TR0=0;
t=(65535-th*256-tl)*0.4;
f=(int)(1000/t);
S3=tab[f%10];
f=f/10;
S2=tab[f%10];
f=f/10;
if(f==0)
S1=0;
else
S1=tab[f];
TR0=1;
}
void key5(void)
{
tl--;
if(tl==0x00)
th++;
}
void judge(void)
{
uchar line,row,de1,de2,keym;
P1=0x0f;
keym=P1;
if(keym==0x0f)return;
for(de1=0;de1<200;de1++)
for(de2=0;de2<125;de2++){;}
P1=0x0f;
keym=P1;
if(keym==0x0f)return;
P1=0x0f;
line=P1;
P1=0xf0;
row=P1;
line=line+row; /*存放特征键值*/
if(line==0xde)key1();
if(line==0x7e)key2();
if(line==0xbd)key3();
if(line==0x7d)key4();
}
80C51的中断有哪些?
80C51单片机有五个中断源,分别是两个外部中断(INT0和INT1)、两个定时器中断(T0和T1)以及一个串行通信中断。每个中断源都有对应的中断请求标志位,当相应的中断事件发生时,这些标志位会被硬件自动置位。如果单片机的中断允许位被设置,且该中断源的优先级最高,那么CPU会在当前机器周期结束后响应...
已知80C51单片机系统使用6MHZ的外部晶体振荡器,请问:
已知80C51单片机系统使用6MHZ的外部晶体振荡器:①当单片机的定时器0(T0)工作在方式1时,T0的最大定时间为131.072ms?②当单片机的定时器0(T0)工作方式2时,要求每记满250us便产生一次定时器溢出,T0中需要预置的初值应为131
单片机80c51,用汇编,编写一个延时子程序,通过调用此程序实现在p1.0输出...
ORG 0000H AJMP MAIN ORG 000BH T0MS EQU NOT 5000 ; 定义定时器初值 12M晶振 5 mS = 5000 10 mS = 10000 MOV TL0, #LOW T0MS MOV TH0, #HIGH T0MS CPL P1.0 ; P1.0 取反 输出 10 mS的方波 REIT MAIN:MOV TMOD,#01H MOV TL0, #LOW T0MS MOV T...
简述80C51 单片机的中断入口地址。
中断入口地址为中断响应后PC 的内容,即中断服务的入口地址。外部中断0 0003H定时器T0 中断000BH外部中断1 0013H定时器T1 中断001BH串行口中断0023H
80C51单片机内部有几个定时\/计数器?它们由哪些功能寄存器组成?怎样实现...
两个16位定时计数器,分别是Timer0 Timer1.应用定时\/计数器使用以下寄存器:1.中断启用寄存器IE 2.中断优先级寄存器IP 3.定时器\/计数器模式寄存器TMOD 4.定时器\/计数器控制寄存器TCON 5.计数寄存器THx TLx:先进行定时\/计数中断相关寄存器的设定即1-4的赋值,再对计数寄存器即5进行赋值,接着开启中断...
80C51单片机,振荡频率是12MHZ,用定时\/计数器T1实现从P1.1产生周期是2s...
主程序里设置:uchar a=0;TMOD=0x10;ET1=1;EA=1;TH1=(65536-50000)\/256;\/\/定时50ms 20次是1STL1=(65536-50000)%256;TR1=1;中断程序:void timer1( ) interrupt 3{ TH1=(65536-50000)\/256;\/\/定时50ms TL1=(65536-50000)%256; a++; if(a==20} { a=0; P1.1=~P1.1;...
80C51单片机,12mhz.利用中断程序控制8个灯依次亮灭,定时时间为1秒。
unsigned char tab[]={0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,0xff};void delay(uint e) \/\/延时函数,延时为0.5s { uint i,j;while(e--){ for(i=0;i<500;i++)for(j=0;j<110;j++);} } void Timer0Init(){ TMOD|=0x01;\/\/选择为定时器0模式,工作方式1,仅用TR0...
如何设置80C51的波特率
51单片机的串行口有四种工作方式,由串行口控制寄存器中的SM0、SM1控制。80C51单片机的串行通信有8位、10位、11位三种帧格式。波特率设置:4种工作方式波特率的计算方法不同,其中方式0和方式2的波特率是固定的,方式1和方式3的波特率是可变的,均由定时器T1的溢出率控制。具体计算如下:方式0:波特率固定为...
单片机中断系统与定时器\/计数器
也不一定都对,你参考一下 1、当定时\/计数器选定为定时器方式时,是对片内机器周期脉冲数进行计数,选定为计数器方式时,是对(外部事件)进行计数 。2、设定时器T0为方式1定时,定时器T1为方式1计数,则工作方式控制字TMOD=( 51H)。3、设(TMOD)=0A5H,则定时器T0的状态是(方式1计数 ),...
80C51单片机引脚有哪些第二功能?
P0和P1,除了做I\/O口外,还做地址总线(当接外部存储器时)有第二功能的还有P3口的八个引脚,除此还有两引脚有第二功能。P3第二功能各引脚功能定义:P3.0:RXD串行口输入 P3.1:TXD串行口输出 P3.2:INT0外部中断0输入 P3.3:INT1外部中断1输入 P3.4:T0定时器0外部输入 ...