La vaca cegahisto.cat



09-03-2021  (350 ) Categoria: Articles

Jesús - PIC 12F629 switch and timer

Programa amb 12F629 inclou un switch i un timer

Hola !! Vull escriure un programa per al xip PIC 12F629 i sóc completament nou en això. Bé, en realitat conec la programació en general, però no sé exactament com he de programar per al xip PIC. Sigui com sigui, vull escriure un programa que parpellegi LEDs concrets en una seqüència. Hi ha 4 LED connectats al xip i tots els LED estan controlats per un commutador connectat a 1 pin d'entrada del xip PIC.

Heus aquí la idea: quan es prem el botó durant menys de 2 segons, no es canvia res, però si el botó es manté durant almenys 2 segons, es canvia un altre patró dels LED al següent.

Aquest és el meu codi que realment no funciona. Tot va, de vegades el programa canvia automàticament de patró només al cap de 2 segons, de vegades es restableix a 0 i així successivament ..... Faig servir el compilador MPLAB IDE Hi Tech C. A continuació, es mostra aquest codi tant inestable:


#include
#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000
#endif
__CONFIG(FOSC_INTRCCLK & WDTE_OFF & MCLRE_OFF);

void main(void)
{
GPIO=0;
CMCON = 0x07; // turn off comparators
TRISIO=0b00001000;
char eepromvar;

//int state=0;
char effect[4] = {0x00,0x04,0x02,0x06};
//int toggle=0;
eepromvar=EEPROM_READ(0x00);
GPIO=effect[eepromvar];
int enterthedragon=0;

while(1){
//__delay_ms(10);
if(GP3==0)
{

if(enterthedragon==0)
{
for(int i=0;i<2000;i++)
{
__delay_ms(1);
}
//__delay_ms(2000);
eepromvar=EEPROM_READ(0x00);
eepromvar++;
//state++;
if(eepromvar>0x03){
eepromvar=0;
}
EEPROM_WRITE(0x00,eepromvar);
enterthedragon=1;
//__delay_ms(10);
}

while(GP3==0);
}

if(GP3==1)
{
enterthedragon=0;
eepromvar=EEPROM_READ(0x00);
GPIO=effect[eepromvar];
//__delay_ms(10);
}
}
}

___________________________________________________________________

Hi!! I want to write a program for PIC 12F629 chip and I completely new to this. Well, I actually know programming in general but I don't know how exactly I should program for PIC chip. Either way, I want to write a program that flashes particular LEDs in a sequence. There are 4 LEDs connected to the chip and all LEDs are controlled by a switch that is connected to 1 input pin in PIC chip.

Here's the idea: when button is pressed for less than 2 seconds, nothing gets switched but if the button is held for at least 2 seconds, another pattern on LEDs gets switched to the next one.

This is my code that doesn't really work. Everything goes, sometimes program automatically switches pattern on its own after 2 seconds, sometimes it resets to 0 and so on..... I use MPLAB IDE Hi Tech C Compiler. Here's what the unstable code looks like right now:


Anyone have any ideas how I should implement a timer that works properly or what changes I should do?? Please for reply. Any advices are strongly appreciated!! Many thanks in advance!!