您的位置 首页 报告

单片机C言语程序举例(三)

14音乐播放此程序在硬件上调试通过本程序的单片机晶振采用110592Mincludereg51h>sbitspeaker=P1^2;unsignedchartimer0h,t

14.音乐播映

//此程序在硬件上调试经过
//本程序的单片机晶振选用11.0592M
#include
sbit speaker=P1^2;
unsigned char timer0h,timer0l,time;
//世上只要妈妈好数据表
code unsigned char sszymmh[]={ 6,2,3, 5,2,1, 3,2,2, 5,2,2, 1,3,2, 6,2,1, 5,2,1,
6,2,4, 3,2,2, 5,2,1, 6,2,1, 5,2,2, 3,2,2, 1,2,1,
6,1,1, 5,2,1, 3,2,1, 2,2,4, 2,2,3, 3,2,1, 5,2,2,
5,2,1, 6,2,1, 3,2,2, 2,2,2, 1,2,4, 5,2,3, 3,2,1,
2,2,1, 1,2,1, 6,1,1, 1,2,1, 5,1,6, 0,0,0
};
// 音阶频率表 高八位
code unsigned char FREQH[]={
0xF2,0xF3,0xF5,0xF5,0xF6,0xF7,0xF8,
0xF9,0xF9,0xFA,0xFA,0xFB,0xFB,0xFC,0xFC, //1,2,3,4,5,6,7,8,i
0xFC,0xFD,0xFD,0xFD,0xFD,0xFE,
0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,
} ;
// 音阶频率表 低八位
code unsigned char FREQL[]={
0x42,0xC1,0x17,0xB6,0xD0,0xD1,0xB6,
0x21,0xE1,0x8C,0xD8,0x68,0xE9,0x5B,0x8F, //1,2,3,4,5,6,7,8,i
0xEE,0x44, 0x6B,0xB4,0xF4,0x2D,
0x47,0x77,0xA2,0xB6,0xDA,0xFA,0x16,
};
void delay(unsigned char t)
{
unsigned char t1;
unsigned long t2;
for(t1=0;t1 {
for(t2=0;t2<8000;t2++)
{
;
}
}
TR0=0;
}
void t0int() interrupt 1
{
TR0=0;
speaker=!speaker;
TH0=timer0h;
TL0=timer0l;
TR0=1;
}
void song()
{
TH0=timer0h;
TL0=timer0l;
TR0=1;
delay(time);
}

void main(void)
{
unsigned char k,i;
TMOD=1; //置CT0守时工作方式1
EA=1;
ET0=1;//IE=0x82 //CPU开中止,CT0开中止
while(1)
{
i=0;
while(i<100){ //音乐数组长度,唱完从头再来
k=sszymmh[i]+7*sszymmh[i+1]-1;
timer0h=FREQH[k];
timer0l=FREQL[k];
time=sszymmh[i+2];
i=i+3;
song();
}
}
}

15.红外收发演示

#include

sbit LED=P3^3;//红外发射
sbit LED1=P1^3;
sbit IR=P3^2;//红外一体化接纳

bit Flag;

void Init_Timer0(void)
{
TMOD |= 0x01;
TH0=0xf0;
TL0=0x00;
EA=1;
ET0=1;
TR0=1;
}

void Timer0_isr(void) interrupt 1 using 1
{
TH0=0x0f;
TL0=0x00;
Flag=!Flag;
}

main()
{

unsigned int j;
Init_Timer0();

while(1)
{

LED1=IR;//读取一体化接纳头数值
if(Flag)
{
for(j=0;j<27;j++)//大约38KHz
{
LED=!LED;//发射管输出
}
}
}
}

16.串口通讯

//翻开串口调试程序,将波特率设置为2400,无奇偶校验

#include

#include

#ifdef MONITOR51
char code reserve [3] _at_ 0x23;
#endif

sbit KEY=P3^2;

void delay(unsigned int U)
{
while(–U);
}

void main (void)
{

#ifndef MONITOR51
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xf3;
TR1 = 1;
TI = 1;
#endif

while (1)
{

if(!KEY)
{
delay(10000);
if(!KEY)
{
printf (“This programer test ok!n”);
printf (“12:25:26n”);
}
}
}
}

17.PWM调光

#include

sbit LED = P1^2;
unsigned char CYCLE; //界说周期 该数字X基准守时时刻 假如是10 则周期是10 x 0.1ms
unsigned char PWM_ON ;//界说高电平时刻
void delay(unsigned int cnt)
{
while(–cnt);
}

main()
{
bit Flag;

TMOD |=0x01;//守时器设置 0.1ms in 12M crystal
TH0=(65536-100)/256;
TL0=(65536-100)%256;//守时0.1mS
IE= 0x82; //翻开中止
TR0=1;

CYCLE = 10;// 时刻能够调整 这个是10调整 8位PWM便是256步
while(!Flag)
{
delay(20000); //延时时刻,从一个亮度到下一个亮度的间隔时刻,速度快就能看到接连作用
PWM_ON++; //这个运用较长延时,以便能看清楚改变进程
if(PWM_ON == CYCLE)
{ //这个里能够增加其他程序 如到最亮时分操控设备
Flag=1;
}
}

while(Flag) //亮度递减 同上,是个相反的进程
{
delay(20000);
PWM_ON–;
if(PWM_ON == 0)
{
Flag=0;
}
}
}

void tim(void) interrupt 1 using 1
{
static unsigned char count; //
TH0=(65536-100)/256;
TL0=(65536-100)%256;//守时0.1mS

if (count==PWM_ON)
{
LED = 1; //灯灭
}
count++;
if(count == CYCLE)
{
count=0;
if(PWM_ON!=0) //假如左右时刻是0 坚持本来状况
LED = 0;//灯亮

}

}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/ceping/baogao/262540.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部