// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// LCD text variables
char txt1[] = "V1 = ";
char txt2[] = "V2 = ";
char txt3[6] = " ";
char txt4[6] = " ";
char txt5[] = "V RMS";
float v, v1, v2, v_peak;
unsigned int adc1, adc2;
unsigned char l_byte, h_byte;
char ch1[5], ch2[5];
float ADR, adc;
unsigned short channel;
unsigned short current_status, prev_status;
/////* Definitions */////
#define v_rms_min 200 // lowest alloable RMS voltage
#define adc_rms_min 806 // 220v
#define source_1 RD0_bit // source_1 @ pin RD0
#define source_2 RD1_bit // source_2 @ pin RD1
#define no_active_source RD2_bit // no_active_source indication LED @ pin RD2
#define source_1_on RD3_bit // source_1_on indication LED @ pin RD3
#define source_2_on RD4_bit // source_2_on indication LED @ pin RD4
// LCD module connections
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
// End LCD module connections
// LCD text variables
char txt1[] = "V1 = ";
char txt2[] = "V2 = ";
char txt3[6] = " ";
char txt4[6] = " ";
char txt5[] = "V RMS";
unsigned int ADCRead(unsigned short channel){
if (channel == 0) {ADCON0 = 0x81;} // convert from channel0
if (channel == 1) {ADCON0 = 0x89;} // convert from channel1
delay_us(80); // Acquisition Delay
GO_DONE_bit = 1; // Set GO_DONE bit to start conversion
while (GO_DONE_bit == 1); // Wait for bit to be cleared
// If bit is cleared, this means conversion is over
l_byte = ADRESL;
h_byte = ADRESH;
ADR = (h_byte<<8)|l_byte;
return ADR;
}
float RMS_calculator (unsigned short source) {
channel = source;
adc = ADCRead(channel);
v_peak = (adc*396)/1023;
v = v_peak/sqrt(2); // rms value for sinewaves
return v;
}
unsigned short check_status() {
/*
if ( adc1 <= adc_rms_min ) {
if ( adc2 >= adc_rms_min )
{ current_status = 2; }
else { current_status = 3; }
}
else { current_status = 1; }
*/
if ( v1 <= v_rms_min ) {
if ( v2 <= v_rms_min ) { current_status = 3; }
else { current_status = 2; }
}
else { current_status = 1; }
return current_status;
}
void apply_status() {
switch ( current_status ) {
case 1:
{
source_2 = 0; // deactivate source_2
delay_ms(10); // wait 5ms
source_1 = 1; // activate source_1
source_2_on = 0; // source_2_on LED is OFF
source_1_on = 1; // source_1_on LED is ON
no_active_source = 0; // no_active_source LED is OFF
break;
}
case 2:
{
source_1 = 0; // deactivate source_1
delay_ms(10); // wait 5ms
source_2 = 1; // activate source_2
source_1_on = 0; // source_1_on LED is OFF
source_2_on = 1; // source_2_on LED is ON
no_active_source = 0; // no_active_source LED is OFF
break;
}
case 3:
{
source_1 = 0; // source_1 off
source_2 = 0; // source_2 off
no_active_source = 1; // no_active_source LED is ON
source_1_on = 0; // source_1_on LED is OFF
source_2_on = 0; // source_2_on LED is OFF
break;
}
}
}
void main() {
CMCON = 7; //Disable comparator
PORTA = 0;
TRISA = 0xff;
PORTD = 0;
TRISD = 0;
PORTC = 0;
TRISC = 0;
PORTB = 0;
TRISB = 0;
ADCON0 = 0x81;
ADCON1 = 0xC0; // all portA pins are analog // was 0xCE
source_1 = 0; // source_1 off
source_2 = 0; // source_2 off
source_1_on = 0; // source_1_on LED is OFF
source_2_on = 0; // source_2_on LED is OFF
no_active_source = 1; // no_active_source LED is OFF
v1 = 0;
v2 = 0;
adc1 = 0;
adc2 = 0;
current_status = 3;
apply_status();
/// LCD initialization ///
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,txt1); // Write text in first row
Lcd_Out(2,1,txt2); // Write text in second row
// option_reg=0b00101111;
while (1){
prev_status = current_status;
// calculate channel0 voltage
v1 = RMS_calculator (0);
//adc1 = ADR;
// calculate channel1 voltage
v2 = RMS_calculator (1);
// adc2 = ADR;
current_status = check_status();
if ( current_status != prev_status ) { apply_status(); }
delay_ms(100);
// display v1
floattostr(v1,ch1);
txt3[0] = ch1[0];
txt3[1] = ch1[1];
txt3[2] = ch1[2];
txt3[3] = ch1[3];
txt3[4] = ch1[4];
Lcd_Out(1,6,txt3);
Lcd_Out(1,12,txt5);
// display v2
floattostr(v2,ch2);
txt4[0] = ch2[0];
txt4[1] = ch2[1];
txt4[2] = ch2[2];
txt4[3] = ch2[3];
txt4[4] = ch2[4];
Lcd_Out(2,6,txt4);
Lcd_Out(2,12,txt5);
}
}
Where is the oscillator setting? Where are the configuration bits? If that's your entire program it likely does not work at all. What does the circuit look like?...Is the crystal or resonator hooked up properly? Are the right pins connected to the power rails?
sbit LCD_D4 at RB0_bit;
Try using another output instead of RB0 or see here how to disable interupts.
**broken link removed**
jayanth.devarayanadurga
The file attached is mikroC project files. Sorry, I don't have a PIC16F877A with me right now.
No. I didn't do any changes. It's just your code as it is. Does your faulty LCD work with other boards/projects?
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?