// LCD module connections
sbit LCD_RS at RD2_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_EN at RD3_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_D4 at RD4_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_D5 at RD5_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_D6 at RD6_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_D7 at RD7_bit; // for writing to output pin always use latch (PIC18 family)
sbit LCD_RS_Direction at TRISD2_bit;
sbit LCD_EN_Direction at TRISD3_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;
// End LCD module connections
unsigned char ch; //
unsigned int adc_rd; // Declare variables
char *text; //
char junk[40]; // bodge to get around error above
long tlong,ulong, tlong1;
char output[7];
int i,rot;
float ufloat,tfloat;
int uint;
void main() {
CMCON |= 7;
TRISA = 0x04; // RA2 is analogue input
ADCON1 = 0x80;
TRISB =0; //
PORTB=0x00;
Lcd_Init(); // LCD display initialization
Lcd_Cmd(_LCD_CLEAR);
text = "Output Voltage"; // Define the second message
Lcd_Out(1,1,text); // Define the first message
Delay_ms(1000);
text = "voltage:"; // Define the third message
while (1) {
adc_rd = ADC_Read(2); // A/D conversion. Pin RA2 is an input.
Lcd_Out(2,1,text); // Write result in the second line
tlong = (long)adc_rd; // Convert the result in millivolts
ch = tlong / 1000; // Extract volts (thousands of millivolts)
// from result
Lcd_Chr(2,9,48+ch); // Write result in ASCII format
Lcd_Chr_CP('.');
ch = (tlong / 1000) % 10; // Extract thousands of millivolts
Lcd_Chr_CP(48+ch);
ch = (tlong / 100) % 10; // Extract hundreds of millivolts
Lcd_Chr_CP(48+ch); // Write result in ASCII format
ch = (tlong / 10) % 10; // Extract tens of millivolts
Lcd_Chr_CP(48+ch); // Write result in ASCII format
ch = tlong % 10; // Extract digits for millivolts
Lcd_Chr_CP(48+ch); // Write result in ASCII format
Lcd_Chr_CP('V');
Delay_ms(1);
UART1_Init(9600); // Initialize UART1 module at 9600 bps
Delay_ms(100); // Wait for UART 1module to stabilize
UART1_Write_Text("Start");
UART1_Write(13);
UART1_Write(10);
while (!UART1_Data_Ready()); // wait for UART character in buffer
// Initialize string with all '\0'
for(i=0; i<7; i++){
output[i] = '\0';
}
UART1_Read_Text(output, "m", 255); // read input from uart
ulong = atol(output);
tlong1= tlong; ///1000;
while(1){
if ( ulong != tlong1 ){
if ( ulong > tlong1 ){
while(1)
{
// UART1_Write_Text("1a");
Delay_ms(20);
PORTB=0b00001100;
Delay_ms(20);
PORTB=0b00000110;
Delay_ms(20);
PORTB=0b00000011;
Delay_ms(20);
PORTB=0b00001001;
adc_rd = ADC_Read(2); // A/D conversion. Pin RA2 is an input.
//Lcd_Out(2,1,text); // Write result in the second line
tlong = (long)adc_rd; // Convert the result in millivolts
tlong1= tlong;
//UART1_Write_Text("1b");
if (tlong1 == ulong ){
UART1_Write_Text("Brk");
break;
}
}
}
else{
while(1)
{
//UART1_Write_Text("2a");
Delay_ms(20);
PORTB=0b00001001;
Delay_ms(20);
PORTB=0b00000011;
Delay_ms(20);
PORTB=0b00000110;
Delay_ms(20);
PORTB=0b00001100;
adc_rd = ADC_Read(2); // A/D conversion. Pin RA2 is an input.
// Lcd_Out(2,1,text); // Write result in the second line
tlong = (long)adc_rd; // Convert the result in millivolts
tlong1= tlong;
//UART1_Write_Text("2b");
if ( tlong1 == ulong ){
UART1_Write_Text("Brk");
break;
}
}
}
}
else
{ break;}
}
}
}