kranthikiran.a
Newbie level 2
Hi Guys,
This is kranthi kiran.
I am working on MAXQ3180 Energy meter IC with PIC18F4520. This IC has SPI communication. I am able to communicate with the MAXQ3180 IC. SPI communication code is perfectly working. But my problem is voltage ADC value in the MAXQ3180 IC is not increasing if I increased the input voltage at voltage channel pin. Here I am placing my code. I am using PIC18F4520 MCU and CCS PIC Compiler. Kindly Solve My Problem.Thanks in Advance.
This is kranthi kiran.
I am working on MAXQ3180 Energy meter IC with PIC18F4520. This IC has SPI communication. I am able to communicate with the MAXQ3180 IC. SPI communication code is perfectly working. But my problem is voltage ADC value in the MAXQ3180 IC is not increasing if I increased the input voltage at voltage channel pin. Here I am placing my code. I am using PIC18F4520 MCU and CCS PIC Compiler. Kindly Solve My Problem.Thanks in Advance.
Code:
#define VOLT_CC 0x014
#define AMP_CC 0x016
#define PWR_CC 0x018
#define ENR_CC 0x01A
#define A_VRMS 0x1C8
#define A_IRMS 0x1CC
#define B_VRMS 0x2B4
#define B_IRMS 0x2B8
#define C_VRMS 0x3A0
#define C_IRMS 0x3A4
//SPI Code Starts Here
#define SPI_SS PIN_A5 //This pin is active low pin.
//SPI_SS should be Low to work SPI Communication
#define SPI_SCLK PIN_C3
#define SPI_SDO PIN_C5
#define SPI_SDI PIN_C4
#define SPI_Enable(); output_low(SPI_SS);
#define SPI_Reset(); output_high(SPI_SS); \
delay_ms(10); \
output_low(SPI_SS); \
delay_cycles(5); //This will Reset the SPI Communication
#define SPI_Disable(); output_high(SPI_SS); //This will Disable the SPI Communication
//ASIC_Add[1] Contains MSB of ASIC Address
//ASIC_Add[0] Contains LSB of ASIC Address
unsigned int8 ASIC_Bytes=0, ASIC_Data[8], ASIC_Add[2], ASIC_Cmd=0, ASIC_Temp=0;
unsigned int16 ASIC_Address=0;
void ASIC_Read(void);
void ASIC_Write(void);
void SPI_TX(unsigned char);
unsigned char Reverse_Bits(unsigned char);
void ASIC_Read(void)
{
/* Refer MAXQ3183 Data Sheet FOR Complete Details About SPI Communication Flow Chart*/
/* ASIC has two Command Bytes.
ASIC Command Byte1 Bits:
Command Code
7:6
0 0 - Read
0 1 - Reserved
1 0 - Write
1 1 - Reserved
Data Length
5:4
0 0 - 1 Byte
0 1 - 2 Bytes
1 0 - 4 Bytes
1 1 - 8 Bytes
3:0 MSB Portion of Data Address*/
unsigned char i = 0;
Read:
ASIC_Add[0] = (unsigned int8)(ASIC_Address & 0x00FF);
//Extract The LSB portion from ASIC_Address 16Bit Register.
ASIC_Add[1] = (unsigned int8)((ASIC_Address & 0x0F00)>>0x08);
//Extract The MSB portion from ASIC_Address 16Bit Register.
//Consider Lower Nibble in that extracted part,Because ASIC has only 12Bit Address.
ASIC_Cmd &= 0x3F; //7th nd 6th bits in Command Byte1 REGISTER Should be zero for ASIC Read REGISTER Operation
switch (ASIC_Bytes)
{
case 1:bit_clear(ASIC_Cmd,4); //This case is FOR 1 Byte Data Length
bit_clear(ASIC_Cmd,5);
break;
case 2:bit_set(ASIC_Cmd,4); //This case is FOR 2 Bytes Data Length
bit_clear(ASIC_Cmd,5);
break;
case 4:bit_clear(ASIC_Cmd,4); //This case is FOR 4 Bytes Data Length
bit_set(ASIC_Cmd,5);
break;
case 8:bit_set(ASIC_Cmd,5); //This case is FOR 8 Bytes Data Length
bit_set(ASIC_Cmd,4);
break;
default:bit_clear(ASIC_Cmd,4); //This Default case is FOR 1 Byte Length
bit_clear(ASIC_Cmd,5);
break;
}
ASIC_Add[1] &= 0x0F; //ASIC_Add[1] Contains MSB of ASIC Address.Higher Nibble in the MSB Should be Zero,
//Because ASIC has only 12Bit Address.So MSB contains data only in Lower Nibble
ASIC_Cmd |= ASIC_Add[1];
spi_write(ASIC_Cmd); //Write SPI Command Byte1
ASIC_Temp = spi_read(); //Read SPI Data Received. ASIC will send 0xC1 as acknoledge;
if(ASIC_Temp==0xC1)
delay_us(60);
else
{
delay_ms(360);
goto Read;
}
ASIC_Cmd = ASIC_Add[0]; //Load ASIC Command Byte2 in ASIC_Cmd REGISTER,
spi_write(ASIC_Cmd); //ASIC_Add[0] Contains LSB of 12Bit Address
ASIC_Temp = spi_read();
if(ASIC_Temp==0xC2)
delay_us(60);
else
{
delay_ms(360); //Reset ASIC_Add Variable to zero and recall the function using GOTO,
ASIC_Cmd = 0; //Because this compiler is not supporting recursive functions
goto Read;
}
spi_write(00); //Send Dummy Byte To get the Data From Slave
ASIC_Temp = spi_read();
while(ASIC_Temp==0x4E)
{
delay_us(60);
spi_write(00); //Send Dummy Byte To get the Data From Slave
ASIC_Temp = spi_read();
}
if(ASIC_Temp==0x41)
{
do
{
delay_us(60);
spi_write(00);
ASIC_Data[i]=spi_read(); //ASIC Will send LSB First.So ASIC_Data[0] Contains LSB and ASIC_Data[n] Conatains MSB
i++;
}while(--ASIC_Bytes);
}
else
{
delay_ms(360);
ASIC_Cmd = 0;
goto Read;
}
}
void ASIC_Write(void)
{
/* Refer MAXQ3183 Data Sheet FOR Complete Details About SPI Communication Flow Chart*/
/* ASIC has two Command Bytes.
ASIC Command Byte1 Bits:
Command Code
7:6 00 - Read
01 - Reserved
10 - Write
11Reserved
Data Length
5:4 00 - 1 Byte
01 - 2 Bytes
10 - 4 Bytes
11 - 8 Bytes
3:0 MSB Portion of Data Address*/
unsigned char i=0;
Write:
//Extract The LSB portion from ASIC_Address 16Bit Register
ASIC_Add[0] = (unsigned int8)(ASIC_Address & 0x00FF);
/*Extract The MSB portion from ASIC_Address 16Bit Register.
Consider Lower Nibble in that extracted part,Because ASIC has only 12Bit Address.
ASIC_Add[1] Contains MSB of ASIC Address.Higher Nibble in the MSB Should be Zero,
Because ASIC has only 12Bit Address.So MSB has data only in Lower Nibble.*/
ASIC_Add[1] = (unsigned int8)((ASIC_Address & 0x0F00)>>0x08);
bit_set(ASIC_Cmd,7); //7th Bit in Command Byte1 REGISTER Should be set and
bit_clear(ASIC_Cmd,6); //6th Bit in Command Byte1 REGISTER should be clear for ASIC Write REGISTER Operation
switch (ASIC_Bytes)
{
case 1:bit_clear(ASIC_Cmd,4); //This case is FOR 1 Byte Data Length
bit_clear(ASIC_Cmd,5);
break;
case 2:bit_set(ASIC_Cmd,4); //This case is FOR 2 Bytes Data Length
bit_clear(ASIC_Cmd,5);
break;
case 4:bit_clear(ASIC_Cmd,4); //This case is FOR 4 Bytes Data Length
bit_set(ASIC_Cmd,5);
break;
case 8:bit_set(ASIC_Cmd,5); //This case is FOR 8 Bytes Data Length
bit_set(ASIC_Cmd,4);
break;
default:bit_clear(ASIC_Cmd,4); //This Default case is FOR 1 Byte Length
bit_clear(ASIC_Cmd,5);
break;
}
ASIC_Cmd |= ASIC_Add[1];
spi_write(ASIC_Cmd); //Write SPI Command Byte1
ASIC_Temp = spi_read(); //Read Received Data Byte. ASIC will send 0xC1;
if(ASIC_Temp==0xC1)
delay_us(60);
else
{
delay_ms (360);
goto Write;
}
ASIC_Cmd = ASIC_Add[0]; //Load ASIC Command Byte2 in ASIC_Cmd REGISTER, ASIC_Add[0] Contains LSB of 12Bit Address
spi_write(ASIC_Cmd); //Write SPI Command Byte2
ASIC_Temp = spi_read(); //Read Received Data Byte. ASIC will send 0xC2;
if(ASIC_Temp==0xC2)
{
do
{
delay_us(60);
spi_write(ASIC_Data[i]); //Write LSByte of Data Byte First.
ASIC_Temp = spi_read(); //Read Received Data Byte.ASIC will send 0x41;
if(ASIC_Temp != 0x41)
{
ASIC_Cmd = 0;
delay_ms (360);
goto Write;
}
i++;
}while(--ASIC_Bytes);
}
else
{
delay_ms(360); //Reset ASIC_Add Variable to zero and recall the function using goto only,
ASIC_Cmd = 0; //Because this compiler is not supporting recursive functions
goto Write;
}
do
{
delay_us(60);
spi_write(00);
ASIC_Temp = spi_read();
}while(ASIC_Temp==0x4E);
if(ASIC_Temp!=0x41)
{
delay_ms(360);
ASIC_Cmd = 0;
goto Write;
}
}
//SPI Code Ends Here
//Never Ending Code Starts Here
while(TRUE)
{
//restart_wdt();
LCD_WriteCmd(0x80);
LCD_Display("VOLTS_CC:");
ASIC_Bytes = 2;
ASIC_Address = VOLT_CC;
ASIC_Read();
Long_Buff = make32(0x00,0x00,ASIC_Data[1],ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0xC0);
LCD_Display("R:");
ASIC_Bytes = 4;
ASIC_Address = A_VRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3],ASIC_Data[2],ASIC_Data[1],ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0x94);
LCD_Display("Y:");
ASIC_Bytes = 4;
ASIC_Address = B_VRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3],ASIC_Data[2],ASIC_Data[1],ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0xD4);
LCD_Display("B:");
ASIC_Bytes = 4;
ASIC_Address = C_VRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3],ASIC_Data[2],ASIC_Data[1],ASIC_Data[0]);
Disp_Long(Long_Buff);
delay_ms(3000);
LCD_WriteCmd(0x01);
LCD_WriteCmd(0x80);
LCD_Display("CURR_CC:");
ASIC_Bytes = 2;
ASIC_Address = AMP_CC;
ASIC_Read();
Long_Buff = make32(0x00,0x00,ASIC_Data[1],ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0xC0);
LCD_Display("R:");
ASIC_Bytes = 4;
ASIC_Address = A_IRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3], ASIC_Data[2], ASIC_Data[1], ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0x94);
LCD_Display("Y:");
ASIC_Bytes = 4;
ASIC_Address = B_IRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3], ASIC_Data[2], ASIC_Data[1], ASIC_Data[0]);
Disp_Long(Long_Buff);
LCD_WriteCmd(0xD4);
LCD_Display("B:");
ASIC_Bytes = 4;
ASIC_Address = C_IRMS;
ASIC_Read();
Long_Buff = make32(ASIC_Data[3], ASIC_Data[2], ASIC_Data[1], ASIC_Data[0]);
Disp_Long(Long_Buff);
delay_ms(3000);
}
//Never Ending Code Ends Here