Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Place these things in a config header */
- #include <htc.h>
- #include "delay.h"
- __CONFIG(MCLRE_OFF & CP_OFF & WDTE_OFF & BOREN_OFF & FOSC_INTOSCCLK);
- /* :) */
- #include "delay.c"
- /* Probably okay macros */
- #define SET_BIT(p,n) ((p) |= (1) << (n))
- #define CLR_BIT(p,n) ((p) &= ~((1) << (n)))
- #define TOG_BIT(p,n) ((p) ^= (1) << (n))
- #define CHK_BIT(p,n) ((p) & ((1) << (n)))
- /* When in a library, use ifdefs to make these default but option pin mappings maybe */
- /* SPI Pins */
- #define SPI_SDI PORTBbits.RB1
- #define SPI_SDO PORTBbits.RB2
- #define SPI_CS PORTBbits.RB3
- #define SPI_CLK PORTBbits.RB4
- /* SR Pins..figure something out later */
- #define SR_LAT PORTBbits.RB5
- #define SR_HSCP PORTBbits.RB6
- #define SR_HTCP PORTBbits.RB7
- void spiInit() {
- SET_BIT(TRISB, SPI_SDI);
- CLR_BIT(TRISB, SPI_SDO);
- CLR_BIT(TRISB, SPI_CLK);
- CLR_BIT(TRISB, SPI_CS);
- SSPCON = 0; // first disable port if it was still running
- SSPSTAT = 0; // Reset all status flags (there are some R/W bits)
- SSPCON = (1<<5) | (1<<1); // enable port, SPI mode Fosc/64
- }
- void srInit() {
- SET_BIT(TRISB, SR_LAT);
- }
- unsigned char spiTxRx(unsigned char byteOut) {
- SSPBUF = byteOut; // Write to buffer to start a transmit.
- while((SSPSTAT & (1 << 0)) == 0); // Wait till buffer indicator says a receive has been completed
- return SSPBUF; // Read the buffer for the received byte.
- }
- void srTx(char byteIn) {
- for(int i = 0; i < 8; i++) {
- /* Transmit byte, ignoring no return */
- spiTxRx(byteIn);
- /* Pulse LATh to transfer shift to storage registers. */
- SR_LAT = 0;
- SR_LAT = 1;
- }
- }
- /* ------------- MAIN PROGRAM -------------------*/
- void main() {
- spiInit();
- srInit();
- /* Display an LED binary counter as a test */
- SPI_CS = 0;
- for(int i = 0; i < 255; i++) {
- srTx(i);
- DelayMs(100);
- }
- SPI_CS = 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement