#define _XTAL_FREQ 20000000
// PIC18F45K80 Configuration Bit Settings
// CONFIG1L
#pragma config RETEN = ON // VREG Sleep Enable bit (Ultra low-power regulator is Enabled (Controlled by SRETEN bit))
#pragma config INTOSCSEL = LOW // LF-INTOSC Low-power Enable bit (LF-INTOSC in Low-power mode during Sleep)
// SOSCSEL = No Setting
#pragma config XINST = OFF // Extended Instruction Set (Disabled)
// CONFIG1H#pragma config FOSC = HS2 // HS oscillator (high power, 16 MHz-25 MHz
#pragma config PLLCFG = OFF // PLL x4 Enable bit (Disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor (Disabled)
#pragma config IESO = OFF // Internal External Oscillator Switch Over Mode (Disabled)
// CONFIG2L#pragma config PWRTEN = ON // Power Up Timer (Enabled)
#pragma config BOREN = OFF // Brown Out Detect (Disabled in hardware, SBOREN disabled)
#pragma config BORV = 0 // Brown-out Reset Voltage bits (3.0V)
#pragma config BORPWR = LOW // BORMV Power level (BORMV set to low power level)
// CONFIG2H#pragma config WDTEN = OFF // Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
#pragma config WDTPS = 1 // Watchdog Postscaler (1:1)
// CONFIG3H#pragma config CANMX = PORTC // ECAN Mux bit (ECAN TX and RX pins are located on RC6 and RC7, respectively)
#pragma config MSSPMSK = MSK5 // MSSP address masking (5 bit address masking mode)
#pragma config MCLRE = OFF // Master Clear Enable (MCLR Disabled, RE3 Enabled)
// CONFIG4L#pragma config STVREN = OFF // Stack Overflow Reset (Disabled)
#pragma config BBSIZ = BB1K // Boot Block Size (1K word Boot Block size)
// CONFIG5L#pragma config CP0 = ON // Code Protect 00800-01FFF (Enabled)
#pragma config CP1 = ON // Code Protect 02000-03FFF (Enabled)
#pragma config CP2 = ON // Code Protect 04000-05FFF (Enabled)
#pragma config CP3 = ON // Code Protect 06000-07FFF (Enabled)
// CONFIG5H#pragma config CPB = ON // Code Protect Boot (Enabled)
#pragma config CPD = ON // Data EE Read Protect (Enabled)
// CONFIG6L#pragma config WRT0 = ON // Table Write Protect 00800-01FFF (Enabled)
#pragma config WRT1 = ON // Table Write Protect 02000-03FFF (Enabled)
#pragma config WRT2 = ON // Table Write Protect 04000-05FFF (Enabled)
#pragma config WRT3 = ON // Table Write Protect 06000-07FFF (Enabled)
// CONFIG6H#pragma config WRTC = ON // Config. Write Protect (Enabled)
#pragma config WRTB = ON // Table Write Protect Boot (Enabled)
#pragma config WRTD = ON // Data EE Write Protect (Enabled)
// CONFIG7L#pragma config EBTR0 = ON // Table Read Protect 00800-01FFF (Enabled)
#pragma config EBTR1 = ON // Table Read Protect 02000-03FFF (Enabled)
#pragma config EBTR2 = ON // Table Read Protect 04000-05FFF (Enabled)
#pragma config EBTR3 = ON // Table Read Protect 06000-07FFF (Enabled)
// CONFIG7H#pragma config EBTRB = ON // Table Read Protect Boot (Enabled)
// #pragma config statements should precede project file includes.// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define TRUE 1
#define CS_PIN LATAbits.LATA5 // MCP3204/3208 CS pin
void Port_Initialized (void)
{
ANCON0 = 0; // Set to digital port
ANCON1 = 0; // Set to digital port
CM1CON = 0; // Comparator off
CM2CON = 0; // Comparator off
ADCON0 = 0; // A/D conversion Disabled
ADCON1 = 0; // A/D conversion Disabled
ADCON2 = 0; // A/D conversion Disabled
LATA = 0; // Make all PORTA pins low
LATB = 0; // Make all PORTB pins low
LATC = 0; // Make all PORTC pins low
LATD = 0; // Make all PORTD pins low
LATE = 0; // Make all PORTE pins low
TRISA = 0b0000000; // Slave Select (SS) RA5/AN4/C2INB/
TRISB = 0b0000000; // all are output, Unused
//Serial Data Out (SDO) RC5/SDO,
//Serial Data In (SDI) RC4/SDA/SDI
//Serial Clock (SCK) RC3/REF0/SCL/SCK
TRISC = 0b00010000;
TRISD = 0b0000000; // all are output, show low byte
TRISE = 0b0000000; // All are output, show high 2 bits
}
void SPI_Init_Master()
{
SSPSTAT = 0b01000000; // SSPSTAT: MSSP STATUS REGISTER (SPI MODE) SMP=0, CKE=1
SSPCON1 = 0b00100010; // Master mode,Serial enable
PIR1bits.SSPIF=0;
}
unsigned char SPI_Xfer(unsigned char Sendchar)
{
SSPBUF = Sendchar; // Copy data in SSBUF to transmit
while (!PIR1bits.SSPIF); // Wait for complete 1 byte transmission
PIR1bits.SSPIF = 0; // Clear SSPIF flag
return SSPBUF;
}
void main(void)
{
unsigned char High_byte; // temporary ADC receive byte - 2 bits only
unsigned char low_byte; // temporary ADC receive byte
Port_Initialized();
SPI_Init_Master();
while (1)
{
CS_PIN = 0; // Set CS pin low
SPI_Xfer(0x01); // Send "Start" bit
High_byte = SPI_Xfer(0b00000000); // 0000 differential CH0 = IN+ CH1 = IN-
low_byte = SPI_Xfer(0x00); // Send dummy, receive lower 8 bits
CS_PIN = 1; // Set CS pin high
LATD = low_byte; // show result on PORTD LEDs
LATE = High_byte; // show result top 2 bits on PORTE LEDs
__delay_ms(10); // limit interrogation rate
}
}