Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**********************************************************
- ;* Exercise - 5 SYNCHRONOUS SERIAL EXPANSION VIA SPI BUS
- ;**********************************************************
- ; BEAU HOBBA 470213225
- ; BEC EGAN 470416594
- ;**********************************************************
- */
- #include <p18f452.h>
- #include "ConfigRegsPIC18F452.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <spi.h>
- void highPriorityISR(void);
- // Config variables
- unsigned char dataIn;
- #pragma code high_vector = 0x08
- void gotolowISR(void){
- _asm
- GOTO highPriorityISR
- _endasm
- }
- void initTimer(void){
- T1CON = 0b10100001;
- // bit 7- enables one 16 bit operation
- // bit 5-4- prescalar of 4
- // bit 0- enables timer 1
- // Compare mode with special event trigger
- CCP1CON = 0b00001011;
- // Clear all interrupts
- RCONbits.IPEN = 1;
- PIE1bits.CCP1IE = 1; // Set up the interrupt for timer 1
- PIR1bits.CCP1IF = 0; // Clear the interrupt flag
- INTCONbits.GIEH = 1; // Enable low and high priority interrupts
- INTCONbits.GIEL = 1;
- }
- void outputClock(void) {
- // Set to 30Hz
- // (Fosc/4) /PS = x/Period
- // x = 4167
- CCPR1H = 0b00010000;
- CCPR1L = 0b01000111;
- }
- void portBInit(void){
- PORTB = 0; // Clear port B
- TRISB = 0; // Set Port B to output
- PORTBbits.RB2 = 0; // Set port B pin 2 to output the SH/LD (30Hz)
- PORTBbits.RB3 = 0; // Set port B pin 3 as output for RCLK (30Hz)
- PORTBbits.RB4 = 0; // Set port B pin 4 as SRCLR(not) // just set to high
- }
- void initSPI(void) {
- // Configure status register
- SSPSTATbits.SMP = 1; // Data sampled at the end // mid?
- SSPSTATbits.CKE = 0; // Data transmitted on falling edge while idling low (CKP = 0)
- // Configure control register
- SSPCON1 = 0b00100000;
- //SSPCON1bits.SSPEN = 1; // Synchronous serial port enable
- //SSPCON1bits.CKP = 0; // Clock polarity select, idle low
- //SSPCON1bits.SSPM3:SSPM0 = 0000; // SPI master mode, clock = Fosc/4
- }
- //unsigned char receiveSPI(void) {
- //
- // // Check if data is ready
- // //while (DataRdySPI() == 0){
- // //}
- //
- // // Now that there is new data in SSPBUF, acquire a byte of data
- //
- // return dataIn;
- //
- //}
- void initTransmit(void){
- TRISC &= 0b00010000; // Set data direction pin for serial transmission
- //TRISC<3> = 0; // Serial clock Master Mode
- }
- //void transmitSPI(unsigned char){
- //
- // // Transmit data but if collision bit set, do again
- // //putcSPI(dataIn);
- // /*if (SSPCON1bits.WCOL == 1){
- // SSPCON1bits.WCOL = 0;
- // transmitSPI(); // This is a bit shit: fix
- // }*/
- //
- //}
- void main(void) {
- // NOTE: may need to set clk inh bit and clear for serial shift in 165
- //initSPI();
- initTimer();
- portBInit();
- outputClock();
- initTransmit();
- //receiveSPI();
- while(1);
- }
- #pragma code
- #pragma interrupt highPriorityISR
- void highPriorityISR(void){
- // Bit toggle port B pin 2
- PORTBbits.RB2 ^= 1;
- // Bit toggle port B pin 3
- PORTBbits.RB3 ^= 1;
- // Read data from 165 chip
- //while (DataRdySPI() == 0){
- //}
- dataIn = ReadSPI();
- // Write the data to the 595 chip
- //putcSPI(dataIn);
- // Clear the interrupt
- PIR1bits.CCP1IF = 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement