这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » DIY与开源设计 » 电子DIY » neuq666 MCU DIY进程帖

共27条 3/3 1 2 3 跳转至
菜鸟
2011-12-16 17:52:24     打赏
21楼

今天外接了超声波测距模块试了一下   基本功能实现了   误差稍微有点大

#include "reg52.h"
//超声波测距2011.12.16
//内部振荡器16MHz
//Timer0_实现方波发生器_40.2kHz
//Timer1_波特率发生器_UART0_9600波特率
//Timer2_超声波时间测量计数

//for循环_方波发生器步距_3.4kHz
//Timer0_方波发生器步距_0.2kHz
#define Max 1
#define Step_1 65
#define Step_2 3
#define Step_3 30000
#define Cal_Set 5000


//unsigned char bdata status_byte_0;//状态位
//unsigned int xdata * data px;
unsigned int Cal_1;
unsigned char Data_1[18]={0};//接收存放

sbit LED30 = P3^0;  //系统运行周期LED显示

sbit Output_32=P3^2; //超声波驱动脉冲输出
sbit Output_33=P3^3; //超声波驱动脉冲输出

void SYSCLK_Init (void)
{

 /*  int i;    // delay counter
   OSCXCN = 0x67;  // start external oscillator with 11.0592MHz crystal
   for (i=0; i < 256; i++){;};  // Wait for osc. to start up
   while (!(OSCXCN & 0x80));  // Wait for crystal osc. to settle

 OSCICN = 0x88;//Enable missing clock detector and select external oscillator as SYSCLK source
*/
 OSCICN = 0x97;//Enable missing clock detector and select internal 16MHz oscillator as SYSCLK source

}
void PORT_Init (void)
{
    XBR0 = 0x6E; //Enable UART0 and UART1
 XBR1 = 0x17;
 XBR2 = 0x44;

// XBR0 = 0x07; //Enable UART0 and UART1
// XBR1 = 0x02;
// XBR2 = 0x44; //Enable crossbar and weak -up
// XBR2    =  0xC4; //Enable crossbar but disable weak pull-ups

//   EMI0CF  = 0x27;
//   EMI0TC  = 0x21;
 P0MDOUT = 0x00; 
 P1MDOUT = 0x00;
// P2MDOUT = 0x00; 
//P3MDOUT = 0x0C;
}

void UART01_Init (void)
{ SCON0  = 0x50;  // SCON0: mode 1, 8-bit, No Parity Bit, 1 bit start and 1 bit stop
 
 TMOD   = 0x22; // timer 1: mode 2, 8-bit reload
     // timer 0: mode 2, 8-bit reload
 TL1    =0x9e;
 TH1    =0x8e; //99_A3=9600 Baud rate

//CKCON=0x08;//Timer0_使用系统时钟,不1/12分频
//CKCON=0x00;//Timer0_使用系统时钟,1/12分频
 CKCON = 0x18; //Timer0_使用系统时钟_Not_1/12分频_Timer1 uses SYSCLK as time base
 PCON  = 0x80; //SMOD0 = 1_UART0波特率加倍

 TR1 = 0;  // Start Timer1 用于串行通讯Baud rate控制
 
}
/*
void SPI0_Init (void)
{
   SPI0CFG = 0x07; // data sampled on 1st SCK rising edge 8-bit data words
   SPI0CFG|=0xC0; // CKPOL =1;
   SPI0CN = 0x03; // Master mode; SPI enabled; flags cleared
   SPI0CKR = SYSCLK/2/8000000-1; // SPI clock <= 8MHz (limited by EEPROM spec.)
}
*/
/*
void Delay1us(unsigned char Count_us)
{unsigned char t1;
 while (Count_us)
 {
 for(t1=1;t1<5;t1++){;}
   Count_us--;
 }
}

void Delay1ms(unsigned char Count_ms)
{ while (Count_ms)
 {
 Delay1us(200);
 Delay1us(200);
 Delay1us(200);
 Delay1us(200);
 Count_ms--;
 }
}
*/


void Send_Addre_32()
{
   ES0=0;
   SBUF0='@';while(!TI0);TI0=0;//

  ACC = Data_1[0];//拆成高4位
 ACC &= 0xF0;
 ACC = ACC/16;
SBUF0=ACC;while(!TI0);TI0=0;

 ACC = Data_1[0];//拆成低4位
 ACC = ACC & 0x0F;
SBUF0=ACC;while(!TI0);TI0=0;
 
 ACC = Data_1[1];//拆成高4位
 ACC &= 0xF0;
 ACC = ACC/16;
SBUF0=ACC;while(!TI0);TI0=0;

 ACC = Data_1[1];//拆成低4位
 ACC = ACC & 0x0F;
SBUF0=ACC;while(!TI0);TI0=0;    
   
   SBUF0='A';while(!TI0);TI0=0;//尾
    ES0=1;
}

