Okay I've had a go at this following the instructions in the datasheet, I may well have cocked this up!
void
read_eeprom (unsigned int ADDRESS){ //Set up function for 'read_eeprom' and define address as unsigned_int.
EEADR = ADDRESS; // load ADDRESS in to EEADR register
EECON1bits.EEPGD =0; //set access to EEPROM memory
EECON1bits.RD = 1; // Initiate EEPROM read, bit cleared by hardware after read.
}
as well as creating function:
void
write_eeprom (unsigned int ADDRESS, unsigned int DATA){
INTCONbits.GIE = 0; //switch off ALL interrupts during read
EECON1bits.WREN = 1; //enable WRITE to EEPROM
EEADR = ADDRESS; //load ADDRESS in to EEADR register
EEDATA = DATA; //load DATA in to DATA register
EECON2 = 0x55; //write 55h to EECON2 to initiate write
EECON2 = 0x0AA; //write 0AAh to EECON2 to initiate write
EECON1bits.WR = 1; //initiate WRITE cycle
INTCONbits.GIE = 1; // re-enable global interupts
***
as well as the following in the High ISR:
if(EEIF =1){ //if EEPROM write successful
EEIF = 0; //reset EEPROM write flag
EECON1bits.WREN = 0; //Inhibit further reads until read_eeprom called.
The whole shebang:
/*
* File: timer_setup.c
* Author: ANDY
*
* Created on January 22, 2017, 10:42 AM
*/
#include <pic18f1330.h>
#include <xc.h>
/*Pumo Driver
*/
// P
// Written by ED REARDON
// Rev 1.0 October 2016
// Written for XC8 C compiler v1.38
// Please do not modify any code without instruction.
// PIC18F1330 Configuration Bit Settings
// 'C' source line config statements
// CONFIG1H
#pragma config OSC = INTIO1 // Oscillator (Internal oscillator, CLKO function on RA6, port function on RA7)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOR = BOHW // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 1 // Brown-out Reset Voltage bits ()
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 128 // Watchdog Timer Postscale Select bits (1:128)
// CONFIG3L
#pragma config PWMPIN = OFF // PWM Output Pins Reset State Control bit (PWM outputs disabled upon Reset)
#pragma config LPOL = HIGH // Low-Side Transistors Polarity bit (Even PWM Output Polarity Control bit) (PWM0, PWM2 and PWM4 are active-high (default))
#pragma config HPOL = HIGH // High Side Transistors Polarity bit (Odd PWM Output Polarity Control bit) (PWM1, PWM3 and PWM5 are active-high (default))
// CONFIG3H
#pragma config FLTAMX = RA7 // FLTA Mux bit (FLTA input is muxed onto RA7)
#pragma config T1OSCMX = HIGH // T1OSO/T1CKI MUX bit (T1OSO/T1CKI pin resides on RA6)
#pragma config MCLRE = OFF // Master Clear Enable bit (RA5 input pin enabled, MCLR pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable bit (Reset on stack overflow/underflow enabled)
#pragma config BBSIZ = BB1K // Boot Block Size Select bits (1K Words (2048 Bytes) Boot Block size)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled)
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit Block 0 (000800-000FFF) (Block 0 is not code-protected)
#pragma config CP1 = OFF // Code Protection bit Block 1 (001000-001FFF) (Block 1 is not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Code Protection bit (Boot Block Memory Area) (Boot Block is not code-protected)
#pragma config CPD = OFF // Code Protection bit (Data EEPROM) (Data EEPROM is not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit Block 0 (000800-000FFF) (Block 0 is not write-protected)
#pragma config WRT1 = OFF // Write Protection bit Block 1 (001000-001FFF) (Block 1 is not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Write Protection bit (Configuration Registers) (Configuration registers are not write-protected)
#pragma config WRTB = OFF // Write Protection bit (Boot Block Memory Area) (Boot Block is not write-protected)
#pragma config WRTD = OFF // Write Protection bit (Data EEPROM) (Data EEPROM is not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit Block 0 (000800-000FFF) (Block 0 is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit Block 1 (001000-001FFF) (Block 1 is not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Table Read Protection bit (Boot Block Memory Area) (Boot Block is not protected from table reads executed in other blocks)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#define _XTAL_FREQ 1000000
#define PULSE LATAbits.LATA0 //define pin RA0 as PULSE pin
#define DIR LATAbits.LATA1 //define pin RA1 as DIRECTION pin
#define SPEED PORTBbits.RB0 //Define SPEED H/L Pin
#define PROGDIR PORTBbits.RB2 //Define Direction Programming Pin
//Global Variables
volatile int T0COUNT; //Variable for Timer0 Counter
volatile int LPFLG ; //Flag for MEDIUM PUMP speed timer
volatile int MEDTIME = 5; //Loops for TIMER0 to run
void read_eeprom (unsigned int ADDR){ //Set up function for 'read_eeprom' and define address as unsigned_int.
EEADR = ADDR; // load ADDRESS in to EEADR register
EECON1bits.EEPGD =0; //set access to EEPROM memory
EECON1bits.RD = 1; // Initiate EEPROM read, bit cleared by hardware after read.
}
void write_eeprom (unsigned int ADDRESS, unsigned int DATA){
INTCONbits.GIE = 0; //switch off ALL interrupts during read
EECON1bits.WREN = 1; //enable WRITE to EEPROM
EEADR = ADDRESS; //load ADDRESS in to EEADR register
EEDATA = DATA; //load DATA in to DATA register
EECON2 = 0x55; //write 55h to EECON2 to initiate write
EECON2 = 0x0AA; //write 0AAh to EECON2 to initiate write
EECON1bits.WR = 1; //initiate WRITE cycle
INTCONbits.GIE = 1; // re-enable global interupts
}
//Set Timer Interrupt Procedure
void interrupt high_isr (void){
if(INTCONbits.TMR0IF == 1) { //Check for status of TMR0 Overflow
INTCONbits.TMR0IF = 0; //if OVERFLOW occurs reset TMR0 overflow flag.
T0COUNT++; //Increment T0COUNT by 1 for every time TMR0 overflows.
if(T0COUNT==MEDTIME) {
LPFLG = 1; //When counter total matches time required, set flag to 1
T0COUNT = 0;
}
}
if(INTCON3bits.INT3IF == 1){ //if programming pin is HIGH
INTCON3bits.INT3IF = 0; //Clear interrupt flag
TRISBbits.RB2 = 1; //enable RB2 PROGDIR pin
if(PROGDIR == 1){ //check for HI PROGDIR pin{
if(DIR == 0);
write_eeprom(0xFF, 1);}
{if (DIR == !0);
write_eeprom(0xFF, 0);}
}
if(EEIF =1){ //if EEPROM write successful
EEIF = 0; //reset EEPROM write flag
EECON1bits.WREN = 0; //Inhibit further reads until read_eeprom called.
}
}
int main (){
OSCCONbits.IRCF2 = 1; //Set clock speed to 1MHz
OSCCONbits.IRCF1 = 0; //Set clock speed to 1MHz
OSCCONbits.IRCF0 = 0; //Set clock speed to 1MHz
RCONbits.IPEN = 1; //enable interrupt priority
INTCONbits.GIE = 1; //enable all interrupts
INTCONbits.PEIE =1; //enable internal interrupts
T0CONbits.TMR0ON = 0; //switch timer OFF in initial state
T0CONbits.T0PS0 = 1; //set TIMER0 prescaler to 1:256 on TOP0-2
T0CONbits.T0PS1 = 1;
T0CONbits.T0PS2 = 1;
T0CONbits.PSA = 1; //Enable pre-scaler
T0CONbits.T0CS = 0; //TIMER0 uses Internal Osccilator
T0CONbits.T016BIT = 0; //TIMER0 uses 16 bit counter
ADCON1 = 0xFF; //switch OFF Port-A ADC
CMCON = 0xFF; //switch OFF comparitors
PWMEN1 = 0xFF; //switch OFF PWM module
INTCONbits.TMR0IE = 1; //enable TIMER0 overflow bit
INTCON3bits.INT3E = 1; //enable INT3 External Interrupt
INTCON2bits.INTEDG3 = 1; //enable INT3 RIDING EDGE interrupt
TRISAbits.RA0 = 0; //define A0 as OUTPUT
TRISAbits.RA1 = 0; //define A1 as OUTPUT
TRISBbits.RB0 = 1; //define B0 as INPUT
DIR = (read_eeprom, 0x00);
while (1) //Do this forever
if(SPEED == 1 ) { //if SPPED pin HI
if(LPFLG == 0){ //If Loop Counter FLAG LO
T0CONbits.TMR0ON = 1; //switch timer ON
PULSE= ~PULSE; //Set PULSE toggle
__delay_us(25); //Toggle pulse MEDIUM
}
if(LPFLG == 1 ) { //if Loop Counter FLAG HI
T0CONbits.TMR0ON = 0; //Switch TIMER0 OFF!
PULSE= ~PULSE; //Set PULSE toggle
__delay_us(50); //Toggle pulse SLOW
}
}
else {
T0CONbits.TMR0ON = 0; //Switch TIMER0 OFF!
LPFLG = 0; //Reset loop counter
PULSE= ~PULSE;
__delay_us(5); //Toggle pulse FAST
}
return 0;
}
Now I've got two issues, assuming I've set up my functions correctly:
Firstly the really confusing one
* When compiling the compiler complains that 'read_eeprom' function isn't called, although it clearly is as 'DIR == (read_eeprom 0xFF)
Stepping through in MPLAB with the software it just ignores this, it doesn't go round the read_eeprom function.
* When write_eeprom is called in the interrupt it steps through 'WRITE*_eeprom' on both 0/!0 on '0!' it does raise the complete interrupt on!0 but not on 0.
I don't know if this is normal behaviour or not but it seems odd that it still calls the function even if the argument isn't the case.
This is going to be numpty here having done something silly, I'm fraustrated now as once this is done I'm ready to 'rock and roll!'
Any suggestions ladies and gents, I can smell this finish line now!
I've only been writing 'C' a few days, so any helpful pointers really are welcomed!
*Edited, initially said 'read!'