mega48的MPR121程序.docx

上传人:b****7 文档编号:11186229 上传时间:2023-02-25 格式:DOCX 页数:16 大小:17.58KB
下载 相关 举报
mega48的MPR121程序.docx_第1页
第1页 / 共16页
mega48的MPR121程序.docx_第2页
第2页 / 共16页
mega48的MPR121程序.docx_第3页
第3页 / 共16页
mega48的MPR121程序.docx_第4页
第4页 / 共16页
mega48的MPR121程序.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

mega48的MPR121程序.docx

《mega48的MPR121程序.docx》由会员分享,可在线阅读,更多相关《mega48的MPR121程序.docx(16页珍藏版)》请在冰豆网上搜索。

mega48的MPR121程序.docx

mega48的MPR121程序

基于mega48的MPR121程序,采用模拟IIC协议操作(普通IO操作),便于IO的灵活变更或其他处理器的程序兼容,uart.h为串口文件在此没有附上。

///////////////////File:

IIC.h////////////////////////

/*

*IIC.h

*

*Created:

2012/12/8周六23:

44:

11

*Author:

ly

*/

#ifndefIIC_H_

#defineIIC_H_

/*使用IO接口EEPROM,设置引脚电平高低OD*/

#defineSCL_LPORTC&=0xDF//(GPIO_WriteBit(GPIOB,PIN_I2C_SCL,Bit_RESET))PC5

#defineSCL_HPORTC|=0x20//(GPIO_WriteBit(GPIOB,PIN_I2C_SCL,Bit_SET))

#defineSDA_LPORTC&=0xEF//(GPIO_WriteBit(GPIOB,PIN_I2C_SDA,Bit_RESET))PC4

#defineSDA_HPORTC|=0x10//(GPIO_WriteBit(GPIOB,PIN_I2C_SDA,Bit_SET))

#defineBIT_SDA(PINC&0x10)//(GPIO_ReadInputDataBit(GPIOB,PIN_I2C_SDA)

#defineIRQ(PINC&0x08)

//MPR121RegisterDefines

#defineMHD_R0x2B

#defineNHD_R0x2C

#defineNCL_R0x2D

#defineFDL_R0x2E

#defineMHD_F0x2F

#defineNHD_F0x30

#defineNCL_F0x31

#defineFDL_F0x32

#defineELE0_T0x41

#defineELE0_R0x42

#defineELE1_T0x43

#defineELE1_R0x44

#defineELE2_T0x45

#defineELE2_R0x46

#defineELE3_T0x47

#defineELE3_R0x48

#defineELE4_T0x49

#defineELE4_R0x4A

#defineELE5_T0x4B

#defineELE5_R0x4C

#defineELE6_T0x4D

#defineELE6_R0x4E

#defineELE7_T0x4F

#defineELE7_R0x50

#defineELE8_T0x51

#defineELE8_R0x52

#defineELE9_T0x53

#defineELE9_R0x54

#defineELE10_T0x55

#defineELE10_R0x56

#defineELE11_T0x57

#defineELE11_R0x58

#defineFIL_CFG0x5D

#defineELE_CFG0x5E

#defineGPIO_CTRL00x73

#defineGPIO_CTRL10x74

#defineGPIO_DATA0x75

#defineGPIO_DIR0x76

#defineGPIO_EN0x77

#defineGPIO_SET0x78

#defineGPIO_CLEAR0x79

#defineGPIO_TOGGLE0x7A

#defineATO_CFG00x7B

#defineATO_CFGU0x7D

#defineATO_CFGL0x7E

#defineATO_CFGT0x7F

//GlobalConstants

#defineTOU_THRESH0x0B

#defineREL_THRESH0x07

#defineWR_ADDR0xB4

#defineRD_ADDR0xB5

//晶振7.3278M

//精确延时

//公用函数

voiddelay_nus(unsignedintn)//延时nus=5实际5~6us

{

//unsignedinti;

while(--n>1);

//for(i=1;i

}

voiddelay_1ms(void)//延时1ms实际999.5us

{

unsignedinti;

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

}

voiddelay_nms(unsignedintn)//Nms精确延时nms