//===main()===
void main (void)
{
 unsigned char M1,M2;
 unsigned int i_1,i_2;

 WDTCN = 0xde; //禁止WDT_disable watchdog timer
 WDTCN = 0xad; //关看门狗
//===
// WDTCN = 0xFF; //锁定WDT_一直使用看门狗
// WDTCN = 0xA5; //使能/复位WDT_喂狗
// WDTCN = 0x07; //设置最大WDT超时极限值
//===
 
 SYSCLK_Init (); //初始化系统时钟
 UART01_Init (); //初始化串口
 PORT_Init (); //初始化IO口
    T2CON = 0x01; //初始化Timer2
 
TH0 =0x46;//Timer0_实现方波发生器_40.2kHz
TL0 =0x46;
TH2 =0x00;
TL2 =0x00;
ET0 = 1;
ET2 = 1;
EX0 = 1;
EA = 1;

//Waiting_1
for(M1=0;M1<9;M1++)
{
LED30=~LED30;
for(i_1=0;i_1<300;i_1++)
{for(i_2=0;i_2<300;i_2++)
{;}
}
}


while(1)
{
M2++;
if(M2>Step_2){M2=0;LED30=~LED30;}

Send_Addre_32();
 
Output_32=0;
Output_33=1;

ET2 = 1;//Timer2_溢出中断允许
TR2 = 1;//Timer2_计时开始

TR0 = 1;//发出超声波

for(i_1=0;i_1<Step_1;i_1++){;}

TR0 = 0;//停止超声波
Output_32=0;
Output_33=1;

for(M1=0;M1<Step_2;M1++)
{
for(i_1=0;i_1<Step_1;i_1++){;}//延时
}

EX0=1; //外中断0允许接收回波

Cal_1=Data_1[0]*256+Data_1[1];

for(i_1=0;i_1<Step_2;i_1++)
{
for(M1=0;M1<Step_2;M1++)
{
for(i_2=0;i_2<Step_3;i_2++){;}//延时
}
 if(Cal_1>Cal_Set){LED30=~LED30;}//如果油位过低_快闪
}

 

}//while(1)

}//main()

void T2_ISR(void) interrupt 5
{
ET2 = 0;//Timer2_溢出中断禁止
TR2 = 0;//Timer2_计时停止
TF2=0;//溢出中断标志
TH2=0;
TL2=0;
}

void T0_ISR(void) interrupt 1
{
//ET0 = 0;
//TR0 = 0;
Output_32=~Output_32;
Output_33=~Output_33;
//ET0 = 1;
//TR0 = 1;
}
/*
void INIT0_ISR(void) interrupt 0
{
  EX0=0;
  ET2 = 0;
  TR2 = 0;//Timer2_计时停止
 
  *px = TH2;
    px++;
  *px = TL2;
   px++;
   
// ACC = TH2;
// B = TL2;
 TH2=0;
 TL2=0;

}
*/
void INIT0_ISR(void) interrupt 0
{
 EX0=0;
 ET2 = 0;
 TR2 = 0;//Timer2_计时停止
 
 Data_1[0] = TH2;//高4位
 Data_1[1] = TL2;//低4位
 
 TH2=0;
 TL2=0;
}


菜鸟
2011-12-18 00:00:12     打赏
22楼
谁帮我看看我上面的超声波程序里的bug,为什么误差很大,有时很不灵敏???今天调试了一下午了也没新进展。。。。。

菜鸟
2011-12-19 14:48:21     打赏
23楼

按键控制音乐播放程序
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

uchar Song_Index = 0,Tone_Index = 0;
sbit SPK = P3^7;
sbit K1 = P1^0;
uchar code DSY_CODE[]=
{
  0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90
};

uchar code HI_LIST[]=
{
  0,266,229,232,233,236,238,240,241,242,244,245,246,247,248
};
uchar code LO_LIST[]=
{
  0,4,13,10,20,3,8,6,2,23,5,26,1,4,3
};

uchar code Song[][50]=
{
  {1,2,3,1,1,2,3,1,3,4,5,3,4,5,3,4,5,5,6,1,2,3,6,2,6,6,2,3,4,5,2,5,2,3,-1},
 {3,3,3,5,6,2,3,4,6,2,6,4,2,1,2,3,6,2,5,6,3,4,5,6,2,3,1,2,3,5,1,2,3,-1},
 {3,2,1,3,2,1,1,2,3,1,3,5,1,5,4,6,2,6,3,4,2,3,6,2,3,4,2,1,1,1,2,2,2,3,3,2,6,3,5,3,5,1,2,6,4,5,-1}
};

