📄 fasheng.c
字号:
//09.12 04:09:05
#include <C8051F120.h> // SFR declarations
#include <intrins.h>
//#define SYSCLK 24500000 // Internal oscillator frequency in Hz
#define uchar unsigned char
#define uint unsigned int
sbit SS = P1^1; //片选 推挽
sbit SCK = P1^0; //ISD4002时钟 推挽
sbit MOSI = P1^2; //数据输入 推挽
sbit ISD_INT= P0^5;
uchar t;
void Watchdog_Init (void) //关闭看门狗
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
WDTCN = 0xDE; // Disable the Watchdog Timer
WDTCN = 0xAD;
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
void Oscillator_Init (void) //时钟初始化
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
OSCICN = 0x83; // Set the internal oscillator to
// 24.5 MHz
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
void PORT_Init (void) //端口初始化
{
unsigned char SFRPAGE_save = SFRPAGE; // Save the current SFRPAGE
SFRPAGE = CONFIG_PAGE; // Switch to the necessary SFRPAGE
P0MDOUT = 0x00;
P1MDOUT = 0x07;
XBR0 = 0x00; // Enable the SPI on the XBAR
// XBR1 = 0x04; // INT0->P0.0
XBR2 = 0x40; // Enable the XBAR and weak pull-ups
SFRPAGE = SFRPAGE_save; // Restore the SFRPAGE
}
void Init_Device (void)
{
Watchdog_Init ();
Oscillator_Init ();
PORT_Init ();
}
void delay()
{
uchar i;
for(i=0;i<5;i++);
}
void delay_25ms() //24.5M系统时钟下
{
uchar i,j;
for(i=0;i<255;i++)
for(j=0;j<255;j++);
}
void SPI_Send(unsigned char dat) //发送一个字节
{
unsigned char i;
// SS=0; //SS=0,打开spi通信端
SCK=0;
for(i=0;i<8;i++) //先发低位再发高位,依次发送(不能搞乱)
{
dat=dat>>1;
MOSI=CY; //输出1位
delay(); //应该稍微延时一下,不过试了一下,不延时也可以
SCK=1;
delay();
SCK=0;
delay();
}
}
void ISD_UpPwr(void) //上电指令
{
delay();
SS=0;
SPI_Send(0x00);
SPI_Send(0x20);
SS=1;
}
void ISD_DnPwr(void) //掉电指令
{
delay();
SS=0;
SPI_Send(0x00);
SPI_Send(0x30);
SS=1;
}
void ISD_Stop(void) //停止指令
{
delay();
SS=0;
SPI_Send(0x00);
SPI_Send(0x10);
SS=1;
}
void ISD_SetPlay(uchar addr) //设置放音地址
{
delay();
SS=0;
SPI_Send(addr); //地址
SPI_Send(0xE0);
SS=1;
}
void ISD_Play() //从当前地址开始放音
{
delay();
SS=0;
SPI_Send(0x00);
SPI_Send(0xF0);
SS=1;
}
void FaYin() //发音
{
ISD_UpPwr(); //上电
delay_25ms(); //等待上电延时
delay_25ms();
ISD_SetPlay(0x10); //发地址为10的SetPlay命令
ISD_Play(); //发Play命令,开始发音
while(ISD_INT); //这个是用来检测放音情况,放完一段,该引脚变低电平
/* t=0;
delay_25ms();
ISD_SetPlay(0x60); //发地址为60的SetPlay命令
ISD_Play(); //发Play命令,开始发音
while(ISD_INT); //这个是用来检测放音情况,放完一段,该引脚变低电平
t=0;
*/
ISD_Stop();
}
void main (void)
{
Init_Device();
FaYin();
while(1)
{
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -