|
|
View previous topic :: View next topic |
Author |
Message |
Wajiha Zubair
Joined: 27 May 2024 Posts: 1
|
MCP2515, PIC16F877A and SPI |
Posted: Tue Jun 04, 2024 10:45 am |
|
|
Hello,
I have this code and have been trying to make it work but it doesn't give the desired output. I am not sure if the SPI connection is the problem or the the way the message is being sent to the mcp2515. Can anyone help please.
I am using XC8 compiler.
#include <xc.h>
#include <string.h>
// Configuration Bits
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (LVP disabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 8000000 // 20 MHz external oscillator frequency
#ifndef MCP2515_H
#define MCP2515_H
void MCP2515_Reset(void);
void MCP2515_Init(void);
void MCP2515_SendMessage(unsigned char id);
unsigned char MCP2515_ReceiveMessage(unsigned char* id);
void blinkLED(int ms);
#endif
#ifndef SPI_H
#define SPI_H
void SPI_Init(int s);
void SPI_Write(unsigned char data);
unsigned char SPI_Read(void);
#endif
/*
void main() {
//MASTER
TRISD = 0; // Configure RC0 as output for LED
TRISBbits.TRISB3 = 1; // Configure RB3 as input for Button
PORTDbits.RD2 = 1; // Initial LED state
SPI_Init(1); // Initialize SPI
MCP2515_Init(); // Initialize MCP2515
while (1) {
if (PORTBbits.RB3 == 1) { // when button is pressed
PORTDbits.RD2 = 1;
blinkLED(100);
}
unsigned char id;
unsigned char receivedMessage[8];
if (MCP2515_ReceiveMessage(&id)) {
//if (strcmp(receivedMessage, "ON") == 1) {
if ( id == 0x01 ){
PORTDbits.RD2 = 0; // Turn on LED
// __delay_ms(1000);
}
//else if (strcmp(receivedMessage, "OFF") == 0) {
// PORTDbits.RD2 = 1; // Turn off LED
//}
}
}
}
*/
// /*
void main() {
//SLAVE
TRISBbits.TRISB3 = 1; // Configure RB3 as input for Button
TRISD = 0; // CS output (assuming RA5 for CS)
PORTDbits.RD2 = 1;
SPI_Init(0); // Initialize SPI
MCP2515_Init(); // Initialize MCP2515
while (1) {
if (PORTBbits.RB3 == 1) {
PORTDbits.RD2 = 0;
// Button pressed, send CAN message to PIC1
unsigned char message[] = "ON";
MCP2515_SendMessage(0x01); // Send "ON" message
__delay_ms(1000); // Debounce delay
blinkLED(100);
}
if (PORTBbits.RB3 == 0){
PORTDbits.RD2 = 1;
}
}
}
// */
void MCP2515_Reset(void) {
PORTDbits.RD0 = 0; // Select MCP2515
SPI_Write(0xC0); // Send reset command
PORTDbits.RD0 = 1; // Deselect MCP2515
__delay_ms(1); // Wait for the reset to complete
}
void MCP2515_Init(void) {
MCP2515_Reset();
// Additional initialization code here (setting up CAN bitrate, filters, etc.)
}
void MCP2515_SendMessage(unsigned char id) {
PORTDbits.RD0 = 0; // Select MCP2515
SPI_Write(0x40); // Load TX buffer command
SPI_Write(id); // Load identifier
PORTDbits.RD0 = 1; // Deselect MCP2515
PORTDbits.RD0 = 0; // Select MCP2515
SPI_Write(0x80); // Request to send command
PORTDbits.RD0 = 1; // Deselect MCP2515
}
unsigned char MCP2515_ReceiveMessage(unsigned char* id) {
PORTDbits.RD0 = 0; // Select MCP2515
SPI_Write(0x90); // Read RX buffer command
*id = SPI_Read(); // Read identifier
PORTDbits.RD0 = 1; // Deselect MCP2515
return 1; // Indicate a message was received
}
void SPI_Init(int s) {
TRISCbits.TRISC5 = 0; // SDO output
TRISCbits.TRISC4 = 1; // SDI input
TRISCbits.TRISC3 = 0; // SCK output for Master (ignored for Slave)
// SPI control register settings
if (s == 1) {
SSPCON = 0b00100010; // SPI Master mode, clock = Fosc/64
} else {
SSPCON = 0b00100100; // SPI Slave mode, clock = SCK pin
}
SSPSTAT = 0b00000000; // Sample at middle, low to high clock
PORTDbits.RD0 = 1; // Set CS high (inactive)
}
void SPI_Write(unsigned char data) {
SSPBUF = data; // Load data into buffer
//blinkLED(50);
while(!SSPSTATbits.BF); // Wait until transmission is complete
}
unsigned char SPI_Read(void) {
SPI_Write(0x00); // Write dummy data to initiate SPI read
//blinkLED(50);
return SSPBUF; // Return the received data
}
void blinkLED(int ms){
PORTDbits.RD2 = 0;
__delay_ms(ms);
PORTDbits.RD2 = 1;
} |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19539
|
|
Posted: Tue Jun 04, 2024 11:38 am |
|
|
This is a forum for the _CCS_ Compiler, not XC8.
You need to ask on the Microchip forums for help with this, not here. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|