PIC单片机CCS之C语言(#IF,#ENDIF的用法)
串转换成整型数送给i
return(i);
}
signed long get_long() {
char s[7]; //声明字符型数组s[7]
signed long l; //声明有符号长整型变量l
get_string(s, 7); //从RS232口读7个字节,存储到s数组中
l=atol(s); //将数组s[]的字符串转换成长整型数送给l
return(l);
}
float get_float() {
char s[20]; //声明字符型数组s[7]
float f; //声明符点型变量l
get_string(s, 20); //从RS232口读20个字节,存储到s数组中
f = atof(s); //将数组s[]的字符串转换成符点数送给f
return(f);
}
#endif //结束if定义
文件: 2416.c如下:
//// Library for a MicroChip 24LC16B ////
//// init_ext_eeprom(); Call before the other functions are used ////
//// write_ext_eeprom(a, d); Write the byte d to the address a ////
//// d = read_ext_eeprom(a); Read the byte d from the address a ////
//// b = ext_eeprom_ready(); Returns TRUE if the eeprom is ready ////
//// to receive opcodes ////
//// The main program may define EEPROM_SDA ////
//// and EEPROM_SCL to override the defaults below. ////
//// Pin Layout ////
//// ----------------------------------------------------------- ////
//// | | ////
//// | 1: NC Not Connected | 8: VCC +5V | ////
//// | 2: NC Not Connected | 7: WP GND | ////
//// | 3: NC Not Connected | 6: SCL EEPROM_SCL and Pull-Up | ////
//// | 4: VSS GND | 5: SDA EEPROM_SDA and Pull-Up | ////
//// ----------------------------------------------------------- ////
#ifndef EEPROM_SDA //若没有定义EEPROM_SDA,则执行下面
#define EEPROM_SDA PIN_C4 //用EEPROM_SDA代替PIN_C4
#define EEPROM_SCL PIN_C3 //用EEPROM_SCL代替PIN_C3
#endif //结束if定义
#use i2c(master, sda=EEPROM_SDA, scl=EEPROM_SCL)
// master设置成主机方式
//除非指定了FORCE_HW,否则会产生模拟I2C的软件函数.
//使能I2C_START, I2C_STOP直到下一个#USE I2C的出现为止.
//使能I2C_READ, I2C_WRITE直到下一个#USE I2C的出现为止.
//使能I2C_POLL直到下一个#USE I2C的出现为止.
//指定sda脚为EEPROM_SDA, 指定scl脚为EEPROM_SCL
#define EEPROM_ADDRESS long int //用EEPROM_ADDRESS代替long int
#define EEPROM_SIZE 2048 //用EEPROM_SIZE代替2048
void init_ext_eeprom() {
output_float(EEPROM_SCL); //将EEPROM_SCL引脚设为输入,开集电极连接
output_float(EEPROM_SDA); //将EEPROM_SDA引脚设为输入,开集电极连接
}
BOOLEAN ext_eeprom_ready() {
int1 ack; //声明位变量ack
i2c_start(); //发送启动条件
ack = i2c_write(0xa0); //发送从机地址0xa0;若ack=0,表示从机应答(ACK);
//若ack=1,表示从机不应答(NO ACK);
i2c_stop(); //发送停止条件
return !ack;
} // ext_eeprom_ready()函数,若返回1,表示从机准备好; 若返回0,表示从机忙或eeprom坏了
void write_ext_eeprom(long int address, BYTE data) {
while(!ext_eeprom_ready()); //若从机忙,则主机等待
i2c_start(); //发送启动条件
i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //发送命令字的高字节(发送写命令)
i2c_write(address); //发送命令字的低字节
i2c_write(data); //发送数据
i2c_stop(); //发送停止条件
}
BYTE read_ext_eeprom(long int address) {
BYTE data; //声明字节变量data
while(!ext_eeprom_ready()); //先发器件地址,若从机忙,则主机等待
i2c_start(); //在此处是:发送重新启动条件
i2c_write( (0xa0|(BYTE)(address>>7))&0xfe); //发送命令字的高字节(发送写命令)
i2c_write(address); //发送命令字的低字节
i2c_start(); //发送启动条件
i2c_write( (0xa0|(BYTE)(address>>7))|1); //发送命令字的高字节(发送读命令)
data=i2c_read(0); //读I2C数据,然后发送ack=0(不用从机应答)
i2c_stop(); //发送停止条件
return(data); //返回所读到的I2C数据
}
上面的例子主要用来读写24C16,通过PC机RS232进行验证
PIC单片机CCSIFENDI 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)