技术热线: 4007-888-234

PIC16C54一个警报声程序

更新时间: 2019-03-23
阅读量:1827

PIC16C54一个警报声程序

刚好最近一个产品要产生这种声音,觉得有些意思,把它贴出来,感兴趣的不妨玩一下.

/*
chip:pic16c54
OSC:HS  
FREQUENCY :20MHz
WDT:DISABLE
*/
#include "pic.h"
#define beep_out RA2    //音效输出
char tmr_buff;
char cycle_buff;
char cycle_set;
int change_set;
int change_count;
bit change_flag;
void main(void)
{TRISA=0X0;
    PORTA=0;
    OPTION=4;
    cycle_buff=52;
    cycle_set=100;
    change_set=196;
    TMR0=0;
    while(1)
    {     
        if(TMR0==0)
            {TMR0=(cycle_set-1)^0XFF;
                beep_out=!beep_out;
                change_count+=cycle_set;
                if(change_count>change_set)
                {change_count-=change_set;
                    if(change_flag)
                    {if(++cycle_set>195)
                        {change_flag=0;
                        }
                    }
                else
                    {if(--cycle_set<52)
                        {change_flag=1;
                        }
                    }
                }
            }
    }
}