简体   繁体   English

PIC 16F628A扬声器声音

[英]PIC 16F628A Speaker sound

I'm trying to understand this code : 我正在尝试理解以下代码

#include <pic.h>
#include  "delay.h"

#define brisi_flag timer_flag=0

unsigned char timer;
unsigned char impuls;
unsigned char ton_koji_ne_svira;
static bit disable @ (unsigned)&PORTA*8+4;
static bit izlaz @ (unsigned)&PORTA*8+1;
static bit izlaz_inv @ (unsigned)&PORTA*8+0;
static unsigned char stanja @ 0x2e;
static bit stanje_izlaza @ (unsigned)&stanja*8+1;
static bit  timer_flag @ (unsigned)&INTCON*8+2; 

void
DelayMs_2(unsigned char cnt)
{
    unsigned char   i;
    do { asm("CLRWDT");
      if(ton_1)
         return;
        i = 4;
        do {
            DelayUs(250);
        } while(--i);
    } while(--cnt);
}

void
DelayMs(unsigned char cnt)
{
    unsigned char   i;
    do { asm("CLRWDT");
        i = 4;
        do {
            DelayUs(250);
        } while(--i);
    } while(--cnt);
}

void ton_1_2(unsigned char onaj_drugi_ton)
{
     izlaz=0;
     izlaz_inv=1;
     stanje_izlaza=0;
     TMR0=timer; 
ton_1_2_start:
     brisi_flag;
     disable=0;
ton_1_2_sviranje:
     while(timer_flag==0)
          {
        if((TMR0-timer)>=impuls)
             {izlaz=0;
              izlaz_inv=0;
             }
          }
     brisi_flag;
     TMR0=timer;
     if(stanje_izlaza==0)
        izlaz_inv=1;
     else   
        izlaz=1;
     stanje_izlaza=stanje_izlaza+1;   
     if((PORTA&0x0c)==onaj_drugi_ton)
        return;
     asm("CLRWDT");
     goto ton_1_2_sviranje;                 
}

void main()
{
    CMCON=0x07;//portA su normalni ulazi (za 16F628A)
    TRISA=0x0c;
    TRISB=0xff;
     disable=1;
    OPTION=0x81;   //WDT na 18 msec, preset TMR0 sa 4   

    izlaz=0;
    izlaz_inv=0;

    timer=107;
        impuls=0x1d;

    ton_1_2(ton_koji_ne_svira);
}

This program generates 800 Hz sound on speaker. 该程序在扬声器上产生800 Hz的声音。 I don't understand how to calculate frequency (it must be something with variables impuls and timer ). 我不懂如何计算频率(一定是带有变量impulstimer东西)。 This code is for PIC16F628A , it has external oscillator set at 4Mhz . 该代码用于PIC16F628A ,它的外部振荡器设置为4Mhz

Here is how I understand the code. 这是我理解代码的方式。

The timer starts at 107. As the timer interrupt triggers on overflow the period is 256-107 = 149 ticks. 计时器从107开始。随着计时器中断在溢出时触发,周期为256-107 = 149滴答。 During each period, the outputs are turned off after IMPULS=29 ticks. 在每个周期中,在IMPULS = 29个滴答之后,输出将关闭。 Hence you have a 29/149=19% duty cycle. 因此,您有一个29/149 = 19%的占空比。 In addition, the pulses alternate between two pins. 另外,脉冲在两个引脚之间交替。

OPTION=0x81 sets a 1:4 prescaler for the timer. OPTION = 0x81设置定时器的1:4预分频器。 Hence if the clock runs at 4MHz (period 0.25µs), the timer ticks at 1MHz (period 1µs). 因此,如果时钟以4MHz(周期0.25µs)运行,则计时器以1MHz(周期1µs)计时。 Thus a period of 149 ticks should equal 149µs and correspond to a frequency of 6711Hz. 因此,149个滴答的周期应等于149µs,并对应于6711Hz的频率。 Adding the time it takes to start the timer each time the actual frequency is slightly lower. 加上每次实际频率稍低时启动计时器所需的时间。

Are you sure the frequency is 800Hz ? 您确定频率为800Hz吗? Did you measure it ? 你测量了吗? Maybe the processor is really running at 500Hz, or there is a global 1:8 prescaler somewhere. 也许处理器确实以500Hz的频率运行,或者某个地方存在全局1:8预分频器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM