' y1
Dim yi As Int = 0
Dim Y_COMMAND_BYTE As Byte = 0x90 ' Send One Byte Y Command
Dim MSB, LSB As Byte = 0 ' Receive Back Two Bytes
SPI.Transfer_Byte(Y_COMMAND_BYTE) ' send command byte
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
yi = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
yi = yi + LSB
yi = Bit.ShiftRight(yi,3)
Sub xpt2046_spi(cmd As Byte) As Int
Dim MSB, LSB As Byte = 0
Dim xpt_data As Int
SPI.Transfer_Byte(cmd) ' send one byte command
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
' xpt_data = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3;
xpt_data = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
xpt_data = xpt_data + LSB
xpt_data = Bit.ShiftRight(xpt_data,3)
Return xpt_data
End Sub
Sub xpt2046_spi(cmd As Byte) As Int
Dim MSB, LSB As Byte = 0
Dim xpt_data As Int
Dim cmd_Array() As Byte = Array As Byte (0,0,0)
Dim rcv_Array() As Byte = Array As Byte (0,0,0)
cmd_Array(0) = cmd
SPI.Transfer_Byte_Array(cmd_Array, rcv_Array) ' Send One Command Byte Rcv Two Data Bytes back
MSB = rcv_Array(1)
LSB = rcv_Array(2)
' xpt_data = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3;
xpt_data = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8)
xpt_data = xpt_data + LSB
xpt_data = Bit.ShiftRight(xpt_data,3)
Return xpt_data
End Sub
Dim MSB, LSB As Byte = 0
Dim xpt_data As Int
SPI.Transfer_Byte(cmd) ' send one byte command
MSB = SPI.Transfer_Byte(0)
LSB = SPI.Transfer_Byte(0)
xpt_data = xpt_data + LSB
xpt_data = Bit.ShiftRight(xpt_data,3)
int16_t data[6];
int z;
if (!isrWake) return;
uint32_t now = millis();
if (now - msraw < MSEC_THRESHOLD) return;
if (_pspi) {
_pspi->beginTransaction(SPI_SETTING);
digitalWrite(csPin, LOW);
_pspi->transfer(0xB1 /* Z1 */);
int16_t z1 = _pspi->transfer16(0xC1 /* Z2 */) >> 3;
z = z1 + 4095;
int16_t z2 = _pspi->transfer16(0x91 /* X */) >> 3;
z -= z2;
if (z >= Z_THRESHOLD) {
_pspi->transfer16(0x91 /* X */); // dummy X measure, 1st is always noisy
data[0] = _pspi->transfer16(0xD1 /* Y */) >> 3;
data[1] = _pspi->transfer16(0x91 /* X */) >> 3; // make 3 x-y measurements
data[2] = _pspi->transfer16(0xD1 /* Y */) >> 3;
data[3] = _pspi->transfer16(0x91 /* X */) >> 3;
}
else data[0] = data[1] = data[2] = data[3] = 0; // Compiler warns these values may be used unset on early exit.
data[4] = _pspi->transfer16(0xD0 /* Y */) >> 3; // Last Y touch power down
data[5] = _pspi->transfer16(0) >> 3;
digitalWrite(csPin, HIGH);
_pspi->endTransaction();
}
// If we do not have either _pspi or _pflexspi than bail.
else return;
It depends on the SPI device used, for example the ADS12xx has a pin that indicates when the conversion is done and thus proceed to read the result.If you don't want to wait, delegate SPI access to an external micro . I did 20 years ago, to keep on testing an external device for allarm condition. When this happened it was able to interrupt the main micro.
I rewrote the first sub and tested both. So, I will answer my own question.
I broke my only touch screen, but these two spi subs seem to work the same.
First Sub uses One Byte Xfer and Rcv. Second one uses the Transfer Byte Array Xfer and Rcv.
Xfer One Byte and Rcv Two Bytes back:Sub xpt2046_spi(cmd As Byte) As Int Dim MSB, LSB As Byte = 0 Dim xpt_data As Int SPI.Transfer_Byte(cmd) ' send one byte command MSB = SPI.Transfer_Byte(0) LSB = SPI.Transfer_Byte(0) ' xpt_data = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3; xpt_data = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8) xpt_data = xpt_data + LSB xpt_data = Bit.ShiftRight(xpt_data,3) Return xpt_data End Sub
Send One Command Byte Rcv Two Data Bytes back:Sub xpt2046_spi(cmd As Byte) As Int Dim MSB, LSB As Byte = 0 Dim xpt_data As Int Dim cmd_Array() As Byte = Array As Byte (0,0,0) Dim rcv_Array() As Byte = Array As Byte (0,0,0) cmd_Array(0) = cmd SPI.Transfer_Byte_Array(cmd_Array, rcv_Array) ' Send One Command Byte Rcv Two Data Bytes back MSB = rcv_Array(1) LSB = rcv_Array(2) ' xpt_data = (((MSB & MSB_BIT_MASK) << 8)+ LSB) >> 3; xpt_data = Bit.ShiftLeft(Bit.And(MSB,MSB_BIT_MASK),8) xpt_data = xpt_data + LSB xpt_data = Bit.ShiftRight(xpt_data,3) Return xpt_data End Sub
Dim b as Byte
b = SPI.Transfer_Byte(SPI.GetByteFromString("01100111"))
'We are interested only in the last 4 bits
b = SPI.ApplyMask(b, SPI.GetByteFromString("00001111"))
Log(b)
'We want to know the value of the 2nd bit from right in the byte
Dim iBitInSecondPositionFromRight As UInt = SPI.GetBitAt(b, 2)
Log(iBitInSecondPositionFromRight)
'This command will write to the logs "--" if the board is not supported
'and "-You can use this library with your board-" if your board is supported
SPI.CheckIfThisBoardIsSupported
'Disable Inerrupts
SPI.DisableInterrupts
'Enable Inerrupts
SPI.EnableInterrupts
SPI.CheckIfThisBoardIsSupported
'CHANGE HERE THE PIN NUMBERS (SCK, MISO, MOSI, CS)
'SS/CS =15 MISO=12 MOSI=13 SCK =14 T_IRQ = 0 <-- Pins for XPT2046 SPI
SPI.ESP32_SPI_Set_Pins(14,12,13,15)
SPI.CSPinDeActivate(SPI.CS__SPI)
..............
I am just reading T_IRQ in a timer loop to see when the display is touched and then I set a read flag.
MY SPI Code works fine.
Also, Hardware Interrupts are not supported in B4R, correct ?
My question is should I be defining T_IRQ in the SPI INIT and then use SPI.EnableInterrupts and SPI.DisableInterrupts to read the status of the T_IRQ pin when the Touch Screen is "Touched" ? Hopefully that makes sense.
..............
B4R generates Arduino code which is then compiled by Arduino. Since the Arduino code supports interrupts I cannot see why isn't it possible to set interrupts and set the program to run voids in case of an interrupt with InLine C. My intention though was the oposite. For uninterrupted communication you just follow this scheme: SPI.DisableInterrupts > {Communicate through SPI} > SPI.EnableInterrupts
As @Erel has mentioned here some times Interrupting can get complicated:
Simple interrupt in B4R
I have a C++ app running on an Arduino Nano which has at its core a fast A/D read triggered by a pin interrupt. So, a shaft running at around 3000rpm has a sensor which triggers the interrupt every revolution. I then need to save the time this occurs and then do several hundred fast A/D...www.b4x.com
Hello
Since the "PENIRQN" pin is available in the "XPT2046" what you can do is define it in the list of pins to notify of any change.
B4R has the "AddListener" Instruction that allows defining a pin so that it generates an interruption of the program and that request is attended. It would have to carry out the management of edges in the interruption call and manage what to do or how to proceed in it, this way of acting frees the ESP32 from being attentive to the change of the pin in question.
Check this option in case it could be valid for you.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?