uchar code Len[][50]=
{
  {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,2,2,2,2,2,2,2,2,2,2,2,1,1,-1},
 {1,1,1,1,1,1,1,1,1,1,1,2,2,1,1,1,2,2,2,2,1,1,1,1,1,1,1,1,-1},
 {1,1,2,1,1,2,1,1,1,1,1,1,1,1,2,1,1,1,2,1,1,1,1,1,1,1,2,1,2,1,2,1,2,1,1,1,1,1,2,2,-1}
};

void EX0_INT() interrupt 0
{
  TR0 = 0;
 Song_Index = (Song_Index+1)%3;
 Tone_Index = 0;
 P2 = DSY_CODE[Song_Index];
}

void T0_INT() interrupt 1
{
  SPK = !SPK;
 TH0 = HI_LIST[Song[Song_Index][Tone_Index]];
 TL0 = LO_LIST[Song[Song_Index][Tone_Index]];
}

void delay(uint ms)
{
  uchar i;
 while(ms--)
 {
  for(i=0;i<120;i++);
 }
}

void main()
{
  P2 = 0xc0;
 IE = 0x83;
 TMOD = 0x00;
 IT0 = 1;
 IP = 0x02;
 while(1)
 {
   while(K1==1);
  while(K1==0);
  TR0 = 1;
  Tone_Index = 0;
  while(Song[Song_Index][Tone_Index]!=-1&&K1==1&&TR0==1)
  {
    delay(300*Len[Song_Index][Tone_Index]);
   Tone_Index++;
  }
  TR0 = 0;
  while(K1==0);
 }
}


菜鸟
2011-12-21 01:50:57     打赏
24楼
弱弱的问一下 谁用过stc12c2052  求指教

菜鸟
2011-12-25 20:42:53     打赏
25楼
假期偷懒了    祝各位圣诞快乐~

菜鸟
2011-12-27 14:11:15     打赏
26楼
比较经典的案例:
项目1 基于AT89S52单片机的交通灯控制系统设计 项目2 基于AT89S52单片机的温度控制系统设计 项目3 基于AT89S52单片机的控制步进电机 项目4 基于AT89S52单片机的人体反应速度测试仪设计 项目5 基于AT89S52单片机的多音阶电子琴设计 项目6 基于AT89S52单片机的数字音乐盒设计 项目7 基于AT89S52单片机的万年历设计 项目8 基于AT89S52单片机的4×4矩阵键盘设计 项目9 基于AT89S52单片机的数字电压表设计 项目10 基于AT89S52单片机的密码锁设计 项目11 基于AT89S52单片机的多模式带音乐跑马灯设计 项目12 基于AT89S52单片机的LED数字倒计时器设计 项目13 基于AT89S52单片机的简易函数信号发生器设计 项目14 基于AT89S52单片机的数字温度计设计 项目15 基于AT89S52单片机的LED点阵显示电子钟设计 项目16 基于AT89S52单片机的超声波测距仪设计 项目17 基于AT89S52的学习型红外线遥控器设计 项目18 基于AT89S52单片机的抢答器设计 项目19 基于AT89S52单片机与上位机的通信系统设计 项目20 基于AT89S52单片机的遥控器设计 项目21 基于AT89S52单片机的数字钟设计 项目22 基于AT89S52单片机的水温控制器设计 项目23 基于AT89S52单片机的SD卡读写器设计 项目24 基于AT89S52单片机的带时间及声光提示的抢答器设计 项目25 基于AT89S52单片机的简易计算器设计 项目26 基于AT89S52单片机的脉搏测量器设计 项目27 基于AT89S52单片机的比赛记分牌设计 项目28 基于AT89S52单片机的简易频率计设计 项目29 基于AT89S52单片机的数显交通灯设计 项目30 基于AT89S52单片机的LCD数字测速仪设计

菜鸟
2011-12-29 13:26:32     打赏
27楼


音乐播放程序:
#include "reg52.h"
unsigned char Count;
sbit _Speak =P2^3 ;   //讯响器控制脚 

