Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] LED doesn't turn off

Status
Not open for further replies.

sacban

Junior Member level 3
Joined
Aug 20, 2015
Messages
29
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
412
Hi There,
I have a problem with my code, could please see on below :

Code:
unsigned int adc_val, adc_val1, adc_val2, adc_val3;
unsigned long Q1, Q2, Q3, Q4;     // These are gate PORTA0 - PORTA3

 
sbit LED0 at PORTD.B0;
sbit LED1 at PORTD.B1;
sbit LED2 at PORTD.B2;
sbit LED3 at PORTD.B3;
sbit BLINK at PORTE.B0;       // I hope this will BLINK every voltage is on, but still not get the result



void main() {
     ADCON1  = 0x0F;                            // configure Vref, and analog channels     0x88
     TRISA   = 0x0F;                            // designate PORTA as input  0xFF
     TRISB   = 0x00;
     TRISC   = 0x00;                               // PORTC all output to LED
     TRISD   = 0x00;                               // PORTD all output to LED
     TRISE   = 0x00;
     PORTA   = 0x30;
     PORTB   = 0x00;
     PORTC   = 0x00;
     PORTD   = 0x00;
     PORTE   = 0x00;


     Delay_ms(100);

     for (;;) {


           adc_val = ADC_read(0);               // get ADC value for U from channel 0
           Q1 = (long)adc_val*5000/1023;       // Convert ADC value to millivolts
           adc_val1 = ADC_read(1);               // get ADC value for U from channel 1
           Q2 = (long)adc_val1*5000/1023;      // Convert ADC value to millivolts
            adc_val2 = ADC_read(2);               // get ADC value for U from channel 2
           Q3 = (long)adc_val2*5000/1023;      // Convert ADC value to millivolts
            adc_val3 = ADC_read(3);               // get ADC value for U from channel 3
           Q4 = (long)adc_val3*5000/1023;      // Convert ADC value to millivolts



           // This an input voltage at PORTA0

           if (Q1 > 200 && Q1 <= 5000) {
            // BLINK = 1; ===> I adding this LED On PORTE to make it BLINK, but it doesn't work only led on or always blinking
                            // I wish to BLINK 1 times when have a voltage input

             LED0 = 1;

              Delay_ms(8);

           }

           else
           
             if (Q1 == 0) {

           {

             LED0 = 0;

           }


           }       // END BUTTON # 1

           //===============================================
              if (Q2 > 200 && Q2 <= 5000) {
             LED1 = 1;

              Delay_ms(8);

           }
           else

             if (Q2 == 0) {

           {
            LED1 = 0;

            }
            }     // BUTTON # 2
            
            /***************************************************************************************/
            /**  MY PROBLEM HERE, PLEASE SEE ON BELOW NOTE *****************************************/

              //===============================================
              if (Q1 > 200 && Q1 <= 5000  && Q2 > 200 && Q2 <= 5000)   {      // If Q1 and Q2 have a voltage then
                         // if (Q2 > 200 && Q2 <= 5000 ) {
             Delay_ms(100); // Wait
             LED0 = 0;   // I add these LED0 And LED1, mean are Q1 && Q2 have a voltage LED2 is ON and LED0 and LED1 become off
             LED1 = 0;   // but it doesn't work, it only blinking
             LED2 = 1;     // LED1 is on
              Delay_ms(50);       // 5 second


           }
           else

             if (Q1 == 0 ||  Q2 == 0)       // If PORTA0 & PORTA1 have 0 Voltage then


           {

            LED2 = 0;          // LED2 should turn Off


            }
             }
             
             
               //===============================================
               /********* I AM WAITING TO SOLVED ABOVE CODE THEN WILL CONTINUE TO ANOTHER CODE ************/
               
              if (Q4 > 200 && Q4 <= 5000) {
             LED3 = 1;

              Delay_ms(8);

           }
           else

             if (Q4 == 0) {

           {
            LED3 = 0;

            }
            
            
             }
             
              while (1);
                // Voltage reference up then xx voltage
             (Q1 > 4700 && Q1 <= 5000);
             (Q2 > 4700 && Q2 <= 5000);
             (Q3 > 4700 && Q3 <= 5000);
             (Q4 > 4700 && Q4 <= 5000);



         }


