SHARE
TWEET

Untitled

a guest Sep 11th, 2019 84 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**********************************************************
  2. ;*  Exercise -  5 SYNCHRONOUS SERIAL EXPANSION VIA SPI BUS
  3. ;**********************************************************
  4. ; BEAU HOBBA 470213225
  5. ; BEC EGAN 470416594
  6. ;**********************************************************
  7. */
  8.  
  9.  
  10. #include <p18f452.h>
  11. #include "ConfigRegsPIC18F452.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <spi.h>
  15.  
  16. void highPriorityISR(void);
  17.  
  18. // Config variables
  19. unsigned char dataIn;
  20.  
  21. #pragma code high_vector = 0x08
  22. void gotolowISR(void){
  23.     _asm
  24.      GOTO highPriorityISR
  25.     _endasm
  26. }
  27.  
  28.  
  29.  
  30. void initTimer(void){
  31.  
  32.  
  33.     T1CON = 0b10100001;
  34.     // bit 7- enables one 16 bit operation
  35.     // bit 5-4- prescalar of 4
  36.     // bit 0- enables timer 1
  37.  
  38.     // Compare mode with special event trigger
  39.     CCP1CON = 0b00001011;
  40.  
  41.     // Clear all interrupts
  42.     RCONbits.IPEN = 1;
  43.     PIE1bits.CCP1IE = 1; // Set up the interrupt for timer 1
  44.     PIR1bits.CCP1IF = 0; // Clear the interrupt flag
  45.  
  46.     INTCONbits.GIEH = 1;    // Enable low and high priority interrupts
  47.     INTCONbits.GIEL = 1;
  48.  
  49. }
  50.  
  51. void outputClock(void) {
  52.  
  53.     // Set to 30Hz
  54.     // (Fosc/4) /PS = x/Period
  55.     // x = 4167
  56.     CCPR1H = 0b00010000;
  57.     CCPR1L = 0b01000111;
  58.  
  59.  
  60. }
  61.  
  62. void portBInit(void){
  63.  
  64.     PORTB = 0; // Clear port B
  65.     TRISB = 0; // Set Port B to output
  66.     PORTBbits.RB2 = 0; // Set port B pin 2 to output the SH/LD (30Hz)
  67.     PORTBbits.RB3 = 0; // Set port B pin 3 as output for RCLK (30Hz)
  68.     PORTBbits.RB4 = 0; // Set port B pin 4 as SRCLR(not) // just set to high
  69.  
  70.  
  71.  
  72. }
  73.  
  74.  
  75. void initSPI(void) {
  76.  
  77.     // Configure status register
  78.     SSPSTATbits.SMP = 1; // Data sampled at the end // mid?
  79.     SSPSTATbits.CKE = 0; // Data transmitted on falling edge while idling low (CKP = 0)
  80.  
  81.     // Configure control register
  82.     SSPCON1 = 0b00100000;
  83.     //SSPCON1bits.SSPEN = 1;    // Synchronous serial port enable
  84.     //SSPCON1bits.CKP = 0;  // Clock polarity select, idle low
  85.     //SSPCON1bits.SSPM3:SSPM0 = 0000; // SPI master mode, clock = Fosc/4
  86.  
  87.  
  88.  
  89. }
  90.  
  91. //unsigned char receiveSPI(void) {
  92. //
  93. //  // Check if data is ready
  94. //  //while (DataRdySPI() == 0){
  95. //  //}
  96. //
  97. //  // Now that there is new data in SSPBUF, acquire a byte of data
  98. //
  99. //    return dataIn;
  100. //
  101. //}
  102.  
  103. void initTransmit(void){
  104.  
  105.     TRISC &= 0b00010000; // Set data direction pin for serial transmission
  106.     //TRISC<3> = 0; // Serial clock Master Mode
  107. }
  108.  
  109. //void transmitSPI(unsigned char){
  110. //
  111. //  // Transmit data but if collision bit set, do again
  112. //  //putcSPI(dataIn);
  113. //  /*if (SSPCON1bits.WCOL == 1){
  114. //      SSPCON1bits.WCOL = 0;
  115. //      transmitSPI(); // This is a bit shit: fix
  116. //  }*/
  117. //
  118. //}
  119.  
  120.  
  121.  
  122.  
  123.  
  124. void main(void) {
  125.  
  126.     // NOTE: may need to set clk inh bit and clear for serial shift in 165
  127.  
  128.     //initSPI();
  129.     initTimer();
  130.     portBInit();
  131.     outputClock();
  132.     initTransmit();
  133.     //receiveSPI();
  134.  
  135.     while(1);
  136.  
  137. }
  138.  
  139. #pragma code
  140. #pragma interrupt highPriorityISR
  141. void highPriorityISR(void){
  142.  
  143.     // Bit toggle port B pin 2
  144.     PORTBbits.RB2 ^= 1;
  145.  
  146.     // Bit toggle port B pin 3
  147.     PORTBbits.RB3 ^= 1;
  148.  
  149.  
  150.     // Read data from 165 chip
  151.     //while (DataRdySPI() == 0){
  152.     //}
  153.     dataIn = ReadSPI();
  154.  
  155.     // Write the data to the 595 chip
  156.     //putcSPI(dataIn);
  157.  
  158.     // Clear the interrupt
  159.     PIR1bits.CCP1IF = 0;
  160.  
  161.  
  162. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top