蓝桥杯模块学习15——频率测量(深夜学习——单片机)

一、硬件电路

1、蓝桥杯板子上的电路:

(1)虽然不知道客观题会不会考555定时器的内容,但是对于编程题我们只需要知道NE555可以输出频率可调的正弦波即可

(2)通过改变RB3的阻值我们可以调节产生的正弦波的频率

(3)我们可以通过将15和16脚连接,用计数器0对产生的正弦波进行测量

二、频率测量实验

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;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值