Sacban.

- - - Updated - - -

My problem are :
/***************************************************************************************/
/** MY PROBLEM HERE, PLEASE SEE ON BELOW NOTE *****************************************/

//===============================================
if (Q1 > 200 && Q1 <= 5000 && Q2 > 200 && Q2 <= 5000) { // If Q1 and Q2 have a voltage then

Delay_ms(100); // Wait
LED0 = 0; // I add these LED0 And LED1, mean are Q1 && Q2 have a voltage LED2 is ON and LED0 and LED1 become off
LED1 = 0; // but it doesn't work, it only blinking
LED2 = 1; // LED1 is on
Delay_ms(50); // 5 second


}
else

if (Q1 == 0 || Q2 == 0) // If PORTA0 & PORTA1 have 0 Voltage then


{

LED2 = 0; // LED2 should turn Off


}
}

As you see here :
LED0 = 0; // I add these LED0 And LED1, mean are Q1 && Q2 have a voltage LED2 is ON and LED0 and LED1 become off
LED1 = 0; // but it doesn't work, it only blinking
LED2 = 1; // LED1 is on

When Q1 and Q2 have a voltage, I wish to turn OFF 2 LED, are LED0 and LED1 and Turn ON LED2, but onto my code, both LED (0 and 1) only blinking or On can't be OFF.

Also
if (Q1 > 200 && Q1 <= 5000) {
// BLINK = 1; ===> I adding this LED On PORTE to make it BLINK, but it doesn't work only led on or always blinking
// I wish to BLINK 1 times when have a voltage input

LED0 = 1;

Delay_ms(8);

}

else

if (Q1 == 0) {

{

LED0 = 0;

}


} // END BUTTON # 1

BLINK is PORTE0, this Led can't blink for xx second then turn off, this LED (BLINK) always blinking and doesn't stop or can blinking everytime, I wish this BLINK will blinking every time Q1 have a voltage then if Q1 don't have a voltage reference he should off for LED (BLINK).

Sacban.
 

is it avr gcc ?
Try send data to direct direct port instead of sbit

PORTE |= (1<<7); // setting 8th bit of the PortE
PORTD &= ~(1<<7);// Cearing 8th bit of the PortE
.
 

Hi Tannara,
Is not AVR GCC, his a MikroC Compiler Pro C.

is it avr gcc ?
Try send data to direct direct port instead of sbit

PORTE |= (1<<7); // setting 8th bit of the PortE
PORTD &= ~(1<<7);// Cearing 8th bit of the PortE
.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
unsigned int adc_val, adc_val1, adc_val2, adc_val3;
unsigned long Q1, Q2, Q3, Q4;     // These are gate PORTA0 - PORTA3
 
sbit LED0 at PORTD.B0;
sbit LED1 at PORTD.B1;
sbit LED2 at PORTD.B2;
sbit LED3 at PORTD.B3;
sbit BLINK at PORTE.B0;       // I hope this will BLINK every voltage is on, but still not get the result
 
PORTE |= (1<<7); // setting 8th bit of the PortE 
PORTD &= ~(1<<7);// Cearing 8th bit of the PortE 
 
