#include "Display_Utils.h"
// 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
unsigned char txt[14];
unsigned long adc_rd;
float Level;
unsigned short shifter, portd_index;
unsigned short portd_array[4];
char digit,digit1, digit10,digit100,i;
void interrupt() {
LATA = 0; // Turn off all 7seg displays
LATD = portd_array[portd_index]; // bring appropriate value to PORTD
LATA = shifter; // turn on appropriate 7seg. display
// move shifter to next digit
shifter <<= 1; if (shifter > 8u)
shifter = 1;
// increment portd_index
portd_index++ ;
if (portd_index > 3u)
portd_index = 0; // turn on 1st, turn off 2nd 7seg.
TMR0L = 0; // reset TIMER0 value
TMR0IF_bit = 0; // Clear TMR0IF
}
void display(){
digit = i % 10u;
digit1 = conversion(digit); // prepare ones digit
portd_array[1] = conversion(digit); // and store it to PORTD array
digit = (char)(i / 10u) % 10u;
digit10 = conversion(digit); // prepare tens digit
portd_array[2] = conversion(digit); // and store it to PORTD array
digit = (i / 100u) % 10u; // extract hundreds digit
digit100 = conversion(digit);
portd_array[3] = conversion(digit); // and store it to PORTD array
}
void level1(void) //function level1
{
LATC =0x01; // LED RED ON indicates the tank is empty
delay_ms(50);
}
void level2(void) //function level2
{
LATC =0x02; // LED YELLOW ON LEVEL 1/4
delay_ms(50);
}
void level3(void) //function level3
{
LATC =0x04; // LED YELLOW ON LEVEL 1/2
delay_ms(50);
}
void level4(void) //function level4
{
LATC =0x08; // LED YELLOW ON LEVEL 3/4
delay_ms(50);
}
void level5(void) //function level5
{
LATC =0x10; // LED green ON indicates the tank is full
//delay_ms(2000);
}
void Melody_alarm_Full(void)
{
Sound_Play(2200, 80); // PLAY 2.2KHZ TONE FOR 80mS
delay_ms(50); // AND PAUSE FOR 50mS
Sound_Play(820, 80); // THEN PLAY A 820HZ TONE FOR 80mS
delay_ms(50);
}
void main(){
ANSELA.F0 = 1; // Set RA0 to analog
ANSELD = 0; // Configure PORTD pins as digital
ANSELB = 0; // Configure PORTB pins as digital
ANSELC = 0; // Configure PORTC pins as digital
ADCON1=0; // ADC ref = Vdd,Vss
ADCON2=0xAF; // right justify, Frc,& 12 TAD ACQ time
TRISA = 0x01; // Configure PORTA as output
LATA = 0; // Clear PORTA
TRISB = 0; // Configure PORTD as output
LATB = 0; // Clear PORTD
TRISD = 0; // Configure PORTD as output
LATD = 0; // Clear PORTD
LATC = 0; // set PORTC to 0
TRISC = 0; // designate PORTC pins as output
T0CON = 0xC4; // Set TMR0 in 8bit mode, assign prescaler to TMR0
TMR0L = 0; // clear TMROL
digit = 0;
portd_index = 0;
Sound_Init(&PORTB, 7);
//Sound_Play(880, 1000); // Play sound at 880Hz for 1 second
shifter = 1;
GIE_bit = 1;
TMR0IE_bit = 1; //INTCON = 0xA0; // Enaable GIE,T0IE
ADCON2=0xAD;
ADC_Init(); // Initialize ADC
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 2, "LET'S THINK BINARY");
Lcd_Out(2, 1, "AUTOMATIC LEVEL TANK");
Lcd_Out(3, 1, "LEVEL TANK IS:");
Lcd_Chr(3,19,'%'); // Display "C" for Celsius
//Lcd_Out(4, 1, " ");
adc_rd = 0;
while(1){ // infinite loop
adc_rd = ADC_Read(0); // Read from channel 0
Level = adc_rd * 100.0/1024.0; // Convert to porcentage
i= Level;
floatToStr(Level, txt); // Convert to string
txt[4]=0;
Lcd_Out(3,15,txt); // Write string in seco
display();
portb.f6=1;
if (i==0) level1();
else if (i == 25) level2();
else if (i == 50) level3();
else if (i == 75) level4();
else if (i == 99) {level5();
portb.f6=0;} // // if the tank is full stop pump
else portc=0x00;
if ((i>=0)&& (i<=25)) Lcd_Out(4, 1, "MOTOR ON BUZZER OFF"); else if ((i>25)&& (i<=50)) Lcd_Out(4, 1, "MOTOR ON BUZZER OFF"); else if ((i>50)&& (i<=75)) Lcd_Out(4, 1, "MOTOR ON BUZZER OFF"); else if ((i>75)&& (i<=98)) Lcd_Out(4, 1, "MOTOR ON BUZZER OFF"); else {Lcd_Out(4, 1, "MOTOR OFF BUZZER ON"); // if the tank is full stop pump Melody_alarm_Full();} } // end while } // end of program
void level1(void) //function level1
{
LATC =0x01; // LED RED ON indicates the tank is empty
delay_ms(50);
}
void level2(void) //function level2
{
LATC =0x02; // LED YELLOW ON LEVEL 1/4 <<<< change this to 0x03
delay_ms(50);
}
void level3(void) //function level3
{
LATC =0x04; // LED YELLOW ON LEVEL 1/2 <<<< change this to 0x07
delay_ms(50);
}
void level4(void) //function level4
{
LATC =0x08; // LED YELLOW ON LEVEL 3/4 <<<< change this to 0x1F
delay_ms(50);
}
void level5(void) //function level5
{
LATC =0x10; // LED green ON indicates the tank is full <<<< change this to 0x3F
//delay_ms(2000);
}
But it is possible to make the leds automatically lights without modifying the pot-hg value?Brian.
And at value of 75% the "full led" it light, and it shouldn't.But it is possible to make the leds automatically lights without modifying the pot-hg value?
{
LATC =0x08; // LED YELLOW ON LEVEL 3/4 <<<< change this to 0x0F
delay_ms(50);
}
void level5(void) //function level5
{
LATC =0x10; // LED green ON indicates the tank is full <<<< change this to 0x1F
//delay_ms(2000);
}
This. I try Brian's solution. At 75% the green led it's on but it shouldn't...Hi,
did you try Brian´s solution? Give feedback about the result.
****
Can you explain what you mean with "pot-hg"? I can neither find it in your code nor in your schematic.
Klaus
Ok. But when it is 99% i want the motor to stop. How I do it?The basic method I proposed relies on the pattern of bits you output to the LEDS. If you look at the binary value of the bits you will see a '1' turns the LED on and a '0' turns it off. All you have to do is change the pattern of 1s and 0s so it gives the LED indication you want. Start by looking at the LED port values in binary and you will see all I did was change from a single bit being '1' with the other s '0', to an increasing number of 1s so it gave a bar graph output instead of a dot output. If you want the EMPTY LED to turn off, all you do is make the bit on RC0 a 0 whenever any of the other bits are 1. You can do that by simply changing the value written to the port.
Brian.
Can you help me to do again the code?You need to show us the full code first, the end is missing in post #1.
A decision has to be made of how you represent the values. You code is confusing.
I would do it this way:
1. read the ADC.
2. you want percentage of FSD figures so divide the ADC value by 10.23. This results in 0 to 100 from 0V to full input voltage.
3. display the percentage on the LCD
4. use "if(percentage < 99)" to turn the pump on. Otherwise turn it off.
5. use "if(percentage < 5) to turn the EMPTY LED on. Otherwise turn the EMPTY LED off. Adjust the '5' as needed.
6. use "if((percentage >= 25) && (percentage < 50))" to turn the "1/4" LED on.
7. use "if((percentage >= 50) && (percentage <75))" to turn the "1/2" LED on.
8. use "if(percentage >= 75) turn the "3/4" LED on.
9. use "if(percentage >= 99) to turn the FULL LED on.
Brian.
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?