Loading Session3/Task1/adc.h +2 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,6 @@ void adc_init(void); //This function initialezes the ADC module uint8_t adc_read(uint8_t channel); //THis function takes in a channel and returns the result of the ADC measurement of the channel uint8_t adc_read(uint8_t channel); //This function takes in a channel and returns the result of the ADC measurement of the channel #endif /* ADC_H_ */ Session3/Task1/main.c +11 −9 Original line number Diff line number Diff line Loading @@ -8,14 +8,16 @@ #include <avr/interrupt.h> #include "adc.h" //#include "adc.c" if on linux #include "uart.h" //#include "uart.c" if on linux /* In this exercice you will be writing an ADC driver. You will use this to read the light sensor on the OI1 Xplained board. Connect the board to the EXT1 Header of the ATmega4809 You will use this to read the light sensor on the IO1 Xplained Pro board. Connect this board to the Xplained Pro Extension Header of the Curiosity Nano Adapter. We send the result of the measurement to the computer using UART. A complete and working UART driver has been provided with the exercise We send the result of the measurement to the computer using USART. A complete and working USART driver has been provided with the exercise It has been setup so that you can use printf to print nicely formated text and variable values to the terminal Loading @@ -26,8 +28,8 @@ int main(void) //Initialize everything we need here sei(); //If we want to receive via UART we need interrupts enabled. //NOTE: UART isn't used to receive here and as such this isn't required. sei(); //If we want to receive via USART we need interrupts enabled. //NOTE: USART isn't used to receive here and as such this isn't required. while (1) { Loading @@ -37,9 +39,9 @@ int main(void) /* About printf: Handles formatting of your print to terminal. This includes special characters, values of variables and more. It takes one string and any variables you want to print. Some handy special characters: \n = newline (enter), \t = tab % marks a variable to be inserted in the string. %d = decimal, %x = hexadecimal, %f = floating point number (There are many more) Variables to be inserted have to be provided comma separated after the string in order of appearance in string. Some handy special characters: \n = newline (enter)(some terminals need \r also), \t = tab '%' marks a variable to be inserted in the string. %d = decimal, %x = hexadecimal, %f = floating point number (There are many more) Variables to be inserted have to be provided separated by commas, after the string in order of appearance in string. */ Loading Session3/Task1/uart.c +38 −15 Original line number Diff line number Diff line /* * uart.c * * Created: 13.02.2017 04:55:46 * Author: Petter */ #include <avr/io.h> Loading @@ -6,17 +12,27 @@ #include "uart.h" void uart_init(unsigned long baud){ void uart_init(uint16_t baud){ //From chapter 24.3 in datasheet PORTB.OUTSET |= (1 << PIN2_bp); PORTB.DIRSET |= (1 << PIN2_bp); //Setting up TX pin as high output USART0.BAUD = baud; //Set baudrate //USART.CTRLC CMODE bits default to: async, 1 stop bit, 8 bit character size USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX //From chapter 22.3.1 in datasheet TX_PORT.OUTSET = (1 << TX_PIN); //Setting up TX pin as output TX_PORT.DIRSET = (1 << TX_PIN); //Setting up TX pin as output USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interupts on RX //Set baud rate register USART3.BAUDL = (uint8_t) baud; //Set baud rate without shifting to get the 8 low bits USART3.BAUDH = (uint8_t)(baud >> 8); //Shift register right by 8 bits to get the 8 high bits //USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size //Since all bits are default 0 we only need to change the character size part USART3.CTRLC = (0x3 << USART_CHSIZE0_bp); //Enable RX and TX USART3.CTRLB = (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable interrupts on incoming data USART3.CTRLA |= (1 << USART_RXCIE_bp); fdevopen(uart_transmit, NULL); //This allows us to use printf instead of writing a single character at a time //It also allows for easily printing variable values: printf("X is currently: %d \n", x_var); Loading @@ -25,17 +41,24 @@ void uart_init(unsigned long baud){ // function to transmit data int uart_transmit(char data, FILE* stream){ while (!(USART0.STATUS & (1 << USART_DREIF_bp))){ //Wait for ongoing transmission to finish (if there is one) } USART0.TXDATAL = data; //Put new data in register int uart_transmit(uint8_t data, FILE* stream){ //In this function we will be send data. //First we should check that there isn't already data being sent // if there is, we should probably wait for it to finish first while (!(USART3.STATUS & (1 << USART_DREIF_bp))){ //wait for previous transmit to finish }; //Put our new data into tx data register USART3.TXDATAL = data; return 0; } //Interrupt service routine for receiving ISR(USART0_RXC_vect){ ISR(USART3_RXC_vect){ uint8_t data = USART0.RXDATAL; //We must read the data to clear the interrupt flag //Here we can do something with data if we would want that Loading Session3/Task1/uart.h +12 −3 Original line number Diff line number Diff line /* * uart.h * * Created: 27.02.2017 16:51:11 * Author: Petter */ #ifndef UART_H_ #define UART_H_ #include <stdio.h> //Required for the FILE* type to be recognzed in the uart_transmit prototype #define TX_PORT PORTB #define TX_PIN 0 void uart_init(unsigned long baud); //This function initializes the UART module int uart_transmit(char data, FILE* stream); //You will be using fprint, which uses this function to send data to terminal void uart_init(uint16_t baud); int uart_transmit(uint8_t data, FILE* stream); #endif /* UART_H_ */ Session3/Task2/display.h +2 −2 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ // Below are the public functions that you can use to play with the screen // Call this method to initialize the screen. This must be done before using any other functions // Call this method to initialize the screen. This must be done before using any other functions related to the screen. void DISP_init(); // Call this method to write a bitmap to the screen. The bitmap must be a 32x16(512) array of bytes Loading Loading
Session3/Task1/adc.h +2 −2 Original line number Diff line number Diff line Loading @@ -5,6 +5,6 @@ void adc_init(void); //This function initialezes the ADC module uint8_t adc_read(uint8_t channel); //THis function takes in a channel and returns the result of the ADC measurement of the channel uint8_t adc_read(uint8_t channel); //This function takes in a channel and returns the result of the ADC measurement of the channel #endif /* ADC_H_ */
Session3/Task1/main.c +11 −9 Original line number Diff line number Diff line Loading @@ -8,14 +8,16 @@ #include <avr/interrupt.h> #include "adc.h" //#include "adc.c" if on linux #include "uart.h" //#include "uart.c" if on linux /* In this exercice you will be writing an ADC driver. You will use this to read the light sensor on the OI1 Xplained board. Connect the board to the EXT1 Header of the ATmega4809 You will use this to read the light sensor on the IO1 Xplained Pro board. Connect this board to the Xplained Pro Extension Header of the Curiosity Nano Adapter. We send the result of the measurement to the computer using UART. A complete and working UART driver has been provided with the exercise We send the result of the measurement to the computer using USART. A complete and working USART driver has been provided with the exercise It has been setup so that you can use printf to print nicely formated text and variable values to the terminal Loading @@ -26,8 +28,8 @@ int main(void) //Initialize everything we need here sei(); //If we want to receive via UART we need interrupts enabled. //NOTE: UART isn't used to receive here and as such this isn't required. sei(); //If we want to receive via USART we need interrupts enabled. //NOTE: USART isn't used to receive here and as such this isn't required. while (1) { Loading @@ -37,9 +39,9 @@ int main(void) /* About printf: Handles formatting of your print to terminal. This includes special characters, values of variables and more. It takes one string and any variables you want to print. Some handy special characters: \n = newline (enter), \t = tab % marks a variable to be inserted in the string. %d = decimal, %x = hexadecimal, %f = floating point number (There are many more) Variables to be inserted have to be provided comma separated after the string in order of appearance in string. Some handy special characters: \n = newline (enter)(some terminals need \r also), \t = tab '%' marks a variable to be inserted in the string. %d = decimal, %x = hexadecimal, %f = floating point number (There are many more) Variables to be inserted have to be provided separated by commas, after the string in order of appearance in string. */ Loading
Session3/Task1/uart.c +38 −15 Original line number Diff line number Diff line /* * uart.c * * Created: 13.02.2017 04:55:46 * Author: Petter */ #include <avr/io.h> Loading @@ -6,17 +12,27 @@ #include "uart.h" void uart_init(unsigned long baud){ void uart_init(uint16_t baud){ //From chapter 24.3 in datasheet PORTB.OUTSET |= (1 << PIN2_bp); PORTB.DIRSET |= (1 << PIN2_bp); //Setting up TX pin as high output USART0.BAUD = baud; //Set baudrate //USART.CTRLC CMODE bits default to: async, 1 stop bit, 8 bit character size USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX //From chapter 22.3.1 in datasheet TX_PORT.OUTSET = (1 << TX_PIN); //Setting up TX pin as output TX_PORT.DIRSET = (1 << TX_PIN); //Setting up TX pin as output USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interupts on RX //Set baud rate register USART3.BAUDL = (uint8_t) baud; //Set baud rate without shifting to get the 8 low bits USART3.BAUDH = (uint8_t)(baud >> 8); //Shift register right by 8 bits to get the 8 high bits //USART.CTRLC CMODE bits default to async, 1 stop bit, 8 bit character size //Since all bits are default 0 we only need to change the character size part USART3.CTRLC = (0x3 << USART_CHSIZE0_bp); //Enable RX and TX USART3.CTRLB = (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable interrupts on incoming data USART3.CTRLA |= (1 << USART_RXCIE_bp); fdevopen(uart_transmit, NULL); //This allows us to use printf instead of writing a single character at a time //It also allows for easily printing variable values: printf("X is currently: %d \n", x_var); Loading @@ -25,17 +41,24 @@ void uart_init(unsigned long baud){ // function to transmit data int uart_transmit(char data, FILE* stream){ while (!(USART0.STATUS & (1 << USART_DREIF_bp))){ //Wait for ongoing transmission to finish (if there is one) } USART0.TXDATAL = data; //Put new data in register int uart_transmit(uint8_t data, FILE* stream){ //In this function we will be send data. //First we should check that there isn't already data being sent // if there is, we should probably wait for it to finish first while (!(USART3.STATUS & (1 << USART_DREIF_bp))){ //wait for previous transmit to finish }; //Put our new data into tx data register USART3.TXDATAL = data; return 0; } //Interrupt service routine for receiving ISR(USART0_RXC_vect){ ISR(USART3_RXC_vect){ uint8_t data = USART0.RXDATAL; //We must read the data to clear the interrupt flag //Here we can do something with data if we would want that Loading
Session3/Task1/uart.h +12 −3 Original line number Diff line number Diff line /* * uart.h * * Created: 27.02.2017 16:51:11 * Author: Petter */ #ifndef UART_H_ #define UART_H_ #include <stdio.h> //Required for the FILE* type to be recognzed in the uart_transmit prototype #define TX_PORT PORTB #define TX_PIN 0 void uart_init(unsigned long baud); //This function initializes the UART module int uart_transmit(char data, FILE* stream); //You will be using fprint, which uses this function to send data to terminal void uart_init(uint16_t baud); int uart_transmit(uint8_t data, FILE* stream); #endif /* UART_H_ */
Session3/Task2/display.h +2 −2 Original line number Diff line number Diff line Loading @@ -7,7 +7,7 @@ // Below are the public functions that you can use to play with the screen // Call this method to initialize the screen. This must be done before using any other functions // Call this method to initialize the screen. This must be done before using any other functions related to the screen. void DISP_init(); // Call this method to write a bitmap to the screen. The bitmap must be a 32x16(512) array of bytes Loading