I wrote a program for ATmega328p and this program works great but i ordered this which came with ATmega328PB chip. here is the link: HW-229 USBtinyISP AVR ISP Programmer Bootloader For Arduino R3 Meag2560 with 10pin Programming Cable – Alexnld.com
I am able to run the bootloader but it doesn't do anything. Here is my program which wrote for ATmega328p
#include <mcp2515.h>
//CAN FRAME FOR EACH BUTTON
struct can_frame canMsg0;
// global variables
byte lastInputState0 = 0x00;
byte lastInputState1 = 0x00;
byte canByte0 = 0x00;
byte canByte1 = 0x00;
// Chip select pin for MCP2515
MCP2515 mcp2515(10);
//Defining the buttons and their pins for arduino R3 and if using a different microcontroller the pin assignment will change
#define button0Pin 3 // SW1_1 PD3
#define button1Pin 4 // SW1_3 PD4
#define button2Pin 5 // SW2_1 PE0
#define button3Pin 6 // SW2_3 PE1
#define button4Pin 7 // SW3_1 PD5
#define button5Pin 8 // SW3_3 PD6
#define button6Pin 9 // SW4_1 PD7
#define button7Pin A0 // SW4_3 PB0
#define button8Pin A1 // SW5_1 PB1
#define button9Pin A2 // SW5_3 PB2
#define button10Pin A3 // SW6_1 PC3
#define button11Pin A4 // SW6_3 PC4
#define button12Pin A5 // SW7_1 PC5
//defining the can with baudrate and clock speed for configuration
#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)
//button states starts with low
int buttonState0 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
int buttonState9 = 0;
int buttonState10 = 0;
int buttonState11 = 0;
int buttonState12 = 0;
void setup() {
//Can bus with id, dlc and data to be transferring
canMsg0.can_id = 0x036;
canMsg0.can_dlc = 2;
canMsg0.data[0] = 0x00;
canMsg0.data[1] = 0x00;
while (!Serial);
Serial.begin(500000);
//Initializing the SPI
SPI.begin();
mcp2515.reset();
//Initializing the CAN protocol with 500k baudrate and 8MHz clock
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
//Starting the CAN in normal mode
mcp2515.setNormalMode();
//Making all pins as input due to using pull down resistor
pinMode(button0Pin, INPUT);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
pinMode(button7Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(button9Pin, INPUT);
pinMode(button10Pin, INPUT);
pinMode(button11Pin, INPUT);
pinMode(button12Pin, INPUT);
lastInputState0 = canByte0;
lastInputState1 = canByte1;
}
void loop() {
canByte0 = 0x00;
canByte1 = 0x00;
//Read the state of the pushbutton value
if (digitalRead(button0Pin) == HIGH) {
canByte0 |= (0x01 << 0);
}
if (digitalRead(button1Pin) == HIGH) {
canByte0 |= (0x01 << 1);
}
if (digitalRead(button2Pin) == HIGH) {
canByte0 |= (0x01 << 2);
}
if (digitalRead(button3Pin) == HIGH) {
canByte0 |= (0x01 << 3);
}
if (digitalRead(button4Pin) == HIGH) {
canByte0 |= (0x01 << 4);
}
if (digitalRead(button5Pin) == HIGH) {
canByte0 |= (0x01 << 5);
}
if (digitalRead(button6Pin) == HIGH) {
canByte0 |= (0x01 << 6);
}
if (digitalRead(button7Pin) == HIGH) {
canByte0 |= (0x01 << 7);
}
if (digitalRead(button8Pin) == HIGH) {
canByte1 |= (0x01 << 0);
}
if (digitalRead(button9Pin) == HIGH) {
canByte1 |= (0x01 << 1);
}
if (digitalRead(button10Pin) == HIGH) {
canByte1 |= (0x01 << 2);
}
if (digitalRead(button11Pin) == HIGH) {
canByte1 |= (0x01 << 3);
}
if (digitalRead(button12Pin) == HIGH) {
canByte1 |= (0x01 << 4);
}
if (lastInputState0 != canByte0 || lastInputState1 != canByte1)
{
Serial.print("\nCAN Byte0: 0x");
Serial.print(canByte0 < 16 ? "0" : "");
Serial.print(canByte0, HEX);
canMsg0.data[0] = canByte0;
Serial.print(", CAN Byte1: 0x");
Serial.print(canByte1 < 16 ? "0" : "");
Serial.println(canByte1, HEX);
canMsg0.data[1] = canByte1;
mcp2515.sendMessage(&canMsg0);
}
else {mcp2515.sendMessage(&canMsg0); } // this will be transmitting zeros for every 500ms
lastInputState0 = canByte0;
lastInputState1 = canByte1;
delay(500); // adding the delay to the system of buttons till displaying the hex symbol.
}
I know the pin configurations are difference for ATmega328PB so here is the code i wrote which compiles but doesn't do anything. any help would be a huge help for me.
#include <mcp2515.h>
//CAN FRAME FOR EACH BUTTON
struct can_frame canMsg0;
// global variables
byte lastInputState0 = 0x00;
byte lastInputState1 = 0x00;
byte canByte0 = 0x00;
byte canByte1 = 0x00;
// Chip select pin for MCP2515
MCP2515 mcp2515(19);
//Defining the buttons and their pins for arduino R3 and if using a different microcontroller the pin assignment will change
#define PIN_SPI_SS 19
#define PIN_SPI_MOSI 22
#define PIN_SPI_MISO 23
#define PIN_SPI_SCK 24
#define button0Pin 1 // SW1_1 PD3
#define button1Pin 2 // SW1_3 PD4
#define button2Pin 3 // SW2_1 PE0
#define button3Pin 6 // SW2_3 PE1
#define button4Pin 9 // SW3_1 PD5
#define button5Pin 10 // SW3_3 PD6
#define button6Pin 11 // SW4_1 PD7
#define button7Pin 12 // SW4_3 PB0
#define button8Pin 13 // SW5_1 PB1
#define button9Pin 14 // SW5_3 PB2
#define button10Pin 26 // SW6_1 PC3
#define button11Pin 27 // SW6_3 PC4
#define button12Pin 28 // SW7_1 PC5
#define button13Pin 30
#define button14Pin 31
#define button15Pin 32
//defining the can with baudrate and clock speed for configuration
#define MCP_8MHz_500kBPS_CFG1 (0x00)
#define MCP_8MHz_500kBPS_CFG2 (0x90)
#define MCP_8MHz_500kBPS_CFG3 (0x82)
//button states starts with low
int buttonState0 = 0;
int buttonState1 = 0;
int buttonState2 = 0;
int buttonState3 = 0;
int buttonState4 = 0;
int buttonState5 = 0;
int buttonState6 = 0;
int buttonState7 = 0;
int buttonState8 = 0;
int buttonState9 = 0;
int buttonState10 = 0;
int buttonState11 = 0;
int buttonState12 = 0;
void setup() {
//Can bus with id, dlc and data to be transferring
canMsg0.can_id = 0x036;
canMsg0.can_dlc = 2;
canMsg0.data[0] = 0x00;
canMsg0.data[1] = 0x00;
while (!Serial);
Serial.begin(500000);
//Initializing the SPI
SPI.begin();
mcp2515.reset();
//Initializing the CAN protocol with 500k baudrate and 8MHz clock
mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
//Starting the CAN in normal mode
mcp2515.setNormalMode();
//Making all pins as input due to using pull down resistor
pinMode(button0Pin, INPUT);
pinMode(button1Pin, INPUT);
pinMode(button2Pin, INPUT);
pinMode(button3Pin, INPUT);
pinMode(button4Pin, INPUT);
pinMode(button5Pin, INPUT);
pinMode(button6Pin, INPUT);
pinMode(button7Pin, INPUT);
pinMode(button8Pin, INPUT);
pinMode(button9Pin, INPUT);
pinMode(button10Pin, INPUT);
pinMode(button11Pin, INPUT);
pinMode(button12Pin, INPUT);
pinMode(button13Pin, INPUT);
pinMode(button14Pin, INPUT);
pinMode(button15Pin, INPUT);
lastInputState0 = canByte0;
lastInputState1 = canByte1;
}
void loop() {
canByte0 = 0x00;
canByte1 = 0x00;
//Read the state of the pushbutton value
if (digitalRead(button0Pin) == HIGH) {
canByte0 |= (0x01 << 0);
}
if (digitalRead(button1Pin) == HIGH) {
canByte0 |= (0x01 << 1);
}
if (digitalRead(button2Pin) == HIGH) {
canByte0 |= (0x01 << 2);
}
if (digitalRead(button3Pin) == HIGH) {
canByte0 |= (0x01 << 3);
}
if (digitalRead(button4Pin) == HIGH) {
canByte0 |= (0x01 << 4);
}
if (digitalRead(button5Pin) == HIGH) {
canByte0 |= (0x01 << 5);
}
if (digitalRead(button6Pin) == HIGH) {
canByte0 |= (0x01 << 6);
}
if (digitalRead(button7Pin) == HIGH) {
canByte0 |= (0x01 << 7);
}
if (digitalRead(button8Pin) == HIGH) {
canByte1 |= (0x01 << 0);
}
if (digitalRead(button9Pin) == HIGH) {
canByte1 |= (0x01 << 1);
}
if (digitalRead(button10Pin) == HIGH) {
canByte1 |= (0x01 << 2);
}
if (digitalRead(button11Pin) == HIGH) {
canByte1 |= (0x01 << 3);
}
if (digitalRead(button12Pin) == HIGH) {
canByte1 |= (0x01 << 4);
}
if (digitalRead(button13Pin) == HIGH) {
canByte1 |= (0x01 << 5);
}
if (digitalRead(button14Pin) == HIGH) {
canByte1 |= (0x01 << 6);
}
if (digitalRead(button15Pin) == HIGH) {
canByte1 |= (0x01 << 7);
}
if (lastInputState0 != canByte0 || lastInputState1 != canByte1)
{
Serial.print("\nCAN Byte0: 0x");
Serial.print(canByte0 < 16 ? "0" : "");
Serial.print(canByte0, HEX);
canMsg0.data[0] = canByte0;
Serial.print(", CAN Byte1: 0x");
Serial.print(canByte1 < 16 ? "0" : "");
Serial.println(canByte1, HEX);
canMsg0.data[1] = canByte1;
mcp2515.sendMessage(&canMsg0);
}
else {mcp2515.sendMessage(&canMsg0); } // this will be transmitting zeros for every 500ms
lastInputState0 = canByte0;
lastInputState1 = canByte1;
delay(500); // adding the delay to the system of buttons till displaying the hex symbol.
}