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

📄 demo_16_3.c~

📁 AVR单片机嵌入式系统原理与应用实践例码
💻 C~
字号:
/*****************************************************
File name           : demo_16_3.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>
#include <stdio.h>				// 使用CVAVR的标准 Input/Output 函数
#include <delay.h>				// 使用CVAVR的延时函数

#define EEPROM_BUS_ADDRESS 0xa0

// TWSR values (not bits) 
#define TWPS0 0 
#define TWPS1 1 
#define TWEN  2 
#define TWIE  0 
#define TWEA  6 
#define TWINT 7 
#define TWSTA 5 
#define TWSTO 4 

// Master 
#define TW_START              0x08 
#define TW_REP_START            0x10 
// Master Transmitter 
#define TW_MT_SLA_ACK            0x18 
#define TW_MT_SLA_NACK            0x20 
#define TW_MT_DATA_ACK            0x28 
#define TW_MT_DATA_NACK            0x30 
#define TW_MT_ARB_LOST            0x38 
// Master Receiver 
#define TW_MR_ARB_LOST            0x38 
#define TW_MR_SLA_ACK            0x40 
#define TW_MR_SLA_NACK            0x48 
#define TW_MR_DATA_ACK            0x50 
#define TW_MR_DATA_NACK            0x58 

void I2C_init(void)
{
// 2 Wire Bus initialization
// Generate Acknowledge Pulse: On
// 2 Wire Bus Slave Address: 0h
// General Call Recognition: Off
// Bit Rate: 250.000 kHz
    TWSR=0x00;
    TWBR=0x00;
    TWAR=0x00;
    TWCR=0x44;
    PORTC.0 = 1;
    PORTC.1 = 1;   
}

//产生启动信号
unsigned char I2C_start(void)
{
  TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
  while (!(TWCR & (1<<TWINT))){};
  return 1;
}

//产生停止信号
void I2C_stop(void)
{
  TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}

//向总线写一字节,并返回有无应答
unsigned char I2C_write(unsigned char c)
{
    unsigned char ack=1;
    TWDR = c;
    TWCR = (1<<TWINT)|(1<<TWEN);
    while (!(TWCR & (1<<TWINT))){};
    if((TWSR & 0xF8) != TW_MT_SLA_ACK)
        ack = 0;
    return ack;  
}

//读一字节 ack: 1时应答,0时不应答
unsigned char I2C_read(unsigned char ack)
{
    if (ack)
        TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWEA);    
    else
        TWCR = (1<<TWINT)|(1<<TWEN);
    while (!(TWCR & (1<<TWINT))){};    
    return(TWDR);  
}

// 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,0x7b);		// 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 + -