timothyarora
Junior Member level 2
I've a problem with timers
Timer 1 is used for printf function...........so, i've used timer 2
for 300 baud rate generation.
but with both timers 1 or 2 programme get stopped at one
run.............spi code
i've problem in making the code for transmitting and receiving data
serially compatible with my code..
earliar code uses t3con registers in aduc 841 which is not available
in philips micrcontroller............
#include <stdio.h>
#include<ctype.h>
#include<C:\KEIL\C51\INC\Philips\p89v51rx2.h>
#include<C:\Documents and Settings\Vijay Ghial\Desktop\Philips SPI coding\spicodetryextra.c>
#include<lcd_array.h>
sbit SS=P1^4;
#define VOLT_CC 0x014
#define SPI_COMMAND_RETRIES 360
#define SPI_RETRIES 360
void transmit(unsigned char chr)
{
SCON=0x50;
T2CON=0x34;
TL2=0xFB;
TH2=0x80;
RCAP2H=0xFB;
RCAP2L=0x80;
TR2=1;
SBUF=chr;
TI=0;
while(TI==0){;};
//delay_ms(250);
return;
}
receive()
{
char ch ;
TMOD=0x20;
TH1=0xA0;
SCON=0x50;
//SCON=0x052;
TR1=1;
TI=1;
while(1)
{
//SBUF=0xFF;
while(RI !=1){;};
ch = SBUF;
printf("\n Received character is %c \n ", ch);
printf("\n Received hexadecimal is %x \n", ch);
printf("\n Received digit is %d \n", ch);
P1=SBUF;
RI=0;
}
}
uint8 Send_SPI(uint8 x)
{
uint8 y = 0;
SPCFG = 0x00; /* Clear transfer complete flag */
SPDAT = x;
while (!(SPCFG & 0x80));
SPCFG =0x00;
y = SPDAT;
// printf("MISO = %01X \n",SPDAT);
SPCFG = 0x00;
msdelay(1);
return y;
}
/*******************************************************
This function transmittes a byte data
on the SPI interface
*******************************************************/
int32 MAXREAD(uint8 Bytes, uint16 Address)
{
// Read:
Cmd=0x00;
regadd = Address & 0xff;
for(i=0; i<16; i++)
record = 0;
switch(Bytes)
{
case 1:
Cmd |= 0x00;
break;
case 2:
Cmd |= 0x10;
break;
case 4:
Cmd |= 0x20;
break;
case 8:
Cmd |= 0x30;
break;
default:
Cmd |= 0x00;
break;
}
Address >>= 8;
Cmd |= Address;
/* for(x=0; x<300; x++);
SPCTL=0x51;*/
SS=0;
i = 0;
// printf("Sending First Byte for Reading %02X", Cmd);
while((Send_SPI(Cmd) != 0xC1));
//Send_SPI(Cmd);
msdelay(1);
x = 0xffffffff;
msdelay(1);
// printf("Sending Second Byte for Reading %02X", regadd);
while((Send_SPI(regadd) != 0xC2));
//Send_SPI(regadd);
i = 0;
// printf("Sending Second Dummmy");
while((Send_SPI(0) != 0x41));
//Send_SPI(0);
//goto spierror;
x = 0;
for(i=0; i<Bytes; i++)
{
msdelay(1);
// printf("Sending Dummy again");
record = Send_SPI(0);
x |= ((uint32)record) << (i * 8);
}
//spierror:
SS=1;
return (int32)x;
}
void MAXWRITE(uint8 Bytes, uint16 Address, uint16 datax)
{
Cmd=0x80;
//write:
Add[0]= (uint8)(Address & 0x00FF);
Add[1]=(uint8)((Address & 0x0F00)>>0x08);
Cmd &=0X8F;
switch(Bytes)
{
case 1:
Cmd |= 0x00;
break;
case 2:
Cmd |= 0x10;
break;
case 4:
Cmd |= 0x20;
break;
case 8:
Cmd |= 0x30;
break;
default:
Cmd |= 0x00;
break;
}
Add[1]&=0x0F;
Cmd |=Add[1];
SS=0;
i = 0;
while((Send_SPI(Cmd) != 0xC1));
msdelay(1);
while((Send_SPI(Add[0]) != 0xC2));
Send_SPI(datax);
i = 0;
while((Send_SPI(0) != 'A'));
SS=1;
}
unsigned long read_FourBytes (unsigned int ADD)
{
signed char XX;
printf("Readed values: ");
for(XX=3; XX>=0; XX--)
{
VALUE4.DATA[XX] = MAXREAD (1 , ADD);
printf("%02X", VALUE4.DATA[XX]);
}
return VALUE4.Long_DATA;
}
unsigned int read_TwoBytes (unsigned int ADD)
{
signed char XX;
uint32 joiner;
for(XX=1; XX>=0; XX--)
{
VALUE2.DATA[XX] = MAXREAD (1 , ADD);
// printf(" mAXREAD%02X \n", VALUE2.DATA[XX]);
ADD++;
}
joiner=(VALUE2.DATA[1]<<0x08)|(VALUE2.DATA[0]);
printf("\n Readed value = %04X ", joiner);
return VALUE2.Int_DATA;
}
void spi_init()
{
P1 = 0xBF; // defines i/o pins
SS = 1; // sets the chip select pin high
SPCFG = 0x00; // clears spi flag
SPCTL = 0x51;//5B // selecting master mode and other configurations
}
void main()
{
uint32 TEST;
unsigned long DATA_READ = 0;
spi_init(); // spi mode initialization
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON |= 0x80;
TR1 = 1 ;
LCD_INIT();
while(1)
{
SS = 0;
printf("\n..............START....................\n");
printf("\n Writing Calliberated Registers ........\n");
MAXWRITE(1, 0x132,0x40);
MAXWRITE(1, 0x133,0x0);
MAXWRITE(1, 0x21E, 0x40);
MAXWRITE(1, 0x21F,0x00);
MAXWRITE(1, 0x30A, 0x40);
MAXWRITE(1,0x30B, 0x00);
MAXWRITE(1, 0x130,0x10);
MAXWRITE(1, 0x131,0x00);
MAXWRITE(1, 0x21C, 0x10);
MAXWRITE(1, 0x21D,0x00);
MAXWRITE(1, 0x308, 0x10);
MAXWRITE(1, 0x309,0x00);
MAXWRITE(1, 0x018 ,0x01 );
MAXWRITE(1, 0x019 ,0x00);
printf("\n Calliberated Registers Reading .......\n ");
printf("\n Voltages:");
read_TwoBytes (0x132);
read_TwoBytes (0x21E);
read_TwoBytes (0x30A);
printf("\n Currents:");
read_TwoBytes (0x130);
read_TwoBytes (0x21C);
read_TwoBytes (0x308);
printf("\n Real Power");
read_TwoBytes (0x018);
printf("\nVoltage Readings\n");
printf("\nA:");
read_FourBytes (0x1C8);
printf("\nB:");
read_FourBytes (0x2B4);
printf("\nC:");
read_FourBytes (0x3A0);
printf("\n Current Readings\n ");
printf("\n A:");
read_FourBytes (0x1CC);
printf("\n B:");
read_FourBytes (0x2B8);
printf("\n C:");
read_FourBytes (0x3A4);
printf("\n Real Power Readings \n");
printf("\n Real Power A:");
TEST= MAXREAD(1,0x881);
printf("%04d ",TEST );
TEST=0;
printf("\n Real Power B:");
TEST= MAXREAD(1,0x882);
printf("%04d ", TEST);
TEST=0;
printf("\n Real Power C:");
TEST= MAXREAD(1, 0x884);
printf("%04d ",TEST);
TEST = MAXREAD(1, 0x887);
printf(" \n Total Real Power Consumed = %04d",TEST );
printf("\n ..........THE END...................\n");
//transmit(0x10);
printf("\n");
// receive();
LCD_WRITE("THEFT");
delay_ms(250);
LCD_CMD(0xC2);
delay_ms(250);
LCD_WRITE("DETECTED ");
SS = 1;
}
}
Timer 1 is used for printf function...........so, i've used timer 2
for 300 baud rate generation.
but with both timers 1 or 2 programme get stopped at one
run.............spi code
i've problem in making the code for transmitting and receiving data
serially compatible with my code..
earliar code uses t3con registers in aduc 841 which is not available
in philips micrcontroller............
#include <stdio.h>
#include<ctype.h>
#include<C:\KEIL\C51\INC\Philips\p89v51rx2.h>
#include<C:\Documents and Settings\Vijay Ghial\Desktop\Philips SPI coding\spicodetryextra.c>
#include<lcd_array.h>
sbit SS=P1^4;
#define VOLT_CC 0x014
#define SPI_COMMAND_RETRIES 360
#define SPI_RETRIES 360
void transmit(unsigned char chr)
{
SCON=0x50;
T2CON=0x34;
TL2=0xFB;
TH2=0x80;
RCAP2H=0xFB;
RCAP2L=0x80;
TR2=1;
SBUF=chr;
TI=0;
while(TI==0){;};
//delay_ms(250);
return;
}
receive()
{
char ch ;
TMOD=0x20;
TH1=0xA0;
SCON=0x50;
//SCON=0x052;
TR1=1;
TI=1;
while(1)
{
//SBUF=0xFF;
while(RI !=1){;};
ch = SBUF;
printf("\n Received character is %c \n ", ch);
printf("\n Received hexadecimal is %x \n", ch);
printf("\n Received digit is %d \n", ch);
P1=SBUF;
RI=0;
}
}
uint8 Send_SPI(uint8 x)
{
uint8 y = 0;
SPCFG = 0x00; /* Clear transfer complete flag */
SPDAT = x;
while (!(SPCFG & 0x80));
SPCFG =0x00;
y = SPDAT;
// printf("MISO = %01X \n",SPDAT);
SPCFG = 0x00;
msdelay(1);
return y;
}
/*******************************************************
This function transmittes a byte data
on the SPI interface
*******************************************************/
int32 MAXREAD(uint8 Bytes, uint16 Address)
{
// Read:
Cmd=0x00;
regadd = Address & 0xff;
for(i=0; i<16; i++)
record = 0;
switch(Bytes)
{
case 1:
Cmd |= 0x00;
break;
case 2:
Cmd |= 0x10;
break;
case 4:
Cmd |= 0x20;
break;
case 8:
Cmd |= 0x30;
break;
default:
Cmd |= 0x00;
break;
}
Address >>= 8;
Cmd |= Address;
/* for(x=0; x<300; x++);
SPCTL=0x51;*/
SS=0;
i = 0;
// printf("Sending First Byte for Reading %02X", Cmd);
while((Send_SPI(Cmd) != 0xC1));
//Send_SPI(Cmd);
msdelay(1);
x = 0xffffffff;
msdelay(1);
// printf("Sending Second Byte for Reading %02X", regadd);
while((Send_SPI(regadd) != 0xC2));
//Send_SPI(regadd);
i = 0;
// printf("Sending Second Dummmy");
while((Send_SPI(0) != 0x41));
//Send_SPI(0);
//goto spierror;
x = 0;
for(i=0; i<Bytes; i++)
{
msdelay(1);
// printf("Sending Dummy again");
record = Send_SPI(0);
x |= ((uint32)record) << (i * 8);
}
//spierror:
SS=1;
return (int32)x;
}
void MAXWRITE(uint8 Bytes, uint16 Address, uint16 datax)
{
Cmd=0x80;
//write:
Add[0]= (uint8)(Address & 0x00FF);
Add[1]=(uint8)((Address & 0x0F00)>>0x08);
Cmd &=0X8F;
switch(Bytes)
{
case 1:
Cmd |= 0x00;
break;
case 2:
Cmd |= 0x10;
break;
case 4:
Cmd |= 0x20;
break;
case 8:
Cmd |= 0x30;
break;
default:
Cmd |= 0x00;
break;
}
Add[1]&=0x0F;
Cmd |=Add[1];
SS=0;
i = 0;
while((Send_SPI(Cmd) != 0xC1));
msdelay(1);
while((Send_SPI(Add[0]) != 0xC2));
Send_SPI(datax);
i = 0;
while((Send_SPI(0) != 'A'));
SS=1;
}
unsigned long read_FourBytes (unsigned int ADD)
{
signed char XX;
printf("Readed values: ");
for(XX=3; XX>=0; XX--)
{
VALUE4.DATA[XX] = MAXREAD (1 , ADD);
printf("%02X", VALUE4.DATA[XX]);
}
return VALUE4.Long_DATA;
}
unsigned int read_TwoBytes (unsigned int ADD)
{
signed char XX;
uint32 joiner;
for(XX=1; XX>=0; XX--)
{
VALUE2.DATA[XX] = MAXREAD (1 , ADD);
// printf(" mAXREAD%02X \n", VALUE2.DATA[XX]);
ADD++;
}
joiner=(VALUE2.DATA[1]<<0x08)|(VALUE2.DATA[0]);
printf("\n Readed value = %04X ", joiner);
return VALUE2.Int_DATA;
}
void spi_init()
{
P1 = 0xBF; // defines i/o pins
SS = 1; // sets the chip select pin high
SPCFG = 0x00; // clears spi flag
SPCTL = 0x51;//5B // selecting master mode and other configurations
}
void main()
{
uint32 TEST;
unsigned long DATA_READ = 0;
spi_init(); // spi mode initialization
SCON = 0x50;
TMOD |= 0x20;
TH1 = 0xFA;
TR1 = 1;
TI = 1;
PCON |= 0x80;
TR1 = 1 ;
LCD_INIT();
while(1)
{
SS = 0;
printf("\n..............START....................\n");
printf("\n Writing Calliberated Registers ........\n");
MAXWRITE(1, 0x132,0x40);
MAXWRITE(1, 0x133,0x0);
MAXWRITE(1, 0x21E, 0x40);
MAXWRITE(1, 0x21F,0x00);
MAXWRITE(1, 0x30A, 0x40);
MAXWRITE(1,0x30B, 0x00);
MAXWRITE(1, 0x130,0x10);
MAXWRITE(1, 0x131,0x00);
MAXWRITE(1, 0x21C, 0x10);
MAXWRITE(1, 0x21D,0x00);
MAXWRITE(1, 0x308, 0x10);
MAXWRITE(1, 0x309,0x00);
MAXWRITE(1, 0x018 ,0x01 );
MAXWRITE(1, 0x019 ,0x00);
printf("\n Calliberated Registers Reading .......\n ");
printf("\n Voltages:");
read_TwoBytes (0x132);
read_TwoBytes (0x21E);
read_TwoBytes (0x30A);
printf("\n Currents:");
read_TwoBytes (0x130);
read_TwoBytes (0x21C);
read_TwoBytes (0x308);
printf("\n Real Power");
read_TwoBytes (0x018);
printf("\nVoltage Readings\n");
printf("\nA:");
read_FourBytes (0x1C8);
printf("\nB:");
read_FourBytes (0x2B4);
printf("\nC:");
read_FourBytes (0x3A0);
printf("\n Current Readings\n ");
printf("\n A:");
read_FourBytes (0x1CC);
printf("\n B:");
read_FourBytes (0x2B8);
printf("\n C:");
read_FourBytes (0x3A4);
printf("\n Real Power Readings \n");
printf("\n Real Power A:");
TEST= MAXREAD(1,0x881);
printf("%04d ",TEST );
TEST=0;
printf("\n Real Power B:");
TEST= MAXREAD(1,0x882);
printf("%04d ", TEST);
TEST=0;
printf("\n Real Power C:");
TEST= MAXREAD(1, 0x884);
printf("%04d ",TEST);
TEST = MAXREAD(1, 0x887);
printf(" \n Total Real Power Consumed = %04d",TEST );
printf("\n ..........THE END...................\n");
//transmit(0x10);
printf("\n");
// receive();
LCD_WRITE("THEFT");
delay_ms(250);
LCD_CMD(0xC2);
delay_ms(250);
LCD_WRITE("DETECTED ");
SS = 1;
}
}