I can upload a sketch via an Uno using Arduino as ISP to by ATMEGA328-PU. Port D works but seems not B and C. The code is just toggling the port as a test. I did the Burn Bootloader step first. What am I missing?
Could you please post the code ?
#define LEDA PB0
#define LEDB PB1
#define PiezoPin PD2
void setup() {
pinMode(LEDA, OUTPUT);
pinMode(LEDB, OUTPUT);
pinMode(PiezoPin, OUTPUT);
pinMode(PB3, OUTPUT);
pinMode(PB4, OUTPUT);
pinMode(PD3, OUTPUT);
pinMode(PD4, OUTPUT);
pinMode(PD5, OUTPUT);
pinMode(PD6, OUTPUT);
pinMode(PD7, OUTPUT);
pinMode(PC0, OUTPUT);
pinMode(PC1, OUTPUT);
pinMode(PC2, OUTPUT);
pinMode(PC3, OUTPUT);
pinMode(PC4, OUTPUT);
pinMode(PC5, OUTPUT); }
void loop() {
for (int X = 0; X < 2; X++) {
digitalWrite(LEDA, X);
digitalWrite(LEDB, X);
digitalWrite(PB3, X);
digitalWrite(PB4, X);
digitalWrite(PD3, X);
digitalWrite(PD4, X);
digitalWrite(PD5, X);
digitalWrite(PD6, X);
digitalWrite(PD7, X);
digitalWrite(PC0, X);
digitalWrite(PC1, X);
digitalWrite(PC2, X);
digitalWrite(PC3, X);
digitalWrite(PC4, X);
digitalWrite(PC5, X); } }
Your problem is using PBx, PCx, and PDx as if they are pin numbers, which they are not. pinMode() and digitialWrite() use the pin designations used on the arduino boards, for an UNO those would be 0 through 13 for pins D0 through D13, and A0 through A5 for the analog pins (you can also use 14-19 for the analog pins, but using the Ax designation makes the code portable between different types of arduino boards). Note that the pin numbers used are not the physical pin numbers of the atmega328 chip, but the pin numbers as labeled on the arduino boards.
Wow - thank you thank you! You have no idea how much time this wasted going down rabbit holes.
But what is the cleanest way to handle using a non-standard chip?
Must I write code like this?
DDRB |= 1; // LEDA = PB0, OUTPUT
DDRB |= 2; // LEDB = PB1, OUTPUT
DDRD |= 4; // PIEZO = PD2, OUTPUT
Which "non-standard chip" are you using?
If using the '328 (vs the standard '328P), then add Minicore to your IDE so you can select it easily within the IDE:
If you put these at the top of your sketch, you'd probably be all set:
byte PD0 = 0; // serial with PC, I suggest not using these in a sketch
byte PD1 = 1; // serial with PC
byte PD2 = 2;
byte PD3 = 3'
byte PD4 = 4;
byte PD5 = 5;
byte PD6 = 6;
byte PD7 = 7;
byte PB0 = 8;
byte PB1 = 9;
byte PB2 = 10;
byte PB3 = 11;
byte PB4 = 12;
byte PB5 = 13;
byte PC0 = 14;
byte PC1 = 15;
byte PC2 = 16;
byte PC3 = 17;
byte PC4 = 18;
byte PC5 = 19;
I'm using ATMEGA328P-MU
Oh, MU, that just uses standard Arduino definitions, just like an Uno.
Bootload the part as an Uno and go from there.
Or select 328P from the MiniCore list.
A 328P is a 328P, independent of package, it's the same die in all the chips:
Standard temperature range, -40C to +85C
(part number, package type)
ATmega328P-AU, 32A << SMD part often used
ATmega328P-AUR, 32A tape & reel packaging
ATmega328P-MMH, 28M1
ATmega328P-MMHR , 28M1 tape & reel packaging
ATmega328P-MU, 32M1-A <<< your "non-standard" part, smaller leadless part
ATmega328P-MUR, 32M1-A tape & reel packaging
ATmega328P-PU, 28P3 << DIP that us usually used
Wider temperature range, -40C to +105C
ATmega328P-AN, 32A
ATmega328P-ANR, 32A
ATmega328P-MN, 32M1-A
ATmega328P-MNR, 32M1-A
ATmega328P-PN, 28P3
Package type definitions:
32A: 32-lead, Thin (1.0mm) Plastic Quad Flat Package (TQFP)
28M1: 28-pad, 4 x 4 x 1.0 body, Lead Pitch 0.45mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/MLF)
28P3: 28-lead, 0.300” Wide, Plastic Dual Inline Package (PDIP)
32M1-A: 32-pad, 5 x 5 x 1.0 body, Lead Pitch 0.50mm Quad Flat No-Lead/Micro Lead Frame Package (QFN/MLF)
Ball-grid array package is an option too, but likely have to order direct from MicroChip, Digikey & Mouser don't list it for ordering:
32CC1, 32-ball (6 x 6 Array),
4 x 4 x 0.6 mm package, ball pitch 0.50 mm,
Ultra Thin, Fine-Pitch Ball Grid Array (UFBGA)
Thanks but then what is the best way to set a pin high / low such as PB0?
In other words, if MU is just a different package, why did my code in post #2 not work?
swisscheese2:
Thanks but then what is the best way to set a pin high / low such as PB0?
Look up the UNO pin diagram, PB0 is pin 8 on the board, so use digitalWrite (8, HIGH) to set it high.
Thanks guys. Now I have another problem. I can't detect a signal on B2 but can see it on a scope.
In the code below the LED turns on momentarily but then stays off. Am I doing something wrong?
// ATMEGA328P-MU
void LEDon () { PORTB = 2; }
void LEDoff () { PORTB = 0; }
void setup() {
DDRB |= bit(0); // B0 LED
DDRB |= bit(1); // B1 LED
DDRB &= ~bit(2); // B2 INP pin 14
LEDon (); delay (500);
LEDoff (); delay (500); }
void loop() {
if (PORTB & bit (2)) { LEDon (); delay (200); }
else LEDoff (); }
Never mind - figured it out I think - have to use PIN not PORT for reading.