📄 main.h
字号:
#define A1 0x40 // A1 = P1_6
#define A0 0x08 // A0 = P1_3
#define I2CBASE 0x40 // I2C address = 0x86/0x86 with no pulldowns
#define LED_AUTO 0x04
#define NBUTTONS 8 // Number of buttons
#define INT_PIN 0x10 // P1_4
#define INT_POL_MASK 0x02 // Control register, bit 1
#define INT_ENA_MASK 0x01 // Control register, bit 0
#define INT_STAT_MASK 0x80 // Control register, bit 7
struct I2C_RegType
{
BYTE Control; // Control register
BYTE LEDs; // LED On/Off register
BYTE Buttons; // Button state
#ifdef debug
int adc[NBUTTONS]; // Button raw data
int baseline[NBUTTONS]; // Baseline data
int difference[NBUTTONS]; // Button differences
#endif
} I2Cregs;
BYTE dacCurrent[] = {44,42,36,35,32,30,34,38}; // Individual DAC current for switch 0 - 7
BYTE scanSpeed[] = {30,29,25,14,10,10,12,13}; // Individual scan speed for switch 0 - 7
BYTE prev_buttons = 0; // Keeps track of button press status
void Setup_IO(void)
{
int i;
//Make sure the LEDs are OFF
PRT2DR = 0xFF;
I2Cregs.LEDs = 0x00;
//ReConfigure I2C I/O. Fixes Start-Up glitch by leaving IO
//in tri-state on power-up, then switching to I2C mode
PRT1DM2 |= 0xA0;
PRT1DM1 |= 0xA0;
PRT1DM0 |= 0xA0;
//Set INT to default polarity (Active Low), ie no button pressed
PRT1DR |= INT_PIN;
//Set default INT values in control reg
I2Cregs.Control = 0x00;
I2Cregs.Control &= ~INT_ENA_MASK; //Enable interrupt pin
//Active low polarity
//No interrupt pending
//Set Pull-Ups for A1/A0, P1_6/P1_3
PRT1DR |= 0x48;
}
void Setup_I2C(void)
{
unsigned char PortData, I2Caddr, I2Cstatus;
//Set up I2C address
//Internal to PSoC, the address is right shifted by 1 bit
//I2CBASE = b0100 0000
//If A0 is pulled low on the chip, we set A0, 0000 0001
//If A1 is pulled low on the chip, we set A1, 0000 0010
//When no resistors are placed, the address is 0100 0011
//This translates to b10000110 or 0x87R, 0x86W. Clear ??
PRT1DR |= (A1|A0); //Turn on internal pull-ups for A1,A0 pins
PortData = PRT1DR; //Read A1,A0 data from P1_6, P1_3
I2Caddr = I2CBASE;
I2Caddr = (PortData & A0) ? (I2Caddr|0x01) : I2Caddr; //If A0 bit=1, OR address with 0x01
I2Caddr = (PortData & A1) ? (I2Caddr|0x02) : I2Caddr; //If A1 bit=1, OR address with 0x02
I2C_Start(); //Start I2C hardware
I2C_SetAddr(I2Caddr); //Set I2C address from strap pins
I2C_SetRamBuffer(sizeof(I2Cregs), 6, (BYTE *) &I2Cregs); //I2Cregs defined in main.h
//Init the other reristers
I2Cregs.Control |= LED_AUTO ;
I2Cregs.Buttons = 0x00;
}
void Setup_Touch(void)
{
SENSE_Start();
SENSE_baBtnFThreshold[0] = 80;
SENSE_baBtnFThreshold[1] = 100;
SENSE_baBtnFThreshold[2] = 100;
SENSE_baBtnFThreshold[3] = 90;
SENSE_baBtnFThreshold[4] = 90;
SENSE_baBtnFThreshold[5] = 100;
SENSE_baBtnFThreshold[6] = 100;
SENSE_baBtnFThreshold[7] = 100;
}
BYTE Scan_Buttons()
{
BYTE i,status;
for( i=0; i < NBUTTONS; i++)
{
SENSE_SetDacCurrent(dacCurrent[i],SENSE_DAC_LOW); //Set DAC current for this button
SENSE_SetScanSpeed(scanSpeed[i]); //Set scan speed for this button
SENSE_StartScan(i,1,SENSE_SCAN_ONCE); //Scan the button
while(!(SENSE_GetScanStatus() & SENSE_SCAN_SET_COMPLETE)); //Wait until it's done
#ifdef debug
I2Cregs.adc[i] = SENSE_iReadSwitch(i);
I2Cregs.baseline[i] = SENSE_iaSwBaseline[i];
I2Cregs.difference[i]= SENSE_iaSwDiff[i];
#endif
}
status = SENSE_bUpdateBaseline(0);
if(*SENSE_baSwOnMask!=prev_buttons) prev_buttons = *SENSE_baSwOnMask;
return(status);
}
void Set_Interrupt_Pin()
{
if(*SENSE_baSwOnMask!=prev_buttons)
{
if( !(I2Cregs.Control&INT_ENA_MASK) )
{
I2Cregs.Control |= INT_STAT_MASK; //Set interrupt in register
PRT1DR = (I2Cregs.Control&INT_POL_MASK)? PRT1DR|INT_PIN : PRT1DR&~INT_PIN; //And set INT pin
}
}
if (I2C_GetActivity() & I2C_READ_ACTIVITY) //On I2C read
{
I2Cregs.Control &= ~INT_STAT_MASK; //Clear control register status bit
PRT1DR = (I2Cregs.Control&INT_POL_MASK)? PRT1DR&~INT_PIN : PRT1DR|INT_PIN; //Clear INT pin
}
}
void Set_LEDS()
{
PRT2DR = *SENSE_baSwOnMask;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -