控制数字电位器X9241的C程序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

贴一个控制数字电位器X9241的C程序,偌觉得好用就顶一下!//调试电位器X9241的程序

//硬件连接

//PE2-----SDA

//PE3-----SCL

//使用超级终端能看到显示的结果,我这里用来控制AD的放大倍数//通过改变数字电位器的阻值,来改变放大倍数。

//作者:黄勇 huangyongbj123@

//ICC-A VR application builder : 2006-9-12 14:32:16

// Target : M128

// CRYSTAL: 11.059MHZ

#include

#include

#define u CHAR unsigned CHAR

#define uint unsigned int

union

{

u CHAR TEMP_ad[2];

uint int_ad;

}ad;

//ADC initialize

// Conversion time: 75uS

void adc_init(void)

{

ADCSRA = 0x00; //disable adc

ADMUX = 0x40; //SELECT adc input 0

ACSR = 0x80;

ADCSRA = 0xE6;

}

void I2c_wait(u CHAR cnt)

{

u CHAR i,j;

for(j=0;j

for(i=0;i<100;i++);

}

void I2c_start(void)

{

DDRE=0X0C; //PE2-SDA PE3-SCL OUTPUT

PORTE|=BIT(PE2); //SDA=1;

PORTE|=BIT(PE3); //SCL=1;

I2c_wait(5);

PORTE&=~BIT(PE2); //SDA=0;

I2c_wait(5);

PORTE&=~BIT(PE3); //SCL=0;

}

void I2c_stop(void)

{

DDRE=0X0C; //PE2-SDA PE3-SCL OUTPUT

PORTE&=~BIT(PE2); //SDA=0;

I2c_wait(5);

PORTE|=BIT(PE3); //SCL=1;

I2c_wait(5);

PORTE|=BIT(PE2); //SDA=1;

}

u CHAR I2c_sendbyte(u CHAR udata)

{

u CHAR i,ack;

DDRE=0X0C;

for(i=0;i<8;i++)

{

if((udata&0x80)==0x80)

PORTE|=BIT(PE2); //SDA=1;

else PORTE&=~BIT(PE2); //SDA=0;

udata<<=1;

I2c_wait(5);

PORTE|=BIT(PE3); //SCL=1;

I2c_wait(5);

PORTE&=~BIT(PE3); //SCL=0;

I2c_wait(5);

}

PORTE|=BIT(PE2); //SDA=1; //接受ACK信号 DDRE=0X08;

I2c_wait(5);

PORTE|=BIT(PE3); //SCL=1;

I2c_wait(5);

if((PINE&0X04)==0x04) ack=0x01;

else ack=0x00;

// DDRE|=0X0C;

PORTE&=~BIT(PE3); //SCL=0;

I2c_wait(5);

return (ack);

}

u CHAR I2c_readbyte(void)

{

u CHAR i,TEMP;

DDRE=0X08;

for(i=0;i<8;i++)

{

PORTE|=BIT(PE3); //SCL=1;

I2c_wait(5);

TEMP<<=1;

if(PINE&0X04) TEMP|=0x01;

PORTE&=~BIT(PE3); //SCL=0; I2c_wait(5);

}

return (TEMP);

}

void delay_ms(u CHAR m)

{

unsigned int j;

for(;m>0;m--)

{

for(j=0;j<1000;j++);

}

}

void port_init(void)//IN-0X00 OUT-0XFF; {

PORTA = 0x00;

DDRA = 0x00;

PORTB = 0x00;

DDRB = 0xff;

PORTC = 0x00; //M103OUTPUT ONL Y DDRC = 0x00;

PORTD = 0x00;

DDRD = 0x00;

PORTE = 0xff;

DDRE = 0x00;

PORTF = 0x00;

DDRF = 0x00;

相关文档
最新文档