SaltForTheThirsty

RawDawg'nTheArduino()

Lesson 1

GPIO, general purpose input & output. This lesson shows how to write to an entire port or to individual pins. You are expected to read some of the datasheets. Uses the Arduino Mega.

Raw Dawg'n the Arduino - Lesson 1

Download Files.
    
/* * Lesson1.c * * Created: 9/7/2017 5:59:43 PM * Author : kidwidget */ #include <avr/io.h> #include <util/delay.h> /* Lesson1 GCC AVR user manual and Reference http://www.nongnu.org/avr-libc/user-manual/pages.html read the following parts of the atmega2560 datasheet, titled "Atmel-2549-8-bit-AVR-Microcontroller-ATmega640-1280-1281-2560-2561_datasheet" page 1: this is a summary of the features and peripherals of the microcontroller pages 2 - 4 pinout of the different packages. pages 5 - 6 a more detailed over view of the peripherals and features pages 7 - 9 breif description of the ports pages 67 - 72 I/O Ports (input and output ports) */ int main(void){ // GPIO - General Purpose Input/Output pins/ports // each port has 3 registers associated with it. // Data Register = PORTx, i.e PORTK, PORTA, etc Read/Write // Data Direction Register = DDRx, i.e DDRK, DDRA, etc Read/Write // Port Input Pins = PINx, i.e PINF1, PINA1, etc Read only . . . but writing a 1 will toggle // the corresponding bit in the Data Register DDRK = 0xFF; // PORTK is configured as all output DDRK = 0b11111111; // same as above but written in binary DDRK = 255; // same as above but written in base 10 DDRK = 0b10111001; // some pins configured as output some as input, it's easy to see which. DDRK = 0xB9; // same as above but written in hex, if you are good with hex then you will recognize the bit pattern DDRK = 185; // same as above but written in base10, conveys no information about which bits are 1 or 0 without converting. // only a jackass would write it like this DDRK = 0xFF; PORTK = 0xFF; // all bits set to 1. There is now 5 volts on all of PORTK's pins. If you have LEDs on them they will light up _delay_ms(500); PORTK = 0x00; // all bits set to 0. There is now 0 volts on all of PORTK's pins. if you have LEDs on them they will turn of _delay_ms(500); uint8_t i; // create an unsigned 8 bit variable named 'i' for(i = 0; i < 255; i++){ // writing the value of i to PORTK. If you have an LED connected to each of the PORTK pins then PORTK = i; // you will see them count up in binary _delay_ms(100); } PORTK =0xFF; for(i = 0; i < 255; i++){ PINK |= _BV(PINK0); // toggle the PINK0 on and off, PINK0 can also be written as PK0, _delay_ms(100); // GCC AVR library reference -> special function registers } for(i = 0; i < 255; i++){ PORTK |= _BV(PK0); // write 1 to PORTK Pin 0 _delay_ms(100); PORTK &= ~(_BV(PF0)); // write 0 to PORTK Pin 0 _delay_ms(100); } // now we will read some pins. You will need switches connected to PORTK. DDRK = 0x00; // all pins on PORTK are input i = PORTK; // take the value on PORTK (determined by which switches you have on or off) and store it in 'i' /* Problems for you: Write a program that takes the input from one port and outputs it to another port. Uses switches for input and LED's for output. Write a program that makes the LED's do the Knight Rider thing. Hint, use only 8 LEDs, all on the same port. Think about the bit pattern has to change. How would you do that in base10 using arithmetic, what do you have to change to do the same thing in binary. */ }