4109 views|3 replies

15

Posts

0

Resources
The OP
 

stc microcontroller simulates I2C bus writing [Copy link]

I am using STC8a8k microcontroller, and want to realize EEPROM reading and writing by simulating I2C bus, but I always feel that nothing is written in, and the result of reading it through serial port is always FF, no matter what is written in. I am a novice, please help me. The program is as follows: #include#include #define OP_READ 0xa1 #define OP_WRITE 0xa0 sbit SDA = P2^4; [size=14px ]sbit SCL = P2^5; void ConfigUART(void); /*Delay*/ //void delay1ms() //{[/size ] // unsigned char i,j; // for(i = 0;i < 10;i++) // { // for(j = 0;j < 33;j++); // } //} void delay1ms() //@11.0592MHz[/size ] { unsigned char i,j; i = 15; j = 90; do {[/ size] while (--j); } while (--i); } /*Delay nms*/ void delaynms(unsigned char n) { unsigned char i; for(i = 0;i < n;i++) { delay1ms(); } } /****************************** ****************** Start function ****************** ***************************/ void start() { SDA = 1; SCL = 1; _nop_(); _nop_ (); _nop_(); _nop_(); SDA = 0; [size=14px ] _nop_(); _nop_(); _nop_(); _nop_(); SCL = 0;[/ size] } /******************************** *************************************** Stop function ************************************************ ****************************/ void stop() [size =14px]{ SDA = 0; SCL = 1; _nop_(); [size= 14px] _nop_(); _nop_(); _nop_(); SDA = 1; [size= 14px] _nop_(); _nop_(); _nop_(); _nop_(); [ size=14px] SDA = 0; SCL = 0; } /******** *************************************************** ********* Read data ************************************************ *************************/ unsigned char ReadData() {[/ size] unsigned char i; unsigned char x; for(i = 0;i < 8;i++) [ size=14px] { SCL = 1; x<<=1; x|=(unsigned char)SDA ; SCL = 0; } return(x); } /********************************** ********************************* Write data [size =14px]****************************************************** *******************/ bit WriteCurrent(unsigned char y) { unsigned char i; bit ack_bit; for(i = 0;i < 8;i++) { SDA = (bit)(y&0x80); _nop_ (); SCL = 1; _nop_(); _nop_(); [size=14px ] SCL = 0; y<<=1; } SDA = 1; [size= 14px] _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); _nop_(); _nop_();[/ size] ack_bit = SDA; SCL = 0; return ack_bit; } /************************************************ ******************* HHH ****************** *********************************************/ void WriteSet(unsigned char add,unsigned char dat) { start(); WriteCurrent(OP_WRITE); WriteCurrent(add);[/size ] WriteCurrent(dat); stop(); delaynms(4); }[/ size] unsigned char ReadCurrent() { unsigned char x; [size =14px] start(); WriteCurrent(OP_READ); x = ReadData(); stop(); return x; } unsigned char ReadSet(unsigned char set_addr)[/ size] { start(); WriteCurrent(OP_WRITE); WriteCurrent(set_addr);[ /size] return(ReadCurrent()); } void main() {[/ size] SDA = 1; SCL = 1; WriteSet(0x00,0x0f); EA = 1; //Enable interrupts ConfigUART(); SBUF = ReadSet( 0x00); } void ConfigUART(void) { PCON &= 0x7F ; //Baud rate is not doubled SCON = 0x50; //8-bit data, variable baud rate AUXR |= 0x40; //Timer 1 clock is Fosc,i.e. 1T AUXR &= 0xFE; //Serial port 1 selects timer 1 as the baud rate generator TMOD &= 0x0F; //Clear timer 1 mode bit TMOD |= 0x20; //Set timer 1 to 8-bit automatic reload mode TL1 =0xDC;//256-(11059200/12/32)/baud; //Set the initial value of the timing TH1 = TL1; //Set the timer reload value ET1 = 0; //Disable timer 1 interrupt ES = 1; TR1 = 1; //Start timer 1 void InterruptUART() interrupt 4{ if(RI) //Check if the receive flag is 1{ RI = 0; //Clear if 1 SBUF = SBUF; //Add 1 to data } if(TI) //Check if the transmit flag is 1{ TI = 0; //Clear if 1 }}
This post is from 51mcu

Latest reply

bit WriteCurrent(unsigned char y) { unsigned char i; bit ack_bit; for(i = 0;i < 8;i++) { SDA = (bit)(y&0x80); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0; y  Details Published on 2018-10-12 17:39
 

3471

Posts

11

Resources
2
 
Check the pin correspondence. Check the timing. An oscilloscope is best. If you don't have the equipment, simulate it.
This post is from 51mcu
 
 
 

19

Posts

0

Resources
3
 
Thanks a lot~
This post is from 51mcu
 
 
 

29

Posts

0

Resources
4
 
bit WriteCurrent(unsigned char y) { unsigned char i; bit ack_bit; for(i = 0;i < 8;i++) { SDA = (bit)(y&0x80); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0; y<<=1; } In this paragraph, does it put the data of SDA into the CY shift register?
This post is from 51mcu
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list