I need help with writing Arduino Uno code for ATMega328p micro-controller . Exam
ID: 2247229 • Letter: I
Question
I need help with writing Arduino Uno code for ATMega328p micro-controller.
Examples of input/output bit-manipulation process:
* DDRD -> 0x2A
* PDRB -> 0x24
* PORT -> 0x2B
Useful arduino uno codes:
PINB & (1 << PINB5)
DDRD = DDRD | (1 << DDRD3)
PORTD = PORTD & ~(1 << DDRD3)
PORTD = (1 << PD0)|(1 << PD3)|(1 << PD6);
PORTD = (1 << 0)|(1 << 3)|(1 << 6);
PORTD = 0x49;
unsigned char value = 0;
value = (PIND & (1 << PORTD1));
Useful arduino uno codes:
Digital Write:
const unsigned char GlobalInterruptBit = 7;
void digitalWritePin13(unsigned char value) {
SREG = SREG & (LOW << GlobalInterruptBit);
if( value == HIGH )
PORTB = PORTB | (value << PORTB5);
else
PORTB = PORTB & (value << PORTB5);
SREG = SREG | (HIGH << GlobalInterruptBit);
}
// If pin is controlled by Port B
if (pin_number > 7) {
// Pin 8 in the board is mapped to bit 0 of port b register.
pin_number = pin_number - 8;
}
else { //pin_number <= 7 (pin is in Port D)
//do not need to modify the pin_number
}
Digital Read:
DDRD = DDRD | (LOW << DDRD3);
PORTD = PORTD | (HIGH << PORTD3);
if( PIND & (HIGH << DDRD3) == LOW) {
// Button pressed on pin 3.
}
else {
// no button pressed on pin 3.
}
Pinmode:
bool turnOn = false;
DDRD = DDRD | (1 << DDRD3);
if( turnOn == true ) {
// Turn on the LED.
PORTD = PORTD | (1 << PORTD3);
}
else {
// Turn off the LED.
PORTD = PORTD | (0 << PORTD3);
}
// Flip the value of the flag for next time around.
turnOn = !turnOn;
Explanation / Answer
if you want to use the serial upload via bootloader, it's important to run the burn bootloader via the Arduino IDE, so the fuses are set as Arduino UNO (FF-DE-05).
If you have done this the serial communication with Sparkfun FTDI has to work. Attempts to reverse the signals tx and rx.
You added quartz with the two capacitors?
You have set in the IDE Arduino UNO?
void digitalWritePin13(unsigned char value) {
SREG = SREG & (LOW << GlobalInterruptBit);
if( value == HIGH )
PORTB = PORTB | (value << PORTB5);
else
PORTB = PORTB & (value << PORTB5);
SREG = SREG | (HIGH << GlobalInterruptBit);
}
// If pin is controlled by Port B
if (pin_number > 7) {
// Pin 8 in the board is mapped to bit 0 of port b register.
pin_number = pin_number - 8;
}
else { //pin_number <= 7 (pin is in Port D)
//do not need to modify the pin_number
}
Digital Read:
DDRD = DDRD | (LOW << DDRD3);
PORTD = PORTD | (HIGH << PORTD3);
if( PIND & (HIGH << DDRD3) == LOW) {
// Button pressed on pin 3.
}
else {
// no button pressed on pin 3.
}
Pinmode:
bool turnOn = false;
DDRD = DDRD | (1 << DDRD3);
if( turnOn == true ) {
// Turn on the LED.
PORTD = PORTD | (1 << PORTD3);
}
else {
// Turn off the LED.
PORTD = PORTD | (0 << PORTD3);
}
// Flip the value of the flag for next time around.
turnOn = !turnOn;