unsigned char code SONG[] ={      //祝你平安
0x26,0x20,0x20,0x20,0x20,0x20,0x26,0x10,0x20,0x10,0x20,0x80,0x26,0x20,0x30,0x20,
0x30,0x20,0x39,0x10,0x30,0x10,0x30,0x80,0x26,0x20,0x20,0x20,0x20,0x20,0x1c,0x20,
0x20,0x80,0x2b,0x20,0x26,0x20,0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x80,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x60,0x40,0x10,0x39,0x10,0x26,0x20,
0x30,0x20,0x30,0x20,0x39,0x10,0x26,0x10,0x26,0x80,0x26,0x20,0x2b,0x10,0x2b,0x10,
0x2b,0x20,0x30,0x10,0x39,0x10,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x20,
0x20,0x10,0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x18,0x20,0x18,0x20,0x26,0x20,
0x20,0x20,0x20,0x40,0x26,0x20,0x2b,0x20,0x30,0x20,0x30,0x20,0x1c,0x20,0x20,0x20,
0x20,0x80,0x1c,0x20,0x1c,0x20,0x1c,0x20,0x30,0x20,0x30,0x60,0x39,0x10,0x30,0x10,
0x20,0x20,0x2b,0x10,0x26,0x10,0x2b,0x10,0x26,0x10,0x26,0x10,0x2b,0x10,0x2b,0x80,
0x18,0x20,0x18,0x20,0x26,0x20,0x20,0x20,0x20,0x60,0x26,0x10,0x2b,0x20,0x30,0x20,
0x30,0x20,0x1c,0x20,0x20,0x20,0x20,0x80,0x26,0x20,0x30,0x10,0x30,0x10,0x30,0x20,
0x39,0x20,0x26,0x10,0x2b,0x10,0x2b,0x20,0x2b,0x40,0x40,0x10,0x40,0x10,0x20,0x10,
0x20,0x10,0x2b,0x10,0x26,0x30,0x30,0x80,0x00,
                                  //路边的野华不要采
0x30,0x1C,0x10,0x20,0x40,0x1C,0x10,0x18,0x10,0x20,0x10,0x1C,0x10,0x18,0x40,0x1C,
0x20,0x20,0x20,0x1C,0x20,0x18,0x20,0x20,0x80,0xFF,0x20,0x30,0x1C,0x10,0x18,0x20,
0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,
0x30,0x80,0xFF,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x20,0x1C,0x10,0x18,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,
0x20,0x2B,0x40,0x20,0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,
0x40,0x20,0x20,0x2B,0x20,0x26,0x20,0x20,0x20,0x30,0x80,0x20,0x30,0x1C,0x10,0x20,
0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,0x15,0x1F,
0x05,0x20,0x10,0x1C,0x10,0x20,0x20,0x26,0x20,0x2B,0x20,0x30,0x20,0x2B,0x40,0x20,
0x30,0x1C,0x10,0x18,0x20,0x15,0x20,0x1C,0x20,0x20,0x20,0x26,0x40,0x20,0x20,0x2B,
0x20,0x26,0x20,0x20,0x20,0x30,0x30,0x20,0x30,0x1C,0x10,0x18,0x40,0x1C,0x20,0x20,
0x20,0x26,0x40,0x13,0x60,0x18,0x20,0x15,0x40,0x13,0x40,0x18,0x80,0x00,

};

void Time0_Init()
{
 TMOD = 0x01;
 IE   = 0x82;
 TH0  = 0xD8;
 TL0  = 0xEF;  //12MZ晶振,10ms
}

void Time0_Int() interrupt 1
{
 TH0 = 0xD8;
 TL0 = 0xEF;
 Count++;   //长度加1
}

/*-------------------------------------------------
功能:1MS延时子程序
-------------------------------------------------*/
void Delay_xMs(unsigned int x)
{
    unsigned int i,j;
    for( i =0;i < x;i++ )
    {
        for( j =0;j<3;j++ );
    }
}

/*-------------------------------------------------
功能:歌曲播放子程序i为播放哪一段曲目
-------------------------------------------------*/
void Play_Song(unsigned char i)
{
 unsigned char Temp1,Temp2;
 unsigned int Addr;
 Count = 0;      //中断计数器清0
 Addr = i * 217;
 while(1)
 {
  Temp1 = SONG[Addr++];
     if ( Temp1 == 0xFF )          //休止符
     {
      TR0 = 0;
      Delay_xMs(100);
     }
     else if ( Temp1 == 0x00 )   //歌曲结束符
     {
      return;
     }
     else
     {
      Temp2 = SONG[Addr++];
      TR0 = 1;
     while(1)
     {
       _Speak = ~_Speak;
       Delay_xMs(Temp1);
       if ( Temp2 == Count )
       {
        Count = 0;
        break;
       }
      }
     }
 }
}
/*-------------------------------------------------
功能:主程序
-------------------------------------------------*/
void main()
{
 Time0_Init();   //定时器0中断初始化
 while(1)
 {
  Play_Song(0);  //播放
 }
}

 


共27条 3/3 1 2 3 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]