#include <htc.h>
#include "usart.h"
// PIC 18F4550 fuse configuration:
// Config word 1 (Oscillator configuration)
// 20Mhz crystal input scaled to 48Mhz and configured for USB operation
// USBPLL clk src from 96MHz PLL/2
// IESODIS Internal External Switch Over Mode
// FCMDIS Fail-Safe Clock Monitor Enable
// HSPLL Oscillator: HS: HS+PLL, USB-HS
// CPUDIV1 CPU Sys CLK Select: no divide
// OSC Select: PLLDIV5 div by 5 {20MHz input}
__CONFIG(1, USBPLL & IESODIS & FCMDIS & HSPLL & CPUDIV1 & PLLDIV5);
// Config word 2
// VREGEN USB Voltage Regulator
// PWRTDIS Power Up Timer
// BOREN Brown Out Detect: Enabled in hardware, SBOREN disabled
// BORV2O Brown Out Voltage: 2.0V
// WDTDIS Watchdog Timer
// WDTPS32K Watchdog Postscaler: 1:32768
__CONFIG(2, VREGEN & PWRTDIS & BOREN & BORV20 & WDTDIS & WDTPS32K);
// Config word 3
// PBDIGITAL PortB A/D Enable: PORTB<4:0> configured as digital I/O on RESET
// LPT1DIS Low Power Timer1 Osc enable: Timer 1 not in low power configuration
// Master Clear Enable: MCLREN MCLR Enabled,RE3 Disabled
__CONFIG(3, PBDIGITAL & LPT1DIS & MCLREN);
// Config word 4
// XINSTDIS Extended CPU Enable: Disable extended instruction set (Legacy mode)
// STVREN Stack Overflow Reset
// LVPDIS Low Voltage Program
// ICPORTDIS Dedicated In-Circuit Port {ICD/ICSP}
// DEBUGDIS Background Debug
__CONFIG(4, XINSTDIS & STVREN & LVPDIS & ICPORTDIS & DEBUGDIS);
void main(void){
unsigned char eightBit;
TRISA = 0b00000000;
// Enable the asyncchronous serial port.
SYNC = 0 ; // Asynchronous mode
SPEN = 1; // Serial port enable.
TRISC6 = 0;
TRISC7 = 1;
//Configure for Transmitter mode.
TXEN = 1; // Transmit enable
//Configure for Receiver mode
CREN = 1; // Enable the reception
//Interrupt
RCIF = 0;
RCIE = 1; // Reception Interrupt Enable
GIE = 1; // Global Interrupt Enable
PEIE = 1; // Perapheral Interrupt Enable
INTCON=0; // purpose of disabling the interrupts.
init_comms(); // set up the USART - settings defined in usart.h
while(1){
RA3 = 0;
// TXREG - Read/Write Transmit buffer Register
// - loaded with data by software
// - empties once transfer to shift register occurs
// - once empty TXIF Flag is set
// TXIF - not alterable by sofware
// - if polling wait a tid after TXREG
// TXIE - interrupt clearing mechanism
// TRMT - shows shift register status. read only
// - poll to determine if Shift register is empty
// SYNC - Enable Asynchronous: 0;
// SPEN - Serial port setting bit: 1;
// RCIE - Interrupt enable
// RX9 - 9 bit reception
// CREN - Enables reception
// RCIE - Flag to generate RCIF interrupts
// RCIF - Reception flag. Indicates complete reception
// RCSTA - 9th bit location. error checking
// RCREG - Reads 8 bit data received
// CREN - Clear error, clearing enable bit
// GIE and PEIE should be set to use interrupts
// eightBit = getch();
eightBit = 0010;
putch(eightBit);
eightBit = 0001;
putch(eightBit);
RA3 = 1;
// }
}