View previous topic :: View next topic |
Author |
Message |
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
EEPROM and adc value recording |
Posted: Tue Oct 02, 2012 7:10 pm |
|
|
Hello,
I have a problem with my program; it is supposed to record some ADC value in the eeprom.
The schematic is very basic, a thermistor is separated from AN2 by a push button, when I press the push button, the PIC (a 16F688) can read the value of the thermistor and record it in the eeprom. It turn on a LED on RC2 too, just to have a something to see.
My program works on my PCB but I would like to have just one record by push.
With this program, if the push button stays in the pushed position, my eeprom will be filled with the same value and that's not what I want.
When the button is pressed, the PIC should read the value, record it in the eeprom and then wait for the button to be released before it can read another value.
I tried to use If input_state(PIN_A2)... but it doesn’t work, I think it is because RA2 is already configured as an ADC port.
I really don't know how to do.
My version of CCS compiler is : Version 4.068
I use the plugin of CCS in MPLAB version 8.36.00.00
Code: |
#include <16F688.h>
#device adc=8
#FUSES NOWDT
#FUSES INTRC
#FUSES PUT
#FUSES NOPROTECT
#FUSES NOBROWNOUT
#use delay(internal=8Mhz)
void init_io()
{
SET_TRIS_A(0x11); // port A In
SET_TRIS_C(0x00); // port D Out
OUTPUT_A(0x00);
OUTPUT_C(0x00);
}
void main(void) {
int8 i;
for (i=0; i<10; i++){
while(true) {
int16 TOUCH=0;
SETUP_ADC(ADC_CLOCK_INTERNAL);
SETUP_ADC_PORTS(sAN2 | VSS_VDD);
set_adc_channel(2);
delay_us(20);
TOUCH=read_adc();
if(TOUCH > 200) {
output_high(PIN_C2);
write_eeprom(i,1);
delay_ms(200);
output_low(PIN_C2);
break;
}
if(TOUCH > 100){
output_high(PIN_C2);
write_eeprom(i,2);
delay_ms(200);
output_low(PIN_C2);
break;
}
if(TOUCH > 5) {
output_high(PIN_C2);
write_eeprom(i,3);
delay_ms(200);
output_low(PIN_C2);
break;
}
}
}
}
|
Thanks ! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Oct 03, 2012 2:14 am |
|
|
Can I presume your thermistor is wired like this?
Code: | Push Button
______
+5V ---WWWWWW--- ------------------ PIC AN2 |
Thermistor |
|
--WWWWWWW---- 0V GND
|
With button up ADC reads small value.
So main loop could go something like this
Code: | while (1)
{
while (TOUCH < 10) { do_nothing; } // waiting for TOUCH to go high
do_TOUCH_action_once;
while (TOUCH > 5) { do_nothing; } // waiting for TOUCH to go low
} |
Mike |
|
|
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
|
Posted: Wed Oct 03, 2012 3:50 am |
|
|
You're right Mike, my thermistor is wired like this.
Your solution seems to be ok, but I can't escape from the while loop. once my program enter in the loop, the ADC value isn't readed anymore so no way to go out even if I put "read_adc()" inside. Could you give me more details about your solution ?
And thanks PCM programmer but for me, it is ADC so I can't control the push of the button by the function "input_state()" but it may help for the rest of my program.
bye ! |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Wed Oct 03, 2012 4:37 am |
|
|
It was not my intention to do the coding for you.
I simply wanted to illustrate the logic of how to make just a single measurement each time you pressed your button.
Code below is as typed & totally untested but SHOULD give you the idea.
It's also neither optimal nor elegant.
Code: |
.
// Headers etc
.
// Code for do_nothing
void do_nothing()
{
set_adc_channel(2);
delay_us(20);
TOUCH=read_adc();
}
main()
{
.
.
// initialisation code
.
.
while (1)
{
while (TOUCH < 10 ) {do_nothing();} // Idle here till button pressed
delay_ms(100); // Allow for key bounce
do_nothing(); // Updates TOUCH value
.
// code to deal with new TOUCH value
.
while (TOUCH >5 ) {do_nothing();} // Idle here till button released
}
}
|
Mike
EDIT Why have you set up for 8 bit ADC then declared TOUCH as 16 bit int? |
|
|
Got-Got
Joined: 26 Feb 2012 Posts: 17
|
|
Posted: Wed Oct 03, 2012 6:30 am |
|
|
Yes for the declaration of TOUCH, it was a mistake, it's corrected.
My program work perfectly. I applied what you told me.
One more time, thanks for your help Mike !
bye ! |
|
|
|