Code:
#include <p18cxxx.h>
#include <delays.h>
#pragma config PLLDIV = 5
#pragma config CPUDIV = OSC1_PLL2
#pragma config USBDIV = 2
#pragma config FOSC = HSPLL_HS
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOR = ON
#pragma config VREGEN = ON
#pragma config WDT = OFF
#pragma config WDTPS = 1
#pragma config MCLRE = ON
#pragma config LPT1OSC = OFF
#pragma config PBADEN = OFF
#pragma config CCP2MX = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#pragma config XINST = OFF
#pragma config DEBUG = OFF
#pragma config CP0 = OFF
#pragma config CPB = OFF
#pragma config WRT0 = OFF
#pragma config WRTB = OFF
#pragma config EBTR0 = OFF
#pragma config EBTRB = OFF
#define INPUT 1
#define OUTPUT 0
#define mInitAllLEDs() TRISCbits.TRISC1=OUTPUT; TRISCbits.TRISC2=OUTPUT;
#define mLED_1 LATCbits.LATC1
#define mLED_2 LATCbits.LATC2
#define mLED_1_On() mLED_1 = 1;
#define mLED_2_On() mLED_2 = 1;
#define mLED_1_Off() mLED_1 = 0;
#define mLED_2_Off() mLED_2 = 0;
#define mLED_1_Toggle() mLED_1 = !mLED_1;
#define mLED_2_Toggle() mLED_2 = !mLED_2;
#define BUFFSIZE 256
#define DISABLE_INTERRUPTS() {while(INTCONbits.GIE) INTCONbits.GIE=0;}
#define ENABLE_INTERRUPTS() {INTCONbits.GIE=1;}
#pragma udata
unsigned char buff[BUFFSIZE];
#pragma code
void EEpromPut(unsigned char addr, unsigned char data)
{
unsigned short temp;
EEADR = addr;
EEDATA = data;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
DISABLE_INTERRUPTS();
_asm
MOVLW 0x55
MOVWF EECON2,0
MOVLW 0xAA
MOVWF EECON2,0
_endasm
EECON1bits.WR=1;
while (EECON1bits.WR == 1);
ENABLE_INTERRUPTS();
EECON1bits.WREN = 0;
}
unsigned char EEpromGet(unsigned char addr)
{
volatile unsigned char eepTemp;
EEADR = addr;
EECON1bits.EEPGD = 0;
EECON1bits.CFGS = 0;
EECON1bits.RD = 1;
eepTemp = EEDATA;
return eepTemp;
}
void main(void)
{
int counter;
unsigned char current3, current4;
ADCON1 |= 0x0F; //set all input as digital.
TRISB=0x00; // default PORTB to output
//only ports currently interested in are 3 and 4
TRISBbits.TRISB3=1;
TRISBbits.TRISB4=1;
INTCON2bits.RBPU=0; //enable PORTB internal pull ups
LATB=0xFF; //make sure the output PORTB pins are high
mInitAllLEDs(); //enable output LEDs
mLED_2_On();
mLED_1_Off();
for(counter=0;counter<BUFFSIZE;counter++)
{
current3 = PORTBbits.RB3;
current4 = PORTBbits.RB4;
buff[counter] = PORTB; //stash it in the buffer
while((current3==PORTBbits.RB3) && (current4==PORTBbits.RB4)); //sit and spin until it changes.
}
mLED_1_On();
for(counter=0;counter<BUFFSIZE;counter+)
{
EEpromPut(counter, buff[counter]);
}
while(1)
{ //blink the light to show us its done
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(250);
Delay10KTCYx(200);
mLED_1_Toggle();
mLED_2_Toggle();
}
}
There's the code I'm currently using. Even though it shouldn't, I get repeated bytes written to the EEPROM. It should never write the same byte twice in a row to the EEPROM, yet it does. A series of FF's mostly, and sometimes others. If I leave the probes floating, it will stay there, correctly waiting for one of the lines to go low. If I barely touch them to ground, like 3 quick taps, it will finish and the EEPROM will show repeated repeated patterns. The internal pullups on PORTB are enabled, and the lines are also pulled high on the board with a 10k resistor. Yes, I have a probe connecting the PIC ground to the Wiimote and peripheral ground.
Schematic description:
PIC 18LF2550
20 MHz crystal, 2x 22pf caps and 1M Ohm resistor on the OSC lines.
Powered by a 9 volt battery through a 7805. 10uF cap between 9v and ground, 10uf cap and 2 .1uf caps between the +5 and ground in different places, another .1uf cap right next to the VCC and GND pins of the PIC
220 ohm resistor and LED between +5 and ground
LED between C1 and ground
LED between C2 and ground
10k pullup resistors on RB0 through RB7
10k pullup resistor on MCLR, along with a reset switch between MCLR and ground.
22 ohm resistors on D+ and D- going to a USB plug. USB is not being using because I can't get the USB bootloader to work on the 2550.
.47uF cap between Vusb pin and ground
Using three hook probes, one connected to ground, one on RB4 and one on RB3.
Using the pullup resistors and checking the state of the RB3 and RB4 lines seemed the way to do it. If there is a better way, pleade enlighten me.