atmega88,内部晶振7.3-8.1M.
DDRC = 0x3F; //SDA,SCL 设置为输出,取消内部上拉电阻。
PORTC = 0x00; //
void twi_init(void)
{
TWCR= 0X00; //disable twi
TWBR= 0x10; //set bit rate
TWSR= 0x00; //set 0 prescale // about 200K bit
//TWAR= 0xA2; //set slave address // mast do not need this
TWCR= 0x44; //enable twi ;have ack when it is necessary; (1<<TWEA)|(1<<TWEN)
}
INT8U tw_start(void)
{
TWCR |=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // send the signal of start
while (!(TWCR & (1<<TWINT))); // wait for the ack
if((TWSR & 0xF8)!=TW_START) //check 0x08
{ PORTB &=~(1<<PB7);return error;}//ERROR();}
return ok;//
}//////
在tw_start中收到 TWSR & 0xF8=0x26,实际应为0x08;观察数据线为一脉冲波。上拉电阻已经加上了,请教下是什么原因?刚开始接触AVR,谢谢各位高手! |