|
|
View previous topic :: View next topic |
Author |
Message |
anilmanisingh
Joined: 31 Mar 2011 Posts: 6
|
low batt adc |
Posted: Fri Apr 10, 2015 4:00 am |
|
|
Hello everyone.
I want to start my switch count when the battery is above 4 volt and switch on the led. If battery is less than 4 volt switch one led at PORT C5.
But I am unable to do so.
Please help me.
Code: |
#include <18F4520.h>
#device adc=8
#fuses INTRC_IO, BROWNOUT, PUT, NOWDT
#use delay(clock=4M)
#define BUTTON_PIN PIN_B0 // This pin must have a pull-up.
#define DEBOUNCE_PERIOD_IN_MS 10
#define DEBOUNCE_COUNT 2
void wait_for_keypress(void);
//==================================
void wait_for_keypress(void)
{
char count;
// First, wait for the button to be released. With the debounce
// values as given above, the button must be in the "up" state
// for two consecutive readings, spaced 10 ms apart.
count = 0;
while(TRUE)
{
if(input(BUTTON_PIN) == 1)
count++;
else
count = 0;
if(count == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
// Now that the button is up, wait until the user presses it.
// In order for the keypress to be considered valid, based
// on the debounce values listed at the beginning of the
// program, the button must be held down for two consecutive
// readings, spaced 10 ms apart.
count = 0;
while(TRUE)
{
if(input(BUTTON_PIN) == 0)
count++;
else
count = 0;
if(count == DEBOUNCE_COUNT)
break;
delay_ms(DEBOUNCE_PERIOD_IN_MS);
}
}
//====================================
void main()
{
int8 count,result;
port_b_pullups(TRUE);
count = 0;
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
while(TRUE)
{
result = read_adc();
if(result<=68) //battery less than 4v
{
output_high(PIN_C5);
}
else if (result>=68 && result<=68 ) //battery above 4v
{
output_low(PIN_C5);
wait_for_keypress();
count++;
if (count > 4) count = 1;
{
if (count==1)
{
output_C(0b00000010);
delay_ms(500);
}
else if (count==2)
{
output_C(0b00000110);
delay_ms(500);
}
else if (count==3)
{
output_C(0b00001010);
delay_ms(500);
}
else if (count==4)
{
output_C(0b00000000);
delay_ms(500);
}
}
}
}
}
|
++++++++++++++++++
Code block added.
- Forum Moderator
++++++++++++++++++ |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9245 Location: Greensville,Ontario
|
|
Posted: Fri Apr 10, 2015 4:55 am |
|
|
first... please use the 'code' button when posting code, it'll mke it a LOT easier to read / debug your code.
2. this statement..
if (result>=68 && result<=68 )
Play 'computer' and see what happens....
Jay |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|