Reputation: 21
What I use
What I know :
The project
I'm trying to program a PIC10F200 for a simple project, basically: GP1 gets power flowing into it or not, based on that information GP0 either turns the LED ON or keeps it OFF.
I have used this code to check that the pins are not shorted :
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#include <PIC10F200.h>
#define _XTAL_FREQ 4000000
// CONFIG
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config CP = OFF // Code Protect (Code protection off)
#pragma config MCLRE = OFF // Master Clear Enable (GP3/MCLR pin fuction is digital I/O, MCLR internally tied to VDD)
void main(void)
{
OPTION = 0b11011111;
TRISGPIO = 0b00001000;
GPIO = 0b00000111;
}
What I need help with
So after that check, this was the code I created (I don't understand also what is wrong when posting the code so... I'm sorry.):
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
// CONFIG
#pragma config WDTE = OFF // Watchdog Timer (WDT disabled)
#pragma config CP = OFF // Code Protect (Code protection off)
#pragma config MCLRE = OFF
#define _XTAL_FREQ 4000000
void main()
{
OPTION = 0b11011111;
TRISGPIO = 0b00000010;
GPIO = 0b00000000;
while(1)
{
if(GP1 == '1')
{
GP0 = '0';
}
else
{
GP0 = '1';
}
}
}
Background (If you ask yourself why I'm so unintelligent...)
Try working project, got no working project.
[edit]
#include <stdio.h>
#include <stdlib.h>
#include <PIC10F200.h>
// CONFIG
#pragma config WDTE = OFF
#pragma config CP = OFF
#pragma config MCLRE = OFF
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 4000000
void main() {
OPTION = 0b11011111;
TRISGPIO = 0b00000010;
GPIO = 0b00000000;
while (1) {
if (GP1 == '1') {
GP0 = '0'; // Turn off the LED
} else {
GP0 = '1'; // Turn on the LED
}
}
}
Header file :
// Register: INDF
#define INDF INDF
extern volatile unsigned char INDF __at(0x000);
#ifndef _LIB_BUILD
asm("INDF equ 00h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned INDF :8;
};
} INDFbits_t;
extern volatile INDFbits_t INDFbits __at(0x000);
// bitfield macros
#define _INDF_INDF_POSN 0x0
#define _INDF_INDF_POSITION 0x0
#define _INDF_INDF_SIZE 0x8
#define _INDF_INDF_LENGTH 0x8
#define _INDF_INDF_MASK 0xFF
// Register: TMR0
#define TMR0 TMR0
extern volatile unsigned char TMR0 __at(0x001);
#ifndef _LIB_BUILD
asm("TMR0 equ 01h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned TMR0 :8;
};
} TMR0bits_t;
extern volatile TMR0bits_t TMR0bits __at(0x001);
// bitfield macros
#define _TMR0_TMR0_POSN 0x0
#define _TMR0_TMR0_POSITION 0x0
#define _TMR0_TMR0_SIZE 0x8
#define _TMR0_TMR0_LENGTH 0x8
#define _TMR0_TMR0_MASK 0xFF
// Register: PCL
#define PCL PCL
extern volatile unsigned char PCL __at(0x002);
#ifndef _LIB_BUILD
asm("PCL equ 02h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned PCL :8;
};
} PCLbits_t;
extern volatile PCLbits_t PCLbits __at(0x002);
// bitfield macros
#define _PCL_PCL_POSN 0x0
#define _PCL_PCL_POSITION 0x0
#define _PCL_PCL_SIZE 0x8
#define _PCL_PCL_LENGTH 0x8
#define _PCL_PCL_MASK 0xFF
// Register: STATUS
#define STATUS STATUS
extern volatile unsigned char STATUS __at(0x003);
#ifndef _LIB_BUILD
asm("STATUS equ 03h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned C :1;
unsigned DC :1;
unsigned Z :1;
unsigned nPD :1;
unsigned nTO :1;
unsigned :2;
unsigned GPWUF :1;
};
struct {
unsigned CARRY :1;
unsigned :1;
unsigned ZERO :1;
};
} STATUSbits_t;
extern volatile STATUSbits_t STATUSbits __at(0x003);
// bitfield macros
#define _STATUS_C_POSN 0x0
#define _STATUS_C_POSITION 0x0
#define _STATUS_C_SIZE 0x1
#define _STATUS_C_LENGTH 0x1
#define _STATUS_C_MASK 0x1
#define _STATUS_DC_POSN 0x1
#define _STATUS_DC_POSITION 0x1
#define _STATUS_DC_SIZE 0x1
#define _STATUS_DC_LENGTH 0x1
#define _STATUS_DC_MASK 0x2
#define _STATUS_Z_POSN 0x2
#define _STATUS_Z_POSITION 0x2
#define _STATUS_Z_SIZE 0x1
#define _STATUS_Z_LENGTH 0x1
#define _STATUS_Z_MASK 0x4
#define _STATUS_nPD_POSN 0x3
#define _STATUS_nPD_POSITION 0x3
#define _STATUS_nPD_SIZE 0x1
#define _STATUS_nPD_LENGTH 0x1
#define _STATUS_nPD_MASK 0x8
#define _STATUS_nTO_POSN 0x4
#define _STATUS_nTO_POSITION 0x4
#define _STATUS_nTO_SIZE 0x1
#define _STATUS_nTO_LENGTH 0x1
#define _STATUS_nTO_MASK 0x10
#define _STATUS_GPWUF_POSN 0x7
#define _STATUS_GPWUF_POSITION 0x7
#define _STATUS_GPWUF_SIZE 0x1
#define _STATUS_GPWUF_LENGTH 0x1
#define _STATUS_GPWUF_MASK 0x80
#define _STATUS_CARRY_POSN 0x0
#define _STATUS_CARRY_POSITION 0x0
#define _STATUS_CARRY_SIZE 0x1
#define _STATUS_CARRY_LENGTH 0x1
#define _STATUS_CARRY_MASK 0x1
#define _STATUS_ZERO_POSN 0x2
#define _STATUS_ZERO_POSITION 0x2
#define _STATUS_ZERO_SIZE 0x1
#define _STATUS_ZERO_LENGTH 0x1
#define _STATUS_ZERO_MASK 0x4
// Register: FSR
#define FSR FSR
extern volatile unsigned char FSR __at(0x004);
#ifndef _LIB_BUILD
asm("FSR equ 04h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned FSR :8;
};
} FSRbits_t;
extern volatile FSRbits_t FSRbits __at(0x004);
// bitfield macros
#define _FSR_FSR_POSN 0x0
#define _FSR_FSR_POSITION 0x0
#define _FSR_FSR_SIZE 0x8
#define _FSR_FSR_LENGTH 0x8
#define _FSR_FSR_MASK 0xFF
// Register: OSCCAL
#define OSCCAL OSCCAL
extern volatile unsigned char OSCCAL __at(0x005);
#ifndef _LIB_BUILD
asm("OSCCAL equ 05h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned FOSC4 :1;
unsigned CAL :7;
};
struct {
unsigned :1;
unsigned CAL0 :1;
unsigned CAL1 :1;
unsigned CAL2 :1;
unsigned CAL3 :1;
unsigned CAL4 :1;
unsigned CAL5 :1;
unsigned CAL6 :1;
};
} OSCCALbits_t;
extern volatile OSCCALbits_t OSCCALbits __at(0x005);
// bitfield macros
#define _OSCCAL_FOSC4_POSN 0x0
#define _OSCCAL_FOSC4_POSITION 0x0
#define _OSCCAL_FOSC4_SIZE 0x1
#define _OSCCAL_FOSC4_LENGTH 0x1
#define _OSCCAL_FOSC4_MASK 0x1
#define _OSCCAL_CAL_POSN 0x1
#define _OSCCAL_CAL_POSITION 0x1
#define _OSCCAL_CAL_SIZE 0x7
#define _OSCCAL_CAL_LENGTH 0x7
#define _OSCCAL_CAL_MASK 0xFE
#define _OSCCAL_CAL0_POSN 0x1
#define _OSCCAL_CAL0_POSITION 0x1
#define _OSCCAL_CAL0_SIZE 0x1
#define _OSCCAL_CAL0_LENGTH 0x1
#define _OSCCAL_CAL0_MASK 0x2
#define _OSCCAL_CAL1_POSN 0x2
#define _OSCCAL_CAL1_POSITION 0x2
#define _OSCCAL_CAL1_SIZE 0x1
#define _OSCCAL_CAL1_LENGTH 0x1
#define _OSCCAL_CAL1_MASK 0x4
#define _OSCCAL_CAL2_POSN 0x3
#define _OSCCAL_CAL2_POSITION 0x3
#define _OSCCAL_CAL2_SIZE 0x1
#define _OSCCAL_CAL2_LENGTH 0x1
#define _OSCCAL_CAL2_MASK 0x8
#define _OSCCAL_CAL3_POSN 0x4
#define _OSCCAL_CAL3_POSITION 0x4
#define _OSCCAL_CAL3_SIZE 0x1
#define _OSCCAL_CAL3_LENGTH 0x1
#define _OSCCAL_CAL3_MASK 0x10
#define _OSCCAL_CAL4_POSN 0x5
#define _OSCCAL_CAL4_POSITION 0x5
#define _OSCCAL_CAL4_SIZE 0x1
#define _OSCCAL_CAL4_LENGTH 0x1
#define _OSCCAL_CAL4_MASK 0x20
#define _OSCCAL_CAL5_POSN 0x6
#define _OSCCAL_CAL5_POSITION 0x6
#define _OSCCAL_CAL5_SIZE 0x1
#define _OSCCAL_CAL5_LENGTH 0x1
#define _OSCCAL_CAL5_MASK 0x40
#define _OSCCAL_CAL6_POSN 0x7
#define _OSCCAL_CAL6_POSITION 0x7
#define _OSCCAL_CAL6_SIZE 0x1
#define _OSCCAL_CAL6_LENGTH 0x1
#define _OSCCAL_CAL6_MASK 0x80
// Register: GPIO
#define GPIO GPIO
extern volatile unsigned char GPIO __at(0x006);
#ifndef _LIB_BUILD
asm("GPIO equ 06h");
#endif
// bitfield definitions
typedef union {
struct {
unsigned GP0 :1;
unsigned GP1 :1;
unsigned GP2 :1;
unsigned GP3 :1;
};
} GPIObits_t;
extern volatile GPIObits_t GPIObits __at(0x006);
// bitfield macros
#define _GPIO_GP0_POSN 0x0
#define _GPIO_GP0_POSITION 0x0
#define _GPIO_GP0_SIZE 0x1
#define _GPIO_GP0_LENGTH 0x1
#define _GPIO_GP0_MASK 0x1
#define _GPIO_GP1_POSN 0x1
#define _GPIO_GP1_POSITION 0x1
#define _GPIO_GP1_SIZE 0x1
#define _GPIO_GP1_LENGTH 0x1
#define _GPIO_GP1_MASK 0x2
#define _GPIO_GP2_POSN 0x2
#define _GPIO_GP2_POSITION 0x2
#define _GPIO_GP2_SIZE 0x1
#define _GPIO_GP2_LENGTH 0x1
#define _GPIO_GP2_MASK 0x4
#define _GPIO_GP3_POSN 0x3
#define _GPIO_GP3_POSITION 0x3
#define _GPIO_GP3_SIZE 0x1
#define _GPIO_GP3_LENGTH 0x1
#define _GPIO_GP3_MASK 0x8
// Register: OPTION
#define OPTION OPTION
extern volatile __control unsigned char OPTION __at(0x000);
// Register: TRIS
#define TRIS TRIS
extern volatile __control unsigned char TRIS __at(0x006);
// Register: TRISGPIO
#define TRISGPIO TRISGPIO
extern volatile __control unsigned char TRISGPIO __at(0x006);
/*
* OPTION bits
*/
#define PS 0x7
#define PSA 0x8
#define T0SE 0x10
#define T0CS 0x20
#define nGPPU 0x40
#define nGPWU 0x80
#define PS0 0x1
#define PS1 0x2
#define PS2 0x4
/*
* Bit Definitions
*/
#define _DEPRECATED __attribute__((__deprecated__))
#ifndef BANKMASK
#define BANKMASK(addr) ((addr)&01Fh)
#endif
#define _BIT_ACCESS(r,b) ___mkstr(BANKMASK(r)) "," ___mkstr(b)
#ifndef PAGEMASK
#define PAGEMASK(addr) ((addr)&01FFh)
#endif
// OSCCAL<CAL0>
extern volatile __bit CAL0 __at(0x29); // @ (0x5 * 8 + 1)
#define CAL0_bit _BIT_ACCESS(OSCCAL,1)
// OSCCAL<CAL1>
extern volatile __bit CAL1 __at(0x2A); // @ (0x5 * 8 + 2)
#define CAL1_bit _BIT_ACCESS(OSCCAL,2)
// OSCCAL<CAL2>
extern volatile __bit CAL2 __at(0x2B); // @ (0x5 * 8 + 3)
#define CAL2_bit _BIT_ACCESS(OSCCAL,3)
// OSCCAL<CAL3>
extern volatile __bit CAL3 __at(0x2C); // @ (0x5 * 8 + 4)
#define CAL3_bit _BIT_ACCESS(OSCCAL,4)
// OSCCAL<CAL4>
extern volatile __bit CAL4 __at(0x2D); // @ (0x5 * 8 + 5)
#define CAL4_bit _BIT_ACCESS(OSCCAL,5)
// OSCCAL<CAL5>
extern volatile __bit CAL5 __at(0x2E); // @ (0x5 * 8 + 6)
#define CAL5_bit _BIT_ACCESS(OSCCAL,6)
// OSCCAL<CAL6>
extern volatile __bit CAL6 __at(0x2F); // @ (0x5 * 8 + 7)
#define CAL6_bit _BIT_ACCESS(OSCCAL,7)
// STATUS<CARRY>
extern volatile __bit CARRY __at(0x18); // @ (0x3 * 8 + 0)
#define CARRY_bit _BIT_ACCESS(STATUS,0)
// STATUS<DC>
extern volatile __bit DC __at(0x19); // @ (0x3 * 8 + 1)
#define DC_bit _BIT_ACCESS(STATUS,1)
// OSCCAL<FOSC4>
extern volatile __bit FOSC4 __at(0x28); // @ (0x5 * 8 + 0)
#define FOSC4_bit _BIT_ACCESS(OSCCAL,0)
// GPIO<GP0>
extern volatile __bit GP0 __at(0x30); // @ (0x6 * 8 + 0)
#define GP0_bit _BIT_ACCESS(GPIO,0)
// GPIO<GP1>
extern volatile __bit GP1 __at(0x31); // @ (0x6 * 8 + 1)
#define GP1_bit _BIT_ACCESS(GPIO,1)
// GPIO<GP2>
extern volatile __bit GP2 __at(0x32); // @ (0x6 * 8 + 2)
#define GP2_bit _BIT_ACCESS(GPIO,2)
// GPIO<GP3>
extern volatile __bit GP3 __at(0x33); // @ (0x6 * 8 + 3)
#define GP3_bit _BIT_ACCESS(GPIO,3)
// STATUS<GPWUF>
extern volatile __bit GPWUF __at(0x1F); // @ (0x3 * 8 + 7)
#define GPWUF_bit _BIT_ACCESS(STATUS,7)
// STATUS<ZERO>
extern volatile __bit ZERO __at(0x1A); // @ (0x3 * 8 + 2)
#define ZERO_bit _BIT_ACCESS(STATUS,2)
// STATUS<nPD>
extern volatile __bit nPD __at(0x1B); // @ (0x3 * 8 + 3)
#define nPD_bit _BIT_ACCESS(STATUS,3)
// STATUS<nTO>
extern volatile __bit nTO __at(0x1C); // @ (0x3 * 8 + 4)
#define nTO_bit _BIT_ACCESS(STATUS,4)
#endif // _PIC10F200_H_
Upvotes: 0
Views: 252
Reputation: 12673
The main error is the usage of character literals (in apostrophes) instead of number literals (without apostrophes).
The value '1'
is 0x31
= 0x00110001
, coded in ASCII, but you want 1
.
A second error is the usage of a struct member in place of a variable. Add GPIObits.
in front of GP0
and GP1
.
Use this:
if (GPIObits.GP1 != 0) {
GPIObits.GP0 = 0; // Turn off the LED
} else {
GPIObits.GP0 = 1; // Turn on the LED
}
The comparison with zero works generally well, independent of how a bit is read and returned. A "high" level returned as 1
or 2
(or 1 << any_bit_offet
) is always not equal to zero.
Additional note on the hardware and setup:
The pin GP1
does not measure "power flowing into it or not", it senses the voltage at it. But you do not provide any other levels than "floating" (switch open) or "high" (switch closed). Most probably "floating" is sensed as "high" because of the charge it holds.
Be aware that an floating input can sense any other level, and even catch electric fields like mains. You can experience any sequence of "low" and "high" read from the pin.
If you add an external pull-down resistor, you define the level while the switch is open. A value of some kiloohms suffices.
Or you use the more common approach:
First, let the switch go to GND, not 5V. You might want to invert your detection logic, therefore.
Then there are two possible solutions:
GPPU
in the OPTION
register to 0
. This enables the built-in weak pull-up resistors. This gives the floating pin a "high" level.Upvotes: 0
Reputation: 214770
GP1 == '1'
is nonsense.
GP0
/GP1
offering bit writes and reads both? I doubt that.'1'
is character literal ("ASCII") equivalent to binary 00110001 and thus nonsense. Use 1
.Assuming that you have a declared register GPIO
then you should have bit masks such as these:
#define GP0 (1u << 0)
#define GP1 (1u << 1)
Or better yet name them GPIO_GP0
etc to document which register they belong to.
Reading the register then becomes:
if((GPIO & GPIO_GP0) > 0)
...
if((GPIO & GPIO_GP1) > 0)
The > 0
part is necessary since when bit masking bit 0 we get 0x01 or 0x00. But when bit masking bit 1, we get 0x02 or 0x00. We do not get 1 or 0 straight out of the register.
Writing to the register:
GPIO |= GPIO_GP0; // set pin GP0 to 1
GPIO &= ~GPIO_GP0; // set pin GP0 to 0
All of this ought to be covered by the average bitwise operator book/tutorial. More details here: How to access a hardware register from firmware?
Upvotes: 0