Hi everyone, i need help. I don't know if it is the right topic but I'll try anyway. I have a ATtiny48, I made a small PCB to program it with an arduino uno and it works well. I'm able to code it to use all of the pin as output to drive LED (I need 18 pins for that). The part that's making me go mad is when i need to use input. Despite all my effort, I'm not able to read pins. I want to have 5 button for my project, but nothing work. I'll add a copie of my code. I know the output works, it's really just the input that doesn't cooperate.
void setup()
{
DDRC |= (1 << PC0); // pin as output
DDRB &= ~(1 << PB0); // pin as input
}
void loop()
{
if (PINB & (1 << PB0)) // When the pin is HIGH
{
PORTC |= (1 << PC0); // LED light
}
else
{
PORTC &= ~(1 << PC0); // LED no light
}
}
You are using direct port manipulation, which is an advanced topic, but appear to be having a basic problem. Is that AI generated code?
Have you connected a pull down resistor (say 10k) to pin PB0 ?
No it’s not AI generated, I do have a 10k pull down on my inputs. I just don’t understand how to Read my inputs in that coding way. I’m not that knowing on that
Which Arduino core are you using for programming the attiny48 ?
Maybe PB0 has been assigned a strange value other than the 0 you are expecting. Maybe an arduino pin number instead. Try replacing PB0 with simply 0 and see if you get further.
Can you use Serial.print() for printing debug information?
For buttons wired PIN >> BUTTON >> GND, configure the button pin with INPUT_PULLUP by setting PB0 as output (1), set PB0 HIGH (1), then set PB0 as input (0).
Try getting your project working without port access... then translate the code to port access.
byte buttonPin[] = {0, 1, 2, 3, 4}; // your PBx
byte ledPin[] = {5, 6, 7, 8, 9}; // your PCx
byte pinSize = sizeof(buttonPin)/sizeof(buttonPin[0]);
void setup() {
for (byte i = 0; i < pinSize; i++) {
pinMode(buttonPin[i], INPUT_PULLUP);
pinMode(ledPin[i], OUTPUT);
}
}
void loop() {
for (byte i = 0; i < pinSize; i++) {
if (digitalRead(buttonPin[i]) == 0)
digitalWrite(ledPin[i], HIGH);
else
digitalWrite(ledPin[i], LOW);
}
}
I'm not sure what level the OP is at, and the cool looking direct port manipulation stuff he/she has churned out may give a misleading impression, but it is probably worth mentioning explicitly that, in this code sample case, the talked about 10k pull down resistors on the input pins have to be removed.
I did a lot of research for programming it that way. It's just that I'm still new to it (it's literally my first time) and the reading part is the only one that I can't make work.
I did try using code that way but the microcontroller doesn't works when I do it that way. That's the reason I went with port access. It's the only way I found to make my output works so I guess it's also the way to make my input work.
If I use exactly the on you sent, my outputs are barely able to light up a red LED (i see a bit of red but it's not much), a green one doesn't show anything. Even if I code it to make all my pins as output on HIGH, not all of them are able to light up my LED, even barely. It's the same for the input, nothing i do is able do make them read anything. Since I only have the chip, I can't use the serial port to debug ( I program the ATtiny48 on my arduino UNO then I put it on my board to test it).
I think this is valid. Use the Arduino, with its Serial Monitor, to program reading and writing registers, and when it is time to compile for the ATtiny48, make your code use ifdef ATTINY48 to use the correct DDRs/PORTs/PINs and no serial monitor.