View previous topic :: View next topic |
Author |
Message |
qbone
Joined: 04 Sep 2012 Posts: 6 Location: Danmark
|
PIC10F220 problem initializing GP0 as input. |
Posted: Mon Sep 24, 2012 5:50 am |
|
|
Hey guys
I have a problem with my code. I cannot get my PIC to set GP0 (PIN_B0) as an input.
I have a 10K pull down resistor on it, but I still measure 1.925V.
Can you see if I am doing something wrong? As far as I could read in the datasheet I just need to turn the ADC off.
Code: | #include <10F220.h>
#fuses NOMCLR,NOPROTECT,NOWDT,NOMCPU,IOSC4
#use delay(clock=4000000)
#rom 0xFF = {0xC00} // Put MOVLW 0x00 at end of ROM
#byte osccal = 0x05
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
void init()
{
osccal = osccal & 0xFE;
set_options(0xDF);
setup_timer_0(T0_INTERNAL);
setup_adc(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
}
void main()
{
init();
long i;
while(TRUE) {
output_low(GP1);
if(!input(GP2) == 1) { // If fridge open
delay_ms(10000); // 10sec delay
if(input(GP0) == 1) { // If postpone
delay_ms(30000); // 30sec delay
}
while(!input(GP2)) { // Buzz frequency
for(i = 0; i < 600; i++) {
delay_us(400);
output_high(GP1);
delay_us(400);
output_low(GP1);
}
delay_ms(1000);
}
}
}
} |
I even tried adding output_float(GP0) / output_float(PIN_B0) in my init() routine, but it doesn't change anything :(
I am programming in MPLAB v8.85, compiling with CCS C Compiler v4.
my programmer is a Olimex PIC-MCP-USB (PICSTART+). |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Mon Sep 24, 2012 7:06 am |
|
|
I'd be triple checking for a short, almost invisible wire 'whisker', connecting the pin to something else.
Best Wishes |
|
|
qbone
Joined: 04 Sep 2012 Posts: 6 Location: Danmark
|
|
Posted: Mon Sep 24, 2012 8:12 am |
|
|
I have just tried burning it on a new IC, and moving it some places on the breadboard, but I still get the same result |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Sep 24, 2012 10:19 am |
|
|
An internal pull up resistor would give you the kind of voltage you're reporting. (Just a thought)
I presume you're working on a white board of some sort.
The weak pull-ups are controlled by the option register and SHOULD default to disabled.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 24, 2012 12:56 pm |
|
|
I was able to make GP0 work as an input on a 10F222. That PIC is in
the same family as the 10F220. I put a 4.7K pull-up resistor (to +5v)
on GP0. Then I put an oscilloscope probe on GP2. Then when I tapped
pin GP0 with a ground lead, the oscilloscope showed GP2 going to 0v.
When I removed the ground on GP0, then GP2 went back to +5v output.
So it's working. This was tested with PCB vs. 4.073 (which is included with
MPLAB vs. 8.86).
Code: |
#include <10F222.h>
#fuses NOMCLR,NOPROTECT,NOWDT,NOMCPU,IOSC4
#use delay(clock=4M)
#define GP0 PIN_B0
#define GP1 PIN_B1
#define GP2 PIN_B2
#define GP3 PIN_B3
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
//====================================
void main(void)
{
int8 temp;
setup_adc_ports(NO_ANALOGS);
set_options(0xDF);
while(1)
{
temp = input(GP0); // Read GP0 input level
output_bit(GP2, temp); // Output same level to GP1
}
} |
|
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Mon Sep 24, 2012 1:25 pm |
|
|
Yes, but is it possible that when you work the other way up, as the original poster is doing, that the internal pull-up is fighting the 10k pull-down.?
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon Sep 24, 2012 2:33 pm |
|
|
Quote: |
void init()
{
osccal = osccal & 0xFE;
set_options(0xDF);
setup_timer_0(T0_INTERNAL);
setup_adc(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
}
|
The line in bold is over-writing the OPTION register settings that were
done by the previous line. Look at the .LST file to see this.
Quote: | void main()
{
init();
long i;
|
CCS doesn't support declaration of variables in the middle of code.
Put all local variable declarations at the start of the function. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19504
|
|
Posted: Tue Sep 25, 2012 8:53 am |
|
|
As another comment, what actual compiler version?. V4, is a 'family' of at present over 130 versions. The versions below about 4.070, were basically 'beta', and are full of bugs....
I notice you are manually setting the OSCCAL register. The compiler does this automatically if the internal oscillator is selected, and if you are doing this because this doesn't work, it suggests a very early compiler, possibly with initialisation problems.
Best Wishes |
|
|
|