/*
buffer : spi buffer
data_in : data to be read from spi
data_out : data to be written to spi
address : AFE sensor address pointer
*/
// LCD module connections
sbit LCD_RS at RC1_bit;
sbit LCD_EN at RC0_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISC1_bit;
sbit LCD_EN_Direction at TRISC0_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
// LCD module connections
char txt3[3],txt4[3];
unsigned short data_in,address,buffer;
unsigned int data_out=0XA880;
unsigned char lowbyte,highbyte;
//Read data from memory array beginning at selected address
void READ(unsigned short address)
{
lowbyte=data_out&0X00A8;
highbyte=data_out>>8;
PORTD.f2=0; // cs low
Spi1_read(0x03); // '0000 x011' READ Instruction
Spi1_Write(highbyte<<8/lowbyte); // send address RDSR
//Spi1_Write(lowbyte);
// Spi1_Write(0x32);
data_in=Spi1_Read(buffer); // READ data
PORTD.f2=1; // cs high
}
//Reset the write enable latch (disable write operations)
void WRDI()
{
Spi1_Write(0x04); // '0000 x100' WRDI Instruction
}
//Set the write enable latch (enable write operations)
void WRENL()
{
Spi1_Write(0x06); // '0000 x110' WREN Instruction
PORTD.f2=1; // cs high
PORTD.f2=0; // cs low
}
//Write data to memory array beginning at selected address
void WRITE(unsigned short address, unsigned short data_out)
{
lowbyte=data_out&0X00A8;
highbyte=data_out>>8;
PORTD.f2=0; // cs low
WRENL();
Spi1_Write(0x02); // '0000 x010' WRITE Instruction
Spi1_Write(address); // send address
Spi1_Write(highbyte); // send data
delay_ms(100);
Spi1_Write(address+1);
spi1_Write(lowbyte);
PORTD.f2=1; // cs high
WRDI();
}
void temperature()
{
WRITE(0X00,0XA880); // Write data 0XA880 TO AFE ADDRESS 0X00
delay_ms(10);
READ(0X00); // Read
PORTD=data_in; // AND DISPLAY DATA ON PORTD
ByteToStr(data_in,txt3);
Lcd_Out(1,1,"TEMP:");
Lcd_Out(1,8,txt3); // Write text in first row
Delay_ms(250);
}
void init()
{
PORTB = 0X00;
PORTD = 0X00;
TRISB = 0x00;
TRISD = 0x00;
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH);
delay_ms(500);
}
void main()
{
Lcd_Init(); // Initialize LCD
init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
temperature();
}