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

📄 init_m16.c~

📁 F:程序24C02EEPROM.rar主要读写外部EEPROM
💻 C~
字号:

void init_m16(void)
{
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x05;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x01;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// I2C Bus initialization
i2c_init();

// Global enable interrupts
#asm("sei")

}     
/*************************************************************
**名称:unsigned char eeprom_readbyte(unsigned char device,unsigned char address)
**功能:向24c02中写入一个字节数据
**输入:EEPROM地址
**返回:EEPROM数据
**说明:
**************************************************************/
unsigned char e2prom_readbyte(unsigned char device,unsigned char address)
{
unsigned char get_data;
i2c_start();
i2c_write(device);
i2c_write(address);
i2c_start();
i2c_write(device|1);
get_data = i2c_read(0);
i2c_stop();
return get_data;
}

/*************************************************************
**名称:void eeprom_writenbyte(unsigned char device,unsigned char address,unsigned char data)
**功能:向24c02中写入一个字节数据
**输入:地址,数据
**返回:无
**说明:
**************************************************************/
void e2prom_writenbyte(unsigned char device,unsigned char address,unsigned char data)
{
i2c_start();
i2c_write(device);
i2c_write(address);
i2c_write(data);
i2c_stop();     
delay_ms(10);
}

/*************************************************************
**名称:bit writenbyte()
**功能:向24c02中写入多字节数据
**输入:
**返回:
**说明:device-器件地址 addresses-数据地址,*s-写入的数据,n-写入的字节数
                (n<=8)
**************************************************************/
void e2prom_writenarray(unsigned char device,unsigned char address,unsigned char *s,unsigned char n)
{
        unsigned char  counter;
        for (counter=0;counter<n;counter++)//写入8字节数据
            {
              e2prom_writenbyte(device,address,*s); 
              address++;
              s++;
            }
}
/************************************************************
**名称:bit readnbyte()
**功能:从24c02中读出n字节数据(n<=8)
**输入:
**返回:
**说明:
**************************************************************/
unsigned char *e2prom_readarray(unsigned char device,unsigned char address,
                                unsigned char *ptr_data,unsigned char n)
{
        unsigned char counter;
        for (counter=0;counter<n;counter++)//读取字节数据
            {
               *ptr_data = e2prom_readbyte(device,address);//读取数据
               address++;
               ptr_data++;
            }
        return ptr_data;
}                     

⌨️ 快捷键说明

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