⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c.bak

📁 ISD1700语音芯片放音的ISP实现
💻 BAK
字号:
#include <iom64v.h>
#include <macros.h>
#include <string.h>
#include "timer.h"
#include "main.h"

unsigned int soundTime=0;

//三个指示灯点灯函数
//id为指示灯号,分别为1,2,3
//cmd为点灯方式,0为关闭指示灯,1为调用该函数就“翻转”一次,2为每次点灯亮一段时间,3为关闭2
void ShowLed(unsigned char id, unsigned char cmd)
{
    static unsigned int LedTimer[3];
    switch (id)
    {
        case 1:
            if (cmd == 0)
            {
                RUN1_OFF();
            }
            else if (cmd == 1)
            {
                RUN1();
            }
            else if (cmd == 2)
            {
                RUN1_ON();
                LedTimer[0] = GetTime();
            }
            else if (cmd == 3)
            {
                if (GetPassTime(LedTimer[0]) >= 50)
                {
                    RUN1_OFF();
                }
            }
            break;
        case 2:
            if (cmd == 0)
            {
                RUN2_OFF();
            }
            else if (cmd == 1)
            {
                RUN2();
            }
            else if (cmd == 2)
            {
                RUN2_ON();
                LedTimer[1] = GetTime();
            }
            else if (cmd == 3)
            {
                if (GetPassTime(LedTimer[1]) >= 50)
                {
                    RUN2_OFF();
                }
            }
            break;
        case 3:
            if (cmd == 0)
            {
                RUN3_OFF();
            }
            else if (cmd == 1)
            {
                RUN3();
            }
            else if (cmd == 2)
            {
                RUN3_ON();
                LedTimer[2] = GetTime();
            }
            else if (cmd == 3)
            {
                if (GetPassTime(LedTimer[2]) >= 50)
                {
                    RUN3_OFF();
                }
            }
            break;
        default:
            break;
    }
}

// 端口初始化函数
void Port_Init(void)
{
    PORTA = 0x00;
    DDRA = 0x00;
    PORTB = 0x00;
    DDRB = 0xE7;
    PORTC = 0x00;
    DDRC = 0x00;
    PORTD = 0x00;
    DDRD = 0xC0;
    PORTE = 0x00;
    DDRE = 0x00;
    PORTF = 0x00;
    DDRF = 0x00;
    PORTG = 0x00;
    DDRG = 0x00;
}

//SPI initialize
// clock rate: 1000000hz
void spi_init(void)
{
    SPCR = 0x7e; //setup SPI
    SPSR = 0x00; //setup SPI
    PORTB |= (1<<PB0);	//SS=1
    /*SPDR = 0x00;
    while (!(SPSR &(1 << SPIF)))
        ;
    SPDR = 0x00;
    while (!(SPSR &(1 << SPIF)))
        ;
    SPDR = 0x00;
    while (!(SPSR &(1 << SPIF)))
        ;
    SPDR = 0x00;
    while (!(SPSR &(1 << SPIF)))
        ;*/
}

//call this routine to initialise all peripherals
void init_devices(void)
{
    //unsigned char temp=0;
    //stop errant interrupts until set up
    CLI(); //disable all interrupts
    XDIV = 0x00; //xtal divider
    XMCRA = 0x40; //external memory
    Port_Init();
    watchdog_init();
    timer1_init();
    spi_init();

    MCUCR = (1 << SRE); //0x80,外部存储使能
    //EICRA:外部中断3-0敏感电平控制BIT7--0:ISC31|ISC30......ISC01|ISC00
    //ISCn1|ISCn0:00-低电平触发,01-保留,10-下降沿触发,11-上升沿触发
    EICRA = 0x02;
    //EICRB:外部中断7-4敏感电平控制BIT7--0:ISC71|ISC70......ISC41|ISC40
    //ISCn1|ISCn0:00-低电平触发,01-任意逻辑电平变化,10-两次采用发生下降沿就触发,11-两次采用发生上升沿就触发
    EICRB = 0x00;
    EIMSK = 0x01; //外部中断屏蔽寄存器BIT7--0:INT7--INT0,1使能0关闭
    //TIMSK:定时器中断屏蔽寄存器BIT7--0:OCIE2|TOIE2|TICIE1|OCIE1A|OCIE1B|TOIE1|OCIE0|TOIE0
    TIMSK = (1 << TOIE1); //0x04
    //ETIMSK:扩展定时器中断屏蔽寄存器BIT7--0:--|--|TICIE3|OCIE3A|OCIE3B|TOIE3|OCIE3C|OCIE1C
    ETIMSK = 0x00; //(1<<TOIE3);	//0x04
    SEI(); //re-enable interrupts
    //all peripherals are now initialised
}

void Sound_Flay(void)
{
    int i;
    PORTB &= ~(1<<PB0);	//SS=0
    for(i=0;i<100;i++)
    	;
    SPDR = 0x50;
    while (!(SPSR &(1 << SPIF)))
        ;
    SPDR = 0x00;
    while (!(SPSR &(1 << SPIF)))
        ;
    /*SPDR = (led >> 8) &0xff;
    while (!(SPSR &(1 << SPIF)))
        ;
    SPDR = (led >> 0) &0xff;
    while (!(SPSR &(1 << SPIF)))
        ;*/
        
	for(i=0;i<100;i++)
    	;
    PORTB |= (1<<PB0);	//SS=1
}

//主函数
void main(void)
{
    static unsigned char secondcount=0;
    unsigned char soundFlag=0;
    
    init_devices(); //全局初始化

    while (1)
    {
        WDR(); //内部watchdog
		if(GetPassTime(soundTime)>=1000)
        {
        	soundTime=GetTime();
        	secondcount++;
        	if(secondcount>=20)
        	{
        		secondcount=0;
        		soundFlag=1;
        	}
        	RUN1();
        }
        
        if(soundFlag)
        {
        	soundFlag=0;
        	Sound_Flay();
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -