Knocker kit -Software

<br />
/*<br />
 * File:   knocker.c<br />
 * Author: Derek Extreme Kits<br />
 * extkits.uk/knocker<br />
 *<br />
 * Created on May 20, 2016, 6:24 PM<br />
 *<br />
 * Config for 12f675 OK<br />
 */</p>
<p>#include &lt;xc.h&gt;</p>
<p>#define    ontime    6100            //Voltage OK level 500v=5v=255, vok=0.51*Vmax, 200V=102dec<br />
#define    offtime    5100        //Voltage OK level 16v=5v=255, vok=15.9*Vmax, 12V=191dec</p>
<p>//Cadences<br />
#define cadances 11 //number of cadances<br />
#define prog0 0b0101110100101000<br />
#define prog1 0b0101010100000000<br />
#define prog2 0b0100110010010011<br />
#define prog3 0b0110011001100110<br />
#define prog4 0b0001000100010001<br />
#define prog5 0b0101010101010101<br />
#define prog6 0b0111011101110111<br />
#define prog7 0b0111010001110100<br />
#define prog8 0b0101110001011101<br />
#define prog9 0b0011110000111100<br />
#define prog9 0b0111110001111100<br />
#define prog10 0b1111111111111111</p>
<p>//pin define gpio<br />
#define    dfa1 GPIObits.GP2    //pin 5<br />
#define solon GPIObits.GP4    //pin 3<br />
#define    dfa2 GPIObits.GP0    //pin 7<br />
#define progbutt GPIObits.GP1     //pin 6<br />
#define dfa3 GPIObits.GP5   //pin2<br />
#define testbutt GPIObits.GP3   //pin 4</p>
<p>// Chip Config<br />
#define _XTAL_FREQ 4000000<br />
#pragma config FOSC = INTRCIO<br />
#pragma config WDTE = OFF<br />
#pragma config PWRTE = ON<br />
#pragma config MCLRE = OFF<br />
#pragma config BOREN = OFF<br />
#pragma config CP = OFF<br />
#pragma config CPD = OFF</p>
<p>#define SLEEP()     asm(&quot;sleep&quot;)</p>
<p>//structures</p>
<p>typedef union {<br />
    unsigned char byte;<br />
    struct {<br />
        unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;<br />
    };<br />
} t_byte;</p>
<p>typedef union {<br />
    unsigned char byte;<br />
    struct {<br />
        unsigned a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1;<br />
    };<br />
} t_flags;</p>
<p>//variable definitions    <br />
int program;<br />
unsigned int prog[cadances];<br />
unsigned char cadance;<br />
 <br />
t_flags flags;</p>
<p>// function definitions<br />
void DoSolPulse(unsigned int);<br />
void shortdelay(unsigned int);<br />
unsigned char read_ee(unsigned char );<br />
void write_ee(unsigned char , unsigned char );</p>
<p>    <br />
int main(){<br />
   <br />
    //port setup</p>
<p>    OPTION_REG =0b01000000; //GPWU=1. GPPU=1,T0CS=0 ToSE=0PSA=0PS=000<br />
    ANSEL = 0;                     // adc off<br />
    CMCON = 0b111;           //    comparator off 1 -&gt; RB0,RB1 digital<br />
    WPU=0b00000010;  //pullup for prog<br />
    TRISIO = 0b11101111; //gpo 4 output<br />
    ADCON0=0b00000000 ; //result left justified -  adc off<br />
    VRCON=0;<br />
    <br />
    //port change interupt setup to remove sleep<br />
    IOC=0b00001010;<br />
    INTCONbits.GPIE=1;    <br />
    INTCONbits.GIE=0;<br />
    <br />
    //define start conditions<br />
    GPIO=0;<br />
    flags.byte=0;<br />
    cadance=0;<br />
    <br />
    //fill program array<br />
    prog[0]=prog0;<br />
    prog[1]=prog1;<br />
    prog[2]=prog2;<br />
    prog[3]=prog3;<br />
    prog[4]=prog4;<br />
    prog[5]=prog5;<br />
    prog[6]=prog6;<br />
    prog[7]=prog7;<br />
    prog[8]=prog8;<br />
    prog[9]=prog9;<br />
    prog[10]=prog10;<br />
   <br />
    //read last cadance from eeprom<br />
    cadance=read_ee(1);<br />
    <br />
    //make sure its a sane value<br />
    if (cadance&gt;=cadances){cadance=0;}<br />
    <br />
 //MAIN LOOP<br />
 while(1){<br />
   program=15;<br />
   while(program&gt;=0){<br />
     int t;<br />
     t= (prog[cadance] &gt;&gt; program) &amp; 1;<br />
     DoSolPulse(t);<br />
     program--;<br />
   } // while<br />
   <br />
   INTCONbits.GPIF=0; //reset interrupt<br />
   SLEEP();<br />
   INTCONbits.GPIF=0; //reset interrupt<br />
   <br />
   if (progbutt==0){<br />
       cadance++;<br />
       if (cadance&gt;=cadances){<br />
           cadance=0;<br />
       }<br />
       write_ee(1,cadance);<br />
   }<br />
 }    <br />
  return 0;<br />
} //main</p>
<p>//----------------------------- Solenoid Pulse ------------------------------</p>
<p>void DoSolPulse(unsigned int x){<br />
    if (x&gt;0){solon=1;}<br />
    shortdelay(ontime);<br />
    solon=0;        <br />
    shortdelay(offtime);<br />
}</p>
<p>void shortdelay(unsigned int time){<br />
    unsigned int tmp=0;<br />
    while(tmp&lt;time){<br />
        tmp++;<br />
    }<br />
}</p>
<p>unsigned char read_ee(unsigned char eeaddr)<br />
{<br />
    EEADR = eeaddr;<br />
    EECON1bits.RD = 1;<br />
    return EEDATA;<br />
}</p>
<p>void write_ee(unsigned char eeaddr, unsigned char data){<br />
    EECON1bits.WREN= 1;<br />
    EEADR = eeaddr;<br />
    EEDATA=data;<br />
    EECON2=0x55;<br />
    EECON2=0xAA;<br />
    EECON1bits.WR= 1;<br />
    EECON1bits.WREN= 0;<br />
    <br />
    while (EECON1bits.WR){}<br />
    <br />
}<br />

Licences
Creative Commons License
Extreme Kits Knocker by Extreme Kits is licensed under a Creative Commons Attribution-NonCommercial 4.0 International License.
Permissions beyond the scope of this license may be available at extkits.uk.

Save