一、硬件电路
1、蓝桥杯板子上的电路:
![](https://img-blog.csdnimg.cn/img_convert/25f72231a271432233d853b3213403a3.png)
(1)虽然不知道客观题会不会考555定时器的内容,但是对于编程题我们只需要知道NE555可以输出频率可调的正弦波即可
(2)通过改变RB3的阻值我们可以调节产生的正弦波的频率
![](https://img-blog.csdnimg.cn/img_convert/c8c67dcf447936cb2a56a1a9d590fe93.png)
(3)我们可以通过将15和16脚连接,用计数器0对产生的正弦波进行测量
二、频率测量实验
![](https://img-blog.csdnimg.cn/img_convert/612be3dd7daa9ce1065ca01ae4ccb451.png)
1、代码思路:
关闭无关设备——》定时器1和计数器0初始化——》数码管——》频率测量
2、参考代码:
#include <STC15F2K60S2.H>
#include <stdio.H>
#define u8 unsigned char
#define u16 unsigned int
code unsigned char Seg_Table[] =
{
0xc0, //0
0xf9, //1
0xa4, //2
0xb0, //3
0x99, //4
0x92, //5
0x82, //6
0xf8, //7
0x80, //8
0x90, //9
0x88, //A
0x83, //b
0xc6, //C
0xa1, //d
0x86, //E
0x8e //F
};
//数码管
u8 COD[8],COT[9],PSI;
u16 seg_delay;
void Close_All();
void SEG_Proc();
void Timer1_Init(void);
void Count0_Init();
void main()
{
Close_All();
Timer1_Init();
Count0_Init();
while(1)
{
SEG_Proc();
}
}
/***********定时器、计数器*************/
void Timer1_Init(void) //1毫秒@12MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xFC; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; //使能定时器1中断
EA = 1;
}
void SEG_Show(u8 COD,u8 PSI);
void Timer1_Isr(void) interrupt 3
{
if(seg_delay++ == 1000)seg_delay = 0;
SEG_Show(COD[PSI],PSI);
if(PSI++ == 7)PSI = 0;
}
void Count0_Init()
{
//计数器模式
TMOD |= 0x05;
TH0 = 0;
TL0 = 0;
TR0 = 1;
}
/****************数码管******************/
/*
将输入的字符串转化成数码管代码
*/
void SEG_STL(u8* input,u8* output)
{
u8 i=0;
for(i=0;i<8;i++)
{
switch(input[i])
{
case '0':output[i] = Seg_Table[0];break;
case '1':output[i] = Seg_Table[1];break;
case '2':output[i] = Seg_Table[2];break;
case '3':output[i] = Seg_Table[3];break;
case '4':output[i] = Seg_Table[4];break;
case '5':output[i] = Seg_Table[5];break;
case '6':output[i] = Seg_Table[6];break;
case '7':output[i] = Seg_Table[7];break;
case '8':output[i] = Seg_Table[8];break;
case '9':output[i] = Seg_Table[9];break;
case 'F':output[i] = Seg_Table[15];break;
default:output[i] = 0xff;
}
}
}
/*
数码管显示函数、
*/
void SEG_Show(u8 COD,u8 PSI)
{
//消隐
P0 = 0xff;
P2 = P2 & 0x1f | (0x70<<1);
P2 &= 0x1f;
//位选
P0 = 0x01<<PSI;
P2 = P2 & 0x1f | (0x60<<1);
P2 &= 0x1f;
//段选
P0 = COD;
P2 = P2 & 0x1f | (0x70<<1);
P2 &= 0x1f;
}
/***************************************/
/*
关闭无关设备
*/
void Close_All()
{
//关闭蜂鸣器和继电器
P0 = 0x00;
P2 = P2 & 0x1f | (0x50<<1);
P2 &= 0x1f;
//关闭LED
P0 = 0xff;
P2 = P2 & 0x1f | (0x40<<1);
P2 &= 0x1f;
}
void SEG_Proc()
{
u16 count;
if(seg_delay)return;
seg_delay = 1;
count = (TH0<<8) | TL0;
sprintf(COT,"F %5u",(u16)count);
SEG_STL(COT,COD);
TH0 = 0;
TL0 = 0;
}