微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 模拟电路设计 > ZipAmp中英文对照表

ZipAmp中英文对照表

时间:07-23 来源:互联网 点击:

i2c.c
/*
Copyright 2003 Nasif Akand (nasif@yifan.net)
http://go.to/zipamp
http://zipamp.virtualave.net

This file is part of ZipAmp MP3 software.

ZipAmp is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License or
(at your option) any later version.

ZipAmp is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this code; if not write to the Free Software
Foundation Inc. 59 Temple Place Suite 330 Boston MA 02111-1307 USA
这一个文件是 ZipAmp MP3 软件的一部份。

ZipAmp 是免费的软件;你能重新分配它及[或] 修正
它在角马公众执照的术语之下当做出版被
免费的软件基础; 或执照的 2 版 或
(在你的选项)任何的较迟版本。

ZipAmp 在希望中被分配它将会是有用的
但是没有任何的担保; 没有更甚至被暗示的担保
为一个特别的目的 MERCHANTABILITY 或健身。 那
为较多的细节角马公众执照。

你应该要接受角马公众执照的副本
连同这一个密码一起; 如果不 写到免费的软件
基础,公司, 59个寺庙地方,随员 330 ,波士顿 文学硕士 02111-1307 美国

*/

//This module performs I2C operations for ZipAmp /这是zipamp的i2c程序组件
//Nasif Akand

//If using CodeVision then following includes aren't needed. /如果使用 CodeVision 不需要包括下列各项
//#include "zcontrol.h"
//#include "zfat.h"
//#include "zata.h"
//#include "ztype.h"
#include "i2c.h"
//#include "" //CodeVision AVR internal delay routines /CodeVision AVR 内在的延迟常式

//I2C implementation for STA013 通过i2c控制sta013

//Port D bit 7 (pin 17) is SDA output while bit 0 (pin 10) is input Pin 16 is SCL
//SDA input is always at HighZ state. If this pin is pulled up or output high STA013 will be damaged.
//这里是定意的i2c sda数据的输入输出口,输出为17脚,输入为10脚,时钟信号scl为16脚。

#define SDA PORTD.7
#define SCL PORTD.6
#define SDAIn PIND.0
#define Out DDRD.7=1
#define In DDRD.7=0

#define qDelay delay_us(3) //According to Phillips I2C specs in all modes.. /这里是按照菲利普i2c总线模式写的
#define delay delay_us(3) //..max I2C delay can be 5 micro seconds. /最大 I2C 延迟可能是 5个微秒。
#define Read 0x87 //STA013 I2C Read address /STA013 I2C 读取地址
#define Write 0x86 //STA013 I2C Write Address /STA013 I2C 写入地址

//下面这段函数是复位sta013
void STA013Reset() {
DDRB.3=1; //Set bit3 to Output mode /设置pb3为输出状态
PORTB.3=0; //Set to 0 for reset /这里可以参看原理图就明白。pb3是连在sta013的26脚/reset上的.
delay_ms(500); //Wait
PORTB.3=1; //Set to 1
delay_ms(500); //Wait
}


//下面这段数据类型声明是用来测试i2c是否有正确应答,如果有就输出0则表示成功
unsigned char wait_Ack (void) {
//returns 1 if ACK was 0 (meaning transfer was successful). In I2C ACK=0 means success.

unsigned char t=1;
delay;

// Take clock high
SCL=1;
delay;

// Test of ACK
delay;
if (PIND.0) { /*test of SDA level if high -> problem*/
t=0;
}

delay;
if (PIND.0) { /*test of SDA level if high -> problem*/
t=0;
}

// Take clock back low
delay;
SCL=0;
delay;

return t;
}



//数据声明,声明i2c的阅读地址 adr为地址的缩写
unsigned char i2cReadAddress(unsigned char adr) {
//Returns data read from register given in adr./从寄存器中读取数据后返回
//In most cases we don't have to worry about it. If we don't get ACK try 10 times before giving up.
//在大部分的情况下,我们不需要为它担忧,在拿不到正确的应答之前要尝试十次。
unsigned char data try=0 ack;
do {
i2cStart(); //I2c start /i2c开始
ack=i2cSend(Write); //Send i2c write address /发送i2c写入地址
ack =i2cSend(adr); //Send register address /发送记录地址
i2cStart(); //i2c re-start
delay; //wait
ack = i2cSend(Read); //Send i2c read address
data=i2cReceive(0); //Receive data without sending ACK
i2cStop(); //Stop
try++;
}
while ((ack==0)(try10)); //try 10 times if failed /尝试10次后返回

return data;
}

//数据声明,声明i2c的写入地址。这段和上面一段类似只不过由读变成了写
unsigned char i2cWriteAddress(unsigned char adr unsigned char data) {
// Writes data to register adr.
//Returns 1 on successful write tries 10 times before giving up.

unsigned char ack try=0;
do {
i2cStart(); //Start
ack = i2cSend(Write); //Send i2c write address
ack = i2cSend(adr); //Send register address
delay; //wait
ack = i2cSend(data); //Send data
i2cStop(); //Stop
try++;
}
while ((ack==0)(try10)); //give up after 10 tries
return ack;
}

//这段函数是初始化i2c
void i2cInit() {
DDRD.7=1; //Set to output mode for SDA
DDRD.6=1; //Set to output mode for SCL
}

//下面这段函数是i2c开始的必要条件
void i2cStart() {
//I2C start condition.

DDRD.7=1; //SDA output mode
SDA=1;
delay;
SCL=1;
delay;
SDA=0;
delay;
SCL=0;
}

//数据声明,发送i2c数据到sta013后返回,成功为1,失败为0,这里发送的字节不大,一次为8个2进制数据,也就是1bit
unsigned char i2cSend(unsigned char value){
//Sends byte value and returs ACK from STA013 1=Success 0=Fail

unsigned char i;
DDRD.7=1;
for (i=0; i8; i++) {
DDRD.7=1;
if (((value >> (7-i)) 0x01)==0x01) SDA=1; //Send bit by bit
else SDA=0;
SCL=1;
delay;
SCL=0;
DDRD.7=1;
SDA=1;
delay;
}
delay;
i=wait_Ack();
return i;
}


//数据声明,和上面的声明类似,不过变成了接收。
unsigned char i2cReceive(unsigned char ack) {
//Receives data byte. Sends back acknoledge if ack is true

unsigned char value=0 i;
DDRD.7=1;
SDA=1;
for (i=0; i8; i++) {
delay;
SCL=1;
delay;
value = (value 1) + PIND.0; //leftshift previous value and add one more bit that's read
delay;
SCL=0;
}
if (ack) { //send ack if requested
DDRD.7=1;
SDA=0;
delay;
SCL=1;
delay;
SCL=0;
}
return value;
}

//此函数是i2c停止的条件
void i2cStop(){
//I2C stop condition.

delay;
DDRD.7=1; //SDA output mode
SDA=0;
delay;
SCL=1;
delay;
SDA=1;
delay;
}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top