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

📄 demo_16_1__.c

📁 CVAVR完整程序
💻 C
字号:
/*****************************************************
File name           : demo_16_1.c
Chip type           : ATmega16
Program type        : Application
Clock frequency     : 4.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/
#include <mega16.h>
	#ifndef __SLEEP_DEFINED__
	#define __SLEEP_DEFINED__
	.EQU __se_bit=0x40
	.EQU __sm_mask=0xB0
	.EQU __sm_powerdown=0x20
	.EQU __sm_powersave=0x30
	.EQU __sm_standby=0xA0
	.EQU __sm_ext_standby=0xB0
	.EQU __sm_adc_noise_red=0x10
	.SET power_ctrl_reg=mcucr
	#endif
#include <stdio.h>				// 使用CVAVR的标准 Input/Output 函数
#include <i2c.h>				// 使用CVAVR的I2C函数
#include <delay.h>				// 使用CVAVR的延时函数

#define EEPROM_BUS_ADDRESS 0xa0

#asm							// 将 2 个通用I/O口定义成SDA、SCL,
    .equ __i2c_port=0x15		// 本例使用PORTC
    .equ __sda_bit=1			// SDA使用PC1
    .equ __scl_bit=0			// SCL使用PC0
#endasm

// read a byte from the 24C256
unsigned char eeprom_read(unsigned int address)
{
    unsigned char data;
    i2c_start();						// 发起始信号
    i2c_write(EEPROM_BUS_ADDRESS);		// 发写从机写寻址字节
    i2c_write(address>>8);				// 发存储单元地址高字节
    i2c_write(address);					// 发存储单元地址低字节
    i2c_start();						// 发起始信号
    i2c_write(EEPROM_BUS_ADDRESS | 1);	// 发从机读寻址字节
    data=i2c_read(0);					// 读一个字节数据,返回NO ACK
    i2c_stop();						// 发停止信号
    return data;
}

// write a byte to the 24C256
void eeprom_write(unsigned int address, unsigned char data)
{
    i2c_start();						// 发起始信号
    i2c_write(EEPROM_BUS_ADDRESS);		// 发写从机写寻址字节
    i2c_write(address>>8);				// 发存储单元地址高字节
    i2c_write(address);					// 发存储单元地址低字节
    i2c_write(data);					// 写一个字节数据到24C256
    i2c_stop();						// 发停止信号
    delay_ms(10);			// 等待10ms,保证24C256内部写操作完成再进行新操作
}

void main(void)
{
    unsigned char i;
    UCSRA=0x00;		// USART initialization
    UCSRB=0x18;		// Communication Parameters: 8 Data, 1 Stop, No Parity
    UCSRC=0x86;		// USART Receiver: On,USART Transmitter: On
    UBRRH=0x00;		// USART Mode: Asynchronous	,USART Baud Rate: 9600
    UBRRL=0x19;
    //================================================
    i2c_init();		  // initialize the I2C bus
    eeprom_write(0x00aa,0x5a);		// write the byte 55h at address 00AAh
    i = eeprom_read(0x00aa);		// read the byte from address 00AAh
    //================================================
    while (1)
    {
     	putchar(i);
       	delay_ms(250);
   	}
}

⌨️ 快捷键说明

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