{

unsignedinti=0;

for(i=0;i

delay_1ms();

}

/****************************************************************************

*输入参数:

*输出参数:

*返回结果:

*调用关系:

*功能介绍:

开始操作EEPROM.当SCL高时,SDA从高到低变化为启动过程

***************************************************************************/

voidI2C_Start(void)

{

//SCL_L;

//Delay

(1);

SDA_H;

delay_nus

(2);

SCL_H;

delay_nus(5);

SDA_L;

delay_nus

(2);

SCL_L;

delay_nus

(2);

}

/****************************************************************************

*输入参数:

ucByteValue-要写入到EEPROM中的字节

*输出参数:

*返回结果:

从EEPROM中读出的字节

*调用关系:

*功能介绍:

将单字节参数值写入EEPROM

***************************************************************************/

voidI2C_WR(unsignedcharucByteValue)

{

unsignedchari;

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

{

SCL_L;

delay_nus

(2);

if((ucByteValue&0x80)!

=0)

{

SDA_H;

}

else

{

SDA_L;

}

ucByteValue<<=1;

delay_nus

(2);

SCL_H;

delay_nus(5);

}

SCL_L;

delay_nus

(2);

}

/****************************************************************************

*输入参数:

*输出参数:

*返回结果:

从EEPROM中读出的字节

*调用关系:

*功能介绍:

从EEPROM中读出一个字节并返回

***************************************************************************/

unsignedcharI2C_RD(void)

{

unsignedchari,ucRdValue;

//ROMDAT=1;

SDA_H;DDRC&=0xEF;//Input

delay_nus

(2);

//PinSDAModeChange(IOMODE_INPUT);

ucRdValue=0;

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

{

ucRdValue<<=1;

SCL_L;

delay_nus(3);

SCL_H;

delay_nus(3);

if(BIT_SDA)

{

ucRdValue+=1;

}

}

//ROMCLK=0;

SCL_L;

//PinSDAModeChange(IOMODE_OUTPUT);

//SET_SDA_LOW;

delay_nus

(2);

DDRC|=0x10;

//I2C_DoSendAck();

return(ucRdValue);

}

/****************************************************************************

***************************************************************************/

voidI2C_End(void)

{

SCL_L;

delay_nus

(2);

SDA_L;

delay_nus

(2);

SCL_H;

delay_nus(5);

SDA_H;

delay_nus

(2);

}

voidI2C_ACK()

{

SDA_L;delay_nus

(2);

SCL_H;

delay_nus(4);

//SDA_H;

SCL_L;

}

voidI2C_NACK()

{

SDA_H;

delay_nus

(2);

SCL_H;

delay_nus(4);

SCL_L;

}

unsignedcharI2C_Check()

{

unsignedchari=0;

SDA_H;

DDRC&=0xEF;

SCL_H;

delay_nus(3);

while(i++<250)

if(!

BIT_SDA){DDRC|=0x10;SCL_L;return1;}

SCL_L;

DDRC|=0x10;

return0;

}

/****************************************************************************

***************************************************************************/

unsignedcharI2C_Read(unsignedchariAddress,unsignedchar*p,unsignedcharn)

{

unsignedchari;

I2C_Start();

I2C_WR(WR_ADDR);//ADDR->CSS

I2C_Check();

I2C_WR(iAddress);

I2C_Check();

I2C_Start();

I2C_WR(RD_ADDR);

I2C_Check();

for(i=0;i

{

p[i]=I2C_RD();

if(i

}

I2C_NACK();

I2C_End();

return1;

}

unsignedcharI2C_ReadB(unsignedcharaddress)

{

unsignedchartemp;

I2C_Start();

I2C_WR(WR_ADDR);//ADDR->CSS

I2C_Check();

I2C_WR(address);

I2C_Check();

I2C_Start();

I2C_WR(RD_ADDR);

I2C_Check();

temp=I2C_RD();

I2C_NACK();

I2C_End();

returntemp;

}

/****************************************************************************

***************************************************************************/

unsignedcharI2C_Write(unsignedchariAddress,unsignedchardat)

{

I2C_Start();//I2C启动

I2C_WR(WR_ADDR);//ADDR->CSS

delay_nus

(2);

if(I2C_Check()==0)return0;//ACK

I2C_WR(iAddress);//写子地址

delay_nus

(2);

if(I2C_Check()==0)return0;//ACK

I2C_WR(dat);//写数据到从器件

delay_nus

(2);

I2C_Check();

I2C_End();//I2C停止

delay_nus(10);

return1;

}

#endif/*IIC_H_*/

///////////////////////File:

main.c////////////////////////

/*CPUAtmega48VMPR121touchchip

*IOconnect(IIC)

*PC3->IRQPC4->SDAPC5->SCL

*DATE:

2012-12-9

*Author:

ly

*SimulationofIIC

*/

#include

#include"IIC.h"

#include"uart.h"

voidmpr121QuickConfig(void)

{

//SectionA

//Thisgroupcontrolsfilteringwhendatais>baseline.

I2C_Write(MHD_R,0x01);

I2C_Write(NHD_R,0x01);

I2C_Write(NCL_R,0x00);

I2C_Write(FDL_R,0x00);

//SectionB

//Thisgroupcontrolsfilteringwhendatais

I2C_Write(MHD_F,0x01);

I2C_Write(NHD_F,0x01);

I2C_Write(NCL_F,0xFF);

I2C_Write(FDL_F,0x02);

//SectionC

//Thisgroupsetstouchandreleasethresholdsforeachelectrode

I2C_Write(ELE0_T,TOU_THRESH);

I2C_Write(ELE0_R,REL_THRESH);

I2C_Write(ELE1_T,TOU_THRESH);

I2C_Write(ELE1_R,REL_THRESH);

I2C_Write(ELE2_T,TOU_THRESH);

I2C_Write(ELE2_R,REL_THRESH);

I2C_Write(ELE3_T,TOU_THRESH);

I2C_Write(ELE3_R,REL_THRESH);

I2C_Write(ELE4_T,TOU_THRESH);

I2C_Write(ELE4_R,REL_THRESH);

I2C_Write(ELE5_T,TOU_THRESH);

I2C_Write(ELE5_R,REL_THRESH);

I2C_Write(ELE6_T,TOU_THRESH);

I2C_Write(ELE6_R,REL_THRESH);

I2C_Write(ELE7_T,TOU_THRESH);

I2C_Write(ELE7_R,REL_THRESH);

I2C_Write(ELE8_T,TOU_THRESH);

I2C_Write(ELE8_R,REL_THRESH);

I2C_Write(ELE9_T,TOU_THRESH);

I2C_Write(ELE9_R,REL_THRESH);

I2C_Write(ELE10_T,TOU_THRESH);

I2C_Write(ELE10_R,REL_THRESH);

I2C_Write(ELE11_T,TOU_THRESH);

I2C_Write(ELE11_R,REL_THRESH);

//SectionD

//SettheFilterConfiguration

//SetESI2

I2C_Write(FIL_CFG,0x04);

//SectionE

//ElectrodeConfiguration

//Enable6Electrodesandsettorunmode

//SetELE_CFGto0x00toreturntostandbymode

//I2C_Write(ELE_CFG,0x0C);//Enablesall12Electrodes

I2C_Write(ELE_CFG,0x06);//Enablefirst6electrodes

//SectionF

//EnableAutoConfigandautoReconfig

I2C_Write(ATO_CFG0,0x0B);

I2C_Write(ATO_CFGU,0xC9);//USL=(Vdd-0.7)/vdd*256=0xC9@3.3VI2C_Write(ATO_CFGL,0x82);//LSL=0.65*USL=0x82@3.3V

I2C_Write(ATO_CFGL,0x82);

I2C_Write(ATO_CFGT,0xB5);//Target=0.9*USL=0xB5@3.3V

}

intmain(void)

{

init_uart(9600);//串口0初始化

/*************IO初始化**************/

DDRC|=0x30;SCL_H;SDA_H;

DDRC&=0xf7;//PC3input

PORTC|=0x08;

DDRD&=0xEF;PORTD|=0x10;

mpr121QuickConfig();

while(PIND&0x10);//PD4isAkeyIfpressed,continue

send_date(0xAA);

while

(1)

{

while(IRQ);

//send_date(0x55);

//send_date(I2C_ReadB(0x01));

send_date(I2C_ReadB(0x00));//sendthetouch_value

delay_nms(500);

}

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 临时分类 > 批量上传

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1