void main() {
     ADCON1  = 0x0F;                            // configure Vref, and analog channels     0x88
     TRISA   = 0x0F;                            // designate PORTA as input  0xFF
     TRISB   = 0x00;
     TRISC   = 0x00;                               // PORTC all output to LED
     TRISD   = 0x00;                               // PORTD all output to LED
     TRISE   = 0x00;
     PORTA   = 0x30;
     PORTB   = 0x00;
     PORTC   = 0x00;
     PORTD   = 0x00;
     PORTE   = 0x00;
 
     Delay_ms(100);
 
     for (;;) {



Sacban.
 
Last edited by a moderator:

Hi Tannara,
Is not AVR GCC, his a MikroC Compiler Pro C.




Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
unsigned int adc_val, adc_val1, adc_val2, adc_val3;
unsigned long Q1, Q2, Q3, Q4;     // These are gate PORTA0 - PORTA3
 
sbit LED0 at PORTD.B0;
sbit LED1 at PORTD.B1;
sbit LED2 at PORTD.B2;
sbit LED3 at PORTD.B3;
sbit BLINK at PORTE.B0;       // I hope this will BLINK every voltage is on, but still not get the result
 
PORTE |= (1<<7); // setting 8th bit of the PortE 
PORTD &= ~(1<<7);// Cearing 8th bit of the PortE 
 
void main() {
     ADCON1  = 0x0F;                            // configure Vref, and analog channels     0x88
     TRISA   = 0x0F;                            // designate PORTA as input  0xFF
     TRISB   = 0x00;
     TRISC   = 0x00;                               // PORTC all output to LED
     TRISD   = 0x00;                               // PORTD all output to LED
     TRISE   = 0x00;
     PORTA   = 0x30;
     PORTB   = 0x00;
     PORTC   = 0x00;
     PORTD   = 0x00;
     PORTE   = 0x00;
 
     Delay_ms(100);
 
     for (;;) {



Sacban.
what does mean the above program ?
 

Hi Tannara,
As well my program are :
1). I have for input are Q1-Q2-Q3-Q4 in PORTA, all those an input are waiting for instruction to get voltage around 2-3,2 Vdc (this led voltage).
2). Q1-Q2-Q3-Q4 working similiar BCD, example when Q1 have a high then the rest is lower, I need to send to output PORTD high example PORTD0.
3). If Q1-Q3-Q4 is no voltage or low, then I need to send to output PORTD high example PORTD1.
4). All those Q1-Q2-Q3-Q4 if run only single, example only Q1 Run the other is low or Q2 Run the other is state low (no voltage) and Q3 or Q4, my program work fine.
5). The problem when I run both of them or 3 or all 4 PORTA (Q1-Q2-Q3-Q4), then I can't turn of the led.

More eaxample :
1). Q1 and Q2 getting high, then I wish PORTD3 should be high other on PORTD is low.
2). Q1 and Q3 getting high, then I wish PORTD4 should be high other on PORTD is low.
3). Q1-Q2-Q3 getting high, then I wish PORTD5 should be high other on PORTD is low.
4). Q1-Q2-Q3-Q4 getting high, then I wish all PORTD should be low.
5). or other Q1-Q2-Q3-Q4.

The point is Q1-Q2-Q3-Q4 are gate and also working similiar as a button, but there are have a voltage (ADC) to control low and high.


adc_val = ADC_read(0); // get ADC value for U from channel 0
Q1 = (long)adc_val*5000/1023; // Convert ADC value to millivolts
adc_val1 = ADC_read(1); // get ADC value for U from channel 1
Q2 = (long)adc_val1*5000/1023; // Convert ADC value to millivolts
adc_val2 = ADC_read(2); // get ADC value for U from channel 2
Q3 = (long)adc_val2*5000/1023; // Convert ADC value to millivolts
adc_val3 = ADC_read(3); // get ADC value for U from channel 3
Q4 = (long)adc_val3*5000/1023; // Convert ADC value to millivolts



// This an input voltage at PORTA0

if (Q1 > 200 && Q1 <= 5000) {
// BLINK = 1; ===> I adding this LED On PORTE to make it BLINK, but it doesn't work only led on or always blinking
// I wish to BLINK 1 times when have a voltage input


I hope you clear what I mean and get the point with above explanation.


Sacban.
 

sorry i dint clearly what do you say .please upload your the file ,also proteus file if you have ?
 

After taken for few day, that the project now is working very well, thank you for your contribution.


Sacban.
:thinker::thinker::thinker:
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top