Knocker kit -Software

/*
 * File:   knocker.c
 * Author: Derek Extreme Kits 
 * extkits.uk/knocker
 *
 * Created on May 20, 2016, 6:24 PM
 * 
 * Config for 12f675 OK
 */


#include <xc.h>

#define    ontime    6100            //Voltage OK level 500v=5v=255, vok=0.51*Vmax, 200V=102dec
#define    offtime    5100        //Voltage OK level 16v=5v=255, vok=15.9*Vmax, 12V=191dec

//Cadences
#define cadances 11 //number of cadances
#define prog0 0b0101110100101000
#define prog1 0b0101010100000000
#define prog2 0b0100110010010011
#define prog3 0b0110011001100110
#define prog4 0b0001000100010001
#define prog5 0b0101010101010101
#define prog6 0b0111011101110111
#define prog7 0b0111010001110100
#define prog8 0b0101110001011101
#define prog9 0b0011110000111100
#define prog9 0b0111110001111100
#define prog10 0b1111111111111111

//pin define gpio
#define    dfa1 GPIObits.GP2    //pin 5
#define solon GPIObits.GP4    //pin 3
#define    dfa2 GPIObits.GP0    //pin 7
#define progbutt GPIObits.GP1     //pin 6
#define dfa3 GPIObits.GP5   //pin2
#define testbutt GPIObits.GP3   //pin 4

// Chip Config 
#define _XTAL_FREQ 4000000
#pragma config FOSC = INTRCIO 
#pragma config WDTE = OFF
#pragma config PWRTE = ON
#pragma config MCLRE = OFF
#pragma config BOREN = OFF
#pragma config CP = OFF
#pragma config CPD = OFF

#define SLEEP()     asm("sleep")

//structures

typedef union {
    unsigned char byte;
    struct {
        unsigned b0:1, b1:1, b2:1, b3:1, b4:1, b5:1, b6:1, b7:1;
    };
} t_byte;

typedef union {
    unsigned char byte;
    struct {
        unsigned a:1, b:1, c:1, d:1, e:1, f:1, g:1, h:1;
    };
} t_flags;



//variable definitions    
int program;
unsigned int prog[cadances];
unsigned char cadance;
 
t_flags flags;

// function definitions
void DoSolPulse(unsigned int);
void shortdelay(unsigned int);
unsigned char read_ee(unsigned char );
void write_ee(unsigned char , unsigned char );

    
int main(){
   
    //port setup

    OPTION_REG =0b01000000; //GPWU=1. GPPU=1,T0CS=0 ToSE=0PSA=0PS=000
    ANSEL = 0;                     // adc off
    CMCON = 0b111;           //    comparator off 1 -> RB0,RB1 digital
    WPU=0b00000010;  //pullup for prog
    TRISIO = 0b11101111; //gpo 4 output
    ADCON0=0b00000000 ; //result left justified -  adc off
    VRCON=0;
    
    //port change interupt setup to remove sleep
    IOC=0b00001010;
    INTCONbits.GPIE=1;    
    INTCONbits.GIE=0;
    
    //define start conditions 
    GPIO=0;
    flags.byte=0;
    cadance=0;
    
    //fill program array
    prog[0]=prog0;
    prog[1]=prog1;
    prog[2]=prog2;
    prog[3]=prog3;
    prog[4]=prog4;
    prog[5]=prog5;
    prog[6]=prog6;
    prog[7]=prog7;
    prog[8]=prog8;
    prog[9]=prog9;
    prog[10]=prog10;
   
    //read last cadance from eeprom
    cadance=read_ee(1);
    
    //make sure its a sane value
    if (cadance>=cadances){cadance=0;}
    
 //MAIN LOOP
 while(1){
   program=15; 
   while(program>=0){
     int t;
     t= (prog[cadance] >> program) & 1;
     DoSolPulse(t);
     program--;
   } // while
   
   INTCONbits.GPIF=0; //reset interrupt
   SLEEP();
   INTCONbits.GPIF=0; //reset interrupt
   
   if (progbutt==0){
       cadance++;
       if (cadance>=cadances){
           cadance=0; 
       }
       write_ee(1,cadance);
   } 
 }    
  return 0;
} //main


//----------------------------- Solenoid Pulse ------------------------------

void DoSolPulse(unsigned int x){
    if (x>0){solon=1;}
    shortdelay(ontime);
    solon=0;        
    shortdelay(offtime);
}



void shortdelay(unsigned int time){
    unsigned int tmp=0;
    while(tmp<time){
        tmp++;
    }
}



unsigned char read_ee(unsigned char eeaddr)
{
    EEADR = eeaddr;
    EECON1bits.RD = 1;
    return EEDATA;
}

void write_ee(unsigned char eeaddr, unsigned char data){
    EECON1bits.WREN= 1;
    EEADR = eeaddr;
    EEDATA=data;
    EECON2=0x55;
    EECON2=0xAA;
    EECON1bits.WR= 1;
    EECON1bits.WREN= 0;
    
    while (EECON1bits.WR){}
    
}

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