Can anyone help with this simple problem, I want to achieve full speed 64MHz, at the moment I am getting 16MHz external, when I run a simple while loop (while(1){PORTD=0xFF;PORTD=0x00;}, I get a 4MHz squarewave, also I am having problems getting I2C working communicating with a DS1803E-010DS1803E-010 digital potentiometer:
#pragma config FOSC = INTIO7, FCMEN = OFF, IESO = OFF // CONFIG1H
#pragma config PWRT = ON, BOREN = OFF, BORV = 30 // CONFIG2L
#pragma config WDTEN = OFF, WDTPS = 32768 // CONFIG2H
#pragma config MCLRE = OFF, LPT1OSC = OFF, PBADEN = OFF, CCP2MX = PORTC // CONFIG3H
#pragma config STVREN = ON, LVP = OFF, XINST = OFF // CONFIG4L
#pragma config CP0 = OFF, CP1 = OFF, CP2 = OFF, CP3 = OFF // CONFIG5L
#pragma config CPB = OFF, CPD = OFF // CONFIG5H
#pragma config WRT0 = OFF, WRT1 = OFF, WRT2 = OFF, WRT3 = OFF // CONFIG6L
#pragma config WRTB = OFF, WRTC = OFF, WRTD = OFF // CONFIG6H
#pragma config EBTR0 = OFF, EBTR1 = OFF, EBTR2 = OFF, EBTR3 = OFF // CONFIG7L
#pragma config EBTRB = OFF // CONFIG7H
/** I N C L U D E S **************************************************/
#include "p18f45k20.h"
#include "07 ADC.h" // header file
#include "delays.h"
#include "spi.h"
#include "i2c.h"
//#include "math.h"
//#include "08 Interrupts.h" // header file ****throws error
/** Prototypes**/
void main(void);
unsigned char I2C_SendByte(char myVolume);//char myByte)
unsigned char IncrementGrey(unsigned char Value); unsigned char DecrementGrey(unsigned char Value);
unsigned char WriteI2C1( unsigned char data_out );
unsigned char ReadI2C1( unsigned char data_out );
/** V A R I A B L E S *************************************************/
#pragma udata // declare statically allocated uinitialized variables
/** D E C L A R A T I O N S *******************************************/
#pragma code // declare executable instructions
/*******Global Variables******************/
/**********************************/
void main(void)
{
unsigned char count=1;
signed char Direction_Up=-1;
unsigned char volume=0x00;
unsigned char PA=0x00;
unsigned char NAPA=0x00;
unsigned char greyCode=0x00;
unsigned char temp;
unsigned char PORTx;
/******************************************************************/
WDTCONbits.SWDTEN = 0; // Disable Watchdog
OSCTUNEbits.PLLEN = 1; // Enable PLL //PLL enabled for INTOSC (4 MHz and 8 MHz only)
// IDLEN IRCF2 IRCF1 IRCF0 OSTS(1) IOFS SCS1 SCS0
OSCCONbits.IDLEN = 0; // Not enable pherimetral during sleep()
OSCCONbits.SCS1 = 1; // Internal oscillator selected
OSCCONbits.SCS0 = 0; //
OSCCONbits.IRCF2 = 1; // 16 Mhz selected, 110=8Mhz
OSCCONbits.IRCF1 = 1;
OSCCONbits.IRCF0 = 1;
while(OSCCONbits.OSTS == 0){} //Oscillator Start-up Time-out Status bit
while(OSCCONbits.IOFS == 0){} //INTOSC Frequency Stable bit
/******************************************************************/
// Init I/O
TRISC = TRISD = 0x00; // PORTD bits 7:0 are all outputs (0)
TRISB = 0xFF; // PORTB (Encoder) are all inputs (1)
// TRISAbits.TRISA0 = 1; // TRISA0 input
INTCON2bits.RBPU = 0; // enable PORTB internal pullups
INTCON = 0b11100000;
INTCONbits.GIEL = 1;
INTCON2bits.INTEDG1 = 0;
INTCON3bits.INT1IP = 0;
INTCON3bits.INT1IE = 1;
INTCON3bits.INT1IF = 0;
//INTCON.PLLEN = 1;
RCONbits.IPEN = 1;
WPUBbits.WPUB0 = 1; // enable pull up on RB0
TRISBbits.TRISB0 = 1; // PORTB bit 0 (connected to switch) is input (1)
PA=NAPA=PORTD=0;
volume=0xFF;PORTD=0xFF;
while(1)
{
PA=NAPA;
/* remove this after test */
while (!Switch_Pin1)
{
Delay1KTCYx(50); //delay for switch debouncing - triggers lagging edge
if (Switch_Pin1) {Delay1KTCYx(50);NAPA = IncrementGrey(NAPA);} //Increments PA on key release
}
/**************************/
// NAPA=PORTB;
if (PA!=NAPA)
{
switch(NAPA)
{
case 0x00:
switch (PA) //check last state
{
case 0x02:Direction_Up=1; break;
case 0x01:Direction_Up=0; break;
default: Direction_Up=-1;break;
}
break;
case 0x01:
switch (PA)
{
case 0x00:Direction_Up=1; break;//TEMP STOP CODE break;
case 0x03:Direction_Up=0; break;
default: Direction_Up=-1;break;
}
break;
case 0x03:
switch (PA)
{
case 0x01:Direction_Up=1; break;
case 0x02:Direction_Up=0; break;
default: Direction_Up=-1;break;
}
break;
case 0x02:
switch (PA)
{
case 0x03: Direction_Up=1; break;
case 0x00: Direction_Up=0; break;
default: Direction_Up=-1;break;
}
default:break;
}
}
switch (Direction_Up)
{
case 1: volume++;PORTx = I2C_SendByte(volume);PORTD = volume;break;
default: break;
case 0: volume--;PORTx = I2C_SendByte(volume);PORTD = volume;break;
}
Direction_Up=-1;
} //while end
}
unsigned char I2C_SendByte(char myVolume)//char myByte)
{
unsigned char SPI_result=-1;
//• MSSP Control Register 1 (SSPCON1)
//• MSSP Control Register 2 (SSPCON2)
//• MSSP STATUS register (SSPSTAT)
//• Serial Receive/Transmit Buffer Register
//(SSPBUF)
//• MSSP Shift Register (SSPSR) – Not directly
//accessible
//• MSSP Address Register (SSPADD)
SSPSTAT = 0x80;//Disable SMBus & Slew Rate Control
SSPCON1 = 0x28; //Enable MSSP Master, SSPCON1=MASTER;
// SPPADD must go after SSPCON1=MASTER; // ADD<7:0> = 1/Fi2c * FOSC-1 = 1/400,000 * 16,000 - 1 = 39 = 0010,0111b = 0x27
SSPCON2 = 0x00; //Clear Control Bits
//SSPADD = 0x18; //Should be 0x18 for 100kHz
SSPADD=0x27;//SCL pin clock period = ((ADD<7:0> + 1) *4)/FOSC
DDRCbits.RC3 = 1; //Configure SCL as Input
DDRCbits.RC4 = 1; //Configure SDA as Input
//StartI2C();
//OpenI2C(MASTER, SLEW_ON);
SPI_result =WriteI2C(0b10100000);// 0101,address
Delay1KTCYx(500);
SPI_result =WriteI2C(myVolume);
//Delay1KTCYx(500);
return ReadI2C();
}
unsigned char IncrementGrey(unsigned char Value)
{
switch (Value)
{
case 0x00:Value=0x01;break;
case 0x01:Value=0x03;break;
case 0x03:Value=0x02;break;
case 0x02:Value=0x00;break;
default:break;
}
return Value;
}
unsigned char DecrementGrey(unsigned char Value)
{
switch (Value)
{
case 0x00:Value=0x02;break;
case 0x01:Value=0x00;break;
case 0x03:Value=0x01;break;
case 0x02:Value=0x03;break;
default:break;
}
return Value;
}

