鲁东大学毕业设计
P2=count;
//用于动态扫描数码管
P0=Display[count]; count++;
if(count==2) //表示扫描2个数码管 count=0; } 其中
P2=count;
P0=Display[count];
此两个语句不能颠倒,否则数码管显示两位是颠倒的。
PCF8591模拟信号输出端为单片机输出的累加数字信号,D/A输出信号波形如图20所示
图20 D/A输出信号
5 总结
经过这次毕业设计的学习,熟练掌握了Proteus和Keil软件的使用方法,锻炼了
运用所学知识解决问题的能力,提高了C语言和汇编编程的水平。能够设计一般I2C总线电路,并运用于工业控制环境中,解决实际问题。
14
鲁东大学毕业设计
程序附录
24C02读写C语言程序[10]:
#include
#define ROM_READ 0xa1 // 器件地址以及读取操作 #define ROM_WRITE 0xa0 // 器件地址以及写入操作
unsigned char code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//共阳极数
int eepromdata; //从EEPROM里读出来的数据 sbit SDA = P3^4; sbit SCL = P3^3; sbit SET = P1^7; sbit CH= P3^7;
void delayms(unsigned char ms) {
unsigned char i; while(ms--) {
for(i=0;i<120;i++); } }
void delay(void) {
int k;
for(k=0;k<400;k++); }
void start() //开始信号 {
SDA = 1; SCL = 1; _nop_(); _nop_(); SDA = 0; _nop_(); _nop_(); _nop_(); _nop_(); SCL = 0; }
void stop() //停止信号 {
SDA = 0; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); _nop_();
15
码管显示
鲁东大学毕业设计
_nop_(); SDA = 1; }
unsigned char shin() //读取数据 {
unsigned char i,read_data; for(i=0;i<8;i++) {
SCL = 1;
read_data <<= 1;
read_data |= (unsigned char)SDA; SCL = 0; }
return(read_data); }
bit shout(unsigned char write_data) //向EEPROM写数据 {
unsigned char i; bit ack_bit;
for(i =0;i<8;i++) {
SDA = (bit)(write_data & 0x80); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0;
write_data <<= 1; }
SDA = 1; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); _nop_(); _nop_();
ack_bit = SDA; // 读取应答 SCL = 0;
return ack_bit; // 返回AT24Cxx应答位 }
void write_byte(unsigned char addr, unsigned char write_data) //向指定地址写数据 {
start();
shout(ROM_WRITE); shout(addr);
shout(write_data); stop();
delayms(10); }
16
鲁东大学毕业设计
unsigned char read_current() //读取当前地址数据 {
unsigned char read_data; start();
shout(ROM_READ); read_data =shin(); stop();
return read_data; }
unsigned char read_random(unsigned char random_addr) //向指定地址读数据 {
start();
shout(ROM_WRITE); shout(random_addr); return(read_current()); }
void display(int k) //显示子程序 {
P2=0x01; //最高位数码管1234 P0=tab[k/1000]; delay(); P2=0x02;
P0=tab[k00/100]; delay(); P2=0x04;
P0=tab[k0/10]; delay(); P2=0x08;
P0=tab[k]; delay(); }
void main(void) //主程序 {
while(SET==1); SDA = 1; SCL = 1;
eepromdata=0;
write_byte(0x01,0x55); //向0x01地址写入0x55(85)的数据 delayms(250);
write_byte(0x02,0xAA); //向0x02地址写入0xAA(170)的数据 delayms(250); delayms(250); delayms(250); while(1) {
if(CH==1)
eepromdata= read_random(0x01);
else eepromdata= read_random(0x02); // 读取其中一个地址内的数据来验证 display(eepromdata);
17
鲁东大学毕业设计
display(eepromdata); display(eepromdata); display(eepromdata); display(eepromdata); if(SET==1) break; } }
PCF8574扩展串行口汇编语言如下:
ORG 0100H SDA BIT P3.4 SCL BIT P3.3 S BIT P1.7 START:JB S,$ SETB SDA SETB SCL CLR SDA
//开始
MOV A,#41H //8574读方式
LCALL SBU1 //调用写一字函数
CLR SCL MOV R6,#08H
L2:SETB SDA //输入一个数 SETB SCL MOV C,SDA RLC A CLR SCL DJNZ R6,L2
ANL A,#0F0H //高四位 CLR SCL SETB SDA
SETB SCL
//读应答
SWAP A //高低四位互换 CPL A
//按位取反
MOV 30H,A
18