Abiss
Newbie level 4
I want to modify this code so that the cursor from POT-HG changes automatically and the LEDs light up one by one. Can someone help me?
C:
#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
--- Updated ---
Last edited by a moderator: