How to Interface and Program Touch Screen Controller (MXB7843) with Microcontroller (PIC18F452) ? Tutorial
For newbies, please refer to:
Overview of MXB7843 interfacing with Microcontroller
First of all take a look at the schematic of the interfacing circuitry :
First of all take a look at the schematic of the interfacing circuitry :
In-order to interface touch controller MXB7843 with the Microcontroller you only need to do 4 or 5 connections apart from the coding. Here is the general concept :
I assume that my readers are well aware of SPI communication. (However for newbies, here is some good stuff on "SPI Tutotial" ).
Timing diagrams of SPI interface with MX7843 is given below:
Touch screen controller gives the coordinates of X axis and Y axis when touch screen is touched. Interrupt is generated at the MXB7843 PENIRQ pin which tells the microcontroller that screen has been touched thereafter the controller send control bytes to it get the coordinates.Datasheet explain the algorithm to get the coordinates correctly. The procedure is given below:
- Set up the control byte and call it TB. TB should begin with the format: 1XXXXXXX binary, where X denotes the particular channel, selected conversion mode
- Use a general-purpose I/O line on the CPU to pull CS low
- Transmit TB and simultaneously receive a byte; call it RB1.
- Transmit a byte of all zeros ($00 hex) and simultaneously receive byte RB2.
- Transmit a byte of all zeros ($00 hex) and simultaneously receive byte RB3.
- Pull CS high.
As far as the control byte is considered, here are the options that one can play around with it :
By now, the formation of control byte remain no more an issue. Once the control byte is formed, what left is the coding, which is explained below for PIC18F452 Microcontroller, developed in C language on C18 compiler with MPLAB as IDE.
This code for touch screen has been designed for the background shown below :
// GROUP MEMBERS:
//
// 1. Muhammad Asif 2. Ahmed Fawad 3. MUHAMMAD Ahmed
//
//
// Code Includes: TOUCH SCREEN INTERFACING WITH USART
//
// Dated: 22 APR 2011
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// INCLUSION OF DIRECTORIES / CRYSTAL INITIALIZATION / MACROS
/////////////////////////////////////////////////////////////////////////////////
#include<p18F452.h>
#include<p18cxxx.h>
#include<delays.h>
#include<timers.h>
#include<spi.h>
#include<usart.h>
#pragma config OSC=HSPLL , OSCS=OFF
#pragma config BORV=45 , PWRT = ON
#pragma config BOR=ON , WDT =OFF
#pragma config DEBUG=OFF, LVP=OFF , STVR=OFF
#define CS LATCbits.LATC1
#pragma code INT_1 = 0x08
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// FUNCTION DECLARATIONS
//////////////////////////////////////////////////////////////////////////////
void INTT ();
unsigned char write(unsigned char);
void newline(void);
//////////////////////////////////////////////////////////////////////////////
unsigned char load_select=0;
unsigned char rms_control=0;
unsigned char tx_byte1=0;
unsigned char tx_byte2=0;
//////////////////////////////////////////////////////////////////////////////
void INT_1 (void)
{
_asm
goto INTT //jump to interrupt routine
_endasm
}
#pragma code
#pragma interrupt INTT
////////////////////////////////////////////////////////////////////////////////////////
// INTEERUPT SERVICE ROUTINE
///////////////////////////////////////////////////////////////////////////////////////
void INTT ()
{
unsigned char dig1,dig2,dig3,dig4,tmp,i,j,k,p,q,temp;
unsigned char x;
unsigned char y; //VARIABLE DECLARATONS
if(INTCONbits.INT0IF)
// if(INTCON3bits.INT1IF)
{
while(1)
{
OpenSPI(SPI_FOSC_64,MODE_00,SMPEND); // OPENS SPI CONNECTION
CS=0; // ENABLE MXB7843
Delay10TCYx(1); // DELAY
temp=write(0b10010000); // CONTROL BYTE TO RETRIEVE X CORDINATES
Delay10TCYx(10);
x=write(0);
Delay10TCYx(10);
p=write(0);
Delay10TCYx(10);
CS=1;
Delay10TCYx(10);
CS=0;
Delay10TCYx(1);
temp=write(0b11010000); // CONTROL BYTE TO RETRIEVE Y CORDINATES
Delay10TCYx(10);
y=write(0);
Delay10TCYx(10);
q=write(0);
Delay10TCYx(10);
CS=1; // PULL CS HIGH
CloseSPI(); // CLOSE SPI
if(x==0||y==0)
break;
else
putrsUSART("\033[2J"); // CLEARS THE HYPER-TERMINAL
Delay10TCYx(1);
putrsUSART("*** TOUCH SCREEN INTERRUPTED *** ");
newline();
newline();
Delay10TCYx(1);
putrsUSART("Co-ordinates = ");
//putrsUSART("*** TOUCH SCREEN INTERRUPTED *** ");
//
// newline();
// newline();
//
// Delay10TCYx(1);
//
// putrsUSART("Co-ordinates = ");
///////////////////////////////////////////////////////////////////////////
// DECIMAL -- BCD -- ASCII -- X - CORDINATES
///////////////////////////////////////////////////////////////////////////
dig1=x/1000+48;
tmp=x%1000;
dig2=tmp/100+48;
tmp=tmp%100;
dig3=tmp/10+48;
dig4=tmp%10+48;
//////////////////////// TRANSMIT X CORDINATES VIA USART //////////////
TXREG=40;
while(TXSTAbits.TRMT==0);
TXREG=dig1;
while(TXSTAbits.TRMT==0);
TXREG=dig2;
while(TXSTAbits.TRMT==0);
TXREG=dig3;
while(TXSTAbits.TRMT==0);
TXREG=dig4;
while(TXSTAbits.TRMT==0);
TXREG=44;
while(TXSTAbits.TRMT==0);
///////////////////////////////////////////////////////////////////////////
// DECIMAL -- BCD -- ASCII -- Y - CORDINATES
///////////////////////////////////////////////////////////////////////////
dig1=y/1000+48;
tmp=y%1000;
dig2=tmp/100+48;
tmp=tmp%100;
dig3=tmp/10+48;
dig4=tmp%10+48;
//////////////////////// TRANSMIT Y CORDINATES VIA USART //////////////
TXREG=dig1;
while(TXSTAbits.TRMT==0);
TXREG=dig2;
while(TXSTAbits.TRMT==0);
TXREG=dig3;
while(TXSTAbits.TRMT==0);
TXREG=dig4;
while(TXSTAbits.TRMT==0);
TXREG=41;
while(TXSTAbits.TRMT==0);
newline();
///////////////////////////////////////////////////////////////////////////
// CO-ORDINATES MAPPING / CALCULATONS
///////////////////////////////////////////////////////////////////////////
if(x<74&&y<50)
{
putrsUSART("DEVICE 1 IS SELECTED");
newline();
putrsUSART("RMS VOLTAGE PERCENTAGE : ");
// if(x<72&&x>60&&y>=39)
// {
// putrsUSART("****TOGGLED****");
// load_select=0b000;
// rms_control=0b1111;
// }
if(x<=49&&y<50)
{
putrsUSART("****0****");
load_select=0b1100;
rms_control=0b0000;
}
else if(x==50&&y<50)
{
putrsUSART("****6****");
load_select=0b1100;
rms_control=0b0001;
}
else if(x==51||x==52&&y<50)
{
putrsUSART("****12****");
load_select=0b1100;
rms_control=0b0010;
}
else if(x==53&&y<50)
{
putrsUSART("****18****");
load_select=0b1100;
rms_control=0b0011;
}
else if(x==54||x==55&&y<50)
{
putrsUSART("****26****");
load_select=0b1100;
rms_control=0b0100;
}
else if(x==56&&y<50)
{
putrsUSART("****30****");
load_select=0b1100;
rms_control=0b0101;
}
else if(x==57||x==58&&y<50)
{
putrsUSART("****36****");
load_select=0b1100;
rms_control=0b0110;
}
else if(x==59&&y<50)
{
putrsUSART("****42****");
load_select=0b1100;
rms_control=0b0111;
}
else if(x==60||x==61&&y<50)
{
putrsUSART("****48****");
load_select=0b1100;
rms_control=0b1000;
}
else if(x==62&&y<50)
{
putrsUSART("****54****");
load_select=0b1100;
rms_control=0b1001;
}
else if(x==63||x==64&&y<50)
{
putrsUSART("****60****");
load_select=0b1100;
rms_control=0b1010;
}
else if(x==65&&y<50)
{
putrsUSART("****66****");
load_select=0b1100;
rms_control=0b1011;
}
else if(x==66||x==67&&y<50)
{
putrsUSART("****72****");
load_select=0b1100;
rms_control=0b1100;
}
else if(x==68&&y<50)
{
putrsUSART("****78****");
load_select=0b1100;
rms_control=0b1101;
}
else if(x==69||x==70&&y<50)
{
putrsUSART("****84****");
load_select=0b1100;
rms_control=0b1110;
}
else if(x==71||x==72||x==73&&y<50)
{
putrsUSART("****96****");
load_select=0b1100;
rms_control=0b1111;
}
}
else if(x<74&&y>49)
{
putrsUSART("DEVICE 2 IS SELECTED");
newline();
putrsUSART("RMS VOLTAGE PERCENTAGE : ");
// if(x<73&&y<=70)
// {
// putrsUSART("****TOGGLED****");
// load_select=0b001;
// rms_control=0b1111;
// }
if(x==36||x==37&&y>49)
{
putrsUSART("****0****");
load_select=0b1101;
rms_control=0b0000;
}
else if(x==38||x==39&&y>49)
{
putrsUSART("****6****");
load_select=0b1101;
rms_control=0b0001;
}
else if(x==40||x==41&&y>49)
{
putrsUSART("****12****");
load_select=0b1101;
rms_control=0b0010;
}
else if(x==42||x==43&&y>49)
{
putrsUSART("****18****");
load_select=0b1101;
rms_control=0b0011;
}
else if(x==44||x==45&&y>49)
{
putrsUSART("****26****");
load_select=0b1101;
rms_control=0b0100;
}
else if(x==46||x==47&&y>49)
{
putrsUSART("****30****");
load_select=0b1101;
rms_control=0b0101;
}
else if(x==48||x==49&&y>49)
{
putrsUSART("****36****");
load_select=0b1101;
rms_control=0b0110;
}
else if(x==50||x==51&&y>49)
{
putrsUSART("****42****");
load_select=0b1101;
rms_control=0b0111;
}
else if(x==52||x==53&&y>49)
{
putrsUSART("****48****");
load_select=0b1101;
rms_control=0b1000;
}
else if(x==54||x==55&&y>49)
{
putrsUSART("****54****");
load_select=0b1101;
rms_control=0b1001;
}
else if(x==56||x==57&&y>49)
{
putrsUSART("****60****");
load_select=0b1101;
rms_control=0b1010;
}
else if(x==58||x==59&&y>49)
{
putrsUSART("****66****");
load_select=0b1101;
rms_control=0b1011;
}
else if(x==60||x==61&&y>49)
{
putrsUSART("****72****");
load_select=0b1101;
rms_control=0b1100;
}
else if(x==62||x==63&&y>49)
{
putrsUSART("****78****");
load_select=0b1101;
rms_control=0b1101;
}
else if(x==64||x==65&&y>49)
{
putrsUSART("****84****");
load_select=0b1101;
rms_control=0b1110;
}
else if(x>65&&x<74&&y>49)
{
putrsUSART("****96****");
load_select=0b1101;
rms_control=0b1111;
}
}
else if(x>73&&x<81&&y<37)
{
putrsUSART("DEVICE 1 IS TURNED OFF ");
newline();
load_select=0b0000;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>36&&y<58)
{
putrsUSART("DEVICE 1 IS TURNED ON ");
newline();
load_select=0b1000;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>57&&y<74)
{
putrsUSART("DEVICE 2 IS TURNED OFF ");
newline();
load_select=0b0001;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>73)
{
putrsUSART("DEVICE 2 IS TURNED ON ");
newline();
load_select=0b1001;
rms_control=0b1111;
}
else if(x>80&&y<37)
{
putrsUSART("DEVICE 3 IS TURNED OFF ");
newline();
load_select=0b0010;
rms_control=0b1111;
}
else if(x>80&&y>36&&y<58)
{
putrsUSART("DEVICE 3 IS TURNED ON ");
newline();
load_select=0b1010;
rms_control=0b1111;
}
else if(x>80&&y>57&&y<74)
{
putrsUSART("DEVICE 4 IS TURNED OFF ");
newline();
load_select=0b0011;
rms_control=0b1111;
}
else if(x>80&&y>73)
{
putrsUSART("DEVICE 4 IS TURNED ON ");
newline();
load_select=0b1011;
rms_control=0b1111;
}
//Delay10TCYx(10);
newline();
newline();
////////////////////////////////////WHAT TO TRANSMIT OVER ETHERNET////////////////////////////////
PORTD=(rms_control<<4)|(load_select);
////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////
// INTCON3bits.INT1IF=0;
INTCONbits.INT0IF=0;
}
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// INTERRUPT ROUTINE ENDED
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MAIN - FUNCTION
////////////////////////////////////////////////////////////////////////
void main()
{
unsigned int temp;
CS=1;
/////////////////////////INTERRUPT INITIALIZATION///////////////////
// INTCON = 0x80;
// INTCON2bits.INTEDG1=0;
// INTCON3bits.INT1IE=1;
// INTCON3bits.INT1IF=0;
// INTCON2bits.RBPU=0;
INTCON = 0x80;
INTCON2bits.INTEDG0=0;
INTCONbits.INT0IE=1;
INTCONbits.INT0IF=0;
INTCON2bits.RBPU=0;
//////////////////////PORT INITIALIZATION INITIALIZATION///////////
TRISB=0b11111111;
TRISD=0x00;
TRISC=0b10010000;
///////////////////////USAER INITIALIZATION///////////////////////
RCSTAbits.SPEN=1;
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
TRISCbits.TRISC5=0;
TXSTA=0x20;
SPBRG=32;
Delay10TCYx(1);
OpenSPI(SPI_FOSC_64,MODE_00,SMPEND); // OPENS SPI CONNECTION
CS=0; // ENABLE MXB7843
Delay10TCYx(1); // DELAY
temp=write(0b10010000);
CS=1;
CloseSPI();
while(1);
}
/////////////////////FUNCTIONS DEFINITION////////////////////////////
unsigned char write(unsigned char data_out)
{
PIR1bits.SSPIF = 0; // Clear interrupt flag
SSPCON1bits.WCOL = 0; //Clear any previous write collision
SSPBUF = data_out; // write byte to SSPBUF register
if ( SSPCON1 & 0x80 ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
// while( !SSPSTATbits.BF ); // wait until bus cycle complete
while( !PIR1bits.SSPIF ); // wait until bus cycle complete
return (SSPBUF);
}
void newline(void)
{
TXREG=13;
while(TXSTAbits.TRMT==0);
TXREG=10;
while(TXSTAbits.TRMT==0);
}
Download the SOURCE CODE here. You can edit the source code according to your need !
By now, the formation of control byte remain no more an issue. Once the control byte is formed, what left is the coding, which is explained below for PIC18F452 Microcontroller, developed in C language on C18 compiler with MPLAB as IDE.
This code for touch screen has been designed for the background shown below :
////////////////////////////////////////////////////////////////////////////////
// PROJECT: CONTROL OF ELECTRIC APPLIANCES (SLIDER CONTROL FOR RMS) WITH TOUCH PANEL INTERFACE// GROUP MEMBERS:
//
// 1. Muhammad Asif 2. Ahmed Fawad 3. MUHAMMAD Ahmed
//
//
// Code Includes: TOUCH SCREEN INTERFACING WITH USART
//
// Dated: 22 APR 2011
//
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// INCLUSION OF DIRECTORIES / CRYSTAL INITIALIZATION / MACROS
/////////////////////////////////////////////////////////////////////////////////
#include<p18F452.h>
#include<p18cxxx.h>
#include<delays.h>
#include<timers.h>
#include<spi.h>
#include<usart.h>
#pragma config OSC=HSPLL , OSCS=OFF
#pragma config BORV=45 , PWRT = ON
#pragma config BOR=ON , WDT =OFF
#pragma config DEBUG=OFF, LVP=OFF , STVR=OFF
#define CS LATCbits.LATC1
#pragma code INT_1 = 0x08
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// FUNCTION DECLARATIONS
//////////////////////////////////////////////////////////////////////////////
void INTT ();
unsigned char write(unsigned char);
void newline(void);
//////////////////////////////////////////////////////////////////////////////
unsigned char load_select=0;
unsigned char rms_control=0;
unsigned char tx_byte1=0;
unsigned char tx_byte2=0;
//////////////////////////////////////////////////////////////////////////////
void INT_1 (void)
{
_asm
goto INTT //jump to interrupt routine
_endasm
}
#pragma code
#pragma interrupt INTT
////////////////////////////////////////////////////////////////////////////////////////
// INTEERUPT SERVICE ROUTINE
///////////////////////////////////////////////////////////////////////////////////////
void INTT ()
{
unsigned char dig1,dig2,dig3,dig4,tmp,i,j,k,p,q,temp;
unsigned char x;
unsigned char y; //VARIABLE DECLARATONS
if(INTCONbits.INT0IF)
// if(INTCON3bits.INT1IF)
{
while(1)
{
OpenSPI(SPI_FOSC_64,MODE_00,SMPEND); // OPENS SPI CONNECTION
CS=0; // ENABLE MXB7843
Delay10TCYx(1); // DELAY
temp=write(0b10010000); // CONTROL BYTE TO RETRIEVE X CORDINATES
Delay10TCYx(10);
x=write(0);
Delay10TCYx(10);
p=write(0);
Delay10TCYx(10);
CS=1;
Delay10TCYx(10);
CS=0;
Delay10TCYx(1);
temp=write(0b11010000); // CONTROL BYTE TO RETRIEVE Y CORDINATES
Delay10TCYx(10);
y=write(0);
Delay10TCYx(10);
q=write(0);
Delay10TCYx(10);
CS=1; // PULL CS HIGH
CloseSPI(); // CLOSE SPI
if(x==0||y==0)
break;
else
putrsUSART("\033[2J"); // CLEARS THE HYPER-TERMINAL
Delay10TCYx(1);
putrsUSART("*** TOUCH SCREEN INTERRUPTED *** ");
newline();
newline();
Delay10TCYx(1);
putrsUSART("Co-ordinates = ");
//putrsUSART("*** TOUCH SCREEN INTERRUPTED *** ");
//
// newline();
// newline();
//
// Delay10TCYx(1);
//
// putrsUSART("Co-ordinates = ");
///////////////////////////////////////////////////////////////////////////
// DECIMAL -- BCD -- ASCII -- X - CORDINATES
///////////////////////////////////////////////////////////////////////////
dig1=x/1000+48;
tmp=x%1000;
dig2=tmp/100+48;
tmp=tmp%100;
dig3=tmp/10+48;
dig4=tmp%10+48;
//////////////////////// TRANSMIT X CORDINATES VIA USART //////////////
TXREG=40;
while(TXSTAbits.TRMT==0);
TXREG=dig1;
while(TXSTAbits.TRMT==0);
TXREG=dig2;
while(TXSTAbits.TRMT==0);
TXREG=dig3;
while(TXSTAbits.TRMT==0);
TXREG=dig4;
while(TXSTAbits.TRMT==0);
TXREG=44;
while(TXSTAbits.TRMT==0);
///////////////////////////////////////////////////////////////////////////
// DECIMAL -- BCD -- ASCII -- Y - CORDINATES
///////////////////////////////////////////////////////////////////////////
dig1=y/1000+48;
tmp=y%1000;
dig2=tmp/100+48;
tmp=tmp%100;
dig3=tmp/10+48;
dig4=tmp%10+48;
//////////////////////// TRANSMIT Y CORDINATES VIA USART //////////////
TXREG=dig1;
while(TXSTAbits.TRMT==0);
TXREG=dig2;
while(TXSTAbits.TRMT==0);
TXREG=dig3;
while(TXSTAbits.TRMT==0);
TXREG=dig4;
while(TXSTAbits.TRMT==0);
TXREG=41;
while(TXSTAbits.TRMT==0);
newline();
///////////////////////////////////////////////////////////////////////////
// CO-ORDINATES MAPPING / CALCULATONS
///////////////////////////////////////////////////////////////////////////
if(x<74&&y<50)
{
putrsUSART("DEVICE 1 IS SELECTED");
newline();
putrsUSART("RMS VOLTAGE PERCENTAGE : ");
// if(x<72&&x>60&&y>=39)
// {
// putrsUSART("****TOGGLED****");
// load_select=0b000;
// rms_control=0b1111;
// }
if(x<=49&&y<50)
{
putrsUSART("****0****");
load_select=0b1100;
rms_control=0b0000;
}
else if(x==50&&y<50)
{
putrsUSART("****6****");
load_select=0b1100;
rms_control=0b0001;
}
else if(x==51||x==52&&y<50)
{
putrsUSART("****12****");
load_select=0b1100;
rms_control=0b0010;
}
else if(x==53&&y<50)
{
putrsUSART("****18****");
load_select=0b1100;
rms_control=0b0011;
}
else if(x==54||x==55&&y<50)
{
putrsUSART("****26****");
load_select=0b1100;
rms_control=0b0100;
}
else if(x==56&&y<50)
{
putrsUSART("****30****");
load_select=0b1100;
rms_control=0b0101;
}
else if(x==57||x==58&&y<50)
{
putrsUSART("****36****");
load_select=0b1100;
rms_control=0b0110;
}
else if(x==59&&y<50)
{
putrsUSART("****42****");
load_select=0b1100;
rms_control=0b0111;
}
else if(x==60||x==61&&y<50)
{
putrsUSART("****48****");
load_select=0b1100;
rms_control=0b1000;
}
else if(x==62&&y<50)
{
putrsUSART("****54****");
load_select=0b1100;
rms_control=0b1001;
}
else if(x==63||x==64&&y<50)
{
putrsUSART("****60****");
load_select=0b1100;
rms_control=0b1010;
}
else if(x==65&&y<50)
{
putrsUSART("****66****");
load_select=0b1100;
rms_control=0b1011;
}
else if(x==66||x==67&&y<50)
{
putrsUSART("****72****");
load_select=0b1100;
rms_control=0b1100;
}
else if(x==68&&y<50)
{
putrsUSART("****78****");
load_select=0b1100;
rms_control=0b1101;
}
else if(x==69||x==70&&y<50)
{
putrsUSART("****84****");
load_select=0b1100;
rms_control=0b1110;
}
else if(x==71||x==72||x==73&&y<50)
{
putrsUSART("****96****");
load_select=0b1100;
rms_control=0b1111;
}
}
else if(x<74&&y>49)
{
putrsUSART("DEVICE 2 IS SELECTED");
newline();
putrsUSART("RMS VOLTAGE PERCENTAGE : ");
// if(x<73&&y<=70)
// {
// putrsUSART("****TOGGLED****");
// load_select=0b001;
// rms_control=0b1111;
// }
if(x==36||x==37&&y>49)
{
putrsUSART("****0****");
load_select=0b1101;
rms_control=0b0000;
}
else if(x==38||x==39&&y>49)
{
putrsUSART("****6****");
load_select=0b1101;
rms_control=0b0001;
}
else if(x==40||x==41&&y>49)
{
putrsUSART("****12****");
load_select=0b1101;
rms_control=0b0010;
}
else if(x==42||x==43&&y>49)
{
putrsUSART("****18****");
load_select=0b1101;
rms_control=0b0011;
}
else if(x==44||x==45&&y>49)
{
putrsUSART("****26****");
load_select=0b1101;
rms_control=0b0100;
}
else if(x==46||x==47&&y>49)
{
putrsUSART("****30****");
load_select=0b1101;
rms_control=0b0101;
}
else if(x==48||x==49&&y>49)
{
putrsUSART("****36****");
load_select=0b1101;
rms_control=0b0110;
}
else if(x==50||x==51&&y>49)
{
putrsUSART("****42****");
load_select=0b1101;
rms_control=0b0111;
}
else if(x==52||x==53&&y>49)
{
putrsUSART("****48****");
load_select=0b1101;
rms_control=0b1000;
}
else if(x==54||x==55&&y>49)
{
putrsUSART("****54****");
load_select=0b1101;
rms_control=0b1001;
}
else if(x==56||x==57&&y>49)
{
putrsUSART("****60****");
load_select=0b1101;
rms_control=0b1010;
}
else if(x==58||x==59&&y>49)
{
putrsUSART("****66****");
load_select=0b1101;
rms_control=0b1011;
}
else if(x==60||x==61&&y>49)
{
putrsUSART("****72****");
load_select=0b1101;
rms_control=0b1100;
}
else if(x==62||x==63&&y>49)
{
putrsUSART("****78****");
load_select=0b1101;
rms_control=0b1101;
}
else if(x==64||x==65&&y>49)
{
putrsUSART("****84****");
load_select=0b1101;
rms_control=0b1110;
}
else if(x>65&&x<74&&y>49)
{
putrsUSART("****96****");
load_select=0b1101;
rms_control=0b1111;
}
}
else if(x>73&&x<81&&y<37)
{
putrsUSART("DEVICE 1 IS TURNED OFF ");
newline();
load_select=0b0000;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>36&&y<58)
{
putrsUSART("DEVICE 1 IS TURNED ON ");
newline();
load_select=0b1000;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>57&&y<74)
{
putrsUSART("DEVICE 2 IS TURNED OFF ");
newline();
load_select=0b0001;
rms_control=0b1111;
}
else if(x>73&&x<81&&y>73)
{
putrsUSART("DEVICE 2 IS TURNED ON ");
newline();
load_select=0b1001;
rms_control=0b1111;
}
else if(x>80&&y<37)
{
putrsUSART("DEVICE 3 IS TURNED OFF ");
newline();
load_select=0b0010;
rms_control=0b1111;
}
else if(x>80&&y>36&&y<58)
{
putrsUSART("DEVICE 3 IS TURNED ON ");
newline();
load_select=0b1010;
rms_control=0b1111;
}
else if(x>80&&y>57&&y<74)
{
putrsUSART("DEVICE 4 IS TURNED OFF ");
newline();
load_select=0b0011;
rms_control=0b1111;
}
else if(x>80&&y>73)
{
putrsUSART("DEVICE 4 IS TURNED ON ");
newline();
load_select=0b1011;
rms_control=0b1111;
}
//Delay10TCYx(10);
newline();
newline();
////////////////////////////////////WHAT TO TRANSMIT OVER ETHERNET////////////////////////////////
PORTD=(rms_control<<4)|(load_select);
////////////////////////////////////////////////////////////////////////
}
/////////////////////////////////////////////////////////////////////////
// INTCON3bits.INT1IF=0;
INTCONbits.INT0IF=0;
}
}
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// INTERRUPT ROUTINE ENDED
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
// MAIN - FUNCTION
////////////////////////////////////////////////////////////////////////
void main()
{
unsigned int temp;
CS=1;
/////////////////////////INTERRUPT INITIALIZATION///////////////////
// INTCON = 0x80;
// INTCON2bits.INTEDG1=0;
// INTCON3bits.INT1IE=1;
// INTCON3bits.INT1IF=0;
// INTCON2bits.RBPU=0;
INTCON = 0x80;
INTCON2bits.INTEDG0=0;
INTCONbits.INT0IE=1;
INTCONbits.INT0IF=0;
INTCON2bits.RBPU=0;
//////////////////////PORT INITIALIZATION INITIALIZATION///////////
TRISB=0b11111111;
TRISD=0x00;
TRISC=0b10010000;
///////////////////////USAER INITIALIZATION///////////////////////
RCSTAbits.SPEN=1;
TRISCbits.TRISC6=0;
TRISCbits.TRISC7=1;
TRISCbits.TRISC5=0;
TXSTA=0x20;
SPBRG=32;
Delay10TCYx(1);
OpenSPI(SPI_FOSC_64,MODE_00,SMPEND); // OPENS SPI CONNECTION
CS=0; // ENABLE MXB7843
Delay10TCYx(1); // DELAY
temp=write(0b10010000);
CS=1;
CloseSPI();
while(1);
}
/////////////////////FUNCTIONS DEFINITION////////////////////////////
unsigned char write(unsigned char data_out)
{
PIR1bits.SSPIF = 0; // Clear interrupt flag
SSPCON1bits.WCOL = 0; //Clear any previous write collision
SSPBUF = data_out; // write byte to SSPBUF register
if ( SSPCON1 & 0x80 ) // test if write collision occurred
return ( -1 ); // if WCOL bit is set return negative #
else
// while( !SSPSTATbits.BF ); // wait until bus cycle complete
while( !PIR1bits.SSPIF ); // wait until bus cycle complete
return (SSPBUF);
}
void newline(void)
{
TXREG=13;
while(TXSTAbits.TRMT==0);
TXREG=10;
while(TXSTAbits.TRMT==0);
}
Download the SOURCE CODE here. You can edit the source code according to your need !
No comments:
Post a Comment