Tamim Neak
Junior Member level 1
Hi all,
I have been working on the following code and running the simulation on Proteus:
I have two problems:
1- I don't get any value for the Battery_voltage in the LCD.
2- When i run the code and make the battery_voltage less than 10V the led at PORTB.F0 keeps blinking instead of staying on till the battery_voltage > 10V.
any help regarding these issues are appreciated
I have been working on the following code and running the simulation on Proteus:
Code:
// ***************************************************************************
// File Name : newproject.c
// Version : 1.0
// Description : Pulse Width Modulation (PWM)
// ADC
// Author(s) : Tamim Neak
// Target(s) : PIC16F887
// Programmer : PICKit
// Last Updated : 14 Feb 2015
// ***************************************************************************
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RD1_bit;
sbit LCD_D5 at RD2_bit;
sbit LCD_D6 at RD3_bit;
sbit LCD_D7 at RD4_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISD1_bit;
sbit LCD_D5_Direction at TRISD2_bit;
sbit LCD_D6_Direction at TRISD3_bit;
sbit LCD_D7_Direction at TRISD4_bit;
// End LCD module connections
unsigned int battery_voltage;
unsigned int output_voltage;
unsigned int output_current;
unsigned int temp;
float temp1;
float battery_voltage1;
char temper[7];
char battery_voltage2[7];
float x = 15.15;
void temp_read(void){
temp = ADC_read(0); // Get 10-bit results of AD conversion
temp1 = (temp* 500./1023);
if (temp > 56) PORTB.F1 = 1; // cooling fan on
else PORTB.F1 = 0; // cooling fan off
IntToStr(temp1,temper);
lcd_out(1,6, Ltrim(temper));
Lcd_Chr_Cp(0xdf);
Lcd_Chr_Cp('C');
Lcd_Chr_Cp(' ');
}
void battery_read(){
battery_voltage = ADC_read(1); // Get 10-bit results of AD conversion
battery_voltage1 = (battery_voltage* 15.15/1023);
if (battery_voltage < 676) PORTB.F0 = 1; // battery LED off
else PORTB.F0 = 0; // battery too low LED on
IntToStr(battery_voltage1,battery_voltage2);
lcd_out(1,6, Ltrim(battery_voltage2));
Lcd_Chr_Cp('V');
Lcd_Chr_Cp(' ');
}
void main() {
ADCON1 = 0x00; // All channels are config as analog I/p.
TRISA=0b00001111;
TRISD.F0 = 1; //Configure 1st bit of PORTD as input for power on
TRISB = 0; //Configure PORTB as output
PORTB = 0;
PORTD.F0 = 1; //Switch off
adc_init(); // Initialize ADC module with default settings
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CURSOR_OFF); // LCD Cursor Off
Lcd_Cmd(_LCD_CLEAR); // Clear display
lcd_out(1,1,"TEMP=");
lcd_out(1,11,"Bat=");
do {
//output_voltage = ADC_read(3);
//output_current = ADC_read(4);
temp_read();
battery_read();
battery_voltage = ADC_read(1);
if ((PORTD.F0 == 0) && (battery_voltage > 676)) //If the switch is pressed
{
Delay_ms(100); //Switch Debounce
if ((PORTD.F0 == 0) && (battery_voltage > 676)) //If the switch is still pressed
{
PORTB.F2 = 1; //PWM LED ON
Delay_ms(100);
temp_read();
}
}else {
PORTB.F2 = 0; //PWM LED Off
Delay_ms(100);
}
} while(1);
}
I have two problems:
1- I don't get any value for the Battery_voltage in the LCD.
2- When i run the code and make the battery_voltage less than 10V the led at PORTB.F0 keeps blinking instead of staying on till the battery_voltage > 10V.
any help regarding these issues are appreciated