Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* All about the spi bus and how to implement this in xc8 *
- * File: spi.h
- * Author: Mark Harrington
- *
- * Created on 18 May 2021, 15:34
- */
- /*Diagram of hardware Link here below */
- /* ******************************* https://photos.app.goo.gl/HpHcyLBu4NtDA3sb8 ************************************/
- #ifndef SPI_H
- #define SPI_H
- /* MD Harrington */
- #pragma config FOSC = INTOSCIO // Oscillator Selection bits (INTRC oscillator; port I/O function on both RA6/OSC2/CLKO pin and RA7/OSC1/CLKI pin)
- #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
- #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
- #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)
- #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
- #pragma config LVP = OFF // Low-Voltage Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
- #pragma config CPD = OFF // Data EE Memory Code Protection bit (Code protection off)
- #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
- #pragma config CCPMX = RB0 // CCP1 Pin Selection bit (CCP1 function on RB0)
- #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
- // CONFIG2
- #pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
- #pragma config IESO = OFF // Internal External Switchover bit (Internal External Switchover mode disabled)
- // #pragma config statements should precede project file includes.
- // Use project enums instead of #define for ON and OFF.
- #include <xc.h>
- /* Define clock frequency required for delay functions */
- #define _XTAL_FREQ 8000000
- #define DATA PORTAbits.RA0
- #define CLOCK PORTAbits.RA1
- #define LATCH PORTAbits.RA2
- void clockdata(void);
- void latchdata(void);
- #ifdef __cplusplus
- extern "C" {
- #endif
- /* anything c ++ goes here */
- #ifdef __cplusplus
- }
- #endif
- #endif /* SPI_H */
- /* Main Method for doing this is below */
- /*
- * File: main.c
- * Author: mark Harrington
- *
- * Created on 17 May 2021, 17:17
- */
- #include "spi.h"
- // spi functions
- void clockdata(void){
- CLOCK = 1 ;
- __delay_ms(5);
- CLOCK =0 ;
- }
- void latchdata(void){
- LATCH =1 ;
- __delay_ms(5);
- LATCH=0 ;
- }
- int main(int argc, char** argv) {
- TRISA=0x0 ; // set port direction
- ANSEL= 0x0; // digital out only
- OSCCON=0b01110100; //osccon set to 8 megs
- PORTA=0x00 ;
- CLOCK =0 ;
- LATCH =0 ;
- unsigned char b = 0x01 ;
- while (1){
- for( unsigned int i = 0;i <8 ;i++)
- {
- DATA = (unsigned char)(b<<i) ; // must cast this to char for correct conversion
- __delay_ms(200);
- clockdata() ;
- latchdata() ;
- }
- DATA = b ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement