BAHKO
Newbie level 4
Hello! I tried to write my own code for 2 push buttons to rotary encoder (UP/DOWN Quadrature generator)
I have flashed it (ofcourse with changed ports) on PIC16F648 and it works perfect, it also works perfect in Proteus simulation with PIC12C509 (C509 was the closest i have to F509).
The problem comes when i program it on real PIC12F509 - the chip doing NOTHING but when i press reset it resets (i put 2 leds on outputs and they flicker on reset)!
GP0 = UP - button used with pull down
GP1 = DN - button used with pull down
GP2 = not connected/set to output
GP3 = RESET trough pullup 10k to Vcc and button to GND
GP4 = OUT B - output 2 - connected LED through 220 ohm to GND
GP5 = OUT A - output 1 - connected LED through 220 ohm to GND
The outputs just stay high and no reaction!
What i am doing wrong? I struggle with it for 3 days now and nothing work on real chip (i have few of them brand new)
MPLAB IDE 8.30 with High-Tech C compile it (for F509) with no errors.
The fuses are configured outside of the code - IntOSC, CP - OFF, RESET - ON, WDT - OFF
My Programmer is PicKit3 and also flash it perfectly!
It just does not work on real chip.
Please help me if anyone can.
Here is the
And schematic:
I have flashed it (ofcourse with changed ports) on PIC16F648 and it works perfect, it also works perfect in Proteus simulation with PIC12C509 (C509 was the closest i have to F509).
The problem comes when i program it on real PIC12F509 - the chip doing NOTHING but when i press reset it resets (i put 2 leds on outputs and they flicker on reset)!
GP0 = UP - button used with pull down
GP1 = DN - button used with pull down
GP2 = not connected/set to output
GP3 = RESET trough pullup 10k to Vcc and button to GND
GP4 = OUT B - output 2 - connected LED through 220 ohm to GND
GP5 = OUT A - output 1 - connected LED through 220 ohm to GND
The outputs just stay high and no reaction!
What i am doing wrong? I struggle with it for 3 days now and nothing work on real chip (i have few of them brand new)
MPLAB IDE 8.30 with High-Tech C compile it (for F509) with no errors.
The fuses are configured outside of the code - IntOSC, CP - OFF, RESET - ON, WDT - OFF
My Programmer is PicKit3 and also flash it perfectly!
It just does not work on real chip.
Please help me if anyone can.
Here is the
Code:
/*
PIC12F509 buttons to
rotary encoder truth table.
===========================
Count UP OUTA OUTB
===========================
<1.1> = H - H
<1.2> = L - H
<2.1> = L - L
<2.2> = H - L
===========================
Count DN OUTA OUTB
===========================
<1.1> = H - H
<1.2> = H - L
<2.1> = L - L
<2.2> = L - H
===========================
*/
#include <pic.h>
#if defined(_12F509)
__CONFIG(0x31FA);
#endif
//define buttons
#define UP GP0
#define DN GP1
unsigned int j;
// 8 = number of loop iterations for 1 ms at 4 MHz
#define loop4_ms 8
//functions
void pause_ms(unsigned int given_ms); // pause for given_ms milli seconds
//end functions
int REP; //Repeat cycle 'n' times
unsigned char const STEPS_MAP[4] = {0b111000,0b011000,0b001000,0b100100};
int step=0;
//main code
void main(void) {
#if defined(_12F509)
TRIS = 0b001011; // Set GP4,GP5 as outputs & GP3, GP0, GP1 as input (0 => Output, 1 => input)
#endif
GPIO = 0b001000; // Set GPIO = 0 for all output but 1 for GP3/MCLR input
OPTION = 0b11010000;
do
{
if (UP) //Check if switch is closed
{
pause_ms(90); //wait for 90ms (switch debouncing)
if (UP) //Check if switch is still closed
{
for(REP=0; REP<=1; REP++) //Repeat steps until 'n'
{
GPIO = (STEPS_MAP[step]); //Load current state
pause_ms(250); //Pulse time
step++; //Count +
if (step>3)step=0;
}
REP; //End REP
}
}
else
{
GPIO = (STEPS_MAP[step]);
}
if (DN) //Check if switch is closed
{
pause_ms(90); //wait for 90ms (switch debouncing)
if (DN) //Check if switch is still closed
{
for(REP=0; REP<=1; REP++)
{
GPIO = (STEPS_MAP[step]); //Load current state
pause_ms(250); //Pulse time
step--; //Count -
if (step<0)step=3;
}
REP; //End REP
}
}
else
{
GPIO = (STEPS_MAP[step]);
}
}
while(1);
}
//end main code
void pause_ms( unsigned int given_ms ) { // pause for given_ms milli seconds
unsigned long j, max_time = given_ms * loop4_ms;
for(j=0; j < max_time; j++); // Make waiting for about given_ms msec
};