Advertisement
Mark2020H

SPI Bus - 3 wire bus with shift bit register using XC8 MPLAB

May 18th, 2021 (edited)
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.14 KB | None | 0 0
  1. /* All about the spi bus and how to implement this in xc8 *
  2. * File: spi.h
  3. * Author: Mark Harrington
  4. *
  5. * Created on 18 May 2021, 15:34
  6. */
  7.  
  8.  
  9. /*Diagram of hardware Link here below */
  10.  
  11.  
  12.  
  13. /* ******************************* https://photos.app.goo.gl/HpHcyLBu4NtDA3sb8 ************************************/
  14.  
  15. #ifndef SPI_H
  16. #define SPI_H
  17.  
  18. /* MD Harrington */
  19.  
  20.  
  21. #pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
  22. #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
  23. #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
  24. #pragma config MCLRE = OFF // RA5/MCLR/VPP Pin Function Select bit (RA5/MCLR/VPP pin function is digital I/O, MCLR internally tied to VDD)
  25. #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
  26. #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
  27. #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
  28. #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
  29. #pragma config CCPMX = RB0 // CCP1 Pin Selection bit (CCP1 function on RB0)
  30. #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
  31.  
  32. // CONFIG2
  33. #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  34. #pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode disabled)
  35.  
  36. // #pragma config statements should precede project file includes.
  37. // Use project enums instead of #define for ON and OFF.
  38.  
  39. #include <xc.h>
  40.  
  41. /* Define clock frequency required for delay functions */
  42.  
  43.  
  44. #define _XTAL_FREQ 8000000
  45.  
  46. #define DATA PORTAbits.RA0
  47. #define CLOCK PORTAbits.RA1
  48. #define LATCH PORTAbits.RA2
  49.  
  50.  
  51. void clockdata(void);
  52. void latchdata(void);
  53.  
  54.  
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58.  
  59. /* anything c ++ goes here */
  60.  
  61.  
  62.  
  63.  
  64. #ifdef __cplusplus
  65. }
  66. #endif
  67.  
  68. #endif /* SPI_H */
  69.  
  70.  
  71. /* Main Method for doing this is below */
  72.  
  73. /*
  74. * File: main.c
  75. * Author: mark Harrington
  76. *
  77. * Created on 17 May 2021, 17:17
  78. */
  79.  
  80. #include "spi.h"
  81.  
  82. // spi functions
  83. void clockdata(void){
  84.  
  85. CLOCK = 1 ;
  86. __delay_ms(5);
  87. CLOCK =0 ;
  88.  
  89.  
  90. }
  91.  
  92.  
  93. void latchdata(void){
  94. LATCH =1 ;
  95. __delay_ms(5);
  96. LATCH=0 ;
  97.  
  98. }
  99.  
  100. int main(int argc, char** argv) {
  101.  
  102.  
  103.  
  104. TRISA=0x0 ; // set port direction
  105. ANSEL= 0x0; // digital out only
  106. OSCCON=0b01110100; //osccon set to 8 megs
  107. PORTA=0x00 ;
  108.  
  109. CLOCK =0 ;
  110. LATCH =0 ;
  111.  
  112.  
  113. unsigned char b = 0x01 ;
  114.  
  115. while (1){
  116.  
  117. for( unsigned int i = 0;i <8 ;i++)
  118. {
  119. DATA = (unsigned char)(b<<i) ; // must cast this to char for correct conversion
  120. __delay_ms(200);
  121. clockdata() ;
  122. latchdata() ;
  123.  
  124. }
  125. DATA = b ;
  126. }
  127.  
  128. }
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement